blob: c86aeb4b5feeaaace4418942ea2be1ec31ed1623 [file] [log] [blame]
koder aka kdanilov39e449e2016-12-17 15:15:26 +02001from typing import List, Dict, Tuple, Any
2
koder aka kdanilov70227062016-11-26 23:23:21 +02003from .test_run_class import TestRun
4from . import sensors_rpc_plugin
koder aka kdanilov39e449e2016-12-17 15:15:26 +02005from .stage import Stage, StepOrder
koder aka kdanilov70227062016-11-26 23:23:21 +02006
7plugin_fname = sensors_rpc_plugin.__file__.rsplit(".", 1)[0] + ".py"
8SENSORS_PLUGIN_CODE = open(plugin_fname).read()
9
10
11# TODO(koder): in case if node has more than one role sensor settigns might be incorrect
koder aka kdanilov70227062016-11-26 23:23:21 +020012
koder aka kdanilov39e449e2016-12-17 15:15:26 +020013class StartSensorsStage(Stage):
14 priority = StepOrder.START_SENSORS
15 config_block = 'sensors'
koder aka kdanilov70227062016-11-26 23:23:21 +020016
koder aka kdanilov39e449e2016-12-17 15:15:26 +020017 def run(self, ctx: TestRun) -> None:
18 if 'sensors' not in ctx.config:
19 return
20
21 per_role_config = {} # type: Dict[str, Dict[str, str]]
22 for name, val in ctx.config['sensors'].copy():
23 if isinstance(val, str):
24 val = {vl.strip(): ".*" for vl in val.split(",")}
25 elif isinstance(val, list):
26 val = {vl: ".*" for vl in val}
27 per_role_config[name] = val
28
29 if 'all' in per_role_config:
30 all_vl = per_role_config.pop('all')
31 all_roles = set(per_role_config)
32
33 for node in ctx.nodes:
34 all_roles.update(node.info.roles)
35
36 for name, vals in list(per_role_config.items()):
37 new_vals = all_vl.copy()
38 new_vals.update(vals)
39 per_role_config[name] = new_vals
koder aka kdanilov70227062016-11-26 23:23:21 +020040
41 for node in ctx.nodes:
koder aka kdanilov39e449e2016-12-17 15:15:26 +020042 node_cfg = {} # type: Dict[str, str]
43 for role in node.info.roles:
44 node_cfg.update(per_role_config.get(role, {}))
koder aka kdanilov70227062016-11-26 23:23:21 +020045
koder aka kdanilov39e449e2016-12-17 15:15:26 +020046 if node_cfg:
47 node.conn.upload_plugin(SENSORS_PLUGIN_CODE)
48 ctx.sensors_run_on.add(node.info.node_id())
49 node.conn.sensors.start()
koder aka kdanilov70227062016-11-26 23:23:21 +020050
51
koder aka kdanilov39e449e2016-12-17 15:15:26 +020052class CollectSensorsStage(Stage):
53 priority = StepOrder.COLLECT_SENSORS
54 config_block = 'sensors'
koder aka kdanilov70227062016-11-26 23:23:21 +020055
koder aka kdanilov39e449e2016-12-17 15:15:26 +020056 def run(self, ctx: TestRun) -> None:
57 for node in ctx.nodes:
58 node_id = node.info.node_id()
59 if node_id in ctx.sensors_run_on:
60
koder aka kdanilov70227062016-11-26 23:23:21 +020061 data, collected_at = node.conn.sensors.stop() # type: Dict[Tuple[str, str], List[int]], List[float]
koder aka kdanilov70227062016-11-26 23:23:21 +020062
koder aka kdanilov39e449e2016-12-17 15:15:26 +020063 mstore = ctx.storage.sub_storage("metric", node_id)
64 for (source_name, sensor_name), values in data.items():
65 mstore[source_name, sensor_name] = values
66 mstore["collected_at"] = collected_at
koder aka kdanilov70227062016-11-26 23:23:21 +020067
68
69# def delta(func, only_upd=True):
70# prev = {}
71# while True:
72# for dev_name, vals in func():
73# if dev_name not in prev:
74# prev[dev_name] = {}
75# for name, (val, _) in vals.items():
76# prev[dev_name][name] = val
77# else:
78# dev_prev = prev[dev_name]
79# res = {}
80# for stat_name, (val, accum_val) in vals.items():
81# if accum_val:
82# if stat_name in dev_prev:
83# delta = int(val) - int(dev_prev[stat_name])
84# if not only_upd or 0 != delta:
85# res[stat_name] = str(delta)
86# dev_prev[stat_name] = val
87# elif not only_upd or '0' != val:
88# res[stat_name] = val
89#
90# if only_upd and len(res) == 0:
91# continue
92# yield dev_name, res
93# yield None, None
94#
95#
96
97