Pavel Loginov 2020-09-26 23:37:14 +06:00
parent e5d2626b05
commit b5b9bff29b
7 changed files with 1902 additions and 1876 deletions

View File

@ -66,6 +66,7 @@ def get_user_group(**kwargs):
return user_group return user_group
def logging(serv, action, **kwargs): def logging(serv, action, **kwargs):
import sql import sql
import http.cookies import http.cookies
@ -203,7 +204,7 @@ def page_for_admin(**kwargs):
def return_ssh_keys_path(serv, **kwargs): def return_ssh_keys_path(serv, **kwargs):
import sql import sql
fullpath = get_config_var('main', 'fullpath') full_path = get_config_var('main', 'fullpath')
ssh_enable = '' ssh_enable = ''
ssh_user_name = '' ssh_user_name = ''
ssh_user_password = '' ssh_user_password = ''
@ -213,13 +214,13 @@ def return_ssh_keys_path(serv, **kwargs):
ssh_enable = sshs[2] ssh_enable = sshs[2]
ssh_user_name = sshs[3] ssh_user_name = sshs[3]
ssh_user_password = sshs[4] ssh_user_password = sshs[4]
ssh_key_name = fullpath+'/keys/%s.pem' % sshs[1] ssh_key_name = full_path+'/keys/%s.pem' % sshs[1]
else: else:
for sshs in sql.select_ssh(serv=serv): for sshs in sql.select_ssh(serv=serv):
ssh_enable = sshs[3] ssh_enable = sshs[3]
ssh_user_name = sshs[4] ssh_user_name = sshs[4]
ssh_user_password = sshs[5] ssh_user_password = sshs[5]
ssh_key_name = fullpath+'/keys/%s.pem' % sshs[2] ssh_key_name = full_path+'/keys/%s.pem' % sshs[2]
return ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name return ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name
@ -325,7 +326,6 @@ def diff_config(oldcfg, cfg):
def get_sections(config, **kwargs): def get_sections(config, **kwargs):
record = False
return_config = list() return_config = list()
with open(config, 'r') as f: with open(config, 'r') as f:
for line in f: for line in f:
@ -390,8 +390,8 @@ def get_section_from_config(config, section):
if end_line == "": if end_line == "":
f = open(config, "r") f = open(config, "r")
lineList = f.readlines() line_list = f.readlines()
end_line = len(lineList) end_line = len(line_list)
return start_line, end_line, return_config return start_line, end_line, return_config
@ -423,7 +423,6 @@ def rewrite_section(start_line, end_line, config, section):
def get_backends_from_config(serv, backends='', **kwargs): def get_backends_from_config(serv, backends='', **kwargs):
configs_dir = get_config_var('configs', 'haproxy_save_configs_dir') configs_dir = get_config_var('configs', 'haproxy_save_configs_dir')
format = 'cfg' format = 'cfg'
record = False
try: try:
cfg = configs_dir+get_files(dir=configs_dir, format=format)[0] cfg = configs_dir+get_files(dir=configs_dir, format=format)[0]
@ -450,23 +449,23 @@ def get_backends_from_config(serv, backends='', **kwargs):
def get_all_stick_table(): def get_all_stick_table():
import sql import sql
haproxy_sock_port = sql.get_setting('haproxy_sock_port') hap_sock_p = sql.get_setting('hap_sock_p')
cmd='echo "show table"|nc %s %s |awk \'{print $3}\' | tr -d \'\n\' | tr -d \'[:space:]\'' % (serv, haproxy_sock_port) cmd = 'echo "show table"|nc %s %s |awk \'{print $3}\' | tr -d \'\n\' | tr -d \'[:space:]\'' % (serv, hap_sock_p)
output, stderr = subprocess_execute(cmd) output, stderr = subprocess_execute(cmd)
return output[0] return output[0]
def get_stick_table(table): def get_stick_table(table):
import sql import sql
haproxy_sock_port = sql.get_setting('haproxy_sock_port') hap_sock_p = sql.get_setting('hap_sock_p')
cmd='echo "show table %s"|nc %s %s |awk -F"#" \'{print $2}\' |head -1 | tr -d \'\n\'' % (table, serv, haproxy_sock_port) cmd = 'echo "show table %s"|nc %s %s |awk -F"#" \'{print $2}\' |head -1 | tr -d \'\n\'' % (table, serv, hap_sock_p)
output, stderr = subprocess_execute(cmd) output, stderr = subprocess_execute(cmd)
tables_head = [] tables_head = []
for i in output[0].split(','): for i in output[0].split(','):
i = i.split(':')[1] i = i.split(':')[1]
tables_head.append(i) tables_head.append(i)
cmd='echo "show table %s"|nc %s %s |grep -v "#"' % (table, serv, haproxy_sock_port) cmd = 'echo "show table %s"|nc %s %s |grep -v "#"' % (table, serv, hap_sock_p)
output, stderr = subprocess_execute(cmd) output, stderr = subprocess_execute(cmd)
return tables_head, output return tables_head, output
@ -475,7 +474,7 @@ def get_stick_table(table):
def install_haproxy(serv, **kwargs): def install_haproxy(serv, **kwargs):
import sql import sql
script = "install_haproxy.sh" script = "install_haproxy.sh"
haproxy_sock_port = sql.get_setting('haproxy_sock_port') hap_sock_p = sql.get_setting('hap_sock_p')
stats_port = sql.get_setting('stats_port') stats_port = sql.get_setting('stats_port')
server_state_file = sql.get_setting('server_state_file') server_state_file = sql.get_setting('server_state_file')
stats_user = sql.get_setting('stats_user') stats_user = sql.get_setting('stats_user')
@ -504,10 +503,10 @@ def install_haproxy(serv, **kwargs):
syn_flood_protect = '1' if kwargs.get('syn_flood') == "1" else '' syn_flood_protect = '1' if kwargs.get('syn_flood') == "1" else ''
commands = ["chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv + commands = ["chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv +
" SOCK_PORT="+haproxy_sock_port+" STAT_PORT="+stats_port+" STAT_FILE="+server_state_file+ " SOCK_PORT=" + hap_sock_p + " STAT_PORT=" + stats_port + " STAT_FILE="+server_state_file +
" SSH_PORT="+ssh_port+ " SSH_PORT=" + ssh_port + " STATS_USER=" + stats_user +
" STATS_USER="+stats_user+" STATS_PASS="+stats_password+" HAPVER="+hapver +" SYN_FLOOD="+syn_flood_protect+" HOST="+serv+ " STATS_PASS=" + stats_password + " HAPVER=" + hapver + " SYN_FLOOD=" + syn_flood_protect +
" USER="+ssh_user_name+" PASS="+ssh_user_password+" KEY="+ssh_key_name ] " HOST=" + serv + " USER=" + ssh_user_name + " PASS=" + ssh_user_password + " KEY=" + ssh_key_name]
output, error = subprocess_execute(commands[0]) output, error = subprocess_execute(commands[0])
@ -589,10 +588,10 @@ def install_nginx(serv):
syn_flood_protect = '1' if form.getvalue('syn_flood') == "1" else '' syn_flood_protect = '1' if form.getvalue('syn_flood') == "1" else ''
commands = [ "chmod +x "+script +" && ./"+script +" PROXY=" + proxy_serv+" STATS_USER="+stats_user+" STATS_PASS="+stats_password+ commands = ["chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv + " STATS_USER=" + stats_user +
" SSH_PORT="+ssh_port+ " STATS_PASS=" + stats_password + " SSH_PORT="+ ssh_port + " CONFIG_PATH=" + config_path +
" CONFIG_PATH="+config_path+" STAT_PORT="+stats_port+" STAT_PAGE="+stats_page+" SYN_FLOOD="+syn_flood_protect+" HOST="+serv+ " STAT_PORT=" + stats_port +" STAT_PAGE=" + stats_page+" SYN_FLOOD=" + syn_flood_protect +
" USER="+ssh_user_name+" PASS="+ssh_user_password+" KEY="+ssh_key_name ] " HOST="+ serv + " USER=" + ssh_user_name + " PASS=" + ssh_user_password + " KEY=" + ssh_key_name]
output, error = subprocess_execute(commands[0]) output, error = subprocess_execute(commands[0])
@ -628,9 +627,9 @@ def update_haproxy_wi(service):
def check_haproxy_version(serv): def check_haproxy_version(serv):
import sql import sql
haproxy_sock_port = sql.get_setting('haproxy_sock_port') hap_sock_p = sql.get_setting('hap_sock_p')
ver = "" ver = ""
cmd="echo 'show info' |nc %s %s |grep Version |awk '{print $2}'" % (serv, haproxy_sock_port) cmd = "echo 'show info' |nc %s %s |grep Version |awk '{print $2}'" % (serv, hap_sock_p)
output, stderr = subprocess_execute(cmd) output, stderr = subprocess_execute(cmd)
for line in output: for line in output:
ver = line ver = line
@ -741,9 +740,9 @@ def upload_and_restart(serv, cfg, **kwargs):
def master_slave_upload_and_restart(serv, cfg, just_save, **kwargs): def master_slave_upload_and_restart(serv, cfg, just_save, **kwargs):
import sql import sql
MASTERS = sql.is_master(serv) masters = sql.is_master(serv)
error = "" error = ""
for master in MASTERS: for master in masters:
if master[0] != None: if master[0] != None:
error += upload_and_restart(master[0], cfg, just_save=just_save, nginx=kwargs.get('nginx')) error += upload_and_restart(master[0], cfg, just_save=just_save, nginx=kwargs.get('nginx'))
@ -822,7 +821,7 @@ def show_log(stdout, **kwargs):
if kwargs.get("html") != 0: if kwargs.get("html") != 0:
i = i + 1 i = i + 1
if kwargs.get('grep'): if kwargs.get('grep'):
line = line.replace(grep, '<span style="color: red; font-weight: bold;">'+grep+'</span>'); line = line.replace(grep, '<span style="color: red; font-weight: bold;">'+grep+'</span>')
line_class = "line3" if i % 2 == 0 else "line" line_class = "line3" if i % 2 == 0 else "line"
out += '<div class="'+line_class+'">' + line + '</div>' out += '<div class="'+line_class+'">' + line + '</div>'
else: else:
@ -1007,8 +1006,8 @@ def subprocess_execute(cmd):
def show_backends(serv, **kwargs): def show_backends(serv, **kwargs):
import json import json
import sql import sql
haproxy_sock_port = sql.get_setting('haproxy_sock_port') hap_sock_p = sql.get_setting('hap_sock_p')
cmd='echo "show backend" |nc %s %s' % (serv, haproxy_sock_port) cmd = 'echo "show backend" |nc %s %s' % (serv, hap_sock_p)
output, stderr = subprocess_execute(cmd) output, stderr = subprocess_execute(cmd)
if stderr: if stderr:
logging('localhost', ' '+stderr, haproxywi=1) logging('localhost', ' '+stderr, haproxywi=1)
@ -1176,7 +1175,6 @@ def check_user_group(**kwargs):
logging('localhost', ' has tried to actions in not his group ', haproxywi=1, login=1) logging('localhost', ' has tried to actions in not his group ', haproxywi=1, login=1)
print('Atata!') print('Atata!')
sys.exit() sys.exit()
return False
def check_is_server_in_group(serv): def check_is_server_in_group(serv):
@ -1190,7 +1188,6 @@ def check_is_server_in_group(serv):
logging('localhost', ' has tried to actions in not his group server ', haproxywi=1, login=1) logging('localhost', ' has tried to actions in not his group server ', haproxywi=1, login=1)
print('Atata!') print('Atata!')
sys.exit() sys.exit()
return False
def check_service(serv, service_name): def check_service(serv, service_name):

