blob: 6b50311f0bdc5bf8389bf9ad1a9ddbdcdcbcb0a1 [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 kdanilovbb5fe072015-05-21 02:50:23 +03005from concurrent.futures import ThreadPoolExecutor
6
7from wally import ssh_utils
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +03008from wally.sensors.api import (with_sensors, sensors_info, SensorConfig)
9
koder aka kdanilov168f6092015-04-19 02:33:38 +030010
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030011logger = logging.getLogger("wally.sensors")
koder aka kdanilov168f6092015-04-19 02:33:38 +030012
13
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030014def get_sensors_config_for_nodes(cfg, nodes, remote_path):
koder aka kdanilov168f6092015-04-19 02:33:38 +030015 monitored_nodes = []
16 sensors_configs = []
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030017 source2roles_map = {}
koder aka kdanilov168f6092015-04-19 02:33:38 +030018
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030019 receiver_url = "csvfile://" + os.path.join(remote_path, "results.csv")
koder aka kdanilov168f6092015-04-19 02:33:38 +030020
21 for role, sensors_str in cfg["roles_mapping"].items():
22 sensors = [sens.strip() for sens in sensors_str.split(",")]
23
24 collect_cfg = dict((sensor, {}) for sensor in sensors)
25
26 for node in nodes:
27 if role in node.roles:
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030028 source2roles_map[node.get_conn_id()] = node.roles
koder aka kdanilov168f6092015-04-19 02:33:38 +030029 monitored_nodes.append(node)
30 sens_cfg = SensorConfig(node.connection,
31 node.get_conn_id(),
32 collect_cfg,
33 source_id=node.get_conn_id(),
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030034 monitor_url=receiver_url)
koder aka kdanilov168f6092015-04-19 02:33:38 +030035 sensors_configs.append(sens_cfg)
36
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030037 return monitored_nodes, sensors_configs, source2roles_map
koder aka kdanilov168f6092015-04-19 02:33:38 +030038
39
koder aka kdanilovbb5fe072015-05-21 02:50:23 +030040PID_FILE = "/tmp/sensors.pid"
41
42
43def clear_old_sensors(sensors_configs):
44 def stop_sensors(sens_cfg):
45 with sens_cfg.conn.open_sftp() as sftp:
46 try:
47 pid = ssh_utils.read_from_remote(sftp, PID_FILE)
48 pid = int(pid.strip())
49 ssh_utils.run_over_ssh(sens_cfg.conn,
50 "kill -9 " + str(pid))
51 sftp.remove(PID_FILE)
52 except:
53 pass
54
55 with ThreadPoolExecutor(32) as pool:
56 list(pool.map(stop_sensors, sensors_configs))
57
58
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030059@contextlib.contextmanager
60def with_sensors_util(cfg, nodes):
61 if 'sensors' not in cfg:
62 yield
koder aka kdanilov416b87a2015-05-12 00:26:04 +030063 return
koder aka kdanilov168f6092015-04-19 02:33:38 +030064
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030065 monitored_nodes, sensors_configs, source2roles_map = \
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030066 get_sensors_config_for_nodes(cfg['sensors'], nodes,
67 cfg['sensors_remote_path'])
koder aka kdanilov168f6092015-04-19 02:33:38 +030068
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030069 with with_sensors(sensors_configs, cfg['sensors_remote_path']):
70 yield source2roles_map
71
72
73@contextlib.contextmanager
74def sensors_info_util(cfg, nodes):
75 if 'sensors' not in cfg:
76 yield None
koder aka kdanilov168f6092015-04-19 02:33:38 +030077 return
78
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030079 _, sensors_configs, _ = \
80 get_sensors_config_for_nodes(cfg['sensors'], nodes,
81 cfg['sensors_remote_path'])
koder aka kdanilov416b87a2015-05-12 00:26:04 +030082
koder aka kdanilovbb5fe072015-05-21 02:50:23 +030083 clear_old_sensors(sensors_configs)
koder aka kdanilov88407ff2015-05-26 15:35:57 +030084 ctx = sensors_info(sensors_configs, cfg['sensors_remote_path'])
85 try:
86 res = ctx.__enter__()
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030087 yield res
koder aka kdanilov88407ff2015-05-26 15:35:57 +030088 except:
89 ctx.__exit__(None, None, None)
90 raise
91 finally:
92 try:
93 ctx.__exit__(None, None, None)
94 except:
95 logger.exception("During stop/collect sensors")
96 del res[:]