mirror of https://github.com/Aidaho12/haproxy-wi
				
				
				
			Add files via upload
							parent
							
								
									ffb57b30fa
								
							
						
					
					
						commit
						728f83b259
					
				| 
						 | 
				
			
			@ -0,0 +1,77 @@
 | 
			
		|||
#!/usr/bin/env python3
 | 
			
		||||
import html
 | 
			
		||||
import cgi
 | 
			
		||||
import listserv as listhap
 | 
			
		||||
import os
 | 
			
		||||
import http.cookies
 | 
			
		||||
import configparser
 | 
			
		||||
import funct
 | 
			
		||||
import paramiko
 | 
			
		||||
from paramiko import SSHClient
 | 
			
		||||
from datetime import datetime
 | 
			
		||||
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()
 | 
			
		||||
 | 
			
		||||
path_config = "haproxy-webintarface.config"
 | 
			
		||||
config = configparser.ConfigParser()
 | 
			
		||||
config.read(path_config)
 | 
			
		||||
 | 
			
		||||
fullpath = config.get('main', 'fullpath')
 | 
			
		||||
hap_configs_dir = config.get('configs', 'haproxy_save_configs_dir')
 | 
			
		||||
 | 
			
		||||
if login is None:
 | 
			
		||||
	print('<meta http-equiv="refresh" content="0; url=login.py?ref=config.py">')
 | 
			
		||||
 | 
			
		||||
if serv is not None:
 | 
			
		||||
	fmt = "%Y-%m-%d.%H:%M:%S"
 | 
			
		||||
	now_utc = datetime.now(timezone('Asia/Almaty'))
 | 
			
		||||
	cfg = hap_configs_dir + serv + "-" + now_utc.strftime(fmt) + ".cfg"
 | 
			
		||||
 | 
			
		||||
funct.chooseServer("config.py#conf", "Edit HAproxy config", "y")
 | 
			
		||||
 | 
			
		||||
if form.getvalue('serv') is not None and form.getvalue('open') is not None :
 | 
			
		||||
	funct.logging(serv, "config.py open config")
 | 
			
		||||
	funct.get_config(serv, cfg)
 | 
			
		||||
	
 | 
			
		||||
	conf = open(cfg, "r")
 | 
			
		||||
	print('<a name="conf"></a>')
 | 
			
		||||
	print("<h3>Config from %s</h3>" % serv)
 | 
			
		||||
	print('<form action="config.py#top" method="get">')
 | 
			
		||||
	print('<input type="hidden" value="%s" name="serv">' % serv)
 | 
			
		||||
	print('<input type="hidden" value="%s.old" name="oldconfig">' % cfg)
 | 
			
		||||
	print('<textarea name="config" rows="35" cols="100">%s</textarea>' % conf.read())
 | 
			
		||||
	print('<p><button type="submit" value="save and restart" onclick="return confirm(\'are u shure?\')">save and restart</button></p></form>')
 | 
			
		||||
	print('<center><h3><a href="#top" title="UP">UP</a></center>')
 | 
			
		||||
	conf.close
 | 
			
		||||
 | 
			
		||||
	os.system("/bin/sudo /bin/mv %s %s.old" % (cfg, cfg))	
 | 
			
		||||
 | 
			
		||||
if form.getvalue('serv') is not None and form.getvalue('config') is not None :
 | 
			
		||||
	funct.logging(serv, "config.py edited config and restarted service")
 | 
			
		||||
	config = form.getvalue('config')
 | 
			
		||||
	oldcfg = form.getvalue('oldconfig')
 | 
			
		||||
 | 
			
		||||
	try:
 | 
			
		||||
		with open(cfg, "a") as conf:
 | 
			
		||||
			conf.write(config)
 | 
			
		||||
	except IOError:
 | 
			
		||||
		print("Can't read import config file")
 | 
			
		||||
 | 
			
		||||
	print("<b>New config was saved as: %s </b></br></br>" % cfg)
 | 
			
		||||
	
 | 
			
		||||
	funct.upload_and_restart(serv, cfg)
 | 
			
		||||
	
 | 
			
		||||
	os.system("/bin/diff -ub %s %s >> %slog/config_edit.log" % (oldcfg, cfg, fullpath))
 | 
			
		||||
	os.system("/bin/sudo /bin/rm -f " + hap_configs_dir + "*.old")
 | 
			
		||||
 | 
			
		||||
	print('</br><a href="viewsttats.py" title="View stats">Go to view stats</a> <br />')
 | 
			
		||||
	
 | 
			
		||||
