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