koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame^] | 1 | import os.path |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 2 | import logging |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame^] | 3 | import contextlib |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 4 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame^] | 5 | from wally.sensors.api import (with_sensors, sensors_info, SensorConfig) |
| 6 | |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 7 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 8 | logger = logging.getLogger("wally.sensors") |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 9 | |
| 10 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame^] | 11 | def get_sensors_config_for_nodes(cfg, nodes, remote_path): |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 12 | monitored_nodes = [] |
| 13 | sensors_configs = [] |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 14 | source2roles_map = {} |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 15 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame^] | 16 | receiver_url = "csvfile://" + os.path.join(remote_path, "results.csv") |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 17 | |
| 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 kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 25 | source2roles_map[node.get_conn_id()] = node.roles |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 26 | 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 kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame^] | 31 | monitor_url=receiver_url) |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 32 | sensors_configs.append(sens_cfg) |
| 33 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 34 | return monitored_nodes, sensors_configs, source2roles_map |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 35 | |
| 36 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame^] | 37 | @contextlib.contextmanager |
| 38 | def with_sensors_util(cfg, nodes): |
| 39 | if 'sensors' not in cfg: |
| 40 | yield |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 41 | return |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 42 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 43 | monitored_nodes, sensors_configs, source2roles_map = \ |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame^] | 44 | get_sensors_config_for_nodes(cfg['sensors'], nodes, |
| 45 | cfg['sensors_remote_path']) |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 46 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame^] | 47 | with with_sensors(sensors_configs, cfg['sensors_remote_path']): |
| 48 | yield source2roles_map |
| 49 | |
| 50 | |
| 51 | @contextlib.contextmanager |
| 52 | def sensors_info_util(cfg, nodes): |
| 53 | if 'sensors' not in cfg: |
| 54 | yield None |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 55 | return |
| 56 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame^] | 57 | _, sensors_configs, _ = \ |
| 58 | get_sensors_config_for_nodes(cfg['sensors'], nodes, |
| 59 | cfg['sensors_remote_path']) |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 60 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame^] | 61 | with sensors_info(sensors_configs, cfg['sensors_remote_path']) as res: |
| 62 | yield res |