funct.footer()
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,47 @@
 | 
			
		|||
#!/usr/bin/env python3
 | 
			
		||||
import html
 | 
			
		||||
import cgi
 | 
			
		||||
import os
 | 
			
		||||
import funct
 | 
			
		||||
import paramiko
 | 
			
		||||
import configparser
 | 
			
		||||
from paramiko import SSHClient
 | 
			
		||||
from datetime import datetime
 | 
			
		||||
from pytz import timezone
 | 
			
		||||
 | 
			
		||||
form = cgi.FieldStorage()
 | 
			
		||||
serv = form.getvalue('serv')
 | 
			
		||||
servNew = form.getvalue('serNew')
 | 
			
		||||
 | 
			
		||||
funct.head("Show HAproxy config")
 | 
			
		||||
funct.check_config()
 | 
			
		||||
 | 
			
		||||
path_config = "haproxy-webintarface.config"
 | 
			
		||||
config = configparser.ConfigParser()
 | 
			
		||||
config.read(path_config)
 | 
			
		||||
 | 
			
		||||
ssh_keys = config.get('ssh', 'ssh_keys')
 | 
			
		||||
ssh_user_name = config.get('ssh', 'ssh_user_name')
 | 
			
		||||
hap_configs_dir = config.get('configs', 'haproxy_save_configs_dir')
 | 
			
		||||
haproxy_config_path  = config.get('haproxy', 'haproxy_config_path')
 | 
			
		||||
 | 
			
		||||
if serv is not None:
 | 
			
		||||
	fmt = "%Y-%m-%d.%H:%M:%S"
 | 
			
		||||
	now_utc = datetime.now(timezone('Asia/Almaty'))
 | 
			
		||||
	cfg = hap_configs_dir + serv + "-" + now_utc.strftime(fmt) + ".cfg"
 | 
			
		||||
 | 
			
		||||
funct.chooseServer("configshow.py#conf", "Show HAproxy config", "n")
 | 
			
		||||
 | 
			
		||||
if form.getvalue('serv') is not None and form.getvalue('open') is not None :
 | 
			
		||||
	funct.get_config(serv, cfg)
 | 
			
		||||
	
 | 
			
		||||
	conf = open(cfg, "r")
 | 
			
		||||
	print('<a name="conf"></a>')
 | 
			
		||||
	print("<h3>Config from %s</h3>" % serv)
 | 
			
		||||
	print('<textarea class="ro" readonly name="config" rows="35" cols="100"> %s </textarea>' % conf.read())
 | 
			
		||||
	print('<center><h3><a href="#top" title="UP">UP</a></center>')
 | 
			
		||||
	conf.close
 | 
			
		||||
 | 
			
		||||
	os.system("/bin/sudo /bin/rm -f " + cfg)	
 | 
			
		||||
	
 | 
			
		||||
funct.footer()
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,87 @@
 | 
			
		|||
#!/usr/bin/env python3
 | 
			
		||||
import html
 | 
			
		||||
import cgi
 | 
			
		||||
import subprocess
 | 
			
		||||
import os
 | 
			
		||||
import http.cookies
 | 
			
		||||
import funct
 | 
			
		||||
import paramiko
 | 
			
		||||
import configparser
 | 
			
		||||
from paramiko import SSHClient
 | 
			
		||||
from datetime import datetime
 | 
			
		||||
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()
 | 
			
		||||
 | 
			
		||||
path_config = "haproxy-webintarface.config"
 | 
			
		||||
config = configparser.ConfigParser()
 | 
			
		||||
config.read(path_config)
 | 
			
		||||
 | 
			
		||||
hap_configs_dir = config.get('configs', 'haproxy_save_configs_dir')
 | 
			
		||||
 | 
			
		||||
if login is None:
 | 
			
		||||
	print('<meta http-equiv="refresh" content="0; url=login.py?ref=configver.py">')
 | 
			
		||||
 | 
			
		||||
funct.chooseServer("configver.py#conf", "Old Versions HAproxy config", "y")
 | 
			
		||||
 | 
			
		||||