View File

@ -1,6 +1,9 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import funct, sql
import funct
import sql
from jinja2 import Environment, FileSystemLoader from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('templates/'), autoescape=True) env = Environment(loader=FileSystemLoader('templates/'), autoescape=True)
template = env.get_template('hapservers.html') template = env.get_template('hapservers.html')
@ -11,6 +14,7 @@ try:
user, user_id, role, token, servers = funct.get_users_params() user, user_id, role, token, servers = funct.get_users_params()
users = sql.select_users() users = sql.select_users()
groups = sql.select_groups() groups = sql.select_groups()
services = []
except: except:
pass pass
@ -41,6 +45,19 @@ else:
else: else:
servers = sql.get_dick_permit(virt=1, haproxy=1) servers = sql.get_dick_permit(virt=1, haproxy=1)
services_name = {'checker_haproxy': 'Master backends checker service',
'keep_alive': 'Auto start service',
'metrics_haproxy': 'Master metrics service'}
for s, v in services_name.items():
if s != 'keep_alive':
service_name = s.split('_')[0]
else:
service_name = s
cmd = "rpm --query haproxy-wi-" + service_name + "-* |awk -F\"" + service_name +"\" '{print $2}' |awk -F\".noa\" '{print $1}' |sed 's/-//1' |sed 's/-/./'"
service_ver, stderr = funct.subprocess_execute(cmd)
services.append([s, service_ver[0]])
haproxy_sock_port = sql.get_setting('haproxy_sock_port') haproxy_sock_port = sql.get_setting('haproxy_sock_port')
servers_with_status1 = [] servers_with_status1 = []
out1 = '' out1 = ''
@ -51,7 +68,8 @@ for s in servers:
servers_with_status.append(s[2]) servers_with_status.append(s[2])
servers_with_status.append(s[11]) servers_with_status.append(s[11])
if service == 'nginx': if service == 'nginx':
cmd = [ "/usr/sbin/nginx -v && systemctl status nginx |grep -e 'Active' |awk '{print $2, $9$10$11$12$13}' && ps ax |grep nginx:|grep -v grep |wc -l" ] cmd = [
"/usr/sbin/nginx -v && systemctl status nginx |grep -e 'Active' |awk '{print $2, $9$10$11$12$13}' && ps ax |grep nginx:|grep -v grep |wc -l"]
out = funct.ssh_command(s[2], cmd) out = funct.ssh_command(s[2], cmd)
h = () h = ()
out1 = [] out1 = []
@ -76,7 +94,6 @@ for s in servers:
servers_with_status1.append(servers_with_status) servers_with_status1.append(servers_with_status)
template = template.render(h2=1, template = template.render(h2=1,
autorefresh=autorefresh, autorefresh=autorefresh,
title=title, title=title,
@ -89,5 +106,6 @@ template = template.render(h2 = 1,
keep_alive=''.join(keep_alive), keep_alive=''.join(keep_alive),
serv=serv, serv=serv,
service=service, service=service,
services=services,
token=token) token=token)
print(template) print(template)

View File

@ -11,6 +11,13 @@ funct.check_login()
try: try:
user, user_id, role, token, servers = funct.get_users_params() user, user_id, role, token, servers = funct.get_users_params()
cmd = "rpm --query haproxy-wi-metrics-* |awk -F\"metrics_haproxy\" '{print $2}' |awk -F\".noa\" '{print $1}' |sed 's/-//1' |sed 's/-/./'"
service_ver, stderr = funct.subprocess_execute(cmd)
if service_ver[0] == '* is not installed':
servers = ''
else:
servers = sql.select_servers_metrics(user_id.value)
except: except:
pass pass
@ -19,7 +26,8 @@ template = template.render(h2 = 1, title = "Metrics",
autorefresh=1, autorefresh=1,
role=role, role=role,
user=user, user=user,
servers = sql.select_servers_metrics(user_id.value), servers=servers,
versions=funct.versions(), versions=funct.versions(),
services=service_ver[0],
token=token) token=token)
print(template) print(template)

