koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import time |
| 4 | import json |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 5 | import glob |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 6 | import signal |
| 7 | import os.path |
| 8 | import argparse |
| 9 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 10 | from sensors.utils import SensorInfo |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 11 | from daemonize import Daemonize |
| 12 | from discover import all_sensors |
| 13 | from protocol import create_protocol |
| 14 | |
| 15 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 16 | # load all sensors |
| 17 | import sensors |
| 18 | sensors_dir = os.path.dirname(sensors.__file__) |
| 19 | for 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 kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 24 | def 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 | |
| 35 | def 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 kdanilov | 8097a4e | 2015-03-18 11:07:35 +0200 | [diff] [blame] | 43 | parser.add_argument('-l', '--list-sensors', action='store_true') |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 44 | parser.add_argument('sensors_config', type=argparse.FileType('r'), |
| 45 | default=None, nargs='?') |
| 46 | return parser.parse_args(args[1:]) |
| 47 | |
| 48 | |
| 49 | def 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 kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame^] | 67 | def pid_running(pid): |
| 68 | return os.path.exists("/proc/" + str(pid)) |
| 69 | |
| 70 | |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 71 | def main(argv): |
| 72 | opts = parse_args(argv) |
| 73 | |
koder aka kdanilov | 8097a4e | 2015-03-18 11:07:35 +0200 | [diff] [blame] | 74 | if opts.list_sensors: |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 75 | print "\n".join(sorted(all_sensors)) |
koder aka kdanilov | 8097a4e | 2015-03-18 11:07:35 +0200 | [diff] [blame] | 76 | return 0 |
| 77 | |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 78 | 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 kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame^] | 93 | if pid_running(pid): |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 94 | os.kill(pid, signal.SIGTERM) |
| 95 | |
| 96 | time.sleep(0.1) |
| 97 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame^] | 98 | if pid_running(pid): |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 99 | 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 kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame^] | 106 | if pid_running(pid): |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 107 | 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 | |
| 117 | if __name__ == "__main__": |
| 118 | exit(main(sys.argv)) |