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