From 55308cc2345dfddfcfcaa30fe892a08867f8bb8e Mon Sep 17 00:00:00 2001 From: Aidaho12 Date: Thu, 25 Jan 2018 15:04:17 +0600 Subject: [PATCH] Revert "v1.4" This reverts commit 555488f48de2f55774b859f8405df21d79fe0778. --- cgi-bin/add.py | 334 ------------------------------------ cgi-bin/config.py | 10 +- cgi-bin/configshow.py | 1 - cgi-bin/configver.py | 11 +- cgi-bin/diff.py | 3 +- cgi-bin/edit.py | 10 +- cgi-bin/funct.py | 176 ++++++------------- cgi-bin/listserv.py | 9 +- cgi-bin/login.py | 32 +--- cgi-bin/logs.py | 3 +- cgi-bin/options.py | 57 ------ cgi-bin/users | 3 +- cgi-bin/viewsttats.py | 1 - haproxy-webintarface.config | 6 +- index.html | 3 +- script.js | 98 ----------- style.css | 24 --- 17 files changed, 90 insertions(+), 691 deletions(-) delete mode 100644 cgi-bin/add.py delete mode 100644 cgi-bin/options.py delete mode 100644 script.js diff --git a/cgi-bin/add.py b/cgi-bin/add.py deleted file mode 100644 index aa9fef61..00000000 --- a/cgi-bin/add.py +++ /dev/null @@ -1,334 +0,0 @@ -#!/usr/bin/env python3 -import html -import cgi -import listserv as listhap -import os -import funct -import paramiko -import configparser -import http.cookies -from paramiko import SSHClient -from datetime import datetime -from pytz import timezone - -funct.head("Add") -funct.check_config() -funct.check_login("add.py") - -path_config = "haproxy-webintarface.config" -config = configparser.ConfigParser() -config.read(path_config) - -haproxy_configs_server = config.get('configs', 'haproxy_configs_server') -hap_configs_dir = config.get('configs', 'haproxy_save_configs_dir') -form = cgi.FieldStorage() - -if form.getvalue('mode') is not None: - serv = form.getvalue('serv') - port = form.getvalue('port') - mode = " mode " + form.getvalue('mode') - - if form.getvalue('balance') is not None: - balance = " balance " + form.getvalue('balance') + "\n" - else: - balance = "" - - if form.getvalue('ip') is not None: - ip = form.getvalue('ip') - else: - ip = "" - - if form.getvalue('listner') is not None: - name = "listen " + form.getvalue('listner') - backend = "" - elif form.getvalue('frontend') is not None: - name = "\nfrontend " + form.getvalue('frontend') - backend = " default_backend " + form.getvalue('backend') + "\n" - elif form.getvalue('backend') is not None: - name = "backend " + form.getvalue('backend') - backend = "" - - if not ip and form.getvalue('port') is not None: - bind = " bind *:"+ port + "\n" - elif port is not None: - bind = " bind " + ip + ":" + port + "\n" - else: - bind = "" - - if form.getvalue('option') is not None: - options = form.getvalue('option') - i = options.split("\n") - options_split = "" - for j in i: - options_split += " " + j + "\n" - else: - options_split = "" - - if form.getvalue('servers') is not None: - servers = form.getvalue('servers') - i = servers.split("\n") - servers_split = "" - for j in i: - servers_split += " " + j + "\n" - else: - servers_split = "" - - config_add = name + "\n" + bind + mode + "\n" + balance + options_split + backend + servers_split + "\n" - - os.chdir(config.get('configs', 'haproxy_save_configs_dir')) - - fmt = "%Y-%m-%d.%H:%M:%S" - now_utc = datetime.now(timezone(config.get('main', 'time_zone'))) - cfg = hap_configs_dir + serv + "-" + now_utc.strftime(fmt) + ".cfg" - - funct.get_config(serv, cfg) - try: - with open(cfg, "a") as conf: - conf.write(config_add) - #print('' % (name, config_add)) - except IOError: - print("Can't read import config file") - - funct.logging(serv, "add.py add new %s" % name) - print('
') - if funct.upload_and_restart(serv, cfg): - print('' % (name, config_add)) - - print('
') - -if form.getvalue('add') is not None: - print('

' + form.getvalue('add') + ' was successfully added

