fix bugs
This commit is contained in:
parent
8516fa921c
commit
f24a4d6982
3 changed files with 38 additions and 30 deletions
4
PKGBUILD
4
PKGBUILD
|
@ -1,6 +1,6 @@
|
|||
# Maintainer: 1ridic <i@8f.al>
|
||||
pkgname=uptimes
|
||||
pkgver=0.1.7
|
||||
pkgver=0.1.9
|
||||
pkgrel=1
|
||||
pkgdesc="status of total uptime"
|
||||
arch=('any')
|
||||
|
@ -11,7 +11,7 @@ source=(
|
|||
local://uptimes
|
||||
local://uptimesd.service
|
||||
)
|
||||
md5sums=('a0d8fd353a37f14508367cb968601309'
|
||||
md5sums=('9eecdc39ee18e28843b1e113fd6090c6'
|
||||
'7a90889f87792317e70ebdce9dae7023')
|
||||
|
||||
package() {
|
||||
|
|
63
uptimes
63
uptimes
|
@ -6,16 +6,16 @@ import sys
|
|||
import argparse
|
||||
import signal
|
||||
import socket
|
||||
import json
|
||||
|
||||
startTime = 0
|
||||
totalTime = 0
|
||||
uptime = 0
|
||||
s = 0
|
||||
|
||||
socket_file = '/run/uptimesd.sock'
|
||||
uptimes_db = '/etc/uptimes/uptimes.db'
|
||||
|
||||
def readUpTime():
|
||||
def readTime():
|
||||
global uptime,startTime
|
||||
|
||||
with open('/proc/uptime', 'r') as f:
|
||||
|
@ -24,22 +24,25 @@ def readUpTime():
|
|||
uptime = float(uptime)
|
||||
return uptime
|
||||
|
||||
def updateTime():
|
||||
global uptime, startTime
|
||||
now = readUpTime()
|
||||
if uptime == 0:
|
||||
with open(uptimes_db, 'r') as f:
|
||||
j = f.readline()
|
||||
j = json.loads(j)
|
||||
uptime = float(j['uptime'])
|
||||
def timeInit():
|
||||
global startTime, totalTime
|
||||
with open(uptimes_db, 'r') as f:
|
||||
totalTime = f.readline()
|
||||
totalTime = float(totalTime)
|
||||
|
||||
if startTime == 0:
|
||||
startTime = now
|
||||
uptime += now - startTime
|
||||
pathExists()
|
||||
print("totalTime: %f" % totalTime)
|
||||
sys.stdout.flush()
|
||||
|
||||
startTime = readTime()
|
||||
print("startTime: %f" % startTime)
|
||||
sys.stdout.flush()
|
||||
|
||||
def updateTime():
|
||||
global uptime, startTime, totalTime
|
||||
now = readTime()
|
||||
totalTime += now - startTime
|
||||
with open(uptimes_db, 'w') as f:
|
||||
j = json.dumps({'uptime': uptime})
|
||||
f.write(j)
|
||||
f.write(str(totalTime))
|
||||
|
||||
def pathExists():
|
||||
# if /etc/uptimes/uptimes.db exist
|
||||
|
@ -54,6 +57,7 @@ def pathExists():
|
|||
os.mkdir(folder, 0o600)
|
||||
# create uptimes.db with 600
|
||||
with open(uptimes_db, 'w') as f:
|
||||
f.write('0')
|
||||
os.chmod(uptimes_db, 0o600)
|
||||
|
||||
|
||||
|
@ -148,6 +152,8 @@ def daemon(fork = True):
|
|||
print('Creating socket...')
|
||||
sys.stdout.flush()
|
||||
|
||||
if os.path.exists(socket_file):
|
||||
os.unlink(socket_file)
|
||||
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
s.bind(socket_file)
|
||||
# socket file permission 777
|
||||
|
@ -166,8 +172,7 @@ def daemon(fork = True):
|
|||
print('Reading db...')
|
||||
sys.stdout.flush()
|
||||
|
||||
readUpTime()
|
||||
print('Uptime before: %f' % uptime)
|
||||
timeInit()
|
||||
print('Uptimesd started')
|
||||
sys.stdout.flush()
|
||||
|
||||
|
@ -206,8 +211,7 @@ def sigterm_handler(signo, frame):
|
|||
sys.stdout.flush()
|
||||
# update uptime
|
||||
updateTime()
|
||||
# close socket
|
||||
s.close()
|
||||
|
||||
if os.path.exists(socket_file):
|
||||
os.unlink(socket_file)
|
||||
# exit
|
||||
|
@ -227,15 +231,18 @@ def comm(command):
|
|||
if not os.path.exists(socket_file):
|
||||
print('Uptimesd not running')
|
||||
sys.exit(1)
|
||||
try:
|
||||
# connect
|
||||
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
s.connect(socket_file)
|
||||
|
||||
# connect
|
||||
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
s.connect(socket_file)
|
||||
|
||||
# send command
|
||||
s.sendall(command.encode('utf-8'))
|
||||
data = s.recv(1024)
|
||||
return data.decode('utf-8')
|
||||
# send command
|
||||
s.sendall(command.encode('utf-8'))
|
||||
data = s.recv(1024)
|
||||
return data.decode('utf-8')
|
||||
except:
|
||||
print('Uptimesd not running')
|
||||
sys.exit(1)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
1
uptimes.db
Normal file
1
uptimes.db
Normal file
|
@ -0,0 +1 @@
|
|||
603.2700000000001
|
Loading…
Reference in a new issue