blob: 600f3bb43496e594ec70d07437021472833dcf91 [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
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030060def with_sensors_util(sensors_cfg, nodes):
61 srp = sensors_cfg['sensors_remote_path']
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030062 monitored_nodes, sensors_configs, source2roles_map = \
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030063 get_sensors_config_for_nodes(sensors_cfg, nodes, srp)
koder aka kdanilov168f6092015-04-19 02:33:38 +030064
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030065 with with_sensors(sensors_configs, srp):
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030066 yield source2roles_map
67
68
69@contextlib.contextmanager
70def sensors_info_util(cfg, nodes):
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030071 if cfg.get('sensors', None) is None:
72 yield
koder aka kdanilov168f6092015-04-19 02:33:38 +030073 return
74
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030075 _, sensors_configs, _ = \
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030076 get_sensors_config_for_nodes(cfg.sensors, nodes,
77 cfg.sensors_remote_path)
koder aka kdanilov416b87a2015-05-12 00:26:04 +030078
koder aka kdanilovbb5fe072015-05-21 02:50:23 +030079 clear_old_sensors(sensors_configs)
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030080 ctx = sensors_info(sensors_configs, cfg.sensors_remote_path)
koder aka kdanilov88407ff2015-05-26 15:35:57 +030081 try:
82 res = ctx.__enter__()
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030083 yield res
koder aka kdanilov88407ff2015-05-26 15:35:57 +030084 except:
85 ctx.__exit__(None, None, None)
86 raise
87 finally:
88 try:
89 ctx.__exit__(None, None, None)
90 except:
91 logger.exception("During stop/collect sensors")
92 del res[:]