') - print('
') - print(form.getvalue('conf')) - print('
') - -print('
' - '' - '
' - '
' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '
Add listner
Select server: ' - '' - '
Name:' - '' - '
IP and Port:' - ':' - '' - '
IP for bind listner, if empty will be assignet on all IPs. Start typing ip, or press down.
' - '
Mode: ' - '' - '
Balance: ' - '' - '
Optinons:' - '
' - 'Start typing options: ' - '' - '' - 'or press down. Read more about options' - '' - '
' - '' - '
Servers:' - '' - '
') -funct.mode_admin("Add Listner") -print('
' - - '' - - '
' - '
' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '
Add frontend
Select server: ' - '' - '
Name:' - '' - '
IP and Port:' - ':' - '' - '
IP for bind listner, if empty will be assignet on all IPs. Start typing ip, or press down.
' - '
Mode: ' - '' - '
Optinons:' - '
' - 'Start typing options: ' - '' - '' - 'or press down. Read more about options' - '' - '
' - '' - '
Default backend' - '
Start typing backend, or press down
' - '' - ' .' - '

Note: If backend don\'t exist, you must create backend first.

' - '
') -funct.mode_admin("Add Frontend") -print('
' - '
' - - - '' - - '
' - '
' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '
Add frontend
Select server: ' - '' - '
Name:' - '' - '
Mode: ' - '' - '
Balance: ' - '' - '
Optinons:' - '
' - 'Start typing options: ' - '' - '' - 'or press down. Read more about options' - '' - '
' - '' - '
Servers:' - '' - '
') -funct.mode_admin("Add Backend") -print('
' - - '
') - -funct.footer() \ No newline at end of file diff --git a/cgi-bin/config.py b/cgi-bin/config.py index 4c446c84..a5495aea 100644 --- a/cgi-bin/config.py +++ b/cgi-bin/config.py @@ -14,10 +14,11 @@ from pytz import timezone form = cgi.FieldStorage() serv = form.getvalue('serv') servNew = form.getvalue('serNew') +cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) +login = cookie.get('login') funct.head("Edit HAproxy config") funct.check_config() -funct.check_login("config.py") path_config = "haproxy-webintarface.config" config = configparser.ConfigParser() @@ -27,6 +28,9 @@ fullpath = config.get('main', 'fullpath') hap_configs_dir = config.get('configs', 'haproxy_save_configs_dir') time_zone = config.get('main', 'time_zone') +if login is None: + print('') + if serv is not None: fmt = "%Y-%m-%d.%H:%M:%S" now_utc = datetime.now(timezone(time_zone)) @@ -45,9 +49,7 @@ if form.getvalue('serv') is not None and form.getvalue('open') is not None : print('' % serv) print('' % cfg) print('' % conf.read()) - print('

') - funct.mode_admin("Save and restart") - print('

') + print('

') conf.close os.system("/bin/sudo /bin/mv %s %s.old" % (cfg, cfg)) diff --git a/cgi-bin/configshow.py b/cgi-bin/configshow.py index fd9a12cc..7a261024 100644 --- a/cgi-bin/configshow.py +++ b/cgi-bin/configshow.py @@ -15,7 +15,6 @@ servNew = form.getvalue('serNew') funct.head("Show HAproxy config") funct.check_config() -funct.check_login("configshow.py") path_config = "haproxy-webintarface.config" config = configparser.ConfigParser() diff --git a/cgi-bin/configver.py b/cgi-bin/configver.py index 5ced61cd..d455111f 100644 --- a/cgi-bin/configver.py +++ b/cgi-bin/configver.py @@ -14,10 +14,11 @@ from pytz import timezone form = cgi.FieldStorage() serv = form.getvalue('serv') configver = form.getvalue('configver') +cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) +login = cookie.get('login') funct.head("Old Versions HAproxy config") funct.check_config() -funct.check_login("configver.py") path_config = "haproxy-webintarface.config" config = configparser.ConfigParser() @@ -25,6 +26,9 @@ config.read(path_config) hap_configs_dir = config.get('configs', 'haproxy_save_configs_dir') +if login is None: + print('') + funct.chooseServer("configver.py#conf", "Old Versions HAproxy config", "y") if serv is not None and form.getvalue('open') is not None: @@ -63,12 +67,9 @@ if serv is not None and form.getvalue('open') is not None: print('
') print('' % serv) print('' % configver) - print('') print('') funct.show_config(configver) - print('

') - funct.mode_admin("Upload and restart") - print('

') + print('