File diff suppressed because it is too large Load Diff

View File

@ -47,6 +47,17 @@
} }
} }
}); });
{% for s in services %}
{% if s.1 == '* is not installed' %}
{% if s.0 == 'checker_haproxy' %}
$(':regex(id, alert)').checkboxradio('disable');
{% elif s.0 == 'keep_alive' %}
$(':regex(id, active-)').checkboxradio('disable');
{% elif s.0 == 'metrics_haproxy' %}
$(':regex(id, metrics-)').checkboxradio('disable');
{% endif %}
{% endif %}
{% endfor %}
}); });
</script> </script>
{% if serv %} {% if serv %}
@ -165,19 +176,19 @@
{% if role <= 2 %} {% if role <= 2 %}
{% set id = 'alert-' + s.8.0.0|string() %} {% set id = 'alert-' + s.8.0.0|string() %}
{% if s.8.0.8 == 1 %} {% if s.8.0.8 == 1 %}
{{ checkbox(id, title='Enable alerting', value='1', desc='Alert', checked='checked') }} {{ checkbox(id, title='Alerting is enabled', value='1', desc='Alert', checked='checked') }}
{% else %} {% else %}
{{ checkbox(id, title='Enable alerting', value='1', desc='Alert') }} {{ checkbox(id, title='Enable alerting', value='1', desc='Alert') }}
{% endif %} {% endif %}
{% set id = 'metrics-' + s.8.0.0|string() %} {% set id = 'metrics-' + s.8.0.0|string() %}
{% if s.8.0.9 == 1 %} {% if s.8.0.9 == 1 %}
{{ checkbox(id, title='Enable collect metrics', value='1', desc='Metrics', checked='checked') }} {{ checkbox(id, title='Collecting metrics is enabled', value='1', desc='Metrics', checked='checked') }}
{% else %} {% else %}
{{ checkbox(id, title='Enable collect metrics', value='1', desc='Metrics') }} {{ checkbox(id, title='Enable collect metrics', value='1', desc='Metrics') }}
{% endif %} {% endif %}
{% set id = 'active-' + s.8.0.0|string() %} {% set id = 'active-' + s.8.0.0|string() %}
{% if s.8.0.12 == 1 %} {% if s.8.0.12 == 1 %}
{{ checkbox(id, title='Enable Compression', value='1', desc='Auto Start', checked='checked') }} {{ checkbox(id, title='Auto Start is enabled', value='1', desc='Auto Start', checked='checked') }}
{% else %} {% else %}
{{ checkbox(id, title='Enable Auto Start', value='1', desc='Auto Start') }} {{ checkbox(id, title='Enable Auto Start', value='1', desc='Auto Start') }}
{% endif %} {% endif %}

