change format to json

This commit is contained in:
iridiumR 2023-08-07 23:15:58 +08:00
parent c22f0ce97c
commit 8516fa921c
No known key found for this signature in database
GPG key ID: 49735733EB1A32C8
2 changed files with 71 additions and 52 deletions

View file

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

117
uptimes
View file

@ -6,6 +6,7 @@ import sys
import argparse
import signal
import socket
import json
startTime = 0
uptime = 0
@ -14,6 +15,48 @@ s = 0
socket_file = '/run/uptimesd.sock'
uptimes_db = '/etc/uptimes/uptimes.db'
def readUpTime():
global uptime,startTime
with open('/proc/uptime', 'r') as f:
uptime = f.readline()
uptime = uptime.split(' ')[0]
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'])
if startTime == 0:
startTime = now
uptime += now - startTime
pathExists()
with open(uptimes_db, 'w') as f:
j = json.dumps({'uptime': uptime})
f.write(j)
def pathExists():
# if /etc/uptimes/uptimes.db exist
if os.path.exists(uptimes_db):
return True
else:
folder = uptimes_db.split('/')
folder.pop()
folder = '/'.join(folder)
if not os.path.exists(folder):
# create folder with 600
os.mkdir(folder, 0o600)
# create uptimes.db with 600
with open(uptimes_db, 'w') as f:
os.chmod(uptimes_db, 0o600)
def main():
global uptime, s
parse = argparse.ArgumentParser(description='Uptimes')
@ -49,50 +92,15 @@ def main():
print('Total Uptime: %d years %d days %d hours %d minutes %d seconds' % (years, days, hours, minutes, seconds))
def readUpTime():
global uptime,startTime
with open('/proc/uptime', 'r') as f:
uptime = f.readline()
uptime = uptime.split(' ')[0]
uptime = float(uptime)
return uptime
def pathExists():
# if /etc/uptimes/uptimes.db exist
if os.path.exists(uptimes_db):
# check permissions
if os.stat(uptimes_db).st_mode != 0o600:
# change permissions
os.chmod(uptimes_db, 0o600)
# check the content
with open(uptimes_db, 'r') as f:
content = f.readline()
if content == '':
# write 0
with open(uptimes_db, 'w') as f:
f.write('0')
os.chmod(uptimes_db, 0o600)
else:
folder = uptimes_db.split('/')
folder.pop()
folder = '/'.join(folder)
if not os.path.exists(folder):
# create folder with 600
os.mkdir(folder, 0o600)
# create uptimes.db with 600
with open(uptimes_db, 'w') as f:
f.write('0')
os.chmod(uptimes_db, 0o600)
def daemon(fork = True):
global uptime, now, s
print('Uptimesd starting...')
sys.stdout.flush()
if os.path.exists(socket_file):
print('Uptimesd already running')
sys.stdout.flush()
sys.exit(1)
# fork
@ -137,6 +145,9 @@ def daemon(fork = True):
import setproctitle
setproctitle.setproctitle('uptimesd')
print('Creating socket...')
sys.stdout.flush()
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.bind(socket_file)
# socket file permission 777
@ -151,9 +162,14 @@ def daemon(fork = True):
pathExists()
# read uptime
with open(uptimes_db, 'r') as f:
uptime = f.readline()
uptime = float(uptime)
print('Reading db...')
sys.stdout.flush()
readUpTime()
print('Uptime before: %f' % uptime)
print('Uptimesd started')
sys.stdout.flush()
while True:
conn, addr = s.accept()
@ -184,15 +200,10 @@ def daemon(fork = True):
conn.close()
continue
def updateTime():
global uptime, startTime
now = readUpTime()
uptime += now - startTime
pathExists()
with open(uptimes_db, 'w') as f:
f.write(str(uptime))
def sigterm_handler(signo, frame):
print('Uptimesd stopping...')
sys.stdout.flush()
# update uptime
updateTime()
# close socket
@ -200,6 +211,14 @@ def sigterm_handler(signo, frame):
if os.path.exists(socket_file):
os.unlink(socket_file)
# exit
print('Uptimesd stopped. Goodbye.')
sys.stdout.flush()
with open(uptimes_db, 'r') as f:
uptime = f.readline()
print('Total Uptime: ' + uptime + ' seconds')
sys.stdout.flush()
sys.exit(0)
def comm(command):