Pavel Loginov 5 years ago
parent a923cf1d40
commit 72d465a1a0

@ -16,6 +16,7 @@ Web interface(user-friendly web GUI, alerting, monitoring and secure) for managi
1. Installation and updating Grafana, Prometheus servers with HAProxy-WI
1. Installation and updating HAProxy and Nginx exporters with HAProxy-WI
2. Configure HAProxy, Nginx and Keepalived In a jiffy with HAProxy-WI
3. Dynamic change of Maxconn, backend's IP address and port with saving changes to the config file
3. View and analyse Status of all Frontend/backend server via HAProxy-WI from a single control panel.
4. Enable/disable servers through stats page without rebooting HAProxy
5. View/Analyse HAproxy, Nginx logs straight from the HAProxy-WI web interface

@ -382,6 +382,57 @@ def rewrite_section(start_line, end_line, config, section):
return return_config
def get_backends_from_config(serv, backends='', **kwargs):
configs_dir = get_config_var('configs', 'haproxy_save_configs_dir')
format = 'cfg'
record = False
try:
cfg = configs_dir+get_files(dir=configs_dir, format=format)[0]
except Exception as e:
logging('localhost', str(e), haproxywi=1)
try:
cfg = configs_dir + serv + "-" + get_data('config') + '.'+format
except:
logging('localhost', ' Cannot generate cfg path', haproxywi=1)
try:
error = get_config(serv, cfg)
except:
logging('localhost', ' Cannot download config', haproxywi=1)
print('error: Cannot get backends')
sys.exit()
with open(cfg, 'r') as f:
for line in f:
if backends == 'frontend':
if (line.startswith('listen') or line.startswith('frontend')) and 'stats' not in line:
line = line.strip()
print(line.split(' ')[1], end="<br>")
def get_all_stick_table():
import sql
haproxy_sock_port = sql.get_setting('haproxy_sock_port')
cmd='echo "show table"|nc %s %s |awk \'{print $3}\' | tr -d \'\n\' | tr -d \'[:space:]\'' % (serv, haproxy_sock_port)
output, stderr = subprocess_execute(cmd)
return output[0]
def get_stick_table(table):
import sql
haproxy_sock_port = sql.get_setting('haproxy_sock_port')
cmd='echo "show table %s"|nc %s %s |awk -F"#" \'{print $2}\' |head -1 | tr -d \'\n\'' % (table, serv, haproxy_sock_port)
output, stderr = subprocess_execute(cmd)
tables_head = []
for i in output[0].split(','):
i = i.split(':')[1]
tables_head.append(i)
cmd='echo "show table %s"|nc %s %s |grep -v "#"' % (table, serv, haproxy_sock_port)
output, stderr = subprocess_execute(cmd)
return tables_head, output
def install_haproxy(serv, **kwargs):
import sql
script = "install_haproxy.sh"

