kdanylov aka koder | 4518318 | 2017-04-30 23:55:40 +0300 | [diff] [blame] | 1 | import bz2 |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 2 | import array |
| 3 | import logging |
kdanylov aka koder | 736e5c1 | 2017-05-07 17:27:14 +0300 | [diff] [blame^] | 4 | from typing import Dict |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 5 | |
koder aka kdanilov | a732a60 | 2017-02-01 20:29:56 +0200 | [diff] [blame] | 6 | import numpy |
| 7 | |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 8 | from cephlib import sensors_rpc_plugin |
| 9 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 10 | from . import utils |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 11 | from .test_run_class import TestRun |
koder aka kdanilov | a732a60 | 2017-02-01 20:29:56 +0200 | [diff] [blame] | 12 | from .result_classes import DataSource |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 13 | from .stage import Stage, StepOrder |
koder aka kdanilov | a732a60 | 2017-02-01 20:29:56 +0200 | [diff] [blame] | 14 | from .hlstorage import ResultStorage |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 15 | |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 16 | |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 17 | plugin_fname = sensors_rpc_plugin.__file__.rsplit(".", 1)[0] + ".py" |
koder aka kdanilov | 23e6bdf | 2016-12-24 02:18:54 +0200 | [diff] [blame] | 18 | SENSORS_PLUGIN_CODE = open(plugin_fname, "rb").read() # type: bytes |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 19 | |
| 20 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 21 | logger = logging.getLogger("wally") |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 22 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 23 | |
| 24 | # 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] | 25 | class StartSensorsStage(Stage): |
| 26 | priority = StepOrder.START_SENSORS |
| 27 | config_block = 'sensors' |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 28 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 29 | def run(self, ctx: TestRun) -> None: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 30 | if array.array('L').itemsize != 8: |
| 31 | message = "Python array.array('L') items should be 8 bytes in size, not {}." + \ |
| 32 | " Can't provide sensors on this platform. Disable sensors in config and retry" |
| 33 | logger.critical(message.format(array.array('L').itemsize)) |
| 34 | raise utils.StopTestError() |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 35 | |
koder aka kdanilov | 23e6bdf | 2016-12-24 02:18:54 +0200 | [diff] [blame] | 36 | # TODO: need carefully fix this |
| 37 | # sensors config is: |
| 38 | # role: |
| 39 | # sensor: [str] |
| 40 | # or |
| 41 | # role: |
| 42 | # sensor: |
| 43 | # allowed: [str] |
| 44 | # dissallowed: [str] |
| 45 | # params: Any |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 46 | per_role_config = {} # type: Dict[str, Dict[str, str]] |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 47 | |
| 48 | for name, val in ctx.config.sensors.roles_mapping.raw().items(): |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 49 | if isinstance(val, str): |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 50 | val = {vl.strip(): (".*" if vl.strip() != 'ceph' else {}) for vl in val.split(",")} |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 51 | elif isinstance(val, list): |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 52 | val = {vl: (".*" if vl != 'ceph' else {}) for vl in val} |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 53 | per_role_config[name] = val |
| 54 | |
| 55 | if 'all' in per_role_config: |
| 56 | all_vl = per_role_config.pop('all') |
| 57 | all_roles = set(per_role_config) |
| 58 | |
| 59 | for node in ctx.nodes: |
koder aka kdanilov | 23e6bdf | 2016-12-24 02:18:54 +0200 | [diff] [blame] | 60 | all_roles.update(node.info.roles) # type: ignore |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 61 | |
| 62 | for name, vals in list(per_role_config.items()): |
| 63 | new_vals = all_vl.copy() |
| 64 | new_vals.update(vals) |
| 65 | per_role_config[name] = new_vals |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 66 | |
| 67 | for node in ctx.nodes: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 68 | node_cfg = {} # type: Dict[str, Dict[str, str]] |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 69 | for role in node.info.roles: |
koder aka kdanilov | 23e6bdf | 2016-12-24 02:18:54 +0200 | [diff] [blame] | 70 | node_cfg.update(per_role_config.get(role, {})) # type: ignore |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 71 | |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 72 | nid = node.node_id |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 73 | if node_cfg: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 74 | # ceph requires additional settings |
| 75 | if 'ceph' in node_cfg: |
| 76 | node_cfg['ceph'].update(node.info.params['ceph']) |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 77 | 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] | 78 | |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame] | 79 | logger.debug("Setting up sensors RPC plugin for node %s", nid) |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 80 | node.upload_plugin("sensors", SENSORS_PLUGIN_CODE) |
| 81 | ctx.sensors_run_on.add(nid) |
| 82 | logger.debug("Start monitoring node %s", nid) |
| 83 | node.conn.sensors.start(node_cfg) |
| 84 | else: |
| 85 | logger.debug("Skip monitoring node %s, as no sensors selected", nid) |
| 86 | |
| 87 | |
| 88 | def collect_sensors_data(ctx: TestRun, stop: bool = False): |
koder aka kdanilov | a732a60 | 2017-02-01 20:29:56 +0200 | [diff] [blame] | 89 | rstorage = ResultStorage(ctx.storage) |
kdanylov aka koder | 736e5c1 | 2017-05-07 17:27:14 +0300 | [diff] [blame^] | 90 | total_sz = 0 |
| 91 | |
| 92 | logger.info("Start loading sensors") |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 93 | for node in ctx.nodes: |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 94 | node_id = node.node_id |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 95 | if node_id in ctx.sensors_run_on: |
kdanylov aka koder | 736e5c1 | 2017-05-07 17:27:14 +0300 | [diff] [blame^] | 96 | func = node.conn.sensors.stop if stop else node.conn.sensors.get_updates |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 97 | |
kdanylov aka koder | 736e5c1 | 2017-05-07 17:27:14 +0300 | [diff] [blame^] | 98 | # hack to calculate total transferred size |
| 99 | offset_map, compressed_blob, compressed_collected_at_b = func() |
| 100 | data_tpl = (offset_map, compressed_blob, compressed_collected_at_b) |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 101 | |
kdanylov aka koder | 736e5c1 | 2017-05-07 17:27:14 +0300 | [diff] [blame^] | 102 | total_sz += len(compressed_blob) + len(compressed_collected_at_b) + sum(map(len, offset_map)) + \ |
| 103 | 16 * len(offset_map) |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 104 | |
kdanylov aka koder | 736e5c1 | 2017-05-07 17:27:14 +0300 | [diff] [blame^] | 105 | for path, value, is_array, units in sensors_rpc_plugin.unpack_rpc_updates(data_tpl): |
koder aka kdanilov | a732a60 | 2017-02-01 20:29:56 +0200 | [diff] [blame] | 106 | if path == 'collected_at': |
kdanylov aka koder | 4518318 | 2017-04-30 23:55:40 +0300 | [diff] [blame] | 107 | ds = DataSource(node_id=node_id, metric='collected_at', tag='csv') |
kdanylov aka koder | 736e5c1 | 2017-05-07 17:27:14 +0300 | [diff] [blame^] | 108 | rstorage.append_sensor(numpy.array(value), ds, units) |
koder aka kdanilov | a732a60 | 2017-02-01 20:29:56 +0200 | [diff] [blame] | 109 | else: |
| 110 | sensor, dev, metric = path.split(".") |
kdanylov aka koder | 4518318 | 2017-04-30 23:55:40 +0300 | [diff] [blame] | 111 | ds = DataSource(node_id=node_id, metric=metric, dev=dev, sensor=sensor, tag='csv') |
| 112 | if is_array: |
kdanylov aka koder | 4518318 | 2017-04-30 23:55:40 +0300 | [diff] [blame] | 113 | rstorage.append_sensor(numpy.array(value), ds, units) |
| 114 | else: |
| 115 | if metric == 'historic': |
| 116 | rstorage.put_sensor_raw(bz2.compress(value), ds(tag='bin')) |
| 117 | else: |
| 118 | assert metric in ('perf_dump', 'historic_js') |
| 119 | rstorage.put_sensor_raw(value, ds(tag='js')) |
kdanylov aka koder | 736e5c1 | 2017-05-07 17:27:14 +0300 | [diff] [blame^] | 120 | logger.info("Download %sB of sensors data", utils.b2ssize(total_sz)) |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 121 | |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 122 | |
| 123 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 124 | class CollectSensorsStage(Stage): |
| 125 | priority = StepOrder.COLLECT_SENSORS |
| 126 | config_block = 'sensors' |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 127 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 128 | def run(self, ctx: TestRun) -> None: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 129 | collect_sensors_data(ctx, True) |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 130 | |