diff --git a/app/options.py b/app/options.py index 8146ad1f..d4efb6f4 100644 --- a/app/options.py +++ b/app/options.py @@ -816,6 +816,8 @@ if serv is not None and act == "showMap": print('
') print("

Map from %s


" % serv) + G = nx.DiGraph() + error = funct.get_config(serv, cfg) if error: print(error) @@ -824,36 +826,37 @@ if serv is not None and act == "showMap": except IOError: print('error: Can\'t read import config file') - G = nx.DiGraph() node = "" line_new2 = [1, ""] - sections = {'listens': dict(), 'backends': dict()} - + i, k = 800, 800 + j = 0 for line in conf: if line.startswith('listen') or line.startswith('frontend'): if "stats" not in line: node = line + i = i - 750 if line.find("backend") == 0: node = line - node = node.split('\n')[0] - sections['backends'][node] = {'servers': dict()} + i = i - 700 + G.add_node(node, pos=(k, i), label_pos=(k, i + 100)) if "bind" in line or (line.startswith('listen') and ":" in line) or ( line.startswith('frontend') and ":" in line): try: bind = line.split(":") - if str(stats_port) not in bind[1]: + if stats_port not in bind[1]: bind[1] = bind[1].strip(' ') bind = bind[1].split("crt") node = node.strip(' \t\n\r') node = node + ":" + bind[0] - node = node.split('\n')[0] - sections['listens'][node] = {'servers': dict()} - except Exception as e: + G.add_node(node, pos=(k, i), label_pos=(k, i + 100)) + except Exception: pass if "server " in line or "use_backend" in line or "default_backend" in line and "stats" not in line and "#" not in line: if "timeout" not in line and "default-server" not in line and "#" not in line and "stats" not in line: + i = i - 1050 + j = j + 1 if "check" in line: line_new = line.split("check") else: @@ -872,133 +875,29 @@ if serv is not None and act == "showMap": except Exception as e: backend_server_port = '' - try: - sections['listens'][node]['servers'][line_new[0]] = {line_new[0]: backend_server_port} - except Exception as e: - pass + if j % 2 == 0: + G.add_node(line_new[0], pos=(k + 250, i - 335), label_pos=(k + 215, i - 180)) + else: + G.add_node(line_new[0], pos=(k - 250, i - 0), label_pos=(k - 245, i + 180)) - try: - sections['backends'][node]['servers'][line_new[0]] = {line_new[0]: backend_server_port} - except Exception as e: - pass + if line_new2[1] != "": + G.add_edge(node, line_new[0], port=backend_server_port) + else: + G.add_edge(node, line_new[0], port='') os.system("/bin/rm -f " + cfg) - i, k, j = 0, 0, 0 - backend_servers_len_dict = 1 - backends_from_frontends = [] - backends_servers = [] - - for key, val in sections.items(): - if key == 'listens': - for k2, v2 in val.items(): - i -= 750 - G.add_node(k2, pos=(k, i), label_pos=(k, i + 250)) - - for k3, v3 in v2.items(): - for k4, v4 in v3.items(): - """ Add backend servers of listens or backend from frontends """ - i -= 300 - j += 1 - server_name = k4 - - if 'default_backend' in k4 or 'use_backend' in k4: - backend_name = k4.split(' ')[1] - backend_name = 'backend ' + backend_name - k4 = backend_name - backends_from_frontends.append(k4) - - if k4 not in backends_servers: - if j % 2 == 0: - G.add_node(k4, pos=(k + 250, i - 100), label_pos=(k + 250, i - 420)) - else: - G.add_node(k4, pos=(k - 250, i - 370), label_pos=(k - 245, i - 650)) - - if v4[server_name] != '': - G.add_edge(k2, k4, port=v4[server_name]) - else: - G.add_edge(k2, k4, port='') - - for k4, v4 in v3.items(): - """ Add servers from backends """ - i -= 300 - j -= 1 - - if 'default_backend' in k4 or 'use_backend' in k4: - backend_name = k4.split(' ')[1] - backend_name = 'backend ' + backend_name - k4 = backend_name - backends_from_frontends.append(k4) - - if j % 2 == 0: - if len(v3) % 2 == 0: - i += (700 * backend_servers_len_dict) + 700 - for k5, v5 in sections['backends'][k4]['servers'].items(): - i -= 700 - s = k + 400 - G.add_node(k5, pos=(s + 250, i - 335), label_pos=(s + 215, i - 580)) - - if v5[k5] != '': - G.add_edge(k4, k5, port=v5[k5]) - else: - G.add_edge(k4, k5, port='') - - backends_servers.append(k5) - else: - for k5, v5 in sections['backends'][k4]['servers'].items(): - i -= 700 - s = k - 400 - G.add_node(k5, pos=(s - 250, i - 0), label_pos=(s - 245, i - 270)) - - if v5[k5] != '': - G.add_edge(k4, k5, port=v5[k5]) - else: - G.add_edge(k4, k5, port='') - - backends_servers.append(k5) - backend_servers_len_dict = len(sections['backends'][k4]['servers']) - - backends_servers.append(k4) - - elif key == 'backends': - for k2, v2 in val.items(): - - if k2 not in backends_from_frontends: - i -= 750 - G.add_node(k2, pos=(k, i), label_pos=(k, i + 250)) - - for k3, v3 in v2.items(): - for k4, v4 in v3.items(): - - if k4 not in backends_servers: - i -= 300 - j += 1 - - if j % 2 == 0: - s = k + 400 - G.add_node(k4, pos=(s + 250, i - 335), label_pos=(s + 215, i - 580)) - else: - s = k - 400 - G.add_node(k4, pos=(s - 250, i - 0), label_pos=(s - 245, i - 270)) - - if v4[k4] != '': - G.add_edge(k2, k4, port=v4[k4]) - else: - G.add_edge(k2, k4, port='') - - backends_servers.append(k4) - pos = nx.get_node_attributes(G, 'pos') pos_label = nx.get_node_attributes(G, 'label_pos') edge_labels = nx.get_edge_attributes(G, 'port') try: - plt.figure(10, figsize=(10, 20)) + plt.figure(10, figsize=(10, 15)) nx.draw(G, pos, with_labels=False, font_weight='bold', width=3, alpha=0.1, linewidths=5) - nx.draw_networkx_nodes(G, pos, node_color="#5d9ceb", node_size=100, alpha=0.8, node_shape="h") - nx.draw_networkx_labels(G, pos=pos_label, alpha=1, font_color="#5CB85C", font_size=10) - nx.draw_networkx_edges(G, pos, width=0.3, alpha=0.7, edge_color="#5D9CEB", arrows=False) - nx.draw_networkx_edge_labels(G, pos, alpha=0.4, label_pos=0.5, font_color="#5d9ceb", edge_labels=edge_labels, font_size=8) + nx.draw_networkx_nodes(G, pos, node_color="skyblue", node_size=100, alpha=0.8, node_shape="p") + nx.draw_networkx_labels(G, pos=pos_label, alpha=1, font_color="green", font_size=10) + nx.draw_networkx_edges(G, pos, width=0.5, alpha=0.5, edge_color="#5D9CEB", arrows=False) + nx.draw_networkx_edge_labels(G, pos, label_pos=0.5, font_color="blue", edge_labels=edge_labels, font_size=8) plt.savefig("map.png") plt.show() @@ -1009,6 +908,7 @@ if serv is not None and act == "showMap": os.getcwd()) + "/map" + date + ".png" output, stderr = funct.subprocess_execute(cmd) print(stderr) + print('map' % date) if form.getvalue('servaction') is not None: diff --git a/app/sql.py b/app/sql.py index 85efd183..7c3be9aa 100644 --- a/app/sql.py +++ b/app/sql.py @@ -1761,16 +1761,18 @@ def select_roles(): def select_alert(**kwargs): - cursor = conn.cursor() - sql = """select ip from servers where alert = 1 and enable = 1 """ if kwargs.get("group") is not None: - sql = """select ip from servers where alert = 1 and `groups` = '{}' and enable = 1 """.format(kwargs.get("group")) + query = Server.select(Server.ip).where((Server.alert == 1) & + (Server.enable == 1) & + (Server.groups == kwargs.get('group'))) + else: + query = Server.select(Server.ip).where((Server.alert == 1) & (Server.enable == 1)) try: - cursor.execute(sql) + query_res = query.execute() except Exception as e: out_error(e) else: - return cursor.fetchall() + return query_res def select_all_alerts(**kwargs): @@ -1790,16 +1792,19 @@ def select_all_alerts(**kwargs): def select_nginx_alert(**kwargs): - cursor = conn.cursor() - sql = """select ip from servers where nginx_alert = 1 and enable = 1 """ if kwargs.get("group") is not None: - sql = """select ip from servers where nginx_alert = 1 and `groups` = '{}' and enable = 1 """.format(kwargs.get("group")) + query = Server.select(Server.ip).where( + (Server.nginx_alert == 1) & + (Server.enable == 1) & + (Server.groups == kwargs.get('group'))) + else: + query = Server.select(Server.ip).where((Server.nginx_alert == 1) & (Server.enable == 1)) try: - cursor.execute(sql) + query_res = query.execute() except Exception as e: out_error(e) else: - return cursor.fetchall() + return query_res def select_keep_alive():