if serv is not None and form.getvalue('open') is not None:
 | 
			
		||||
	
 | 
			
		||||
	print('<center><h3>Choose old version</h3>')
 | 
			
		||||
	print('<form action="configver.py#conf" method="get">')
 | 
			
		||||
	print('<p><select autofocus required name="configver">')
 | 
			
		||||
	print('<option disabled selected>Choose version</option>')
 | 
			
		||||
	
 | 
			
		||||
	import glob
 | 
			
		||||
 | 
			
		||||
	os.chdir(hap_configs_dir)
 | 
			
		||||
 | 
			
		||||
	for files in glob.glob('*.cfg'):
 | 
			
		||||
		ip = files.split("-")
 | 
			
		||||
		if serv == ip[0]:
 | 
			
		||||
			if configver == files:
 | 
			
		||||
				selected = 'selected'
 | 
			
		||||
			else:
 | 
			
		||||
				selected = ''
 | 
			
		||||
			print('<option value="%s" %s>%s</option>' % (files, selected, files))
 | 
			
		||||
 | 
			
		||||
	print('</select>')
 | 
			
		||||
	print('<input type="hidden" value="%s" name="serv">' % serv)
 | 
			
		||||
	print('<input type="hidden" value="open" name="open">')
 | 
			
		||||
	print('<p><button type="submit" value="Select" name="Select">Select</button></p></form>') 
 | 
			
		||||
 | 
			
		||||
	Select = form.getvalue('Select')
 | 
			
		||||
 | 
			
		||||
	if Select is not None:
 | 
			
		||||
		
 | 
			
		||||
		configver = form.getvalue('configver')
 | 
			
		||||
		funct.logging(serv, "open old config %s" % configver)
 | 
			
		||||
 | 
			
		||||
		conf = open(configver, "r")
 | 
			
		||||
		print("<h3>Config from %s, and version is: %s</h3>" % (serv, configver))
 | 
			
		||||
		print('<form action="configver.py#conf" method="get">')
 | 
			
		||||
		print('<input type="hidden" value="%s" name="serv">' % serv)
 | 
			
		||||
		print('<input type="hidden" value="%s" name="configver">' % configver)
 | 
			
		||||
		print('<a name="conf"></a>')
 | 
			
		||||
		print('<textarea class="ro" name="config" rows="35" cols="100" readonly> %s </textarea>' % conf.read())
 | 
			
		||||
		print('<p><button type="submit" value="Upload and restart" onclick="return confirm(\'are u shure?\')">Upload and restart</button></p></form>')
 | 
			
		||||
		print('<center><h3><a href="#top" title="UP">UP</a></center>')
 | 
			
		||||
		conf.close
 | 
			
		||||
 | 
			
		||||
if form.getvalue('serv') is not None and form.getvalue('config') is not None:
 | 
			
		||||
	configver = form.getvalue('configver')
 | 
			
		||||
	configver = hap_configs_dir + configver
 | 
			
		||||
	
 | 
			
		||||
	funct.logging(serv, "configver.py upload old config %s" % configver)
 | 
			
		||||
	
 | 
			
		||||
	print("<b>Uploaded old config ver: %s </b></br></br>" % configver)
 | 
			
		||||
 | 
			
		||||
	funct.upload_and_restart(serv, configver)
 | 
			
		||||
 | 
			
		||||
	print('</br><a href="viewsttats.py" title="View stats">Go to view stats</a> <br />')
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,77 @@
 | 
			
		|||
#!/usr/bin/env python3
 | 
			
		||||
import html
 | 
			
		||||
import cgi
 | 
			
		||||
import listserv as listhap
 | 
			
		||||
import subprocess
 | 
			
		||||
import os
 | 
			
		||||
import funct
 | 
			
		||||
import glob
 | 
			
		||||
import paramiko
 | 
			
		||||
import configparser
 | 
			
		||||
from paramiko import SSHClient
 | 
			
		||||
	
 | 
			
		||||
form = cgi.FieldStorage()
 | 
			
		||||
serv = form.getvalue('serv')
 | 
			
		||||
left = form.getvalue('left')
 | 
			
		||||
right = form.getvalue('right')
 | 
			
		||||
 | 
			
		||||
funct.head("Compare HAproxy configs")
 | 
			
		||||
funct.check_config()
 | 
			
		||||
 | 
			
		||||
path_config = "haproxy-webintarface.config"
 | 
			
		||||
config = configparser.ConfigParser()
 | 
			
		||||
config.read(path_config)
 | 
			
		||||
 | 
			
		||||
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')
 | 
			
		||||
 | 
			
		||||
