blob: 1e0b1ad05237ee955594fb39056a73fbf8acf02d [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
17def save_sensors_data(data_q, mon_q, fd):
18 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
29 if addr not in observed_nodes:
30 mon_q.put(addr + (data['source_id'],))
31 observed_nodes.add(addr)
32
33 fd.write("{0!s} : {1!r}\n".format(time.time(), repr(val)))
34 except Exception:
35 logger.exception("Error in sensors thread")
36 logger.info("Sensors thread exits")
37
38
39def get_sensors_config_for_nodes(cfg, nodes):
40 monitored_nodes = []
41 sensors_configs = []
42
43 receiver_url = cfg["receiver_url"]
44 assert '{ip}' in receiver_url
45
46 for role, sensors_str in cfg["roles_mapping"].items():
47 sensors = [sens.strip() for sens in sensors_str.split(",")]
48
49 collect_cfg = dict((sensor, {}) for sensor in sensors)
50
51 for node in nodes:
52 if role in node.roles:
53
54 if node.monitor_url is not None:
55 monitor_url = node.monitor_url
56 else:
57 ext_ip = utils.get_ip_for_target(node.get_ip())
58 monitor_url = receiver_url.format(ip=ext_ip)
59
60 monitored_nodes.append(node)
61 sens_cfg = SensorConfig(node.connection,
62 node.get_conn_id(),
63 collect_cfg,
64 source_id=node.get_conn_id(),
65 monitor_url=monitor_url)
66 sensors_configs.append(sens_cfg)
67
68 return monitored_nodes, sensors_configs
69
70
71def start_sensor_process_thread(ctx, cfg, sensors_configs):
72 receiver_url = cfg["receiver_url"]
73 sensors_data_q, stop_sensors_loop = \
74 start_listener_thread(receiver_url.format(ip='0.0.0.0'))
75
76 mon_q = Queue.Queue()
77 fd = open(cfg_dict['sensor_storage'], "w")
78 logger.info("Start sensors data receiving thread")
79 sensor_listen_th = threading.Thread(None, save_sensors_data, None,
80 (sensors_data_q, mon_q, fd))
81 sensor_listen_th.daemon = True
82 sensor_listen_th.start()
83
84 def stop_sensors_receiver(cfg, ctx):
85 stop_sensors_loop()
86 sensors_data_q.put(None)
87 sensor_listen_th.join()
88
89 ctx.clear_calls_stack.append(stop_sensors_receiver)
90 return mon_q
91
92
93def deploy_sensors_stage(cfg, ctx, nodes=None, undeploy=True):
94 if 'sensors' not in cfg:
95 return
96
97 cfg = cfg.get('sensors')
98
99 if nodes is None:
100 nodes = ctx.nodes
101
102 logger.info("Deploing new sensors on {0} node(s)".format(len(nodes)))
103 monitored_nodes, sensors_configs = get_sensors_config_for_nodes(cfg,
104 nodes)
105
106 if len(monitored_nodes) == 0:
107 logger.info("Nothing to monitor, no sensors would be installed")
108 return
109
110 if ctx.sensors_mon_q is None:
111 ctx.sensors_mon_q = start_sensor_process_thread(ctx, cfg,
112 sensors_configs)
113
114 if undeploy:
115 def remove_sensors_stage(cfg, ctx):
116 stop_and_remove_sensors(sensors_configs)
117 ctx.clear_calls_stack.append(remove_sensors_stage)
118
119 deploy_and_start_sensors(sensors_configs)
120 wait_for_new_sensors_data(ctx, monitored_nodes)
121
122
123def wait_for_new_sensors_data(ctx, monitored_nodes):
124 MAX_WAIT_FOR_SENSORS = 10
125 etime = time.time() + MAX_WAIT_FOR_SENSORS
126
127 msg = "Waiting at most {0}s till all {1} nodes starts report sensors data"
128 nodes_ids = set(node.get_conn_id() for node in monitored_nodes)
129 logger.debug(msg.format(MAX_WAIT_FOR_SENSORS, len(nodes_ids)))
130
131 # wait till all nodes start sending data
132 while len(nodes_ids) != 0:
133 tleft = etime - time.time()
134 try:
135 source_id = ctx.sensors_mon_q.get(True, tleft)[2]
136 except Queue.Empty:
137 msg = "Node {0} not sending any sensor data in {1}s"
138 msg = msg.format(", ".join(nodes_ids), MAX_WAIT_FOR_SENSORS)
139 raise RuntimeError(msg)
140
141 if source_id not in nodes_ids:
142 msg = "Receive sensors from extra node: {0}".format(source_id)
143 logger.warning(msg)
144
145 nodes_ids.remove(source_id)