blob: eef78646714dea5ce65ea007685f40785dd726d3 [file] [log] [blame]
koder aka kdanilov962ee5f2016-12-19 02:40:08 +02001import array
2import logging
koder aka kdanilov3af3c332016-12-19 17:12:34 +02003import collections
koder aka kdanilov962ee5f2016-12-19 02:40:08 +02004from typing import List, Dict, Tuple
koder aka kdanilov39e449e2016-12-17 15:15:26 +02005
koder aka kdanilov962ee5f2016-12-19 02:40:08 +02006from . import utils
koder aka kdanilov70227062016-11-26 23:23:21 +02007from .test_run_class import TestRun
8from . import sensors_rpc_plugin
koder aka kdanilov39e449e2016-12-17 15:15:26 +02009from .stage import Stage, StepOrder
koder aka kdanilov70227062016-11-26 23:23:21 +020010
11plugin_fname = sensors_rpc_plugin.__file__.rsplit(".", 1)[0] + ".py"
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020012SENSORS_PLUGIN_CODE = open(plugin_fname, "rb").read()
koder aka kdanilov70227062016-11-26 23:23:21 +020013
14
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020015logger = logging.getLogger("wally")
koder aka kdanilov70227062016-11-26 23:23:21 +020016
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020017
18# TODO(koder): in case if node has more than one role sensor settings might be incorrect
koder aka kdanilov39e449e2016-12-17 15:15:26 +020019class StartSensorsStage(Stage):
20 priority = StepOrder.START_SENSORS
21 config_block = 'sensors'
koder aka kdanilov70227062016-11-26 23:23:21 +020022
koder aka kdanilov39e449e2016-12-17 15:15:26 +020023 def run(self, ctx: TestRun) -> None:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020024 if array.array('L').itemsize != 8:
25 message = "Python array.array('L') items should be 8 bytes in size, not {}." + \
26 " Can't provide sensors on this platform. Disable sensors in config and retry"
27 logger.critical(message.format(array.array('L').itemsize))
28 raise utils.StopTestError()
koder aka kdanilov39e449e2016-12-17 15:15:26 +020029
30 per_role_config = {} # type: Dict[str, Dict[str, str]]
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020031
32 for name, val in ctx.config.sensors.roles_mapping.raw().items():
koder aka kdanilov39e449e2016-12-17 15:15:26 +020033 if isinstance(val, str):
34 val = {vl.strip(): ".*" for vl in val.split(",")}
35 elif isinstance(val, list):
36 val = {vl: ".*" for vl in val}
37 per_role_config[name] = val
38
39 if 'all' in per_role_config:
40 all_vl = per_role_config.pop('all')
41 all_roles = set(per_role_config)
42
43 for node in ctx.nodes:
44 all_roles.update(node.info.roles)
45
46 for name, vals in list(per_role_config.items()):
47 new_vals = all_vl.copy()
48 new_vals.update(vals)
49 per_role_config[name] = new_vals
koder aka kdanilov70227062016-11-26 23:23:21 +020050
51 for node in ctx.nodes:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020052 node_cfg = {} # type: Dict[str, Dict[str, str]]
koder aka kdanilov39e449e2016-12-17 15:15:26 +020053 for role in node.info.roles:
54 node_cfg.update(per_role_config.get(role, {}))
koder aka kdanilov70227062016-11-26 23:23:21 +020055
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020056 nid = node.info.node_id()
koder aka kdanilov39e449e2016-12-17 15:15:26 +020057 if node_cfg:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020058 # ceph requires additional settings
59 if 'ceph' in node_cfg:
60 node_cfg['ceph'].update(node.info.params['ceph'])
61 node_cfg['ceph']['osds'] = [osd.id for osd in node.info.params['ceph-osds']]
62
63 logger.debug("Setting up sensort RPC plugin for node %s", nid)
64 node.upload_plugin("sensors", SENSORS_PLUGIN_CODE)
65 ctx.sensors_run_on.add(nid)
66 logger.debug("Start monitoring node %s", nid)
67 node.conn.sensors.start(node_cfg)
68 else:
69 logger.debug("Skip monitoring node %s, as no sensors selected", nid)
70
71
72def collect_sensors_data(ctx: TestRun, stop: bool = False):
73 for node in ctx.nodes:
74 node_id = node.info.node_id()
75 if node_id in ctx.sensors_run_on:
76
77 if stop:
78 func = node.conn.sensors.stop
79 else:
80 func = node.conn.sensors.get_updates
81
koder aka kdanilov3af3c332016-12-19 17:12:34 +020082 # TODO: data is unpacked/repacked here with no reason
83 for path, value in sensors_rpc_plugin.unpack_rpc_updates(func()):
84 ctx.storage.append(value, "metric", node_id, path)
koder aka kdanilov70227062016-11-26 23:23:21 +020085
86
koder aka kdanilov39e449e2016-12-17 15:15:26 +020087class CollectSensorsStage(Stage):
88 priority = StepOrder.COLLECT_SENSORS
89 config_block = 'sensors'
koder aka kdanilov70227062016-11-26 23:23:21 +020090
koder aka kdanilov39e449e2016-12-17 15:15:26 +020091 def run(self, ctx: TestRun) -> None:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020092 collect_sensors_data(ctx, True)
koder aka kdanilov70227062016-11-26 23:23:21 +020093
94
95# def delta(func, only_upd=True):
96# prev = {}
97# while True:
98# for dev_name, vals in func():
99# if dev_name not in prev:
100# prev[dev_name] = {}
101# for name, (val, _) in vals.items():
102# prev[dev_name][name] = val
103# else:
104# dev_prev = prev[dev_name]
105# res = {}
106# for stat_name, (val, accum_val) in vals.items():
107# if accum_val:
108# if stat_name in dev_prev:
109# delta = int(val) - int(dev_prev[stat_name])
110# if not only_upd or 0 != delta:
111# res[stat_name] = str(delta)
112# dev_prev[stat_name] = val
113# elif not only_upd or '0' != val:
114# res[stat_name] = val
115#
116# if only_upd and len(res) == 0:
117# continue
118# yield dev_name, res
119# yield None, None
120#
121#
122
123