View File

@ -1,3 +1,6 @@
{% for s in services %}
{% if s.0 == 'checker_haproxy' %}
{% if s.3 != '* is not installed' %}
<table id="checker_table" class="overview"> <table id="checker_table" class="overview">
<tr class="overviewHead" style="width: 50%;"> <tr class="overviewHead" style="width: 50%;">
<td class="padding10 first-collumn" style="width: 25%;"> <td class="padding10 first-collumn" style="width: 25%;">
@ -49,3 +52,13 @@
<div class="add-note addName alert-info" style="width: inherit; margin-right: 15px;"> <div class="add-note addName alert-info" style="width: inherit; margin-right: 15px;">
You can read the description of all parameters <a href="https://haproxy-wi.org/description.py?description=checker" title="Servers description" target="_blank">here</a> You can read the description of all parameters <a href="https://haproxy-wi.org/description.py?description=checker" title="Servers description" target="_blank">here</a>
</div> </div>
{% else %}
<center>
<h4 style="font-size: 25px">Welcome and let's get started!</h4>
You do not have installed Backends checker.
Read <a href="https://haproxy-wi.org/services.py?service=checker#installation" title="Checker installation" style="color: #5d9ceb;" target="_blank">hear</a>
how to install it
</center>
{% endif %}
{% endif %}
{% endfor %}

View File

@ -1,5 +1,6 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block content %} {% block content %}
<style> <style>
table, th, tr, td { table, th, tr, td {
border: 1px solid #ddd; border: 1px solid #ddd;
@ -15,6 +16,15 @@ th, tr, td {
<link href="/inc/chart.min.css" rel="stylesheet"> <link href="/inc/chart.min.css" rel="stylesheet">
<script src="/inc/metrics.js"></script> <script src="/inc/metrics.js"></script>
<script src="/inc/chart.min.js"></script> <script src="/inc/chart.min.js"></script>
{% if services == '* is not installed' %}
<center>
<h4 style="font-size: 25px">Welcome and let's get started!</h4>
You do not have installed Metrics service.
Read <a href="https://haproxy-wi.org/services.py?service=metrics#installation" title="Metrics installation" style="color: #5d9ceb;" target="_blank">hear</a>
how to install it
</center>
{% else %}
{% if servers|length == 0 %} {% if servers|length == 0 %}
{% include 'include/getstarted.html' %} {% include 'include/getstarted.html' %}
{% else %} {% else %}
@ -43,4 +53,5 @@ th, tr, td {
showMetrics() showMetrics()
</script> </script>
{% endif %} {% endif %}
{% endif %}
{% endblock %} {% endblock %}