koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 1 | import array |
| 2 | import logging |
koder aka kdanilov | 3af3c33 | 2016-12-19 17:12:34 +0200 | [diff] [blame^] | 3 | import collections |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 4 | from typing import List, Dict, Tuple |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 5 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 6 | from . import utils |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 7 | from .test_run_class import TestRun |
| 8 | from . import sensors_rpc_plugin |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 9 | from .stage import Stage, StepOrder |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 10 | |
| 11 | plugin_fname = sensors_rpc_plugin.__file__.rsplit(".", 1)[0] + ".py" |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 12 | SENSORS_PLUGIN_CODE = open(plugin_fname, "rb").read() |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 13 | |
| 14 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 15 | logger = logging.getLogger("wally") |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 16 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 17 | |
| 18 | # TODO(koder): in case if node has more than one role sensor settings might be incorrect |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 19 | class StartSensorsStage(Stage): |
| 20 | priority = StepOrder.START_SENSORS |
| 21 | config_block = 'sensors' |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 22 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 23 | def run(self, ctx: TestRun) -> None: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 24 | 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 kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 29 | |
| 30 | per_role_config = {} # type: Dict[str, Dict[str, str]] |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 31 | |
| 32 | for name, val in ctx.config.sensors.roles_mapping.raw().items(): |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 33 | 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 kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 50 | |
| 51 | for node in ctx.nodes: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 52 | node_cfg = {} # type: Dict[str, Dict[str, str]] |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 53 | for role in node.info.roles: |
| 54 | node_cfg.update(per_role_config.get(role, {})) |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 55 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 56 | nid = node.info.node_id() |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 57 | if node_cfg: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 58 | # 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 | |
| 72 | def 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 kdanilov | 3af3c33 | 2016-12-19 17:12:34 +0200 | [diff] [blame^] | 82 | # 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 kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 85 | |
| 86 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 87 | class CollectSensorsStage(Stage): |
| 88 | priority = StepOrder.COLLECT_SENSORS |
| 89 | config_block = 'sensors' |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 90 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 91 | def run(self, ctx: TestRun) -> None: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 92 | collect_sensors_data(ctx, True) |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 93 | |
| 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 | |