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