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