-
Notifications
You must be signed in to change notification settings - Fork 7
/
pkwatch
executable file
·55 lines (49 loc) · 1.73 KB
/
pkwatch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
PKMeter Desktop System Monitor
Author: M.Shepanski (Apr 2014)
"""
import os, signal, sys
import json, subprocess, time
from os.path import abspath, dirname, join
# Add pkm to sys.path if not already there. Useful when running
# this application without officially installing it.
if dirname(__file__) not in sys.path:
sys.path.append(dirname(__file__))
from pkm import CONFIGPATH, STATUSFILE
from pkm import log
PKMETER = join(dirname(abspath(__file__)), 'pkmeter')
def safe_kill(p):
try:
os.kill(p.pid, signal.SIGINT)
except:
pass
if __name__ == '__main__':
with open(CONFIGPATH, 'r') as handle:
config = json.load(handle)
intervals = {p:v.get('interval',60) for p,v in config.items()}
p = subprocess.Popen(['/usr/bin/python3', PKMETER])
time.sleep(20)
while True:
try:
time.sleep(10)
with open(STATUSFILE, 'r') as handle:
status = json.load(handle)
for plugin, lastupdate in status.items():
timeago = int(time.time() - lastupdate)
timeout = int(max(30, intervals.get(plugin,60) * 3))
if timeago > timeout:
log.info('%s lastupdate %ss ago (timeout=%s), restarting.' % (plugin, timeago, timeout))
safe_kill(p)
time.sleep(2)
break
if p.poll() is not None:
p = subprocess.Popen(['/usr/bin/python3', PKMETER])
time.sleep(20)
except KeyboardInterrupt:
log.info('KeyboardInterrupt: Quitting..')
break
except Exception as err:
log.error('PKWatch Error: %s', err)
continue