blob: 4f0a8d11274344727c6aafc702384e775f1e7100 [file] [log] [blame]
koder aka kdanilovdda86d32015-03-16 11:20:04 +02001import os
2import sys
3import time
4import json
5import signal
6import os.path
7import argparse
8
9import io_sensors
10import net_sensors
Ved-vampir602a5172015-03-16 15:22:36 +030011import syscpu_sensors
Ved-vampirf0089e52015-03-16 15:52:54 +030012import sysram_sensors
Ved-vampirf2e13832015-03-17 13:24:18 +030013import pscpu_sensors
Ved-vampir98a99172015-03-17 14:58:15 +030014import psram_sensors
koder aka kdanilovdda86d32015-03-16 11:20:04 +020015
16from utils import SensorInfo
17from daemonize import Daemonize
18from discover import all_sensors
19from protocol import create_protocol
20
21
22def get_values(required_sensors):
23 result = {}
24 for sensor_name, params in required_sensors:
25 if sensor_name in all_sensors:
26 result.update(all_sensors[sensor_name](**params))
27 else:
28 msg = "Sensor {0!r} isn't available".format(sensor_name)
29 raise ValueError(msg)
30 return time.time(), result
31
32
33def parse_args(args):
34 parser = argparse.ArgumentParser()
35 parser.add_argument('-d', '--daemon',
36 choices=('start', 'stop', 'status'),
37 default=None)
38
39 parser.add_argument('-u', '--url', default='stdout://')
40 parser.add_argument('-t', '--timeout', type=float, default=1)
41 parser.add_argument('sensors_config', type=argparse.FileType('r'),
42 default=None, nargs='?')
43 return parser.parse_args(args[1:])
44
45
46def daemon_main(required_sensors, opts):
47 sender = create_protocol(opts.url)
48 prev = {}
49
50 while True:
51 gtime, data = get_values(required_sensors.items())
52 curr = {'time': SensorInfo(gtime, True)}
53 for name, val in data.items():
54 if val.is_accumulated:
55 if name in prev:
56 curr[name] = SensorInfo(val.value - prev[name], True)
57 prev[name] = val.value
58 else:
59 curr[name] = SensorInfo(val.value, False)
60 sender.send(curr)
61 time.sleep(opts.timeout)
62
63
64def main(argv):
65 opts = parse_args(argv)
66
67 if opts.daemon is not None:
68 pid_file = "/tmp/sensors.pid"
69 if opts.daemon == 'start':
70 required_sensors = json.loads(opts.sensors_config.read())
71
72 def root_func():
73 daemon_main(required_sensors, opts)
74
75 daemon = Daemonize(app="perfcollect_app",
76 pid=pid_file,
77 action=root_func)
78 daemon.start()
79 elif opts.daemon == 'stop':
80 if os.path.isfile(pid_file):
81 pid = int(open(pid_file).read())
82 if os.path.exists("/proc/" + str(pid)):
83 os.kill(pid, signal.SIGTERM)
84
85 time.sleep(0.1)
86
87 if os.path.exists("/proc/" + str(pid)):
88 os.kill(pid, signal.SIGKILL)
89
90 if os.path.isfile(pid_file):
91 os.unlink(pid_file)
92 elif opts.daemon == 'status':
93 if os.path.isfile(pid_file):
94 pid = int(open(pid_file).read())
95 if os.path.exists("/proc/" + str(pid)):
96 print "running"
97 return
98 print "stopped"
99 else:
100 raise ValueError("Unknown daemon operation {}".format(opts.daemon))
101 else:
102 required_sensors = json.loads(opts.sensors_config.read())
103 daemon_main(required_sensors, opts)
104 return 0
105
106if __name__ == "__main__":
107 exit(main(sys.argv))