async def async_get_overviewWaf(serv1, serv2): haproxy_dir = sql.get_setting('haproxy_dir') commands = [ "ps ax |grep waf/bin/modsecurity |grep -v grep |wc -l" ] commands1 = [ "cat %s/waf/modsecurity.conf |grep SecRuleEngine |grep -v '#' |awk '{print $2}'" % haproxy_dir ] server_status = (serv1,serv2, funct.ssh_command(serv2, commands), funct.ssh_command(serv2, commands1).strip(), sql.select_waf_metrics_enable_server(serv2)) return server_status
def main(): port = sql.get_setting('haproxy_sock_port') readstats = "" killer = GracefulKiller() while True: servers = sql.select_keep_alive() for serv in servers: try: readstats = subprocess.check_output( ["echo show stat | nc " + serv[0] + " " + port], shell=True) except CalledProcessError as e: alert = "Try start HAProxy serivce at " + serv[0] funct.logging("localhost", " " + alert, keep_alive=1) start_command = [] start_command.append('sudo ' + sql.get_setting('restart_command')) funct.ssh_command(serv[0], start_command) time.sleep(30) continue except OSError as e: print(e) sys.exit() else: cur_stat_service = "Ok" time.sleep(40)
def get_overviewServers(): listhap = sql.get_dick_permit() commands = ["top -u haproxy -b -n 1"] for server in sorted(listhap): print('<tr><td class="overviewTr first-collumn"><a name="' + server[1] + '"></a><h3 title="IP ' + server[2] + '">' + server[1] + ':</h3></td>') print( '<td class="overviewTd" style="padding-top: 10px;"><pre style="font-size: 12px;">' ) cmd = 'echo "show info" |nc %s 1999 |grep -e "Ver\|CurrConns\|SessRate\|Maxco\|MB\|Uptime:"' % server[ 2] out = funct.subprocess_execute(cmd) for k in out: if "Ncat: Connection refused." not in k: for r in k: print(r) else: print("Can\'t connect to HAproxy") print( '</pre></td><td style="padding-top: 10px;"><pre style="font-size: 12px; padding-left: 0px;">' ) funct.ssh_command(server[2], commands) print('</td><td style="padding: 10px;font-size: 12px;">') funct.show_backends(server[2]) print('</pre></td><td></td></tr>')
async def async_get_overview(serv1, serv2): server_status = () commands2 = [ "ps ax |grep waf/bin/modsecurity |grep -v grep |wc -l" ] cmd = 'echo "show info" |nc %s %s -w 1|grep -e "Process_num"' % (serv2, sql.get_setting('haproxy_sock_port')) keepalived = sql.select_keealived(serv2) if keepalived == 1: command = [ "ps ax |grep keepalived|grep -v grep|wc -l" ] keepalived_process = funct.ssh_command(serv2, command) else: keepalived_process = '' nginx = sql.select_nginx(serv2) if nginx == 1: command = [ "ps ax |grep nginx:|grep -v grep|wc -l" ] nginx_process = funct.ssh_command(serv2, command) else: nginx_process = '' server_status = (serv1, serv2, funct.server_status(funct.subprocess_execute(cmd)), sql.select_servers(server=serv2, keep_alive=1), funct.ssh_command(serv2, commands2), sql.select_waf_servers(serv2), keepalived, keepalived_process, nginx, nginx_process) return server_status
def comapre_show(): left = form.getvalue('left') right = form.getvalue('right') haproxy_configs_server = config.get('configs', 'haproxy_configs_server') hap_configs_dir = config.get('configs', 'haproxy_save_configs_dir') commands = [ 'diff -ub %s%s %s%s' % (hap_configs_dir, left, hap_configs_dir, right) ] funct.ssh_command(haproxy_configs_server, commands, compare="1")
async def async_get_overview(serv1, serv2): haproxy_config_path = sql.get_setting('haproxy_config_path') commands = [ "ls -l %s |awk '{ print $6\" \"$7\" \"$8}'" % haproxy_config_path ] commands1 = ["ps ax |grep waf/bin/modsecurity |grep -v grep |wc -l"] cmd = 'echo "show info" |nc %s %s |grep -e "Process_num"' % ( serv2, haproxy_sock_port) server_status = (serv1, serv2, funct.server_status(funct.subprocess_execute(cmd)), funct.ssh_command(serv2, commands), funct.ssh_command(serv2, commands1)) return server_status
def get_overviewServers(): listhap = sql.get_dick_permit() commands = [ "uname -smor", "haproxy -v |head -1", status_command + "|grep Active | sed 's/^[ \t]*//'" ] commands1 = [ "top -u haproxy -b -n 1" ] for server in sorted(listhap): print('<tr><td class="overviewTr first-collumn"><a name="'+server[1]+'"></a><h3 title="IP ' + server[2] + '">' + server[1] + ':</h3></td>') print('<td class="overviewTd"><pre>') funct.ssh_command(server[2], commands) print('</pre></td><td><pre>') funct.ssh_command(server[2], commands1) print('</td><td style="padding-top: 10px; padding-bottom: 10px;">') funct.show_backends(server[2]) print('</pre></td><td></td></tr>')
def get_overviewServers(): import http.cookies from jinja2 import Environment, FileSystemLoader env = Environment(loader=FileSystemLoader('templates/ajax')) template = env.get_template('overviewServers.html') cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) user_id = cookie.get('uuid') haproxy_sock_port = sql.get_setting('haproxy_sock_port') listhap = sql.get_dick_permit() commands = ["top -u haproxy -b -n 1"] servers = [] for server in sorted(listhap): server_status = () cmd = 'echo "show info" |nc %s %s |grep -e "Ver\|CurrConns\|SessRate\|Maxco\|MB\|Uptime:"' % ( server[2], haproxy_sock_port) out = funct.subprocess_execute(cmd) out1 = "" for k in out: if "Ncat: Connection refused." not in k: for r in k: out1 += r out1 += "<br />" else: out1 = "Can\'t connect to HAproxy" server_status = (server[1], server[2], out1, funct.ssh_command(server[2], commands), funct.show_backends(server[2], ret=1)) servers.append(server_status) template = template.render(service_status=servers, role=sql.get_user_role_by_uuid(user_id.value)) print(template)
def get_overview(): import http.cookies from jinja2 import Environment, FileSystemLoader env = Environment(loader=FileSystemLoader('templates/ajax')) template = env.get_template('overview.html') haproxy_config_path = sql.get_setting('haproxy_config_path') cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) user_id = cookie.get('uuid') haproxy_sock_port = sql.get_setting('haproxy_sock_port') listhap = sql.get_dick_permit() commands = [ "ls -l %s |awk '{ print $6\" \"$7\" \"$8}'" % haproxy_config_path ] servers = [] for server in listhap: server_status = () cmd = 'echo "show info" |nc %s %s |grep -e "Process_num"' % ( server[2], haproxy_sock_port) server_status = (server[1], server[2], funct.server_status(funct.subprocess_execute(cmd)), funct.ssh_command(server[2], commands)) servers.append(server_status) template = template.render(service_status=servers, role=sql.get_user_role_by_uuid(user_id.value)) print(template)
async def async_get_overviewServers(serv1, serv2): commands = [ "top -u haproxy -b -n 1 -w 67 |grep -e 'haproxy\|PID\|Cpu\|KiB' |grep -v Swap" ] # if desc == "hapservers.py": # cmd = 'echo "show info" |nc %s %s -w 1|grep -e "Ver\|CurrConns\|Maxco\|MB\|Uptime:"' % (serv2, haproxy_sock_port) # else: # cmd = 'echo "show info" |nc %s %s -w 1|grep -e "Ver\|CurrConns\|SessRate\|Maxco\|MB\|Uptime:"' % (serv2, haproxy_sock_port) cmd = 'echo "show info" |nc %s %s -w 1|grep -e "Ver\|CurrConns\|Maxco\|MB\|Uptime:"' % (serv2, haproxy_sock_port) out = funct.subprocess_execute(cmd) out1 = "" # user_id = cookie.get('uuid') # role = sql.get_user_role_by_uuid(user_id.value) # hap_configs_dir = funct.get_config_var('configs', 'haproxy_save_configs_dir') # cfg = hap_configs_dir + serv2 + "-" + funct.get_data('config') + ".cfg" # funct.get_config(serv2, cfg) # backends = funct.get_sections(cfg) # os.system("/bin/rm -f " + cfg) for k in out: if "Ncat:" not in k: for r in k: out1 += r out1 += "<br />" else: out1 = "Can\'t connect to HAproxy" # if role <= 2: # server_status = (serv1,serv2, out1, funct.ssh_command(serv2, commands),backends, desc) # else: # server_status = (serv1,serv2, out1, funct.ssh_command(serv2, commands),funct.show_backends(serv2, ret=1), desc) server_status = (serv1,serv2, out1, funct.ssh_command(serv2, commands)) return server_status
def get_overviewServers(): template = env.get_template('overviewServers.html') commands = ["top -u haproxy -b -n 1"] for server in sorted(listhap): cmd = 'echo "show info" |nc %s %s |grep -e "Ver\|CurrConns\|SessRate\|Maxco\|MB\|Uptime:"' % ( server[2], haproxy_sock_port) out = funct.subprocess_execute(cmd) out1 = "" for k in out: if "Ncat: Connection refused." not in k: for r in k: out1 += r out1 += "<br />" else: out1 = "Can\'t connect to HAproxy" server_status = (server[1], server[2], out1, funct.ssh_command(server[2], commands), funct.show_backends(server[2], ret=1)) servers.append(server_status) template = template.render(service_status=servers) print(template)
async def async_get_overview(serv1, serv2): server_status = () commands2 = ["ps ax |grep waf/bin/modsecurity |grep -v grep |wc -l"] cmd = 'echo "show info" |nc %s %s -w 1|grep -e "Process_num"' % ( serv2, sql.get_setting('haproxy_sock_port')) server_status = (serv1, serv2, funct.server_status(funct.subprocess_execute(cmd)), sql.select_servers(server=serv2, keep_alive=1), funct.ssh_command(serv2, commands2), sql.select_waf_servers(serv2)) return server_status
def get_overviewServers(): listhap = sql.get_dick_permit() commands = [ "cat " + haproxy_config_path + " |grep -E '^listen|^backend|^frontend' |grep -v stats |wc -l", "uname -smor", "haproxy -v |head -1", status_command + "|grep Active | sed 's/^[ \t]*//'" ] commands1 = ["top -u haproxy -b -n 1"] for server in sorted(listhap): print('<tr><td class="overviewTr first-collumn"><a name="' + server[1] + '"></a><h3 title="IP ' + server[2] + '">' + server[1] + ':</h3></td>') print( '<td class="overviewTd"><span>Total listen/frontend/backend:</span><pre>' ) funct.ssh_command(server[2], commands) print('</pre></td><td><pre>') funct.ssh_command(server[2], commands1) print('</pre></td></tr>')
def get_overview(): template = env.get_template('overview.html') haproxy_config_path = sql.get_setting('haproxy_config_path') commands = [ "ls -l %s |awk '{ print $6\" \"$7\" \"$8}'" % haproxy_config_path ] commands1 = ["ps ax |grep waf/bin/modsecurity |grep -v grep |wc -l"] for server in listhap: cmd = 'echo "show info" |nc %s %s |grep -e "Process_num"' % ( server[2], haproxy_sock_port) server_status = (server[1], server[2], funct.server_status(funct.subprocess_execute(cmd)), funct.ssh_command(server[2], commands), funct.ssh_command(server[2], commands1)) servers.append(server_status) template = template.render(service_status=servers, role=sql.get_user_role_by_uuid(user_id.value)) print(template)
def get_overviewWaf(url): template = env.get_template('overivewWaf.html') haproxy_dir = sql.get_setting('haproxy_dir') commands = ["ps ax |grep waf/bin/modsecurity |grep -v grep |wc -l"] commands1 = [ "cat %s/waf/modsecurity.conf |grep SecRuleEngine |grep -v '#' |awk '{print $2}'" % haproxy_dir ] for server in listhap: server_status = (server[1], server[2], funct.ssh_command(server[2], commands), funct.ssh_command(server[2], commands1), sql.select_waf_metrics_enable_server(server[2])) servers.append(server_status) template = template.render(service_status=servers, role=sql.get_user_role_by_uuid(user_id.value), url=url) print(template)
async def async_get_overviewServers(serv1, serv2, desc): # commands = [ "top -u haproxy -b -n 1" ] # commands = [ "top -u haproxy -b -n 1 -o %MEM |grep -e 'haproxy\|PID' |awk '{print $1\"\t\"$5\"\t\"$9\"\t\"$10\"\t\"$11}'" ] commands = [ "top -u haproxy -b -n 1 -w 67 |grep -e 'haproxy\|PID\|Cpu\|KiB' |grep -v Swap" ] if desc == "hapservers.py": cmd = 'echo "show info" |nc %s %s -w 1|grep -e "Ver\|CurrConns\|Maxco\|MB\|Uptime:"' % ( serv2, haproxy_sock_port) else: cmd = 'echo "show info" |nc %s %s -w 1|grep -e "Ver\|CurrConns\|SessRate\|Maxco\|MB\|Uptime:"' % ( serv2, haproxy_sock_port) out = funct.subprocess_execute(cmd) out1 = "" user_id = cookie.get('uuid') role = sql.get_user_role_by_uuid(user_id.value) hap_configs_dir = funct.get_config_var('configs', 'haproxy_save_configs_dir') cfg = hap_configs_dir + serv2 + "-" + funct.get_data('config') + ".cfg" funct.get_config(serv2, cfg) backends = funct.get_sections(cfg) os.system("/bin/rm -f " + cfg) for k in out: if "Ncat: Connection refused." not in k and "Ncat: Connection timed out." not in k: for r in k: out1 += r out1 += "<br />" else: out1 = "Can\'t connect to HAproxy" if role <= 2: server_status = (serv1, serv2, out1, funct.ssh_command(serv2, commands), backends, desc) else: server_status = (serv1, serv2, out1, funct.ssh_command(serv2, commands), funct.show_backends(serv2, ret=1), desc) return server_status
def get_overview(): listhap = sql.get_dick_permit() commands = ["ps -Af |grep [h]aproxy |wc -l"] commands1 = [ "ls -l %s |awk '{ print $6\" \"$7\" \"$8}'" % haproxy_config_path ] for server in listhap: print( '<tr><td class="padding10 first-collumn"><a href="#%s" title="Go to %s status" style="color: #000">%s</a></td><td class="second-collumn">' % (server[1], server[1], server[1])) funct.ssh_command(server[2], commands, server_status="1") print('</td><td>') if funct.is_admin(): print( '<a id="%s" class="start" title="Start HAproxy service"><img src=/image/pic/start.png alt="start" class="icon"></a>' % server[2]) print( '<a id="%s" class="stop" title="Stop HAproxy service"><img src=/image/pic/stop.png alt="start" class="icon"></a>' % server[2]) print( '<a id="%s" class="restart" title="Restart HAproxy service"><img src=/image/pic/update.png alt="restart" class="icon"></a>' % server[2]) if funct.is_admin(level=2): print( '<a href="/app/configshow.py?serv=%s&open=open#conf" title="Show config"><img src=/image/pic/show.png alt="show" class="icon"></a>' % server[2]) print( '<a href="/app/config.py?serv=%s&open=open#conf" title="Edit config"><img src=/image/pic/edit.png alt="edit" class="icon"></a>' % server[2]) print( '<a href="/app/diff.py?serv=%s&open=open#diff" title="Compare config"><img src=/image/pic/compare.png alt="compare" class="icon"></a>' % server[2]) print( '<a href="/app/map.py?serv=%s&open=open#map" title="Map listen/frontend/backend"><img src=/image/pic/map.png alt="map" class="icon"></a>' % server[2]) print('</td><td>') funct.ssh_command(server[2], commands1) print('</td><td></td></tr>')
async def async_get_overview(serv1, serv2): haproxy_config_path = sql.get_setting('haproxy_config_path') commands = [ "ls -l %s |awk '{ print $6\" \"$7\" \"$8}'" % haproxy_config_path ] commands2 = "ps ax |grep keep_alive.py |grep -v grep |wc -l" cmd = 'echo "show info" |nc %s %s -w 1|grep -e "Process_num"' % (serv2, haproxy_sock_port) server_status = (serv1, serv2, funct.server_status(funct.subprocess_execute(cmd)), funct.ssh_command(serv2, commands), sql.select_servers(server=serv2, keep_alive=1), funct.subprocess_execute(commands2)) return server_status
def get_overviewWaf(): import http.cookies from jinja2 import Environment, FileSystemLoader env = Environment(loader=FileSystemLoader('templates/ajax')) template = env.get_template('overivewWaf.html') cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) user_id = cookie.get('uuid') haproxy_dir = sql.get_setting('haproxy_dir') haproxy_sock_port = sql.get_setting('haproxy_sock_port') listhap = sql.get_dick_permit() commands = [ "ps ax |grep waf/bin/modsecurity |grep -v grep |wc -l" ] commands1 = [ "cat %s/waf/modsecurity.conf |grep SecRuleEngine |grep -v '#' |awk '{print $2}'" % haproxy_dir ] servers = [] for server in listhap: server_status = () server_status = (server[1],server[2], funct.ssh_command(server[2], commands), funct.ssh_command(server[2], commands1)) servers.append(server_status) template = template.render(service_status = servers, role = sql.get_user_role_by_uuid(user_id.value)) print(template)
async def async_get_overviewServers(serv1, serv2, desc): commands = [ "top -u haproxy -b -n 1" ] cmd = 'echo "show info" |nc %s %s |grep -e "Ver\|CurrConns\|SessRate\|Maxco\|MB\|Uptime:"' % (serv2, haproxy_sock_port) out = funct.subprocess_execute(cmd) out1 = "" for k in out: if "Ncat: Connection refused." not in k: for r in k: out1 += r out1 += "<br />" else: out1 = "Can\'t connect to HAproxy" server_status = (serv1,serv2, out1, funct.ssh_command(serv2, commands),funct.show_backends(serv2, ret=1), desc) return server_status
def actions(server_id, action): if action == 'start' or action == 'stop' or action == 'restart': try: servers = check_permit_to_server(server_id) for s in servers: cmd = [ "sudo systemctl %s haproxy" % action ] error = funct.ssh_command(s[2], cmd) done = error if error else 'done' data = {'server_id':s[0],'ip':s[2],'action':action,'hostname':s[1],'status':done} return dict(status=data) except: return dict(status='error') else: return dict(status='wrong action')
async def async_get_overviewServers(serv1, serv2): server_status = () commands = [ "top -u haproxy -b -n 1 -w 67 |grep -e 'haproxy\|PID\|Cpu\|KiB' |grep -v Swap" ] cmd = 'echo "show info" |nc %s %s -w 1|grep -e "Ver\|CurrConns\|Maxco\|MB\|Uptime:"' % (serv2, sql.get_setting('haproxy_sock_port')) out = funct.subprocess_execute(cmd) out1 = "" for k in out: if "Ncat:" not in k: for r in k: out1 += r out1 += "<br />" else: out1 = "Can\'t connect to HAproxy" server_status = (serv1,serv2, out1, funct.ssh_command(serv2, commands)) return server_status
def runtime(id): data = {} try: action = request.headers.get('action') haproxy_sock = sql.get_setting('haproxy_sock') servers = check_permit_to_server(id) cmd = ['echo "%s" |sudo socat stdio %s' % (action, haproxy_sock)] for s in servers: out = funct.ssh_command(s[2], cmd) data = {} data[id] = {} sep_data = out.split('\r\n') data[id] = {'ouput': sep_data} return dict(status=data) except: return dict(status='error')
def runtime(server_id): data = {} try: body = request.body.getvalue().decode('utf-8') json_loads = json.loads(body) action = json_loads['command'] haproxy_sock = sql.get_setting('haproxy_sock') servers = check_permit_to_server(server_id) cmd = [ 'echo "%s" |sudo socat stdio %s' % (action, haproxy_sock) ] for s in servers: out = funct.ssh_command(s[2], cmd) data = {server_id: {}} sep_data = out.split('\r\n') data = {server_id: sep_data} return dict(status=data) except: return dict(status='error')
cmd = 'echo "show info" |nc %s %s -w 1 |grep -e "Ver\|Uptime:\|Process_num"' % ( s[2], haproxy_sock_port) out = funct.subprocess_execute(cmd) servers_with_status.append(s[0]) servers_with_status.append(s[1]) servers_with_status.append(s[2]) servers_with_status.append(s[11]) for k in out: if "Ncat:" not in k: out1 = out else: out1 = False servers_with_status.append(out1) servers_with_status.append(s[12]) try: servers_with_status.append(funct.ssh_command(s[2], commands)) except: servers_with_status.append('Cannot get last date') servers_with_status1.append(servers_with_status) template = template.render(h2=1, autorefresh=0, title="HAProxy servers overview", role=sql.get_user_role_by_uuid(user_id.value), user=user, users=users, groups=groups, servers=servers_with_status1, versions=funct.versions(), keep_alive=''.join(keep_alive),
servers = sql.get_dick_permit(ip=serv) for server in servers: if server != "": print("ok") sys.exit() sys.exit() if not sql.check_token_exists(form.getvalue('token')): print("Your token has been expired") sys.exit() if form.getvalue('getcerts') is not None and serv is not None: cert_path = sql.get_setting('cert_path') commands = ["ls -1t " + cert_path + " |grep pem"] try: funct.ssh_command(serv, commands, ip="1") except: print( '<div class="alert alert-danger" style="margin:0">Can not connect to the server</div>' ) if form.getvalue('checkSshConnect') is not None and serv is not None: try: funct.ssh_command(serv, ["ls -1t"]) except: print( '<div class="alert alert-danger" style="margin:0">Can not connect to the server</div>' ) if form.getvalue('getcert') is not None and serv is not None: id = form.getvalue('getcert')
form = cgi.FieldStorage() req = form.getvalue('req') serv = form.getvalue('serv') act = form.getvalue('act') backend = form.getvalue('backend') print('Content-type: text/html\n') if form.getvalue('token') is None: print("What the f**k?! U r hacker Oo?!") sys.exit() if form.getvalue('getcert') is not None and serv is not None: commands = ["ls -1t /etc/ssl/certs/ |grep pem"] try: funct.ssh_command(serv, commands, ip="1") except: print( '<div class="alert alert-danger" style="margin:0">Can not connect to the server</div>' ) if form.getvalue('ssh_cert'): fullpath = funct.get_config_var('main', 'fullpath') name = form.getvalue('name') ssh_keys = fullpath + '/keys/' + name + '.pem' try: with open(ssh_keys, "w") as conf: conf.write(form.getvalue('ssh_cert')) except IOError: print(
servers = sql.get_dick_permit(ip=serv) for server in servers: if server != "": print("ok") sys.exit() sys.exit() if not sql.check_token_exists(form.getvalue('token')): print("Your token has been expired") sys.exit() if form.getvalue('getcerts') is not None and serv is not None: cert_path = sql.get_setting('cert_path') commands = [ "ls -1t "+cert_path+" |grep pem" ] try: funct.ssh_command(serv, commands, ip="1") except: print('<div class="alert alert-danger" style="margin:0">Can not connect to the server</div>') if form.getvalue('checkSshConnect') is not None and serv is not None: try: funct.ssh_command(serv, ["ls -1t"]) except: print('<div class="alert alert-danger" style="margin:0">Can not connect to the server</div>') if form.getvalue('getcert') is not None and serv is not None: id = form.getvalue('getcert') cert_path = sql.get_setting('cert_path') commands = [ "cat "+cert_path+"/"+id ] try: funct.ssh_command(serv, commands, ip="1")
def get_overview(): USERS = sql.select_users() print('<table class="overview">') if funct.is_admin(): print('<tr class="overviewHead">' '<td class="padding10 first-collumn">Login</td>' '<td class="padding10">Email</td>' '<td class="padding10">Group</td>' '<td class="padding10">Role</td>' '<td style="width: 200px;">' '<span class="add-button">' '<a href="#" title="Show all users" id="show-all-users" style="color: #fff">' 'Show all' '</a>' '</span>' '</td>' '</tr>') i = 0 style = "" for users in USERS: i = i + 1 if i is 4: style = 'style="display: none;" class="show-users"' print('<tr ' + style + '><td class="padding10 first-collumn">' + users[1] +'</td><td class="second-collumn">') print(users[2]+'</td><td>') GROUPS = sql.select_user_name_group(users[5]) for group in GROUPS: print(group) print('</td><td>') print(users[4]) print('</td><td></td></tr>') print('</table>') print('<table class="overview">' '<tr class="overviewHead">' '<td class="padding10 first-collumn"">Server</td>' '<td class="padding10">' 'HAproxy status' '</td>' '<td class="padding10">' 'Action' '</td>' '<td class="padding10">' 'Last edit' '</td>' '<td></td>' '</tr>') listhap = sql.get_dick_permit() commands = [ "ps -Af |grep [h]aproxy |wc -l" ] commands1 = [ "ls -l %s |awk '{ print $6\" \"$7\" \"$8}'" % haproxy_config_path ] for server in listhap: print('<tr><td class="padding10 first-collumn"><a href="#%s" title="Go to %s status" style="color: #000">%s</a></td><td class="second-collumn">' % (server[1], server[1], server[1])) funct.ssh_command(server[2], commands, server_status="1") print('</td><td>') if funct.is_admin(): print('<a id="%s" class="start" title="Start HAproxy service" onclick = "if (! confirm(\'Start service?\')) return false;"><img src=/image/pic/start.png alt="start" class="icon"></a>' % server[2]) print('<a id="%s" class="stop" title="Stop HAproxy service" onclick = "return confirm(\'Stop service?\')"><img src=/image/pic/stop.png alt="start" class="icon"></a>' % server[2]) print('<a id="%s" class="restart" title="Restart HAproxy service" onclick = "if (! confirm(\'Restart service?\')) return false;"><img src=/image/pic/update.png alt="restart" class="icon"></a>' % server[2]) print('<a href="/app/configshow.py?serv=%s&open=open#conf" title="Show config"><img src=/image/pic/show.png alt="show" class="icon"></a>' % server[2]) print('<a href="/app/config.py?serv=%s&open=open#conf" title="Edit config"><img src=/image/pic/edit.png alt="edit" class="icon"></a>' % server[2]) print('<a href="/app/diff.py?serv=%s&open=open#diff" title="Compare config"><img src=/image/pic/compare.png alt="compare" class="icon"></a>' % server[2]) print('<a href="/app/map.py?serv=%s&open=open#map" title="Map listen/frontend/backend"><img src=/image/pic/map.png alt="map" class="icon"></a>' % server[2]) print('</td><td>') funct.ssh_command(server[2], commands1) print('</td><td></td></tr>') print('</table><table class="overview"><tr class="overviewHead">' '<td class="padding10 first-collumn" style="width: 15%;">Server</td>' '<td>' 'HAproxy info' '</td>' '<td>' 'Server status' '</td>' '</tr>') print('</td></tr>') commands = [ "cat " + haproxy_config_path + " |grep -E '^listen|^backend|^frontend' |grep -v stats |wc -l", "uname -smor", "haproxy -v |head -1", status_command + "|grep Active | sed 's/^[ \t]*//'" ] commands1 = [ "top -u haproxy -b -n 1" ] for server in sorted(listhap): print('<tr><td class="overviewTr first-collumn"><a name="'+server[1]+'"></a><h3 title="IP ' + server[2] + '">' + server[1] + ':</h3></td>') print('<td class="overviewTd"><span>Total listen/frontend/backend:</span><pre>') funct.ssh_command(server[2], commands) print('</pre></td><td class="overviewTd"><pre>') funct.ssh_command(server[2], commands1) print('</pre></td></tr>') print('<tr></table>')
def get_map(serv): from datetime import datetime from pytz import timezone import networkx as nx import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt date = funct.get_data('config') cfg = hap_configs_dir + serv + "-" + date + ".cfg" print('<center>') print("<h3>Map from %s</h3><br />" % serv) G = nx.DiGraph() funct.get_config(serv, cfg) try: conf = open(cfg, "r") except IOError: print('<div class="alert alert-danger">Can\'t read import config file</div>') node = "" line_new2 = [1,""] i = 1200 k = 1200 j = 0 m = 0 for line in conf: if "listen" in line or "frontend" in line: if "stats" not in line: node = line i = i - 500 if line.find("backend") == 0: node = line i = i - 500 G.add_node(node,pos=(k,i),label_pos=(k,i+150)) if "bind" in line: bind = line.split(":") 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] G.add_node(node,pos=(k,i),label_pos=(k,i+150)) if "server " in line or "use_backend" in line or "default_backend" in line and "stats" 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 - 300 j = j + 1 if "check" in line: line_new = line.split("check") else: line_new = line.split("if ") if "server" in line: line_new1 = line_new[0].split("server") line_new[0] = line_new1[1] line_new2 = line_new[0].split(":") line_new[0] = line_new2[0] line_new[0] = line_new[0].strip(' \t\n\r') line_new2[1] = line_new2[1].strip(' \t\n\r') if j % 2 == 0: G.add_node(line_new[0],pos=(k+250,i-350),label_pos=(k+225,i-100)) else: G.add_node(line_new[0],pos=(k-250,i-50),label_pos=(k-225,i+180)) if line_new2[1] != "": G.add_edge(node, line_new[0], port=line_new2[1]) else: G.add_edge(node,line_new[0]) os.system("/bin/rm -f " + cfg) os.chdir(cgi_path) 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=(9.5,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="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", labels=edge_labels, font_size=8) plt.savefig("map.png") plt.show() except Exception as e: print('<div class="alert alert-danger">' + str(e) + '</div>') commands = [ "rm -f "+fullpath+"/map*.png", "mv %s/map.png %s/map%s.png" % (cgi_path, fullpath, date) ] funct.ssh_command("localhost", commands) print('<img src="/map%s.png" alt="map">' % date)