blob: d4e1af85dd5402e0238a62ececfa30cedf3b99eb [file] [log] [blame]
koder aka kdanilov168f6092015-04-19 02:33:38 +03001import time
2import Queue
3import logging
4import threading
5
6from wally import utils
7from wally.config import cfg_dict
8from wally.sensors.api import (start_listener_thread,
9 deploy_and_start_sensors,
10 SensorConfig,
11 stop_and_remove_sensors)
12
13
14logger = logging.getLogger("wally")
15
16
Ved-vampir34d52012015-04-23 14:44:39 +030017def save_sensors_data(data_q, mon_q, fd):
koder aka kdanilov168f6092015-04-19 02:33:38 +030018 fd.write("\n")
19
20 observed_nodes = set()
21
22 try:
23 while True:
24 val = data_q.get()
25 if val is None:
26 break
27
28 addr, data = val
Alyona Kiseleva7f6de4f2015-04-21 01:04:20 +030029
koder aka kdanilov168f6092015-04-19 02:33:38 +030030 if addr not in observed_nodes:
31 mon_q.put(addr + (data['source_id'],))
32 observed_nodes.add(addr)
33
Alyona Kiseleva7f6de4f2015-04-21 01:04:20 +030034 fd.write("{0!s} : {1!r}\n".format(time.time(),
35 repr((addr, data))))
koder aka kdanilov168f6092015-04-19 02:33:38 +030036 except Exception:
37 logger.exception("Error in sensors thread")
38 logger.info("Sensors thread exits")
39
40
41def get_sensors_config_for_nodes(cfg, nodes):
42 monitored_nodes = []
43 sensors_configs = []
44
45 receiver_url = cfg["receiver_url"]
46 assert '{ip}' in receiver_url
47
48 for role, sensors_str in cfg["roles_mapping"].items():
49 sensors = [sens.strip() for sens in sensors_str.split(",")]
50
51 collect_cfg = dict((sensor, {}) for sensor in sensors)
52
53 for node in nodes:
54 if role in node.roles:
55
56 if node.monitor_url is not None:
57 monitor_url = node.monitor_url
58 else:
koder aka kdanilov0c598a12015-04-21 03:01:40 +030059 ip = node.get_ip()
60 if ip == '127.0.0.1':
61 ext_ip = '127.0.0.1'
62 else:
63 ext_ip = utils.get_ip_for_target(ip)
koder aka kdanilov168f6092015-04-19 02:33:38 +030064 monitor_url = receiver_url.format(ip=ext_ip)
65
66 monitored_nodes.append(node)
67 sens_cfg = SensorConfig(node.connection,
68 node.get_conn_id(),
69 collect_cfg,
70 source_id=node.get_conn_id(),
71 monitor_url=monitor_url)
72 sensors_configs.append(sens_cfg)
73
74 return monitored_nodes, sensors_configs
75
76
77def start_sensor_process_thread(ctx, cfg, sensors_configs):
78 receiver_url = cfg["receiver_url"]
79 sensors_data_q, stop_sensors_loop = \
80 start_listener_thread(receiver_url.format(ip='0.0.0.0'))
81
82 mon_q = Queue.Queue()
83 fd = open(cfg_dict['sensor_storage'], "w")
koder aka kdanilov168f6092015-04-19 02:33:38 +030084 sensor_listen_th = threading.Thread(None, save_sensors_data, None,
Ved-vampir34d52012015-04-23 14:44:39 +030085 (sensors_data_q, mon_q, fd))
koder aka kdanilov168f6092015-04-19 02:33:38 +030086 sensor_listen_th.daemon = True
87 sensor_listen_th.start()
88
89 def stop_sensors_receiver(cfg, ctx):
90 stop_sensors_loop()
91 sensors_data_q.put(None)
92 sensor_listen_th.join()
93
94 ctx.clear_calls_stack.append(stop_sensors_receiver)
95 return mon_q
96
97
98def deploy_sensors_stage(cfg, ctx, nodes=None, undeploy=True):
99 if 'sensors' not in cfg:
100 return
101
102 cfg = cfg.get('sensors')
103
104 if nodes is None:
105 nodes = ctx.nodes
106
koder aka kdanilov168f6092015-04-19 02:33:38 +0300107 monitored_nodes, sensors_configs = get_sensors_config_for_nodes(cfg,
108 nodes)
109
110 if len(monitored_nodes) == 0:
111 logger.info("Nothing to monitor, no sensors would be installed")
112 return
113
114 if ctx.sensors_mon_q is None:
koder aka kdanilove87ae652015-04-20 02:14:35 +0300115 logger.info("Start sensors data receiving thread")
koder aka kdanilov168f6092015-04-19 02:33:38 +0300116 ctx.sensors_mon_q = start_sensor_process_thread(ctx, cfg,
117 sensors_configs)
118
119 if undeploy:
120 def remove_sensors_stage(cfg, ctx):
121 stop_and_remove_sensors(sensors_configs)
122 ctx.clear_calls_stack.append(remove_sensors_stage)
123
koder aka kdanilove87ae652015-04-20 02:14:35 +0300124 logger.info("Deploing new sensors on {0} node(s)".format(len(nodes)))
koder aka kdanilov168f6092015-04-19 02:33:38 +0300125 deploy_and_start_sensors(sensors_configs)
126 wait_for_new_sensors_data(ctx, monitored_nodes)
127
128
129def wait_for_new_sensors_data(ctx, monitored_nodes):
130 MAX_WAIT_FOR_SENSORS = 10
131 etime = time.time() + MAX_WAIT_FOR_SENSORS
132
133 msg = "Waiting at most {0}s till all {1} nodes starts report sensors data"
134 nodes_ids = set(node.get_conn_id() for node in monitored_nodes)
135 logger.debug(msg.format(MAX_WAIT_FOR_SENSORS, len(nodes_ids)))
136
137 # wait till all nodes start sending data
138 while len(nodes_ids) != 0:
139 tleft = etime - time.time()
140 try:
141 source_id = ctx.sensors_mon_q.get(True, tleft)[2]
142 except Queue.Empty:
143 msg = "Node {0} not sending any sensor data in {1}s"
144 msg = msg.format(", ".join(nodes_ids), MAX_WAIT_FOR_SENSORS)
145 raise RuntimeError(msg)
146
147 if source_id not in nodes_ids:
148 msg = "Receive sensors from extra node: {0}".format(source_id)
149 logger.warning(msg)
150
151 nodes_ids.remove(source_id)