koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 1 | import array |
| 2 | import logging |
| 3 | from typing import List, Dict, Tuple |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 4 | |
koder aka kdanilov | a732a60 | 2017-02-01 20:29:56 +0200 | [diff] [blame] | 5 | import numpy |
| 6 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 7 | from . import utils |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 8 | from .test_run_class import TestRun |
koder aka kdanilov | a732a60 | 2017-02-01 20:29:56 +0200 | [diff] [blame] | 9 | from .result_classes import DataSource |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 10 | from . import sensors_rpc_plugin |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 11 | from .stage import Stage, StepOrder |
koder aka kdanilov | a732a60 | 2017-02-01 20:29:56 +0200 | [diff] [blame] | 12 | from .hlstorage import ResultStorage |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 13 | |
| 14 | plugin_fname = sensors_rpc_plugin.__file__.rsplit(".", 1)[0] + ".py" |
koder aka kdanilov | 23e6bdf | 2016-12-24 02:18:54 +0200 | [diff] [blame] | 15 | SENSORS_PLUGIN_CODE = open(plugin_fname, "rb").read() # type: bytes |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 16 | |
| 17 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 18 | logger = logging.getLogger("wally") |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 19 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 20 | |
koder aka kdanilov | a732a60 | 2017-02-01 20:29:56 +0200 | [diff] [blame] | 21 | sensor_units = { |
| 22 | "system-cpu.idle_time": "ms", |
| 23 | "system-cpu.nice_processes": "", |
| 24 | "system-cpu.procs_blocked": "", |
| 25 | "system-cpu.procs_queue_x10": "", |
| 26 | "system-cpu.system_processes": "", |
| 27 | "system-cpu.user_processes": "", |
| 28 | "net-io.recv_bytes": "B", |
| 29 | "net-io.recv_packets": "", |
| 30 | "net-io.send_bytes": "B", |
| 31 | "net-io.send_packets": "", |
| 32 | "block-io.io_queue": "", |
| 33 | "block-io.io_time": "ms", |
| 34 | "block-io.reads_completed": "", |
| 35 | "block-io.rtime": "ms", |
| 36 | "block-io.sectors_read": "B", |
| 37 | "block-io.sectors_written": "B", |
| 38 | "block-io.writes_completed": "", |
| 39 | "block-io.wtime": "ms" |
| 40 | } |
| 41 | |
| 42 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 43 | # 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] | 44 | class StartSensorsStage(Stage): |
| 45 | priority = StepOrder.START_SENSORS |
| 46 | config_block = 'sensors' |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 47 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 48 | def run(self, ctx: TestRun) -> None: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 49 | if array.array('L').itemsize != 8: |
| 50 | message = "Python array.array('L') items should be 8 bytes in size, not {}." + \ |
| 51 | " Can't provide sensors on this platform. Disable sensors in config and retry" |
| 52 | logger.critical(message.format(array.array('L').itemsize)) |
| 53 | raise utils.StopTestError() |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 54 | |
koder aka kdanilov | 23e6bdf | 2016-12-24 02:18:54 +0200 | [diff] [blame] | 55 | # TODO: need carefully fix this |
| 56 | # sensors config is: |
| 57 | # role: |
| 58 | # sensor: [str] |
| 59 | # or |
| 60 | # role: |
| 61 | # sensor: |
| 62 | # allowed: [str] |
| 63 | # dissallowed: [str] |
| 64 | # params: Any |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 65 | per_role_config = {} # type: Dict[str, Dict[str, str]] |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 66 | |
| 67 | for name, val in ctx.config.sensors.roles_mapping.raw().items(): |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 68 | if isinstance(val, str): |
| 69 | val = {vl.strip(): ".*" for vl in val.split(",")} |
| 70 | elif isinstance(val, list): |
| 71 | val = {vl: ".*" for vl in val} |
| 72 | per_role_config[name] = val |
| 73 | |
| 74 | if 'all' in per_role_config: |
| 75 | all_vl = per_role_config.pop('all') |
| 76 | all_roles = set(per_role_config) |
| 77 | |
| 78 | for node in ctx.nodes: |
koder aka kdanilov | 23e6bdf | 2016-12-24 02:18:54 +0200 | [diff] [blame] | 79 | all_roles.update(node.info.roles) # type: ignore |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 80 | |
| 81 | for name, vals in list(per_role_config.items()): |
| 82 | new_vals = all_vl.copy() |
| 83 | new_vals.update(vals) |
| 84 | per_role_config[name] = new_vals |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 85 | |
| 86 | for node in ctx.nodes: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 87 | node_cfg = {} # type: Dict[str, Dict[str, str]] |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 88 | for role in node.info.roles: |
koder aka kdanilov | 23e6bdf | 2016-12-24 02:18:54 +0200 | [diff] [blame] | 89 | node_cfg.update(per_role_config.get(role, {})) # type: ignore |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 90 | |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 91 | nid = node.node_id |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 92 | if node_cfg: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 93 | # ceph requires additional settings |
| 94 | if 'ceph' in node_cfg: |
| 95 | node_cfg['ceph'].update(node.info.params['ceph']) |
koder aka kdanilov | 23e6bdf | 2016-12-24 02:18:54 +0200 | [diff] [blame] | 96 | node_cfg['ceph']['osds'] = [osd.id for osd in node.info.params['ceph-osds']] # type: ignore |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 97 | |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame] | 98 | logger.debug("Setting up sensors RPC plugin for node %s", nid) |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 99 | node.upload_plugin("sensors", SENSORS_PLUGIN_CODE) |
| 100 | ctx.sensors_run_on.add(nid) |
| 101 | logger.debug("Start monitoring node %s", nid) |
| 102 | node.conn.sensors.start(node_cfg) |
| 103 | else: |
| 104 | logger.debug("Skip monitoring node %s, as no sensors selected", nid) |
| 105 | |
| 106 | |
| 107 | def collect_sensors_data(ctx: TestRun, stop: bool = False): |
koder aka kdanilov | a732a60 | 2017-02-01 20:29:56 +0200 | [diff] [blame] | 108 | rstorage = ResultStorage(ctx.storage) |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 109 | for node in ctx.nodes: |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 110 | node_id = node.node_id |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 111 | if node_id in ctx.sensors_run_on: |
| 112 | |
| 113 | if stop: |
| 114 | func = node.conn.sensors.stop |
| 115 | else: |
| 116 | func = node.conn.sensors.get_updates |
| 117 | |
koder aka kdanilov | a732a60 | 2017-02-01 20:29:56 +0200 | [diff] [blame] | 118 | # TODO: units should came along with data |
koder aka kdanilov | 3af3c33 | 2016-12-19 17:12:34 +0200 | [diff] [blame] | 119 | for path, value in sensors_rpc_plugin.unpack_rpc_updates(func()): |
koder aka kdanilov | a732a60 | 2017-02-01 20:29:56 +0200 | [diff] [blame] | 120 | if path == 'collected_at': |
| 121 | ds = DataSource(node_id=node_id, metric='collected_at') |
| 122 | units = 'us' |
| 123 | else: |
| 124 | sensor, dev, metric = path.split(".") |
| 125 | ds = DataSource(node_id=node_id, metric=metric, dev=dev, sensor=sensor) |
| 126 | units = sensor_units["{}.{}".format(sensor, metric)] |
| 127 | rstorage.append_sensor(numpy.array(value), ds, units) |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 128 | |
| 129 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 130 | class CollectSensorsStage(Stage): |
| 131 | priority = StepOrder.COLLECT_SENSORS |
| 132 | config_block = 'sensors' |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 133 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 134 | def run(self, ctx: TestRun) -> None: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 135 | collect_sensors_data(ctx, True) |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 136 | |
| 137 | |
| 138 | # def delta(func, only_upd=True): |
| 139 | # prev = {} |
| 140 | # while True: |
| 141 | # for dev_name, vals in func(): |
| 142 | # if dev_name not in prev: |
| 143 | # prev[dev_name] = {} |
| 144 | # for name, (val, _) in vals.items(): |
| 145 | # prev[dev_name][name] = val |
| 146 | # else: |
| 147 | # dev_prev = prev[dev_name] |
| 148 | # res = {} |
| 149 | # for stat_name, (val, accum_val) in vals.items(): |
| 150 | # if accum_val: |
| 151 | # if stat_name in dev_prev: |
| 152 | # delta = int(val) - int(dev_prev[stat_name]) |
| 153 | # if not only_upd or 0 != delta: |
| 154 | # res[stat_name] = str(delta) |
| 155 | # dev_prev[stat_name] = val |
| 156 | # elif not only_upd or '0' != val: |
| 157 | # res[stat_name] = val |
| 158 | # |
| 159 | # if only_upd and len(res) == 0: |
| 160 | # continue |
| 161 | # yield dev_name, res |
| 162 | # yield None, None |
| 163 | # |
| 164 | # |
| 165 | |
| 166 | |