funct.chooseServer("diff.py#diff", "Compare HAproxy configs", "n")
 | 
			
		||||
 | 
			
		||||
if form.getvalue('serv') is not None and form.getvalue('open') is not None :
 | 
			
		||||
	
 | 
			
		||||
	print('<form action="diff.py#diff" method="get">')
 | 
			
		||||
	print('<center><h3><span style="padding: 75px;">Choose left</span><span style="padding: 100px;">Choose right</span></h3>')
 | 
			
		||||
	
 | 
			
		||||
	print('<p><select autofocus required name="left">')
 | 
			
		||||
	print('<option disabled selected>Choose version</option>')
 | 
			
		||||
	
 | 
			
		||||
	os.chdir(hap_configs_dir)
 | 
			
		||||
	
 | 
			
		||||
	for files in glob.glob('*.cfg'):
 | 
			
		||||
		ip = files.split("-")
 | 
			
		||||
		if serv == ip[0]:
 | 
			
		||||
			if left == files:
 | 
			
		||||
				selected = 'selected'
 | 
			
		||||
			else:
 | 
			
		||||
				selected = ''
 | 
			
		||||
			print('<option value="%s" %s>%s</option>' % (files, selected, files))
 | 
			
		||||
 | 
			
		||||
	print('</select>')
 | 
			
		||||
 | 
			
		||||
	print('<select autofocus required name="right">')
 | 
			
		||||
	print('<option disabled selected>Choose version</option>')
 | 
			
		||||
	
 | 
			
		||||
	for files in glob.glob('*.cfg'):
 | 
			
		||||
		ip = files.split("-")
 | 
			
		||||
		if serv == ip[0]:
 | 
			
		||||
			if right == files:
 | 
			
		||||
				selected = 'selected'
 | 
			
		||||
			else:
 | 
			
		||||
				selected = ''
 | 
			
		||||
			print('<option value="%s" %s>%s</option>' % (files, selected, files))
 | 
			
		||||
 | 
			
		||||
	print('</select>')
 | 
			
		||||
	print('<input type="hidden" value="%s" name="serv">' % serv)
 | 
			
		||||
	print('<input type="hidden" value="open" name="open">')
 | 
			
		||||
	print('<p><button type="submit" value="Compare" name="Compare">Compare</button></p></form>')
 | 
			
		||||
	
 | 
			
		||||
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)
 | 
			
		||||
	
 | 
			
		||||
	print('<h3><a href="#top" title="UP">UP</a></center>')
 | 
			
		||||
	
 | 
			
		||||
funct.footer()
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,84 @@
 | 
			
		|||
#!/usr/bin/env python3
 | 
			
		||||
import html
 | 
			
		||||
import cgi
 | 
			
		||||
import listserv as listhap
 | 
			
		||||
import subprocess 
 | 
			
		||||
import os
 | 
			
		||||
import http.cookies
 | 
			
		||||
import funct
 | 
			
		||||
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")
 | 
			
		||||
 | 
			
		||||
if login is None:
 | 
			
		||||
	print('<meta http-equiv="refresh" content="0; url=login.py?ref=configver.py">')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
print('<center><h2>Edit & show HAproxy settings</h2></center>')
 | 
			
		||||
print('<center><h3>Choose server & action: Disable/Enable server or output any information about the server:</h3>')
 | 
			
		||||
print('<form action="edit.py" method="get">')
 | 
			
		||||
print('<p><select autofocus required name="serv">')
 | 
			
		||||
print('<option disabled selected>Choose server</option>')
 | 
			
		||||
	
 | 
			
		||||
funct.choose_server_with_vip(serv)
 | 
			
		||||
 | 
			
		||||
action = form.getvalue('servaction')
 | 
			
		||||
backend = form.getvalue('servbackend')
 | 
			
		||||
 | 
			
		||||
if action == '1':
 | 
			
		||||
	selected1 = 'selected'
 | 
			
		||||
	selected2 = ''
 | 
			
		||||
	selected3 = ''
 | 
			
		||||
elif action == '2':
 | 
			
		||||
	selected1 = ''
 | 
			
		||||
	selected2 = 'selected'
 | 
			
		||||
	selected3 = ''
 | 
			
		||||
elif action == '3':
 | 
			
		||||
	selected1 = ''
 | 
			
		||||
	selected2 = ''
 | 
			
		||||
	selected3 = 'selected'
 | 
			
		||||
