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 timeInit():
global startTime, totalTime
with open(uptimes_db, 'r') as f:
totalTime = f.readline()
totalTime = float(totalTime)
print("totalTime: %f" % totalTime)
sys.stdout.flush()
startTime = readTime()
print("startTime: %f" % startTime)
sys.stdout.flush()
def updateTime(): def updateTime():
global uptime, startTime global uptime, startTime, totalTime
now = readUpTime() now = readTime()
if uptime == 0: totalTime += now - startTime
with open(uptimes_db, 'r') as f:
j = f.readline()
j = json.loads(j)
uptime = float(j['uptime'])
if startTime == 0:
startTime = now
uptime += now - startTime
pathExists()
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 # connect
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(socket_file) s.connect(socket_file)
# send command # send command
s.sendall(command.encode('utf-8')) s.sendall(command.encode('utf-8'))
data = s.recv(1024) data = s.recv(1024)
return data.decode('utf-8') return data.decode('utf-8')
except:
print('Uptimesd not running')
sys.exit(1)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

1
uptimes.db Normal file
View File

@ -0,0 +1 @@
603.2700000000001