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) |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 32 | return result |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 33 | |
| 34 | |
| 35 | def parse_args(args): |
| 36 | parser = argparse.ArgumentParser() |
| 37 | parser.add_argument('-d', '--daemon', |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 38 | choices=('start', 'stop', 'status', |
| 39 | 'start_monitoring', 'stop_monitoring', |
| 40 | 'dump_ram_data'), |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 41 | default=None) |
| 42 | |
| 43 | parser.add_argument('-u', '--url', default='stdout://') |
| 44 | parser.add_argument('-t', '--timeout', type=float, default=1) |
koder aka kdanilov | 8097a4e | 2015-03-18 11:07:35 +0200 | [diff] [blame] | 45 | parser.add_argument('-l', '--list-sensors', action='store_true') |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 46 | parser.add_argument('sensors_config', type=argparse.FileType('r'), |
| 47 | default=None, nargs='?') |
| 48 | return parser.parse_args(args[1:]) |
| 49 | |
| 50 | |
| 51 | def daemon_main(required_sensors, opts): |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 52 | try: |
| 53 | source_id = str(required_sensors.pop('source_id')) |
| 54 | except KeyError: |
| 55 | source_id = None |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 56 | |
Alyona Kiseleva | 7f6de4f | 2015-04-21 01:04:20 +0300 | [diff] [blame] | 57 | sender = create_protocol(opts.url) |
| 58 | prev = {} |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 59 | next_data_record_time = time.time() |
Alyona Kiseleva | 7f6de4f | 2015-04-21 01:04:20 +0300 | [diff] [blame] | 60 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 61 | first_round = True |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 62 | while True: |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 63 | real_time = int(time.time()) |
| 64 | |
| 65 | if real_time < int(next_data_record_time): |
| 66 | if int(next_data_record_time) - real_time > 2: |
| 67 | print "Error: sleep too small portion!!" |
| 68 | report_time = int(next_data_record_time) |
| 69 | elif real_time > int(next_data_record_time): |
| 70 | if real_time - int(next_data_record_time) > 2: |
| 71 | report_time = real_time |
| 72 | else: |
| 73 | report_time = int(next_data_record_time) |
| 74 | else: |
| 75 | report_time = real_time |
| 76 | |
| 77 | data = get_values(required_sensors.items()) |
| 78 | curr = {'time': SensorInfo(report_time, True)} |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 79 | for name, val in data.items(): |
| 80 | if val.is_accumulated: |
| 81 | if name in prev: |
| 82 | curr[name] = SensorInfo(val.value - prev[name], True) |
| 83 | prev[name] = val.value |
| 84 | else: |
| 85 | curr[name] = SensorInfo(val.value, False) |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 86 | |
| 87 | if source_id is not None: |
| 88 | curr['source_id'] = source_id |
| 89 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 90 | # on first round not all fields was ready |
| 91 | # this leads to setting wrong headers list |
| 92 | if not first_round: |
| 93 | sender.send(curr) |
| 94 | else: |
| 95 | first_round = False |
Alyona Kiseleva | 7f6de4f | 2015-04-21 01:04:20 +0300 | [diff] [blame] | 96 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 97 | next_data_record_time = report_time + opts.timeout + 0.5 |
| 98 | time.sleep(next_data_record_time - time.time()) |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 99 | |
| 100 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 101 | def pid_running(pid): |
| 102 | return os.path.exists("/proc/" + str(pid)) |
| 103 | |
| 104 | |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 105 | def main(argv): |
| 106 | opts = parse_args(argv) |
| 107 | |
koder aka kdanilov | 8097a4e | 2015-03-18 11:07:35 +0200 | [diff] [blame] | 108 | if opts.list_sensors: |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 109 | print "\n".join(sorted(all_sensors)) |
koder aka kdanilov | 8097a4e | 2015-03-18 11:07:35 +0200 | [diff] [blame] | 110 | return 0 |
| 111 | |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 112 | if opts.daemon is not None: |
| 113 | pid_file = "/tmp/sensors.pid" |
| 114 | if opts.daemon == 'start': |
| 115 | required_sensors = json.loads(opts.sensors_config.read()) |
| 116 | |
| 117 | def root_func(): |
| 118 | daemon_main(required_sensors, opts) |
| 119 | |
| 120 | daemon = Daemonize(app="perfcollect_app", |
| 121 | pid=pid_file, |
| 122 | action=root_func) |
| 123 | daemon.start() |
| 124 | elif opts.daemon == 'stop': |
| 125 | if os.path.isfile(pid_file): |
| 126 | pid = int(open(pid_file).read()) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 127 | if pid_running(pid): |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 128 | os.kill(pid, signal.SIGTERM) |
| 129 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 130 | time.sleep(0.5) |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 131 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 132 | if pid_running(pid): |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 133 | os.kill(pid, signal.SIGKILL) |
| 134 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 135 | time.sleep(0.5) |
| 136 | |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 137 | if os.path.isfile(pid_file): |
| 138 | os.unlink(pid_file) |
| 139 | elif opts.daemon == 'status': |
| 140 | if os.path.isfile(pid_file): |
| 141 | pid = int(open(pid_file).read()) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 142 | if pid_running(pid): |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 143 | print "running" |
| 144 | return |
| 145 | print "stopped" |
| 146 | else: |
| 147 | raise ValueError("Unknown daemon operation {}".format(opts.daemon)) |
| 148 | else: |
| 149 | required_sensors = json.loads(opts.sensors_config.read()) |
| 150 | daemon_main(required_sensors, opts) |
| 151 | return 0 |
| 152 | |
| 153 | if __name__ == "__main__": |
| 154 | exit(main(sys.argv)) |