blob: 2b6ab8ba16a2b50bd68ef8bb08e6cc928c25275c [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 kdanilovcff7b2e2015-04-18 20:48:15 +030010from .sensors.utils import SensorInfo
11from .daemonize import Daemonize
12from .discover import all_sensors
13from .protocol import create_protocol
koder aka kdanilovdda86d32015-03-16 11:20:04 +020014
15
koder aka kdanilove06762a2015-03-22 23:32:09 +020016# load all sensors
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030017from . import sensors
koder aka kdanilove06762a2015-03-22 23:32:09 +020018sensors_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])
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030021 __import__("sensors.sensors." + mod_name)
koder aka kdanilove06762a2015-03-22 23:32:09 +020022
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
koder aka kdanilove87ae652015-04-20 02:14:35 +030053 try:
54 source_id = str(required_sensors.pop('source_id'))
55 except KeyError:
56 source_id = None
koder aka kdanilov168f6092015-04-19 02:33:38 +030057
koder aka kdanilove87ae652015-04-20 02:14:35 +030058 while True:
koder aka kdanilovdda86d32015-03-16 11:20:04 +020059 gtime, data = get_values(required_sensors.items())
60 curr = {'time': SensorInfo(gtime, True)}
61 for name, val in data.items():
62 if val.is_accumulated:
63 if name in prev:
64 curr[name] = SensorInfo(val.value - prev[name], True)
65 prev[name] = val.value
66 else:
67 curr[name] = SensorInfo(val.value, False)
koder aka kdanilov168f6092015-04-19 02:33:38 +030068
69 if source_id is not None:
70 curr['source_id'] = source_id
71
koder aka kdanilovdda86d32015-03-16 11:20:04 +020072 sender.send(curr)
73 time.sleep(opts.timeout)
74
75
koder aka kdanilov2c473092015-03-29 17:12:13 +030076def pid_running(pid):
77 return os.path.exists("/proc/" + str(pid))
78
79
koder aka kdanilovdda86d32015-03-16 11:20:04 +020080def main(argv):
81 opts = parse_args(argv)
82
koder aka kdanilov8097a4e2015-03-18 11:07:35 +020083 if opts.list_sensors:
koder aka kdanilove06762a2015-03-22 23:32:09 +020084 print "\n".join(sorted(all_sensors))
koder aka kdanilov8097a4e2015-03-18 11:07:35 +020085 return 0
86
koder aka kdanilovdda86d32015-03-16 11:20:04 +020087 if opts.daemon is not None:
88 pid_file = "/tmp/sensors.pid"
89 if opts.daemon == 'start':
90 required_sensors = json.loads(opts.sensors_config.read())
91
92 def root_func():
93 daemon_main(required_sensors, opts)
94
95 daemon = Daemonize(app="perfcollect_app",
96 pid=pid_file,
97 action=root_func)
98 daemon.start()
99 elif opts.daemon == 'stop':
100 if os.path.isfile(pid_file):
101 pid = int(open(pid_file).read())
koder aka kdanilov2c473092015-03-29 17:12:13 +0300102 if pid_running(pid):
koder aka kdanilovdda86d32015-03-16 11:20:04 +0200103 os.kill(pid, signal.SIGTERM)
104
105 time.sleep(0.1)
106
koder aka kdanilov2c473092015-03-29 17:12:13 +0300107 if pid_running(pid):
koder aka kdanilovdda86d32015-03-16 11:20:04 +0200108 os.kill(pid, signal.SIGKILL)
109
110 if os.path.isfile(pid_file):
111 os.unlink(pid_file)
112 elif opts.daemon == 'status':
113 if os.path.isfile(pid_file):
114 pid = int(open(pid_file).read())
koder aka kdanilov2c473092015-03-29 17:12:13 +0300115 if pid_running(pid):
koder aka kdanilovdda86d32015-03-16 11:20:04 +0200116 print "running"
117 return
118 print "stopped"
119 else:
120 raise ValueError("Unknown daemon operation {}".format(opts.daemon))
121 else:
122 required_sensors = json.loads(opts.sensors_config.read())
123 daemon_main(required_sensors, opts)
124 return 0
125
126if __name__ == "__main__":
127 exit(main(sys.argv))