koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame^] | 1 | from typing import List, Dict, Tuple |
| 2 | from .test_run_class import TestRun |
| 3 | from . import sensors_rpc_plugin |
| 4 | |
| 5 | |
| 6 | plugin_fname = sensors_rpc_plugin.__file__.rsplit(".", 1)[0] + ".py" |
| 7 | SENSORS_PLUGIN_CODE = open(plugin_fname).read() |
| 8 | |
| 9 | |
| 10 | # TODO(koder): in case if node has more than one role sensor settigns might be incorrect |
| 11 | def start_sensors_stage(ctx: TestRun) -> None: |
| 12 | if 'sensors' not in ctx.config: |
| 13 | return |
| 14 | |
| 15 | per_role_config = {} |
| 16 | for name, val in ctx.config['sensors'].copy(): |
| 17 | if isinstance(val, str): |
| 18 | val = {vl.strip(): ".*" for vl in val.split(",")} |
| 19 | elif isinstance(val, list): |
| 20 | val = {vl: ".*" for vl in val} |
| 21 | per_role_config[name] = val |
| 22 | |
| 23 | if 'all' in per_role_config: |
| 24 | all_vl = per_role_config.pop('all') |
| 25 | all_roles = set(per_role_config) |
| 26 | |
| 27 | for node in ctx.nodes: |
| 28 | all_roles.update(node.info.roles) |
| 29 | |
| 30 | for name, vals in list(per_role_config.items()): |
| 31 | new_vals = all_roles.copy() |
| 32 | new_vals.update(vals) |
| 33 | per_role_config[name] = new_vals |
| 34 | |
| 35 | for node in ctx.nodes: |
| 36 | node_cfg = {} |
| 37 | for role in node.info.roles: |
| 38 | node_cfg.update(per_role_config.get(role, {})) |
| 39 | |
| 40 | if node_cfg: |
| 41 | node.conn.upload_plugin(SENSORS_PLUGIN_CODE) |
| 42 | ctx.sensors_run_on.add(node.info.node_id()) |
| 43 | node.conn.sensors.start() |
| 44 | |
| 45 | |
| 46 | def collect_sensors_stage(ctx: TestRun, stop: bool = True) -> None: |
| 47 | for node in ctx.nodes: |
| 48 | node_id = node.info.node_id() |
| 49 | if node_id in ctx.sensors_run_on: |
| 50 | |
| 51 | if stop: |
| 52 | data, collected_at = node.conn.sensors.stop() # type: Dict[Tuple[str, str], List[int]], List[float] |
| 53 | else: |
| 54 | data, collected_at = node.conn.sensors.get_updates() |
| 55 | |
| 56 | for (source_name, sensor_name), values in data.items(): |
| 57 | path = "metric/{}/{}/{}".format(node_id, source_name, sensor_name) |
| 58 | ctx.storage.append(path, values) |
| 59 | ctx.storage.append("metric/{}/collected_at".format(node_id), collected_at) |
| 60 | |
| 61 | |
| 62 | # def delta(func, only_upd=True): |
| 63 | # prev = {} |
| 64 | # while True: |
| 65 | # for dev_name, vals in func(): |
| 66 | # if dev_name not in prev: |
| 67 | # prev[dev_name] = {} |
| 68 | # for name, (val, _) in vals.items(): |
| 69 | # prev[dev_name][name] = val |
| 70 | # else: |
| 71 | # dev_prev = prev[dev_name] |
| 72 | # res = {} |
| 73 | # for stat_name, (val, accum_val) in vals.items(): |
| 74 | # if accum_val: |
| 75 | # if stat_name in dev_prev: |
| 76 | # delta = int(val) - int(dev_prev[stat_name]) |
| 77 | # if not only_upd or 0 != delta: |
| 78 | # res[stat_name] = str(delta) |
| 79 | # dev_prev[stat_name] = val |
| 80 | # elif not only_upd or '0' != val: |
| 81 | # res[stat_name] = val |
| 82 | # |
| 83 | # if only_upd and len(res) == 0: |
| 84 | # continue |
| 85 | # yield dev_name, res |
| 86 | # yield None, None |
| 87 | # |
| 88 | # |
| 89 | |
| 90 | |