diff --git a/app/funct.py b/app/funct.py index 2cad4a82..9815d1a4 100644 --- a/app/funct.py +++ b/app/funct.py @@ -186,13 +186,13 @@ def ssh_connect(serv, **kwargs): return 'Unable to verify server\'s host key: %s ' % badHostKeyException pass except Exception as e: - if e.args[1] == "No such file or directory": + if e == "No such file or directory": if kwargs.get('check'): - print('
{}. Check ssh key
'.format(e.args[1])) + print('
{}. Check ssh key
'.format(e)) else: - return '{}. Check ssh key'.format(e.args[1]) + return '{}. Check ssh key'.format(e) pass - elif e.args[1] == "Invalid argument": + elif e == "Invalid argument": if kwargs.get('check'): print('
Check the IP of the new server
') else: @@ -200,9 +200,9 @@ def ssh_connect(serv, **kwargs): pass else: if kwargs.get('check'): - print('
{}
'.format(e.args[1])) + print('
{}
'.format(e)) else: - error = e.args[1] + error = e pass if kwargs.get('check'): return False @@ -462,15 +462,9 @@ def server_status(stdout): if "Ncat: Connection refused." not in line: for k in line: proc_count = k.split(":")[1] - err = 1 else: - err = 0 proc_count = 0 - - if err != 0: - print(' UP running %s processes' % proc_count) - else: - print(' DOWN running %s processes' % proc_count) + return proc_count def ssh_command(serv, commands, **kwargs): ssh = ssh_connect(serv) @@ -490,7 +484,7 @@ def ssh_command(serv, commands, **kwargs): elif kwargs.get("server_status") == "1": server_status(stdout) else: - print(stdout.read().decode(encoding='UTF-8')) + return stdout.read().decode(encoding='UTF-8') print(stderr.read().decode(encoding='UTF-8')) try: @@ -510,17 +504,24 @@ def subprocess_execute(cmd): return output, stderr -def show_backends(serv): +def show_backends(serv, **kwargs): import json cmd='echo "show backend" |nc %s 1999' % serv output, stderr = subprocess_execute(cmd) - + ret = "" for line in output: if "#" in line or "stats" in line: continue if line != "": back = json.dumps(line).split("\"") - print(back[1]+"
") + if kwargs.get('ret'): + ret += back[1] + ret += "
" + else: + print(back[1]+"
") + + if kwargs.get('ret'): + return ret def get_files(): import glob diff --git a/app/overview.py b/app/overview.py index 9f58d5c6..b1a73d03 100644 --- a/app/overview.py +++ b/app/overview.py @@ -23,12 +23,12 @@ try: except: pass -output_from_parsed_template = template.render(h2 = 1, - autorefresh = 1, - title = "Overview", - role = sql.get_user_role_by_uuid(user_id.value), - user = user, - users = users, - groups = groups, - token = token) -print(output_from_parsed_template) \ No newline at end of file +template = template.render(h2 = 1, + autorefresh = 1, + title = "Overview", + role = sql.get_user_role_by_uuid(user_id.value), + user = user, + users = users, + groups = groups, + token = token) +print(template) \ No newline at end of file diff --git a/app/ovw.py b/app/ovw.py index 6fdfef28..44768f48 100644 --- a/app/ovw.py +++ b/app/ovw.py @@ -12,49 +12,57 @@ hap_configs_dir = funct.get_config_var('configs', 'haproxy_save_configs_dir') form = cgi.FieldStorage() def get_overview(): + import os, http.cookies + from jinja2 import Environment, FileSystemLoader + env = Environment(loader=FileSystemLoader('templates/ajax')) + template = env.get_template('overview.html') + cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) + user_id = cookie.get('uuid') + listhap = sql.get_dick_permit() - commands = [ "ls -l %s |awk '{ print $6\" \"$7\" \"$8}'" % haproxy_config_path ] + servers = [] for server in listhap: - print('%s' % (server[1], server[1], server[1])) + server_status = () cmd = 'echo "show info" |nc %s 1999 |grep -e "Process_num"' % server[2] - funct.server_status(funct.subprocess_execute(cmd)) - print('') - if funct.is_admin(): - print('start' % server[2]) - print('start' % server[2]) - print('restart' % server[2]) - if funct.is_admin(level = 2): - print('show' % server[2]) - print('edit' % server[2]) - print('compare' % server[2]) - print('map' % server[2]) - print('') - funct.ssh_command(server[2], commands) - print('') + 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) + def get_overviewServers(): + import os, 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') + listhap = sql.get_dick_permit() commands = [ "top -u haproxy -b -n 1" ] + servers = [] for server in sorted(listhap): - print('

' + server[1] + ':

') - print('
')
+		server_status = ()
 		cmd = 'echo "show info" |nc %s 1999 |grep -e "Ver\|CurrConns\|SessRate\|Maxco\|MB\|Uptime:"' % server[2]
 		out = funct.subprocess_execute(cmd)
-
+		out1 = ""
 		for k in out:
 			if "Ncat: Connection refused." not in k:
 				for r in k:
-					print(r)
+					out1 += r
+					out1 += "
" else: - print("Can\'t connect to HAproxy") - print('
')
-		funct.ssh_command(server[2], commands)
-		print('')
-		funct.show_backends(server[2])
-		print('
') + 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) + + #print(servers) + template = template.render(service_status = servers, role = sql.get_user_role_by_uuid(user_id.value)) + print(template) def get_map(serv): from datetime import datetime diff --git a/app/templates/ajax/overview.html b/app/templates/ajax/overview.html new file mode 100644 index 00000000..1da2bc92 --- /dev/null +++ b/app/templates/ajax/overview.html @@ -0,0 +1,45 @@ +{% for service in service_status %} + + + {{ service.0 }} + + + {% if service.2|int() >= 1 %} + UP running {{service.2 }} processes + {% else %} + DOWN running {{service.2 }} processes + {% endif %} + + + {% if role <= 1 %} + + start + + + start + + + restart + + {% endif %} + {% if role <= 2 %} + + show + + + edit + + + compare + + + map + + {% endif %} + + + {{ service.3 }} + + + +{% endfor %} \ No newline at end of file diff --git a/app/templates/ajax/overviewServers.html b/app/templates/ajax/overviewServers.html new file mode 100644 index 00000000..8c39c905 --- /dev/null +++ b/app/templates/ajax/overviewServers.html @@ -0,0 +1,24 @@ +{% for service in service_status %} + + + +

{{ service.0 }} :

+ + +
+{{ service.2 }}
+			
+ + +
+{{ service.3 }}
+			
+ + +
+{{ service.4 }}
+			
+ + + +{% endfor %} \ No newline at end of file diff --git a/app/templates/login.html b/app/templates/login.html index 6a16a158..658fbb53 100644 --- a/app/templates/login.html +++ b/app/templates/login.html @@ -1,7 +1,7 @@ {% extends "base.html" %} {% block content %}
-
+ {{error_log}} {{error}} {{db_create}} diff --git a/app/templates/servers.html b/app/templates/servers.html index b7989aec..89393d4a 100644 --- a/app/templates/servers.html +++ b/app/templates/servers.html @@ -309,7 +309,7 @@