else:
 | 
			
		||||
	selected1 = ''
 | 
			
		||||
	selected2 = ''
 | 
			
		||||
	selected3 = ''
 | 
			
		||||
 | 
			
		||||
print('</select>')
 | 
			
		||||
print('<select autofocus required name="servaction">')
 | 
			
		||||
print('<option disabled selected>Choose action</option>')
 | 
			
		||||
print('<option value=1 %s>Disable server</option>' % selected1)
 | 
			
		||||
print('<option value=2 %s>Enable server</option>' % selected2)
 | 
			
		||||
print('<option value=3 %s>Show</option>' % selected3)
 | 
			
		||||
print('</select>')
 | 
			
		||||
print('<input type="text" name="servbackend"  size=40 placeholder="Backend/Server, show info, pools or help" required>')
 | 
			
		||||
print('<p><button type="submit">Enter</button></p></form>')
 | 
			
		||||
 | 
			
		||||
if form.getvalue('servaction') is not None:
 | 
			
		||||
	action = form.getvalue('servaction')
 | 
			
		||||
	backend = form.getvalue('servbackend')
 | 
			
		||||
 | 
			
		||||
	if action == '1':
 | 
			
		||||
		enable = 'disable server'
 | 
			
		||||
	elif action == '2':
 | 
			
		||||
		enable = 'enable server'
 | 
			
		||||
	elif action == '3':
 | 
			
		||||
		enable = 'show'
 | 
			
		||||
 | 
			
		||||
	cmd='echo "%s %s" |nc %s 1999' % (enable, backend, serv)
 | 
			
		||||
	p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True)
 | 
			
		||||
	stdout, stderr = p.communicate()
 | 
			
		||||
	output = stdout.splitlines()
 | 
			
		||||
 | 
			
		||||
	print('<center><h3>You %s %s on HAproxy %s. <a href="viewsttats.py?serv=%s" title="View stat" target="_blank">Look it		    </a> or <a href="edit.py" title="Edit">Edit something else</a>'  % (enable, backend, serv, serv))
 | 
			
		||||
	print('</center>')
 | 
			
		||||
 | 
			
		||||
	print('\n<center><p>'.join(map(str, output)))
 | 
			
		||||
	
 | 
			
		||||
	action = 'edit.py ' + enable + ' ' + backend
 | 
			
		||||
	funct.logging(serv, action)
 | 
			
		||||
 | 
			
		||||
funct.footer()
 | 
			
		||||