') if form.getvalue('serv') is not None and form.getvalue('config') is not None: diff --git a/cgi-bin/diff.py b/cgi-bin/diff.py index f6741d83..4069896b 100644 --- a/cgi-bin/diff.py +++ b/cgi-bin/diff.py @@ -17,7 +17,6 @@ right = form.getvalue('right') funct.head("Compare HAproxy configs") funct.check_config() -funct.check_login("diff.py") path_config = "haproxy-webintarface.config" config = configparser.ConfigParser() @@ -71,6 +70,6 @@ if form.getvalue('serv') is not None and form.getvalue('open') is not None : if form.getvalue('serv') is not None and form.getvalue('right') is not None: commands = [ 'diff -ub %s%s %s%s' % (hap_configs_dir, left, hap_configs_dir, right) ] - funct.ssh_command(haproxy_configs_server, commands, compare="compare") + funct.ssh_command(haproxy_configs_server, commands) funct.footer() \ No newline at end of file diff --git a/cgi-bin/edit.py b/cgi-bin/edit.py index 1837cf52..42a07a39 100644 --- a/cgi-bin/edit.py +++ b/cgi-bin/edit.py @@ -10,10 +10,14 @@ from funct import head as head form = cgi.FieldStorage() serv = form.getvalue('serv') +cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) +login = cookie.get('login') head("Edit & show HAproxy settings") -funct.check_login("edit.py") +if login is None: + print('') + print('

Edit & show HAproxy settings

') print('

Choose server & action: Disable/Enable server or output any information about the server:

') @@ -51,9 +55,7 @@ print('' % selected2) print('' % selected3) print('') print('') -print('

') -funct.mode_admin("Enter") -print('

') +print('

') if form.getvalue('servaction') is not None: action = form.getvalue('servaction') diff --git a/cgi-bin/funct.py b/cgi-bin/funct.py index d7627ccc..5a99de42 100644 --- a/cgi-bin/funct.py +++ b/cgi-bin/funct.py @@ -28,7 +28,6 @@ ssh_user_name = config.get('ssh', 'ssh_user_name') haproxy_configs_server = config.get('configs', 'haproxy_configs_server') hap_configs_dir = config.get('configs', 'haproxy_save_configs_dir') haproxy_config_path = config.get('haproxy', 'haproxy_config_path') -tmp_config_path = config.get('haproxy', 'tmp_config_path') restart_command = config.get('haproxy', 'restart_command') time_zone = config.get('main', 'time_zone') @@ -43,43 +42,17 @@ def logging(serv, action): log = open(fullpath + "log/config_edit.log", "a") log.write(mess) log.close - -def check_login(ref): - cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) - login = cookie.get('login') - if login is None: - print('' % ref) - -def show_login_links(): - cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) - login = cookie.get('login') - - if login is None: - print('Login') - else: - print('Logout') - -def mode_admin(button): - cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) - role = cookie.get('role') - - if role.value == "admin": - print('' % button) - def links(): print('Home Page ') print('Stats ') - print('Monitoring ') - print('Logs') + print('Logs') print('Edit settings ') print(' | Configs: ') print('Show ') print('Compare ') - print('Add ') print('Edit ') print('Upload old') - show_login_links() def head(title): print('Content-type: text/html\n') @@ -89,11 +62,21 @@ def head(title): '' '' '' - '' '' '' - '' - '
') + '' + '') + print('
') if config.get('main', 'logo_enable') == "1": print('' % config.get('main', 'logo_path')) print('
') - print('
') - - for line in stdout: - i = i + 1 - - if i is 1: - print('
' + line + '
') - elif i is 2: - print(line + '
') - elif line.find("-") == 0 and i is not 1: - print('
' + line + '
') - minus = minus + 1 - elif line.find("+") == 0 and i is not 2: - print('
' + line + '
') - plus = plus + 1 - elif line.find("@") == 0: - print('
' + line + '
') - else: - print('
' + line + '
') - - total_change = minus + plus - print('
Total change: %s, additions: %s & deletions: %s
' % (total_change, minus, plus)) - print('
') - -def show_log(stdout): - i = 0 - for line in stdout: - i = i + 1 - if i % 2 == 0: - print('
' + line + '
') - else: - print('
' + line + '
') - - print('') - -def show_ip(stdout): - for line in stdout: - print(line) - -def ssh_command(serv, commands, **kwargs): +def ssh_command(serv, commands): ssh = SSHClient() ssh.load_system_host_keys() k = paramiko.RSAKey.from_private_key_file(ssh_keys) ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect( hostname = serv, username = ssh_user_name, pkey = k ) - - ip = 0 - compare_funct = 0 - show_log_funct = 0 - - for k in kwargs: - if "ip" in kwargs[k]: - ip = 1 - if "compare" in kwargs[k]: - compare_funct = 1 - if "show_log" in kwargs[k]: - show_log_funct = 1 - for command in commands: stdin , stdout, stderr = ssh.exec_command(command) - - if ip is 1: - show_ip(stdout) - if compare_funct is 1: - compare(stdout) - if show_log_funct is 1: - show_log(stdout) - + print('
') + if serv == haproxy_configs_server: + print('
') + i = 0 + minus = 0 + plus = 0 + for line in stdout: + i = i + 1 + if serv == haproxy_configs_server: + if i is 1: + print('
' + line + '
') + elif i is 2: + print(line + '
') + elif line.find("-") == 0 and i is not 1: + print('
' + line + '
') + minus = minus + 1 + elif line.find("+") == 0 and i is not 2: + print('
' + line + '
') + plus = plus + 1 + elif line.find("@") == 0: + print('
' + line + '
') + else: + print('
' + line + '
') + elif i % 2 == 0: + print('
' + line + '
') + else: + print('
' + line + '
') + total_change = minus + plus + if serv == haproxy_configs_server: + print('
Total change: %s, additions: %s & deletions: %s
' % (total_change, minus, plus)) + print('
') print(stderr.read().decode(encoding='UTF-8')) ssh.close() @@ -320,6 +249,3 @@ def merge_two_dicts(x, y): z = x.copy() z.update(y) return z - - - diff --git a/cgi-bin/listserv.py b/cgi-bin/listserv.py index 181b72ce..b88c2c07 100644 --- a/cgi-bin/listserv.py +++ b/cgi-bin/listserv.py @@ -1,7 +1,10 @@ listhap= { - 'haproxy1': '172.28.0.1', - 'haproxy2': '172.28.0.2' + 'kz-webhap01': '172.28.9.159', + 'kz-webhap02': '172.28.9.160', + 'kz-mysqlhap01': '172.28.5.6', + 'kz-mysqlhap02': '172.28.5.5', } list_hap_vip = { - 'haproxy-vip': '172.28.0.3' + 'kz-webhap-vip': '172.28.9.161', + 'kz-mysqlhap-vip': '172.28.5.17' } diff --git a/cgi-bin/login.py b/cgi-bin/login.py index 6dfacbc0..617d3546 100644 --- a/cgi-bin/login.py +++ b/cgi-bin/login.py @@ -13,8 +13,6 @@ login = form.getvalue('login') password = form.getvalue('pass') USERS = 'cgi-bin/users' -funct.check_login("login.py") - try: with open(USERS, "r") as user: pass @@ -23,16 +21,12 @@ except IOError: def login_page(error): if error == "error": - printError = "Somthing wrong :( I'm sad about this, but try again!

" + printError = "Somthing wrong :( I'm sad about this, but try again!

" else: - printError = "First you need to login.

" - - ref = form.getvalue('ref') - if ref is None: - ref = "/index.html" + printError = "First you need to login.

" funct.head("Login page") - + print('
') print(printError) print(' ') @@ -41,34 +35,26 @@ def login_page(error): print('') print('
') -if form.getvalue('logout') is not None: - print("Set-cookie: login=; expires=Wed May 18 03:33:20 2003; path=/cgi-bin/; httponly") - print("Set-cookie: FirstName=; expires=Wed May 18 03:33:20 2003; path=/cgi-bin/; httponly") - print("Set-cookie: LastName=; expires=Wed May 18 03:33:20 2003; path=/cgi-bin/; httponly") - print("Set-cookie: role=; expires=Wed May 18 03:33:20 2003; path=/cgi-bin/; httponly") - print('') - if login is None: login_page("n") - + if login is not None and password is not None: for f in open(USERS, 'r'): users = json.loads(f) - print(users['login']) if login in users['login'] and password == users['password']: print("Set-cookie: login=%s; expires=Wed May 18 03:33:20 2033; path=/cgi-bin/; httponly" % login) print("Set-cookie: FirstName=%s; expires=Wed May 18 03:33:20 2033; path=/cgi-bin/; httponly" % users['firstName']) print("Set-cookie: LastName=%s; expires=Wed May 18 03:33:20 2033; path=/cgi-bin/; httponly" % users['lastName']) - print("Set-cookie: role=%s; expires=Wed May 18 03:33:20 2033; path=/cgi-bin/; httponly" % users['role']) - if form.getvalue('ref') is None: - ref = "/index.html" + if ref is None: + ref = "index.html" print("Content-type: text/html\n") print('Redirecting') print('') print('' % ref) + else: + login_page("error") break - login_page("error") - + funct.footer() diff --git a/cgi-bin/logs.py b/cgi-bin/logs.py index 2a4711cb..5b033b8a 100644 --- a/cgi-bin/logs.py +++ b/cgi-bin/logs.py @@ -12,7 +12,6 @@ serv = form.getvalue('serv') funct.head("HAproxy Logs") funct.check_config() -funct.check_login("config.py") path_config = "haproxy-webintarface.config" config = configparser.ConfigParser() @@ -71,6 +70,6 @@ if form.getvalue('serv') is not None: commands = [ 'sudo tail -%s /var/log/%s/syslog.log %s %s' % (rows, serv, grep_act, grep) ] syslog_server = config.get('logs', 'syslog_server') - funct.ssh_command(syslog_server, commands, show_log="show_log") + funct.ssh_command(syslog_server, commands) funct.footer() \ No newline at end of file diff --git a/cgi-bin/options.py b/cgi-bin/options.py deleted file mode 100644 index 9998c951..00000000 --- a/cgi-bin/options.py +++ /dev/null @@ -1,57 +0,0 @@ -#!/usr/bin/env python3 -import html -import cgi -import json -import subprocess -import funct -options = [ "acl", "http-request", "http-response", "set-uri", "set-url", "set-header", "add-header", "del-header", "replace-header", "path_beg", "url_beg()", "urlp_sub()", "tcpka", "tcplog", "forwardfor", "option" ] - -form = cgi.FieldStorage() -req = form.getvalue('req') -serv = form.getvalue('serv') -print('Content-type: text/html\n') -#print('Content-type: application/json\n') - -if req is not None: - if req is 1: - for i in options: - if req in i: - print(i) - else: - for i in options: - print(i) - -backend = form.getvalue('backend') -if backend is not None: - - cmd='echo "show backend" |nc %s 1999' % serv - p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True) - stdout, stderr = p.communicate() - output = stdout.splitlines() - - for line in output: - if "#" in line or "stats" in line: - continue - if backend != "1": - if backend in line: - print(json.dumps(line)) - continue - if backend == "1": - print(json.dumps(line)) - continue - - -if form.getvalue('ip') is not None and serv is not None: - commands = [ "ip a |grep inet |egrep -v '::1' |awk '{ print $2 }' |awk -F'/' '{ print $1 }'" ] - funct.ssh_command(serv, commands, ip="ip") - -if form.getvalue('name') is not None: - name = form.getvalue('name') - conf = open("/home/ploginov/haproxy/cgi-bin/hap_config/test.cfg", "r") - s = form.getvalue('s') - for line in conf: - #print(line) - if s in line and name in line: -# print(line) - print("yes") - break diff --git a/cgi-bin/users b/cgi-bin/users index a0502589..c42529ed 100644 --- a/cgi-bin/users +++ b/cgi-bin/users @@ -1,2 +1 @@ -{ "firstName": "admin", "lastName": "admin", "login": "admin", "password": "admin", "role": "admin" } -{ "firstName": "Guest", "lastName": "Guest", "login": "Guest", "password": "Guest@123", "role": "guest" } +{ "firstName": "admin", "lastName": "admin", "login": "admin", "password": "admin" } diff --git a/cgi-bin/viewsttats.py b/cgi-bin/viewsttats.py index 4f5b7ad9..75d5cb29 100644 --- a/cgi-bin/viewsttats.py +++ b/cgi-bin/viewsttats.py @@ -8,7 +8,6 @@ import configparser from requests_toolbelt.utils import dump funct.check_config() -funct.check_login("config.py") path_config = "haproxy-webintarface.config" config = configparser.ConfigParser() diff --git a/haproxy-webintarface.config b/haproxy-webintarface.config index 9c049ea4..a9534a17 100644 --- a/haproxy-webintarface.config +++ b/haproxy-webintarface.config @@ -6,7 +6,7 @@ server_port = 8000 log_path = %(fullpath)s/log/ time_zone = UTC #Enable logo on top menu. Default disable -logo_enable = 0 +logo_enable = 1 logo_path = /logo.png [configs] @@ -36,7 +36,5 @@ user = admin password = password stats_port = 8085 haproxy_config_path = /etc/haproxy/haproxy.cfg -#Temp store configs, for haproxy check -tmp_config_path = /tmp #Time in seconds for auto refresh view stats_port -refresh_time = 120 \ No newline at end of file +refresh_time = 30 \ No newline at end of file diff --git a/index.html b/index.html index e9e0f85d..9b08fdce 100644 --- a/index.html +++ b/index.html @@ -2,7 +2,7 @@ -HAProxy web manager +HAProxy monitoryng @@ -17,7 +17,6 @@ Logs
Compare
Show
- Add
Edit config
Upload old config