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 | |
koder aka kdanilov | 8097a4e | 2015-03-18 11:07:35 +0200 | [diff] [blame^] | 9 | # pylint: disable=W0611 |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 10 | import io_sensors |
| 11 | import net_sensors |
Ved-vampir | f2e1383 | 2015-03-17 13:24:18 +0300 | [diff] [blame] | 12 | import pscpu_sensors |
Ved-vampir | 98a9917 | 2015-03-17 14:58:15 +0300 | [diff] [blame] | 13 | import psram_sensors |
koder aka kdanilov | 8097a4e | 2015-03-18 11:07:35 +0200 | [diff] [blame^] | 14 | import syscpu_sensors |
| 15 | import sysram_sensors |
| 16 | # pylint: enable=W0611 |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 17 | |
| 18 | from utils import SensorInfo |
| 19 | from daemonize import Daemonize |
| 20 | from discover import all_sensors |
| 21 | from protocol import create_protocol |
| 22 | |
| 23 | |
| 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 | |
| 67 | def main(argv): |
| 68 | opts = parse_args(argv) |
| 69 | |
koder aka kdanilov | 8097a4e | 2015-03-18 11:07:35 +0200 | [diff] [blame^] | 70 | if opts.list_sensors: |
| 71 | print " ".join(all_sensors) |
| 72 | return 0 |
| 73 | |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 74 | if opts.daemon is not None: |
| 75 | pid_file = "/tmp/sensors.pid" |
| 76 | if opts.daemon == 'start': |
| 77 | required_sensors = json.loads(opts.sensors_config.read()) |
| 78 | |
| 79 | def root_func(): |
| 80 | daemon_main(required_sensors, opts) |
| 81 | |
| 82 | daemon = Daemonize(app="perfcollect_app", |
| 83 | pid=pid_file, |
| 84 | action=root_func) |
| 85 | daemon.start() |
| 86 | elif opts.daemon == 'stop': |
| 87 | if os.path.isfile(pid_file): |
| 88 | pid = int(open(pid_file).read()) |
| 89 | if os.path.exists("/proc/" + str(pid)): |
| 90 | os.kill(pid, signal.SIGTERM) |
| 91 | |
| 92 | time.sleep(0.1) |
| 93 | |
| 94 | if os.path.exists("/proc/" + str(pid)): |
| 95 | os.kill(pid, signal.SIGKILL) |
| 96 | |
| 97 | if os.path.isfile(pid_file): |
| 98 | os.unlink(pid_file) |
| 99 | elif opts.daemon == 'status': |
| 100 | if os.path.isfile(pid_file): |
| 101 | pid = int(open(pid_file).read()) |
| 102 | if os.path.exists("/proc/" + str(pid)): |
| 103 | print "running" |
| 104 | return |
| 105 | print "stopped" |
| 106 | else: |
| 107 | raise ValueError("Unknown daemon operation {}".format(opts.daemon)) |
| 108 | else: |
| 109 | required_sensors = json.loads(opts.sensors_config.read()) |
| 110 | daemon_main(required_sensors, opts) |
| 111 | return 0 |
| 112 | |
| 113 | if __name__ == "__main__": |
| 114 | exit(main(sys.argv)) |