blob: c773b344a5f90471ffa3b0344511c5a0ff91932d [file] [log] [blame]
koder aka kdanilov962ee5f2016-12-19 02:40:08 +02001import array
2import logging
3from typing import List, Dict, Tuple
koder aka kdanilov39e449e2016-12-17 15:15:26 +02004
koder aka kdanilova732a602017-02-01 20:29:56 +02005import numpy
6
koder aka kdanilov962ee5f2016-12-19 02:40:08 +02007from . import utils
koder aka kdanilov70227062016-11-26 23:23:21 +02008from .test_run_class import TestRun
koder aka kdanilova732a602017-02-01 20:29:56 +02009from .result_classes import DataSource
koder aka kdanilov70227062016-11-26 23:23:21 +020010from . import sensors_rpc_plugin
koder aka kdanilov39e449e2016-12-17 15:15:26 +020011from .stage import Stage, StepOrder
koder aka kdanilova732a602017-02-01 20:29:56 +020012from .hlstorage import ResultStorage
koder aka kdanilov70227062016-11-26 23:23:21 +020013
14plugin_fname = sensors_rpc_plugin.__file__.rsplit(".", 1)[0] + ".py"
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +020015SENSORS_PLUGIN_CODE = open(plugin_fname, "rb").read() # type: bytes
koder aka kdanilov70227062016-11-26 23:23:21 +020016
17
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020018logger = logging.getLogger("wally")
koder aka kdanilov70227062016-11-26 23:23:21 +020019
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020020
koder aka kdanilova732a602017-02-01 20:29:56 +020021sensor_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 kdanilov962ee5f2016-12-19 02:40:08 +020043# TODO(koder): in case if node has more than one role sensor settings might be incorrect
koder aka kdanilov39e449e2016-12-17 15:15:26 +020044class StartSensorsStage(Stage):
45 priority = StepOrder.START_SENSORS
46 config_block = 'sensors'
koder aka kdanilov70227062016-11-26 23:23:21 +020047
koder aka kdanilov39e449e2016-12-17 15:15:26 +020048 def run(self, ctx: TestRun) -> None:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020049 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 kdanilov39e449e2016-12-17 15:15:26 +020054
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +020055 # 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 kdanilov39e449e2016-12-17 15:15:26 +020065 per_role_config = {} # type: Dict[str, Dict[str, str]]
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020066
67 for name, val in ctx.config.sensors.roles_mapping.raw().items():
koder aka kdanilov39e449e2016-12-17 15:15:26 +020068 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 kdanilov23e6bdf2016-12-24 02:18:54 +020079 all_roles.update(node.info.roles) # type: ignore
koder aka kdanilov39e449e2016-12-17 15:15:26 +020080
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 kdanilov70227062016-11-26 23:23:21 +020085
86 for node in ctx.nodes:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020087 node_cfg = {} # type: Dict[str, Dict[str, str]]
koder aka kdanilov39e449e2016-12-17 15:15:26 +020088 for role in node.info.roles:
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +020089 node_cfg.update(per_role_config.get(role, {})) # type: ignore
koder aka kdanilov70227062016-11-26 23:23:21 +020090
koder aka kdanilov108ac362017-01-19 20:17:16 +020091 nid = node.node_id
koder aka kdanilov39e449e2016-12-17 15:15:26 +020092 if node_cfg:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020093 # ceph requires additional settings
94 if 'ceph' in node_cfg:
95 node_cfg['ceph'].update(node.info.params['ceph'])
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +020096 node_cfg['ceph']['osds'] = [osd.id for osd in node.info.params['ceph-osds']] # type: ignore
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020097
kdanylov aka koder0e0cfcb2017-03-27 22:19:09 +030098 logger.debug("Setting up sensors RPC plugin for node %s", nid)
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020099 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
107def collect_sensors_data(ctx: TestRun, stop: bool = False):
koder aka kdanilova732a602017-02-01 20:29:56 +0200108 rstorage = ResultStorage(ctx.storage)
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200109 for node in ctx.nodes:
koder aka kdanilov108ac362017-01-19 20:17:16 +0200110 node_id = node.node_id
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200111 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 kdanilova732a602017-02-01 20:29:56 +0200118 # TODO: units should came along with data
koder aka kdanilov3af3c332016-12-19 17:12:34 +0200119 for path, value in sensors_rpc_plugin.unpack_rpc_updates(func()):
koder aka kdanilova732a602017-02-01 20:29:56 +0200120 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 kdanilov70227062016-11-26 23:23:21 +0200128
129
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200130class CollectSensorsStage(Stage):
131 priority = StepOrder.COLLECT_SENSORS
132 config_block = 'sensors'
koder aka kdanilov70227062016-11-26 23:23:21 +0200133
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200134 def run(self, ctx: TestRun) -> None:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200135 collect_sensors_data(ctx, True)
koder aka kdanilov70227062016-11-26 23:23:21 +0200136
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