@ -8,11 +8,13 @@ form = funct.form
serv = form.getvalue('serv')
act = form.getvalue('act')
if form.getvalue('new_metrics') or form.getvalue('new_waf_metrics') or form.getvalue('metrics_hapwi_ram') or form.getvalue('metrics_hapwi_cpu'):
print('Content-type: application/json\n')
else:
print('Content-type: text/html\n')
if act == "checkrestart":
servers = sql.get_dick_permit(ip=serv)
for server in servers:
@ -90,6 +92,150 @@ if form.getvalue('backend') is not None:
funct.show_backends(serv)
if form.getvalue('ip_select') is not None:
funct.show_backends(serv)
if form.getvalue('ipbackend') is not None and form.getvalue('backend_server') is None:
import sql
haproxy_sock_port = sql.get_setting('haproxy_sock_port')
backend = form.getvalue('ipbackend')
cmd='echo "show servers state"|nc %s %s |grep "%s" |awk \'{print $4}\'' % (serv, haproxy_sock_port, backend)
output, stderr = funct.subprocess_execute(cmd)
for i in output:
if i == ' ':
continue
i = i.strip()
print(i+'<br>')
if form.getvalue('ipbackend') is not None and form.getvalue('backend_server') is not None:
import sql
haproxy_sock_port = sql.get_setting('haproxy_sock_port')
backend = form.getvalue('ipbackend')
backend_server = form.getvalue('backend_server')
cmd='echo "show servers state"|nc %s %s |grep "%s" |grep "%s" |awk \'{print $5":"$19}\' |head -1' % (serv, haproxy_sock_port, backend, backend_server)
output, stderr = funct.subprocess_execute(cmd)
print(output[0])
if form.getvalue('backend_ip') is not None:
import sql
backend_backend = form.getvalue('backend_backend')
backend_server = form.getvalue('backend_server')
backend_ip = form.getvalue('backend_ip')
backend_port = form.getvalue('backend_port')
if form.getvalue('backend_ip') is None:
print('error: Backend IP must be IP and not 0')
sys.exit()
if form.getvalue('backend_port') is None:
print('error: Backend port must be integer and not 0')
sys.exit()
haproxy_sock_port = sql.get_setting('haproxy_sock_port')
MASTERS = sql.is_master(serv)
for master in MASTERS:
if master[0] != None:
cmd='echo "set server %s/%s addr %s port %s check-port %s" |nc %s %s' % (backend_backend, backend_server, backend_ip, backend_port, backend_port, master[0], haproxy_sock_port)
output, stderr = funct.subprocess_execute(cmd)
print(output[0])
cmd='echo "set server %s/%s addr %s port %s check-port %s" |nc %s %s' % (backend_backend, backend_server, backend_ip, backend_port, backend_port, serv, haproxy_sock_port)
output, stderr = funct.subprocess_execute(cmd)
if stderr != '':
print('error: '+stderr[0])
else:
print(output[0])
configs_dir = funct.get_config_var('configs', 'haproxy_save_configs_dir')
cfg = configs_dir + serv + "-" + funct.get_data('config') + ".cfg"
error = funct.get_config(serv, cfg)
cmd = 'string=`grep %s %s -n -A25 |grep "server %s" |head -1|awk -F"-" \'{print $1}\'` && sed -Ei "$( echo $string)s/((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5]):[0-9]+/%s:%s/g" %s' % (backend_backend, cfg, backend_server, backend_ip, backend_port, cfg)
output, stderr = funct.subprocess_execute(cmd)
stderr = funct.master_slave_upload_and_restart(serv, cfg, just_save='save')
if form.getvalue('maxconn_select') is not None:
serv = form.getvalue('maxconn_select')
funct.get_backends_from_config(serv, backends='frontend')
if form.getvalue('maxconn_frontend') is not None:
import sql
frontend = form.getvalue('maxconn_frontend')
maxconn = form.getvalue('maxconn_int')
if form.getvalue('maxconn_int') is None:
print('error: Maxconn must be integer and not 0')
sys.exit()
haproxy_sock_port = sql.get_setting('haproxy_sock_port')
MASTERS = sql.is_master(serv)
for master in MASTERS:
if master[0] != None:
if frontend == 'global':
cmd='echo "set maxconn %s %s" |nc %s %s' % (frontend, maxconn, master[0], haproxy_sock_port)
else:
cmd='echo "set maxconn frontend %s %s" |nc %s %s' % (frontend, maxconn, master[0], haproxy_sock_port)
output, stderr = funct.subprocess_execute(cmd)
if frontend == 'global':
cmd='echo "set maxconn %s %s" |nc %s %s' % (frontend, maxconn, serv, haproxy_sock_port)
else:
cmd='echo "set maxconn frontend %s %s" |nc %s %s' % (frontend, maxconn, serv, haproxy_sock_port)
output, stderr = funct.subprocess_execute(cmd)
if stderr != '':
print(stderr[0])
elif output[0] == '':
configs_dir = funct.get_config_var('configs', 'haproxy_save_configs_dir')
cfg = configs_dir + serv + "-" + funct.get_data('config') + ".cfg"
error = funct.get_config(serv, cfg)
cmd = 'string=`grep %s %s -n -A5 |grep maxcon -n |awk -F":" \'{print $2}\'|awk -F"-" \'{print $1}\'` && sed -Ei "$( echo $string)s/[0-9]+/%s/g" %s' % (frontend, cfg, maxconn, cfg)
output, stderr = funct.subprocess_execute(cmd)
stderr = funct.master_slave_upload_and_restart(serv, cfg, just_save='save')
print('Maxconn for %s has been set to %s ' % (frontend, maxconn))
else:
print('error: '+output[0])
if form.getvalue('table_serv_select') is not None:
print(funct.get_all_stick_table())
if form.getvalue('table_select') is not None:
from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('templates/ajax'), autoescape=True,extensions=['jinja2.ext.loopcontrols', 'jinja2.ext.do'], trim_blocks=True, lstrip_blocks=True)
table = form.getvalue('table_select')
if table == 'All':
template = env.get_template('/stick_tables.html')
tables = funct.get_all_stick_table()
table = []
for t in tables.split(','):
if t != '':
id = []
tables_head = []
tables_head1, table1 = funct.get_stick_table(t)
id.append(tables_head1)
id.append(table1)
table.append(id)
template = template.render(table=table)
else:
template = env.get_template('/stick_table.html')
tables_head, table = funct.get_stick_table(table)
template = template.render(tables_head=tables_head, table=table)
print(template)
if form.getvalue('change_pos') is not None:
import sql
pos = form.getvalue('change_pos')

@ -111,13 +111,13 @@
{% else %}
{% set action_service = 'hap' %}
{% endif %}
<a id="start-{{ s.2 }}" class="start" title="Start {{service}} service">
<a id="start-{{ s.2 }}" class="start" title="Start {{service}} service" style="padding-right: 4px;">
<span class="service-start" onclick="confirmAjaxAction('start', '{{action_service}}', '{{s.2}}')"></span>
</a>
<a id="reload-{{ s.2 }}" class="reload" title="Reload {{service}} service">
<a id="reload-{{ s.2 }}" class="reload" title="Reload {{service}} service" style="padding-right: 4px;">
<span class="service-reload" onclick="confirmAjaxAction('reload', '{{action_service}}', '{{s.2}}', '{{s.1}}')"></span>
</a>
<a id="restart-{{ s.2 }}" class="restart" title="Restart {{service}} service">
<a id="restart-{{ s.2 }}" class="restart" title="Restart {{service}} service" style="padding-right: 4px;">
<span class="service-reload service-restart" onclick="confirmAjaxAction('restart', '{{action_service}}', '{{s.2}}')"></span>
</a>
<a id="stop-{{ s.2 }}" class="stop" title="Stop {{service}} service">

Loading…
Cancel
Save