blob: 61a5c0876582dbe4f1a5098c4a366348e9c64b0f [file] [log] [blame]
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +03001import os.path
koder aka kdanilov168f6092015-04-19 02:33:38 +03002import logging
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +03003import contextlib
koder aka kdanilov168f6092015-04-19 02:33:38 +03004
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +03005from wally.sensors.api import (with_sensors, sensors_info, SensorConfig)
6
koder aka kdanilov168f6092015-04-19 02:33:38 +03007
koder aka kdanilov57ce4db2015-04-25 21:25:51 +03008logger = logging.getLogger("wally.sensors")
koder aka kdanilov168f6092015-04-19 02:33:38 +03009
10
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030011def get_sensors_config_for_nodes(cfg, nodes, remote_path):
koder aka kdanilov168f6092015-04-19 02:33:38 +030012 monitored_nodes = []
13 sensors_configs = []
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030014 source2roles_map = {}
koder aka kdanilov168f6092015-04-19 02:33:38 +030015
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030016 receiver_url = "csvfile://" + os.path.join(remote_path, "results.csv")
koder aka kdanilov168f6092015-04-19 02:33:38 +030017
18 for role, sensors_str in cfg["roles_mapping"].items():
19 sensors = [sens.strip() for sens in sensors_str.split(",")]
20
21 collect_cfg = dict((sensor, {}) for sensor in sensors)
22
23 for node in nodes:
24 if role in node.roles:
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030025 source2roles_map[node.get_conn_id()] = node.roles
koder aka kdanilov168f6092015-04-19 02:33:38 +030026 monitored_nodes.append(node)
27 sens_cfg = SensorConfig(node.connection,
28 node.get_conn_id(),
29 collect_cfg,
30 source_id=node.get_conn_id(),
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030031 monitor_url=receiver_url)
koder aka kdanilov168f6092015-04-19 02:33:38 +030032 sensors_configs.append(sens_cfg)
33
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030034 return monitored_nodes, sensors_configs, source2roles_map
koder aka kdanilov168f6092015-04-19 02:33:38 +030035
36
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030037@contextlib.contextmanager
38def with_sensors_util(cfg, nodes):
39 if 'sensors' not in cfg:
40 yield
koder aka kdanilov416b87a2015-05-12 00:26:04 +030041 return
koder aka kdanilov168f6092015-04-19 02:33:38 +030042
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030043 monitored_nodes, sensors_configs, source2roles_map = \
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030044 get_sensors_config_for_nodes(cfg['sensors'], nodes,
45 cfg['sensors_remote_path'])
koder aka kdanilov168f6092015-04-19 02:33:38 +030046
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030047 with with_sensors(sensors_configs, cfg['sensors_remote_path']):
48 yield source2roles_map
49
50
51@contextlib.contextmanager
52def sensors_info_util(cfg, nodes):
53 if 'sensors' not in cfg:
54 yield None
koder aka kdanilov168f6092015-04-19 02:33:38 +030055 return
56
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030057 _, sensors_configs, _ = \
58 get_sensors_config_for_nodes(cfg['sensors'], nodes,
59 cfg['sensors_remote_path'])
koder aka kdanilov416b87a2015-05-12 00:26:04 +030060
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030061 with sensors_info(sensors_configs, cfg['sensors_remote_path']) as res:
62 yield res