This commit is contained in:
iridiumR 2023-08-07 23:51:56 +08:00
parent 8516fa921c
commit f24a4d6982
No known key found for this signature in database
GPG key ID: 49735733EB1A32C8
3 changed files with 38 additions and 30 deletions

View file

@ -1,6 +1,6 @@
# Maintainer: 1ridic <i@8f.al> # Maintainer: 1ridic <i@8f.al>
pkgname=uptimes pkgname=uptimes
pkgver=0.1.7 pkgver=0.1.9
pkgrel=1 pkgrel=1
pkgdesc="status of total uptime" pkgdesc="status of total uptime"
arch=('any') arch=('any')
@ -11,7 +11,7 @@ source=(
local://uptimes local://uptimes
local://uptimesd.service local://uptimesd.service
) )
md5sums=('a0d8fd353a37f14508367cb968601309' md5sums=('9eecdc39ee18e28843b1e113fd6090c6'
'7a90889f87792317e70ebdce9dae7023') '7a90889f87792317e70ebdce9dae7023')
package() { package() {

63
uptimes
View file

@ -6,16 +6,16 @@ import sys
import argparse import argparse
import signal import signal
import socket import socket
import json
startTime = 0 startTime = 0
totalTime = 0
uptime = 0 uptime = 0
s = 0 s = 0
socket_file = '/run/uptimesd.sock' socket_file = '/run/uptimesd.sock'
uptimes_db = '/etc/uptimes/uptimes.db' uptimes_db = '/etc/uptimes/uptimes.db'
def readUpTime(): def readTime():
global uptime,startTime global uptime,startTime
with open('/proc/uptime', 'r') as f: with open('/proc/uptime', 'r') as f:
@ -24,22 +24,25 @@ def readUpTime():
uptime = float(uptime) uptime = float(uptime)
return uptime return uptime
def updateTime(): def timeInit():
global uptime, startTime global startTime, totalTime
now = readUpTime() with open(uptimes_db, 'r') as f:
if uptime == 0: totalTime = f.readline()
with open(uptimes_db, 'r') as f: totalTime = float(totalTime)
j = f.readline()
j = json.loads(j)
uptime = float(j['uptime'])
if startTime == 0: print("totalTime: %f" % totalTime)
startTime = now sys.stdout.flush()
uptime += now - startTime
pathExists() 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: with open(uptimes_db, 'w') as f:
j = json.dumps({'uptime': uptime}) f.write(str(totalTime))
f.write(j)
def pathExists(): def pathExists():
# if /etc/uptimes/uptimes.db exist # if /etc/uptimes/uptimes.db exist
@ -54,6 +57,7 @@ def pathExists():
os.mkdir(folder, 0o600) os.mkdir(folder, 0o600)
# create uptimes.db with 600 # create uptimes.db with 600
with open(uptimes_db, 'w') as f: with open(uptimes_db, 'w') as f:
f.write('0')
os.chmod(uptimes_db, 0o600) os.chmod(uptimes_db, 0o600)
@ -148,6 +152,8 @@ def daemon(fork = True):
print('Creating socket...') print('Creating socket...')
sys.stdout.flush() sys.stdout.flush()
if os.path.exists(socket_file):
os.unlink(socket_file)
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.bind(socket_file) s.bind(socket_file)
# socket file permission 777 # socket file permission 777
@ -166,8 +172,7 @@ def daemon(fork = True):
print('Reading db...') print('Reading db...')
sys.stdout.flush() sys.stdout.flush()
readUpTime() timeInit()
print('Uptime before: %f' % uptime)
print('Uptimesd started') print('Uptimesd started')
sys.stdout.flush() sys.stdout.flush()
@ -206,8 +211,7 @@ def sigterm_handler(signo, frame):
sys.stdout.flush() sys.stdout.flush()
# update uptime # update uptime
updateTime() updateTime()
# close socket
s.close()
if os.path.exists(socket_file): if os.path.exists(socket_file):
os.unlink(socket_file) os.unlink(socket_file)
# exit # exit
@ -227,15 +231,18 @@ def comm(command):
if not os.path.exists(socket_file): if not os.path.exists(socket_file):
print('Uptimesd not running') print('Uptimesd not running')
sys.exit(1) sys.exit(1)
try:
# connect
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(socket_file)
# connect # send command
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.sendall(command.encode('utf-8'))
s.connect(socket_file) data = s.recv(1024)
return data.decode('utf-8')
# send command except:
s.sendall(command.encode('utf-8')) print('Uptimesd not running')
data = s.recv(1024) sys.exit(1)
return data.decode('utf-8')
if __name__ == '__main__': if __name__ == '__main__':
main() main()

1
uptimes.db Normal file
View file

@ -0,0 +1 @@
603.2700000000001