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 | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 10 | from .sensors.utils import SensorInfo |
| 11 | from .daemonize import Daemonize |
| 12 | from .discover import all_sensors |
| 13 | from .protocol import create_protocol |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 14 | |
| 15 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 16 | # load all sensors |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 17 | from . import sensors |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 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]) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 21 | __import__("sensors.sensors." + mod_name) |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 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 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame^] | 53 | try: |
| 54 | source_id = str(required_sensors.pop('source_id')) |
| 55 | except KeyError: |
| 56 | source_id = None |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 57 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame^] | 58 | while True: |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 59 | 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 kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 68 | |
| 69 | if source_id is not None: |
| 70 | curr['source_id'] = source_id |
| 71 | |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 72 | sender.send(curr) |
| 73 | time.sleep(opts.timeout) |
| 74 | |
| 75 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 76 | def pid_running(pid): |
| 77 | return os.path.exists("/proc/" + str(pid)) |
| 78 | |
| 79 | |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 80 | def main(argv): |
| 81 | opts = parse_args(argv) |
| 82 | |
koder aka kdanilov | 8097a4e | 2015-03-18 11:07:35 +0200 | [diff] [blame] | 83 | if opts.list_sensors: |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 84 | print "\n".join(sorted(all_sensors)) |
koder aka kdanilov | 8097a4e | 2015-03-18 11:07:35 +0200 | [diff] [blame] | 85 | return 0 |
| 86 | |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 87 | 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 kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 102 | if pid_running(pid): |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 103 | os.kill(pid, signal.SIGTERM) |
| 104 | |
| 105 | time.sleep(0.1) |
| 106 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 107 | if pid_running(pid): |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 108 | 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 kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 115 | if pid_running(pid): |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 116 | 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 | |
| 126 | if __name__ == "__main__": |
| 127 | exit(main(sys.argv)) |