blob: 3c953fa4215e0c340db4efec3c165ac701d36e29 [file] [log] [blame]
koder aka kdanilovdda86d32015-03-16 11:20:04 +02001import os
2import sys
3import time
4import json
koder aka kdanilove06762a2015-03-22 23:32:09 +02005import glob
koder aka kdanilovdda86d32015-03-16 11:20:04 +02006import signal
7import os.path
8import argparse
9
koder aka kdanilove06762a2015-03-22 23:32:09 +020010from sensors.utils import SensorInfo
koder aka kdanilovdda86d32015-03-16 11:20:04 +020011from daemonize import Daemonize
12from discover import all_sensors
13from protocol import create_protocol
14
15
koder aka kdanilove06762a2015-03-22 23:32:09 +020016# load all sensors
17import sensors
18sensors_dir = os.path.dirname(sensors.__file__)
19for fname in glob.glob(os.path.join(sensors_dir, "*.py")):
20 mod_name = os.path.basename(fname[:-3])
21 __import__("sensors." + mod_name)
22
23
koder aka kdanilovdda86d32015-03-16 11:20:04 +020024def 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
koder aka kdanilov2c473092015-03-29 17:12:13 +030067def pid_running(pid):
68 return os.path.exists("/proc/" + str(pid))
69
70
koder aka kdanilovdda86d32015-03-16 11:20:04 +020071def main(argv):
72 opts = parse_args(argv)
73
koder aka kdanilov8097a4e2015-03-18 11:07:35 +020074 if opts.list_sensors:
koder aka kdanilove06762a2015-03-22 23:32:09 +020075 print "\n".join(sorted(all_sensors))
koder aka kdanilov8097a4e2015-03-18 11:07:35 +020076 return 0
77
koder aka kdanilovdda86d32015-03-16 11:20:04 +020078 if opts.daemon is not None:
79 pid_file = "/tmp/sensors.pid"
80 if opts.daemon == 'start':
81 required_sensors = json.loads(opts.sensors_config.read())
82
83 def root_func():
84 daemon_main(required_sensors, opts)
85
86 daemon = Daemonize(app="perfcollect_app",
87 pid=pid_file,
88 action=root_func)
89 daemon.start()
90 elif opts.daemon == 'stop':
91 if os.path.isfile(pid_file):
92 pid = int(open(pid_file).read())
koder aka kdanilov2c473092015-03-29 17:12:13 +030093 if pid_running(pid):
koder aka kdanilovdda86d32015-03-16 11:20:04 +020094 os.kill(pid, signal.SIGTERM)
95
96 time.sleep(0.1)
97
koder aka kdanilov2c473092015-03-29 17:12:13 +030098 if pid_running(pid):
koder aka kdanilovdda86d32015-03-16 11:20:04 +020099 os.kill(pid, signal.SIGKILL)
100
101 if os.path.isfile(pid_file):
102 os.unlink(pid_file)
103 elif opts.daemon == 'status':
104 if os.path.isfile(pid_file):
105 pid = int(open(pid_file).read())
koder aka kdanilov2c473092015-03-29 17:12:13 +0300106 if pid_running(pid):
koder aka kdanilovdda86d32015-03-16 11:20:04 +0200107 print "running"
108 return
109 print "stopped"
110 else:
111 raise ValueError("Unknown daemon operation {}".format(opts.daemon))
112 else:
113 required_sensors = json.loads(opts.sensors_config.read())
114 daemon_main(required_sensors, opts)
115 return 0
116
117if __name__ == "__main__":
118 exit(main(sys.argv))