| 
						 | 
				
			
			@ -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('<b style="color: red">Check config file, no %s section</b>' % 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('<html><head><title>%s</title>' % title)
 | 
			
		||||
	print('<link href="/favicon.ico" rel="icon" type="image/png" />')
 | 
			
		||||
	print('<link href="/style.css" rel="stylesheet"><meta charset="UTF-8"></head><body>')
 | 
			
		||||
	print('<a name="top"></a>')
 | 
			
		||||
	print('<div class="top-menu"><div class="top-link">')
 | 
			
		||||
	print('<a href=/ title="Home Page" style="size:5">Home Page</a> ')
 | 
			
		||||
	print('<a href=/cgi-bin/viewsttats.py title="View Stats" style="size:5">Stats</a> ')	
 | 
			
		||||
	print('<a href=/cgi-bin/edit.py title="Edit settings" style="size:5">Edit settings</a> ')
 | 
			
		||||
	print('<a href=/cgi-bin/logs.py title="Logs" style="size:6">Logs</a>')
 | 
			
		||||
	print('<span style="color: #fff">  | Configs: </span>')
 | 
			
		||||
	print('<a href=/cgi-bin/configshow.py title="Show Config">Show</a> ')
 | 
			
		||||
	print('<a href=/cgi-bin/diff.py title="Compare Configs">Compare</a> ')
 | 
			
		||||
	print('<a href=/cgi-bin/config.py title="Edit Config" style="size:5">Edit</a> ')
 | 
			
		||||
	print('<a href=/cgi-bin/configver.py title="Upload old config" style="size:5">Upload old</a>')	
 | 
			
		||||
	print('</div></div><div class="conteiner">')
 | 
			
		||||
 | 
			
		||||
def footer():
 | 
			
		||||
	print('</center></div><div class="footer"><div class="footer-link">')
 | 
			
		||||
	print('<a href=/ title="Home Page" style="size:5">Home Page</a> ')
 | 
			
		||||
	print('<a href=/cgi-bin/viewsttats.py title="View Stats" style="size:5">Stats</a> ')	
 | 
			
		||||
	print('<a href=/cgi-bin/edit.py title="Edit settings" style="size:5">Edit settings</a> ')
 | 
			
		||||
	print('<a href=/cgi-bin/logs.py title="Logs" style="size:6">Logs</a>')
 | 
			
		||||
	print('<span style="color: #fff">  | Configs: </span>')
 | 
			
		||||
	print('<a href=/cgi-bin/configshow.py title="Show Config">Show</a> ')
 | 
			
		||||
	print('<a href=/cgi-bin/diff.py title="Compare Configs">Compare</a> ')
 | 
			
		||||
	print('<a href=/cgi-bin/config.py title="Edit Config" style="size:5">Edit</a> ')
 | 
			
		||||
	print('<a href=/cgi-bin/configver.py title="Upload old config" style="size:5">Upload old</a>')	
 | 
			
		||||
	print('</div></div></body></html>')
 | 
			
		||||
	
 | 
			
		||||
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<br />")
 | 
			
		||||
	ssh.connect( hostname = serv, username = ssh_user_name, pkey = k )
 | 
			
		||||
	print("connected<br />")
 | 
			
		||||
	sftp = ssh.open_sftp()
 | 
			
		||||
	sftp.put(cfg, haproxy_config_path)
 | 
			
		||||
	sftp.close()
 | 
			
		||||
	commands = [ "service haproxy restart" ]
 | 
			
		||||
	for command in commands:
 | 
			
		||||
		print("</br>Executing: {}".format( command ))
 | 
			
		||||
		print("</br>")
 | 
			
		||||
		stdin , stdout, stderr = ssh.exec_command(command)
 | 
			
		||||
		print(stdout.read().decode(encoding='UTF-8'))
 | 
			
		||||
		print("</br>Errors:")
 | 
			
		||||
		print(stderr.read().decode(encoding='UTF-8'))
 | 
			
		||||
		print("</br>")
 | 
			
		||||
	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('<pre>')
 | 
			
		||||
		print(stdout.read().decode(encoding='UTF-8'))
 | 
			
		||||
		print('</pre>')
 | 
			
		||||
	ssh.close()
 | 
			
		||||
	
 | 
			
		||||
def chooseServer(formName, title, note):
 | 
			
		||||
	print('<center><h2>' + title + '</h2>')
 | 
			
		||||
	print('<h3>Choose server</h3>')
 | 
			
		||||
	print('<form action=' + formName + ' method="get">')
 | 
			
		||||
	print('<p><select autofocus required name="serv">')
 | 
			
		||||
	print('<option disabled>Choose server</option>')
 | 
			
		||||
 | 
			
		||||
	form = cgi.FieldStorage()
 | 
			
		||||
	serv = form.getvalue('serv')
 | 
			
		||||
	servNew = form.getvalue('serNew')
 | 
			
		||||
 | 
			
		||||
	for i in sorted(listhap.listhap):
 | 
			
		||||
		if listhap.listhap.get(i) == serv or listhap.listhap.get(i) == servNew:
 | 
			
		||||
			selected = 'selected'
 | 
			
		||||
		else:
 | 
			
		||||
			selected = ''
 | 
			
		||||
 | 
			
		||||
		print('<option value="%s" %s>%s</option>' % (listhap.listhap.get(i), selected, i))
 | 
			
		||||
 | 
			
		||||
	print('</select>')
 | 
			
		||||
	print('<p><button type="submit" value="open" name="open">Open</button></p></form>')
 | 
			
		||||
	if note == "y":
 | 
			
		||||
		print('<p><b>Note:</b> If you reconfigure First server, second will reconfigured automatically</p><br />')
 | 
			
		||||
		
 | 
			
		||||
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('<option value="%s" %s>%s</option>' % (listhap.listhap.get(i), selected, i))
 | 
			
		||||
 | 
			
		||||
def merge_two_dicts(x, y):
 | 
			
		||||
    z = x.copy()
 | 
			
		||||
    z.update(y)
 | 
			
		||||
    return z		
 | 
			
		||||
| 
						 | 
				
			
			@ -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' 
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -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 = "<b style='color: red'>Somthing wrong :( I'm sad about this, but try again!</b></br></br>"
 | 
			
		||||
	else:
 | 
			
		||||
		printError = "<b style='color: red'>First you need to login.</b></br></br>"	
 | 
			
		||||
		
 | 
			
		||||
	funct.head("Login page")
 | 
			
		||||
	
 | 
			
		||||
	print('<center><div class="conteiner"><form name="auth" action="login.py" class="form-horizontal" method="post">')
 | 
			
		||||
	print(printError)
 | 
			
		||||
	print('<label for="login"> Login: </label>  <input type="text" name="login" class="form-control">')
 | 
			
		||||
	print('<label for="pass"> Pass: </label>   <input type="password" name="pass" class="form-control">')
 | 
			
		||||
	print('<input type="hidden" value="%s" name="ref">' % ref)
 | 
			
		||||
	print('<button type="submit" name="Login" value="Enter">Sign</button>')
 | 
			
		||||
	print('</form></div></center>')
 | 
			
		||||
 | 
			
		||||
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('<html><head><title>Redirecting</title><meta charset="UTF-8">')
 | 
			
		||||
			print('<link href="/style.css" rel="stylesheet">')
 | 
			
		||||
			print('<meta http-equiv="refresh" content="0; url=%s">' % ref)
 | 
			
		||||
		else:
 | 
			
		||||
			login_page("error")
 | 
			
		||||
			break
 | 
			
		||||
 | 
			
		||||
funct.footer()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -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('<center><h2>HAproxy Logs</h2></center>')
 | 
			
		||||
print('<center><h3>Choose server & number rows</h3>')
 | 
			
		||||
print('<form action="logs.py" method="get">')
 | 
			
		||||
print('<p><select autofocus required name="serv">')
 | 
			
		||||
print('<option disabled>Choose server</option>')
 | 
			
		||||
 | 
			
		||||
for i in sorted(listhap.listhap):
 | 
			
		||||
	if listhap.listhap.get(i) == serv:
 | 
			
		||||
		selected = 'selected'
 | 
			
		||||
	else:
 | 
			
		||||
		selected = ''
 | 
			
		||||
 | 
			
		||||
	if listhap.listhap.get(i) == '172.28.5.17' or listhap.listhap.get(i) == '172.28.9.161':
 | 
			
		||||
		continue
 | 
			
		||||
 | 
			
		||||
	print('<option value="%s" %s>%s</option>' % (listhap.listhap.get(i), selected, i))
 | 
			
		||||
 | 
			
		||||
print('</select>')
 | 
			
		||||
 | 
			
		||||
if form.getvalue('serv') is not None:
 | 
			
		||||
        rows = 'value='+form.getvalue('rows')
 | 
			
		||||
else:
 | 
			
		||||
	rows = 'value=10'
 | 
			
		||||
 | 
			
		||||
if form.getvalue('grep') is not None:
 | 
			
		||||
	grep = 'value='+form.getvalue('grep')
 | 
			
		||||
else:
 | 
			
		||||
	grep = 'value='
 | 
			
		||||
 | 
			
		||||
print('<input type="text" name="rows" %s required>' % rows)
 | 
			
		||||
print('|grep') 
 | 
			
		||||
print('<input type="text" name="grep" %s>' % grep)
 | 
			
		||||
print('<p><button type="submit">Show</button></p></form>')
 | 
			
		||||
 | 
			
		||||
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('<center><h3><a href="#top" title="UP">UP</a></center>')
 | 
			
		||||
 | 
			
		||||
funct.footer()
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1 @@
 | 
			
		|||
{ "firstName": "admin", "lastName": "admin", "login": "admin", "password": "admin" }
 | 
			
		||||
| 
						 | 
				
			
			@ -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('<h3>Curent server IP - %s, name - %s </h3></br>' % (serv, servname))
 | 
			
		||||
print('<a href=/ title="Home Page" style="size:5">Home Page</a></br></br>')
 | 
			
		||||
 | 
			
		||||
print('<h3>Choose server!</h3></br>')
 | 
			
		||||
print('<form action="viewsttats.py" method="get">')
 | 
			
		||||
print('<p><select autofocus required name="serv">')
 | 
			
		||||
print('<option disabled>Choose server</option>')
 | 
			
		||||
 | 
			
		||||
funct.choose_server_with_vip(serv)
 | 
			
		||||
 | 
			
		||||
print('</select><input type="submit"></p></form>')
 | 
			
		||||
 | 
			
		||||
data = dump.dump_all(response)
 | 
			
		||||
print('<a name="conf"></a>')
 | 
			
		||||
print(data.decode('utf-8'))
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue