'.join(map(str, output)))
+
+ action = 'edit.py ' + enable + ' ' + backend
+ funct.logging(serv, action)
+
+funct.footer()
\ No newline at end of file
diff --git a/cgi-bin/funct.py b/cgi-bin/funct.py
new file mode 100644
index 00000000..5a07e3bc
--- /dev/null
+++ b/cgi-bin/funct.py
@@ -0,0 +1,164 @@
+import cgi
+import os
+import paramiko
+import http.cookies
+from paramiko import SSHClient
+import listserv as listhap
+from datetime import datetime
+from pytz import timezone
+import configparser
+
+def check_config():
+ path_config = "haproxy-webintarface.config"
+ config = configparser.ConfigParser()
+ config.read(path_config)
+
+
+ for section in [ 'main', 'configs', 'ssh', 'logs', 'haproxy' ]:
+ if not config.has_section(section):
+ print('Check config file, no %s section' % section)
+
+
+path_config = "haproxy-webintarface.config"
+config = configparser.ConfigParser()
+config.read(path_config)
+
+fullpath = config.get('main', 'fullpath')
+ssh_keys = config.get('ssh', 'ssh_keys')
+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')
+restart_command = config.get('haproxy', 'restart_command')
+
+def logging(serv, action):
+ dateFormat = "%b %d %H:%M:%S"
+ now_utc = datetime.now(timezone('Asia/Almaty'))
+ IP = cgi.escape(os.environ["REMOTE_ADDR"])
+ cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
+ firstName = cookie.get('FirstName')
+ lastName = cookie.get('LastName')
+ mess = now_utc.strftime(dateFormat) + " from " + IP + " user: " + firstName.value + " " + lastName.value + " " + action + " for: " + serv + "\n"
+ log = open(fullpath + "log/config_edit.log", "a")
+ log.write(mess)
+ log.close
+
+def head(title):
+ print("Content-type: text/html\n")
+ print('
%s' % title)
+ print('')
+ print('')
+ print('')
+ print('')
+
+def footer():
+ print('
')
+
+def get_config(serv, cfg):
+ os.chdir(hap_configs_dir)
+
+ ssh = SSHClient()
+ ssh.load_system_host_keys()
+ k = paramiko.RSAKey.from_private_key_file(ssh_keys)
+ ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
+ ssh.connect(hostname = serv, username = ssh_user_name, pkey = k )
+ sftp = ssh.open_sftp()
+ sftp.get(haproxy_config_path, cfg)
+ sftp.close()
+ ssh.close()
+
+def upload_and_restart(serv, cfg):
+ 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())
+ print("connecting
")
+ ssh.connect( hostname = serv, username = ssh_user_name, pkey = k )
+ print("connected
")
+ sftp = ssh.open_sftp()
+ sftp.put(cfg, haproxy_config_path)
+ sftp.close()
+ commands = [ "service haproxy restart" ]
+ for command in commands:
+ print("Executing: {}".format( command ))
+ print("")
+ stdin , stdout, stderr = ssh.exec_command(command)
+ print(stdout.read().decode(encoding='UTF-8'))
+ print("Errors:")
+ print(stderr.read().decode(encoding='UTF-8'))
+ print("")
+ ssh.close()
+
+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 )
+ for command in commands:
+ stdin , stdout, stderr = ssh.exec_command(command)
+ print('')
+ print(stdout.read().decode(encoding='UTF-8'))
+ print('
')
+ ssh.close()
+
+def chooseServer(formName, title, note):
+ print('' + title + '
')
+ print('Choose server
')
+ print('')
+ if note == "y":
+ print('Note: If you reconfigure First server, second will reconfigured automatically
')
+
+def choose_server_with_vip(serv):
+ import listserv as listhap
+ listhap.listhap = merge_two_dicts(listhap.listhap, listhap.list_hap_vip)
+ for i in sorted(listhap.listhap):
+ if listhap.listhap.get(i) == serv:
+ selected = 'selected'
+ else:
+ selected = ''
+ print('' % (listhap.listhap.get(i), selected, i))
+
+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
new file mode 100644
index 00000000..b88c2c07
--- /dev/null
+++ b/cgi-bin/listserv.py
@@ -0,0 +1,10 @@
+listhap= {
+ '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 = {
+ '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
new file mode 100644
index 00000000..52c80c65
--- /dev/null
+++ b/cgi-bin/login.py
@@ -0,0 +1,61 @@
+#!/usr/bin/env python3
+import cgi
+import html
+import os
+import funct
+import http.cookies
+import json
+
+cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
+form = cgi.FieldStorage()
+ref = form.getvalue('ref')
+login = form.getvalue('login')
+password = form.getvalue('pass')
+USERS = 'cgi-bin/users'
+
+try:
+ with open(USERS, "r") as user:
+ pass
+except IOError:
+ print("Can't load users DB")
+
+def login_page(error):
+ if error == "error":
+ printError = "Somthing wrong :( I'm sad about this, but try again!"
+ else:
+ printError = "First you need to login."
+
+ funct.head("Login page")
+
+ 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)
+ 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'])
+ if ref is None:
+ ref = "index.html"
+ print("Content-type: text/html\n")
+ print('Redirecting')
+ print('')
+ print('' % ref)
+ else:
+ login_page("error")
+ break
+
+funct.footer()
+
+
+
diff --git a/cgi-bin/logs.py b/cgi-bin/logs.py
new file mode 100644
index 00000000..f60f5be9
--- /dev/null
+++ b/cgi-bin/logs.py
@@ -0,0 +1,77 @@
+#!/usr/bin/env python3
+import html
+import cgi
+import listserv as listhap
+import subprocess
+import os
+import funct
+import configparser
+
+form = cgi.FieldStorage()
+serv = form.getvalue('serv')
+
+funct.head("HAproxy Logs")
+funct.check_config()
+
+path_config = "haproxy-webintarface.config"
+config = configparser.ConfigParser()
+config.read(path_config)
+
+print('HAproxy Logs
')
+print('Choose server & number rows
')
+print('')
+
+if form.getvalue('serv') is not None:
+ rows = form.getvalue('rows')
+ grep = form.getvalue('grep')
+
+ if grep is not None:
+ grep_act = '|grep'
+ else:
+ grep_act = ''
+ grep = ''
+
+ syslog_server_enable = config.get('logs', 'syslog_server_enable')
+ if syslog_server_enable is None or syslog_server_enable == "disable":
+ local_path_logs = config.get('logs', 'local_path_logs')
+ syslog_server = serv
+ commands = [ 'sudo tail -%s %s %s %s' % (rows, local_path_logs, grep_act, grep) ]
+ else:
+ 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)
+
+ print('')
+
+funct.footer()
\ No newline at end of file
diff --git a/cgi-bin/users b/cgi-bin/users
new file mode 100644
index 00000000..c42529ed
--- /dev/null
+++ b/cgi-bin/users
@@ -0,0 +1 @@
+{ "firstName": "admin", "lastName": "admin", "login": "admin", "password": "admin" }
diff --git a/cgi-bin/viewsttats.py b/cgi-bin/viewsttats.py
new file mode 100644
index 00000000..89446a28
--- /dev/null
+++ b/cgi-bin/viewsttats.py
@@ -0,0 +1,55 @@
+#!/usr/bin/env python3
+import html
+import cgi
+import requests
+import funct
+import listserv as listhap
+import configparser
+from requests_toolbelt.utils import dump
+
+funct.check_config()
+
+path_config = "haproxy-webintarface.config"
+config = configparser.ConfigParser()
+config.read(path_config)
+haproxy_user = config.get('haproxy', 'user')
+haproxy_pass = config.get('haproxy', 'password')
+stats_port = config.get('haproxy', 'stats_port')
+
+listhap.listhap = funct.merge_two_dicts(listhap.listhap, listhap.list_hap_vip)
+
+form = cgi.FieldStorage()
+serv = form.getvalue('serv')
+
+if serv is None:
+ serv = '172.28.9.161'
+
+try:
+ response = requests.get('http://%s:%s/stats' % (serv, stats_port), auth=(haproxy_user, haproxy_pass))
+except requests.exceptions.ConnectTimeout:
+ print('Oops. Connection timeout occured!')
+except requests.exceptions.ReadTimeout:
+ print('Oops. Read timeout occured')
+
+print("Content-type: text/html\n")
+
+for i in listhap.listhap:
+ if listhap.listhap.get(i) == serv:
+ servname = i
+
+print('Curent server IP - %s, name - %s
' % (serv, servname))
+print('Home Page')
+
+print('Choose server!
')
+print('')
+
+data = dump.dump_all(response)
+print('')
+print(data.decode('utf-8'))
+