Compare commits
10 commits
f81a440916
...
874252642a
Author | SHA1 | Date | |
---|---|---|---|
|
874252642a | ||
|
5d3886bb51 | ||
|
f24a4d6982 | ||
|
8516fa921c | ||
|
c22f0ce97c | ||
|
64ac9ad155 | ||
|
98d4802871 | ||
|
f094e57dba | ||
|
0d939165de | ||
|
b310d1391f |
5 changed files with 153 additions and 84 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
*.pkg.tar.zst
|
8
PKGBUILD
8
PKGBUILD
|
@ -1,6 +1,6 @@
|
||||||
# Maintainer: 1ridic <i@8f.al>
|
# Maintainer: 1ridic <i@8f.al>
|
||||||
pkgname=uptimes
|
pkgname=uptimes
|
||||||
pkgver=0.1.0
|
pkgver=0.1.11
|
||||||
pkgrel=1
|
pkgrel=1
|
||||||
pkgdesc="status of total uptime"
|
pkgdesc="status of total uptime"
|
||||||
arch=('any')
|
arch=('any')
|
||||||
|
@ -11,10 +11,10 @@ source=(
|
||||||
local://uptimes
|
local://uptimes
|
||||||
local://uptimesd.service
|
local://uptimesd.service
|
||||||
)
|
)
|
||||||
md5sums=('b2bac26fc929e533923497311f4de415'
|
md5sums=('d2e8cfc73a64173a4d0749fc31193be6'
|
||||||
'f598fdb7ab331933d3495c0bb53358c2')
|
'7a90889f87792317e70ebdce9dae7023')
|
||||||
|
|
||||||
package() {
|
package() {
|
||||||
install -Dm755 uptimes "$pkgdir/usr/bin/uptimes"
|
install -Dm755 uptimes "$pkgdir/usr/bin/uptimes"
|
||||||
install -Dm644 uptimesd.service "$pkgdir/usr/lib/systemd/system/uptimes.service"
|
install -Dm644 uptimesd.service "$pkgdir/usr/lib/systemd/system/uptimesd.service"
|
||||||
}
|
}
|
||||||
|
|
167
uptimes
167
uptimes
|
@ -8,21 +8,82 @@ import signal
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
startTime = 0
|
startTime = 0
|
||||||
|
totalTime = 0
|
||||||
uptime = 0
|
uptime = 0
|
||||||
s = 0
|
s = 0
|
||||||
|
|
||||||
socket_file = '/var/tmp/uptimesd.sock'
|
socket_file = '/run/uptimesd.sock'
|
||||||
uptimes_db = '/etc/uptimes/uptimes.db'
|
uptimes_db = '/etc/uptimes/uptimes.db'
|
||||||
|
|
||||||
|
def readTime():
|
||||||
|
global uptime,startTime
|
||||||
|
|
||||||
|
with open('/proc/uptime', 'r') as f:
|
||||||
|
uptime = f.readline()
|
||||||
|
uptime = uptime.split(' ')[0]
|
||||||
|
uptime = float(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():
|
||||||
|
global uptime, startTime, totalTime
|
||||||
|
now = readTime()
|
||||||
|
print("Now: %f" % now)
|
||||||
|
sys.stdout.flush()
|
||||||
|
print("Uptime: %f" % uptime)
|
||||||
|
sys.stdout.flush()
|
||||||
|
print("startTime: %f" % startTime)
|
||||||
|
sys.stdout.flush()
|
||||||
|
print("totalTime: %f" % totalTime)
|
||||||
|
sys.stdout.flush()
|
||||||
|
totalTime += now - startTime
|
||||||
|
startTime = now
|
||||||
|
print("totalTime: %f" % totalTime)
|
||||||
|
sys.stdout.flush()
|
||||||
|
with open(uptimes_db, 'w') as f:
|
||||||
|
f.write(str(totalTime))
|
||||||
|
|
||||||
|
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:
|
||||||
|
f.write('0')
|
||||||
|
os.chmod(uptimes_db, 0o600)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
global uptime, s
|
global uptime, s
|
||||||
parse = argparse.ArgumentParser(description='Uptimes')
|
parse = argparse.ArgumentParser(description='Uptimes')
|
||||||
parse.add_argument('-d', '--daemon', action='store_true', help='run as daemon')
|
parse.add_argument('-d', '--daemon', action='store_true', help='run as daemon')
|
||||||
|
parse.add_argument('-s', '--systemd', action='store_true', help='run as systemd service')
|
||||||
|
|
||||||
|
|
||||||
args = parse.parse_args()
|
args = parse.parse_args()
|
||||||
if args.daemon:
|
if args.daemon:
|
||||||
daemon()
|
daemon()
|
||||||
|
elif args.systemd:
|
||||||
|
daemon(fork=False)
|
||||||
else:
|
else:
|
||||||
# check socket file
|
# check socket file
|
||||||
if not os.path.exists(socket_file):
|
if not os.path.exists(socket_file):
|
||||||
|
@ -46,18 +107,19 @@ def main():
|
||||||
print('Total Uptime: %d years %d days %d hours %d minutes %d seconds' % (years, days, hours, minutes, seconds))
|
print('Total Uptime: %d years %d days %d hours %d minutes %d seconds' % (years, days, hours, minutes, seconds))
|
||||||
|
|
||||||
|
|
||||||
def readUpTime():
|
def daemon(fork = True):
|
||||||
global uptime,startTime
|
|
||||||
|
|
||||||
with open('/proc/uptime', 'r') as f:
|
|
||||||
uptime = f.readline()
|
|
||||||
uptime = uptime.split(' ')[0]
|
|
||||||
uptime = float(uptime)
|
|
||||||
return uptime
|
|
||||||
|
|
||||||
def daemon():
|
|
||||||
global uptime, now, s
|
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
|
# fork
|
||||||
|
if fork:
|
||||||
pid = os.fork()
|
pid = os.fork()
|
||||||
if pid > 0:
|
if pid > 0:
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
@ -65,16 +127,22 @@ def daemon():
|
||||||
os.chdir('/')
|
os.chdir('/')
|
||||||
os.setsid()
|
os.setsid()
|
||||||
os.umask(0)
|
os.umask(0)
|
||||||
|
# redirect
|
||||||
|
sys.stdout.flush()
|
||||||
|
sys.stderr.flush()
|
||||||
|
|
||||||
|
si = open('/dev/null', 'r')
|
||||||
|
so = open('/dev/null', 'a+')
|
||||||
|
se = open('/dev/null', 'a+')
|
||||||
|
|
||||||
|
os.dup2(si.fileno(), sys.stdin.fileno())
|
||||||
|
os.dup2(so.fileno(), sys.stdout.fileno())
|
||||||
|
os.dup2(se.fileno(), sys.stderr.fileno())
|
||||||
# fork again
|
# fork again
|
||||||
pid = os.fork()
|
pid = os.fork()
|
||||||
if pid > 0:
|
if pid > 0:
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
# change process name
|
|
||||||
import setproctitle
|
|
||||||
setproctitle.setproctitle('uptimesd')
|
|
||||||
|
|
||||||
# redirect
|
# redirect
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
sys.stderr.flush()
|
sys.stderr.flush()
|
||||||
|
@ -87,15 +155,20 @@ def daemon():
|
||||||
os.dup2(so.fileno(), sys.stdout.fileno())
|
os.dup2(so.fileno(), sys.stdout.fileno())
|
||||||
os.dup2(se.fileno(), sys.stderr.fileno())
|
os.dup2(se.fileno(), sys.stderr.fileno())
|
||||||
|
|
||||||
# create socket file
|
|
||||||
try:
|
|
||||||
os.unlink(socket_file)
|
|
||||||
except OSError:
|
|
||||||
if os.path.exists(socket_file):
|
|
||||||
raise
|
|
||||||
|
|
||||||
|
# change process name
|
||||||
|
import setproctitle
|
||||||
|
setproctitle.setproctitle('uptimesd')
|
||||||
|
|
||||||
|
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 = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||||
s.bind(socket_file)
|
s.bind(socket_file)
|
||||||
|
# socket file permission 777
|
||||||
|
os.chmod(socket_file, 0o777)
|
||||||
s.listen(1)
|
s.listen(1)
|
||||||
|
|
||||||
# handle signal
|
# handle signal
|
||||||
|
@ -104,27 +177,15 @@ def daemon():
|
||||||
signal.signal(signal.SIGQUIT, sigterm_handler)
|
signal.signal(signal.SIGQUIT, sigterm_handler)
|
||||||
signal.signal(signal.SIGPIPE, sigterm_handler)
|
signal.signal(signal.SIGPIPE, sigterm_handler)
|
||||||
|
|
||||||
# if /etc/uptimes/uptimes.db exist
|
pathExists()
|
||||||
if os.path.exists(uptimes_db):
|
# read uptime
|
||||||
with open(uptimes_db, 'r') as f:
|
|
||||||
uptime = f.readline()
|
|
||||||
if uptime == '':
|
|
||||||
uptime = 0
|
|
||||||
else:
|
|
||||||
uptime = float(uptime)
|
|
||||||
else:
|
|
||||||
|
|
||||||
folder = uptimes_db.split('/')
|
print('Reading db...')
|
||||||
folder.pop()
|
sys.stdout.flush()
|
||||||
folder = '/'.join(folder)
|
|
||||||
if not os.path.exists(folder):
|
|
||||||
# create folder with 600
|
|
||||||
os.mkdir(folder, 0o600)
|
|
||||||
# create db with 600
|
|
||||||
with open(uptimes_db, 'w') as f:
|
|
||||||
f.write(str(uptime))
|
|
||||||
|
|
||||||
startTime = uptime
|
timeInit()
|
||||||
|
print('Uptimesd started')
|
||||||
|
sys.stdout.flush()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
conn, addr = s.accept()
|
conn, addr = s.accept()
|
||||||
|
@ -146,7 +207,7 @@ def daemon():
|
||||||
|
|
||||||
elif data == b'get':
|
elif data == b'get':
|
||||||
updateTime()
|
updateTime()
|
||||||
conn.sendall(b'%f' % uptime)
|
conn.sendall(b'%f' % totalTime)
|
||||||
conn.close()
|
conn.close()
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
@ -155,21 +216,24 @@ def daemon():
|
||||||
conn.close()
|
conn.close()
|
||||||
continue
|
continue
|
||||||
|
|
||||||
def updateTime():
|
|
||||||
global uptime, startTime
|
|
||||||
now = readUpTime()
|
|
||||||
uptime += now - startTime
|
|
||||||
with open(uptimes_db, 'w') as f:
|
|
||||||
f.write(str(uptime))
|
|
||||||
|
|
||||||
def sigterm_handler(signo, frame):
|
def sigterm_handler(signo, frame):
|
||||||
|
print('Uptimesd stopping...')
|
||||||
|
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
|
||||||
|
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)
|
sys.exit(0)
|
||||||
|
|
||||||
def comm(command):
|
def comm(command):
|
||||||
|
@ -178,7 +242,7 @@ 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)
|
||||||
|
@ -187,6 +251,9 @@ def comm(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
1
uptimes.db
Normal file
|
@ -0,0 +1 @@
|
||||||
|
603.2700000000001
|
|
@ -5,6 +5,6 @@ After=network.target network-online.target
|
||||||
[Service]
|
[Service]
|
||||||
Type=simple
|
Type=simple
|
||||||
User=root
|
User=root
|
||||||
ExecStart=/usr/bin/uptimes --daemon
|
ExecStart=/usr/bin/uptimes --systemd
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
Loading…
Reference in a new issue