Pavel Loginov 2021-06-08 13:37:42 +06:00
parent ac8a22669e
commit 7d4e504337
5 changed files with 142 additions and 130 deletions

View File

@ -33,15 +33,18 @@ def get_data(log_type):
from datetime import datetime from datetime import datetime
from pytz import timezone from pytz import timezone
import sql import sql
fmt = "%Y-%m-%d.%H:%M:%S"
try: try:
now_utc = datetime.now(timezone(sql.get_setting('time_zone'))) now_utc = datetime.now(timezone(sql.get_setting('time_zone')))
except Exception: except Exception:
now_utc = datetime.now(timezone('UTC')) now_utc = datetime.now(timezone('UTC'))
if log_type == 'config': if log_type == 'config':
fmt = "%Y-%m-%d.%H:%M:%S" fmt = "%Y-%m-%d.%H:%M:%S"
if log_type == 'logs': elif log_type == 'logs':
fmt = '%Y%m%d' fmt = '%Y%m%d'
if log_type == "date_in_log": elif log_type == "date_in_log":
fmt = "%b %d %H:%M:%S" fmt = "%b %d %H:%M:%S"
return now_utc.strftime(fmt) return now_utc.strftime(fmt)
@ -124,13 +127,14 @@ def logging(serv, action, **kwargs):
log.close() log.close()
except IOError as e: except IOError as e:
print('<center><div class="alert alert-danger">Can\'t write log. Please check log_path in config %e</div></center>' % e) print('<center><div class="alert alert-danger">Can\'t write log. Please check log_path in config %e</div></center>' % e)
pass
def telegram_send_mess(mess, **kwargs): def telegram_send_mess(mess, **kwargs):
import telebot import telebot
from telebot import apihelper from telebot import apihelper
import sql import sql
token_bot = ''
channel_name = ''
if kwargs.get('telegram_channel_id'): if kwargs.get('telegram_channel_id'):
telegrams = sql.get_telegram_by_id(kwargs.get('telegram_channel_id')) telegrams = sql.get_telegram_by_id(kwargs.get('telegram_channel_id'))
@ -164,6 +168,8 @@ def slack_send_mess(mess, **kwargs):
import sql import sql
from slack_sdk import WebClient from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError from slack_sdk.errors import SlackApiError
slack_token = ''
channel_name = ''
if kwargs.get('slack_channel_id'): if kwargs.get('slack_channel_id'):
slacks = sql.get_slack_by_id(kwargs.get('slack_channel_id')) slacks = sql.get_slack_by_id(kwargs.get('slack_channel_id'))
@ -248,6 +254,7 @@ def return_ssh_keys_path(serv, **kwargs):
ssh_enable = '' ssh_enable = ''
ssh_user_name = '' ssh_user_name = ''
ssh_user_password = '' ssh_user_password = ''
ssh_key_name = ''
if kwargs.get('id'): if kwargs.get('id'):
for sshs in sql.select_ssh(id=kwargs.get('id')): for sshs in sql.select_ssh(id=kwargs.get('id')):
@ -271,15 +278,16 @@ def ssh_connect(serv, **kwargs):
import sql import sql
ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = return_ssh_keys_path(serv) ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = return_ssh_keys_path(serv)
servers = sql.select_servers(server=serv) servers = sql.select_servers(server=serv)
ssh_port = 22 ssh_port = 22
for server in servers: for server in servers:
ssh_port = server[10] ssh_port = server[10]
ssh = SSHClient() ssh = SSHClient()
ssh.load_system_host_keys() ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try: try:
if ssh_enable == 1: if ssh_enable == 1:
cloud = sql.is_cloud() cloud = sql.is_cloud()
@ -293,27 +301,19 @@ def ssh_connect(serv, **kwargs):
return ssh return ssh
except paramiko.AuthenticationException: except paramiko.AuthenticationException:
return 'Authentication failed, please verify your credentials' return 'Authentication failed, please verify your credentials'
pass
except paramiko.SSHException as sshException: except paramiko.SSHException as sshException:
return 'error: Unable to establish SSH connection: %s ' % sshException return 'error: Unable to establish SSH connection: %s ' % sshException
pass
except paramiko.PasswordRequiredException as e: except paramiko.PasswordRequiredException as e:
return 'error: %s ' % e return 'error: %s ' % e
pass
except paramiko.BadHostKeyException as badHostKeyException: except paramiko.BadHostKeyException as badHostKeyException:
return 'error: Unable to verify server\'s host key: %s ' % badHostKeyException return 'error: Unable to verify server\'s host key: %s ' % badHostKeyException
pass
except Exception as e: except Exception as e:
if e == "No such file or directory": if e == "No such file or directory":
return 'error: %s. Check ssh key' % e return 'error: %s. Check ssh key' % e
pass
elif e == "Invalid argument": elif e == "Invalid argument":
error = 'error: Check the IP of the server' return 'error: Check the IP of the server'
pass
else: else:
error = e return str(e)
pass
return str(error)
def get_config(serv, cfg, **kwargs): def get_config(serv, cfg, **kwargs):
@ -417,7 +417,6 @@ def get_section_from_config(config, section):
record = True record = True
continue continue
if record: if record:
if ( if (
line.startswith('listen') or line.startswith('listen') or
line.startswith('frontend') or line.startswith('frontend') or
@ -470,7 +469,7 @@ def rewrite_section(start_line, end_line, config, section):
return return_config return return_config
def get_userlists(config, **kwargs): def get_userlists(config):
return_config = '' return_config = ''
with open(config, 'r') as f: with open(config, 'r') as f:
for line in f: for line in f:
@ -480,7 +479,8 @@ def get_userlists(config, **kwargs):
return return_config return return_config
def get_backends_from_config(serv, backends='', **kwargs):
def get_backends_from_config(serv, backends=''):
configs_dir = get_config_var('configs', 'haproxy_save_configs_dir') configs_dir = get_config_var('configs', 'haproxy_save_configs_dir')
format_cfg = 'cfg' format_cfg = 'cfg'
@ -557,7 +557,7 @@ def install_haproxy(serv, **kwargs):
stats_user = sql.get_setting('stats_user') stats_user = sql.get_setting('stats_user')
stats_password = sql.get_setting('stats_password') stats_password = sql.get_setting('stats_password')
proxy = sql.get_setting('proxy') proxy = sql.get_setting('proxy')
hapver = kwargs.get('hapver') haproxy_ver = kwargs.get('hapver')
server_for_installing = kwargs.get('server') server_for_installing = kwargs.get('server')
ssh_port = 22 ssh_port = 22
ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = return_ssh_keys_path(serv) ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = return_ssh_keys_path(serv)
@ -571,8 +571,8 @@ def install_haproxy(serv, **kwargs):
os.system("cp scripts/%s ." % script) os.system("cp scripts/%s ." % script)
if hapver is None: if haproxy_ver is None:
hapver = '2.3.0-1' haproxy_ver = '2.3.0-1'
if proxy is not None and proxy != '' and proxy != 'None': if proxy is not None and proxy != '' and proxy != 'None':
proxy_serv = proxy proxy_serv = proxy
@ -584,7 +584,7 @@ def install_haproxy(serv, **kwargs):
commands = ["chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv + commands = ["chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv +
" SOCK_PORT=" + hap_sock_p + " 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 + " STATS_USER=" + stats_user + " SSH_PORT=" + ssh_port + " STATS_USER=" + stats_user +
" STATS_PASS=" + stats_password + " HAPVER=" + hapver + " SYN_FLOOD=" + syn_flood_protect + " STATS_PASS=" + stats_password + " HAPVER=" + haproxy_ver + " SYN_FLOOD=" + syn_flood_protect +
" HOST=" + serv + " 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])
@ -694,6 +694,7 @@ def check_haproxy_version(serv):
output, stderr = subprocess_execute(cmd) output, stderr = subprocess_execute(cmd)
for line in output: for line in output:
ver = line ver = line
return ver return ver
@ -709,7 +710,6 @@ def upload(serv, path, file, **kwargs):
error = e.args error = e.args
logging('localhost', str(e.args[0]), haproxywi=1) logging('localhost', str(e.args[0]), haproxywi=1)
print(' Cannot upload '+file+' to '+full_path+' to server: '+serv+' error: '+str(e.args)) print(' Cannot upload '+file+' to '+full_path+' to server: '+serv+' error: '+str(e.args))
pass
try: try:
sftp = ssh.open_sftp() sftp = ssh.open_sftp()
@ -724,7 +724,6 @@ def upload(serv, path, file, **kwargs):
error = e.args error = e.args
print('Cannot upload '+file+' to '+full_path+' to server: '+serv+' error: '+str(e.args)) print('Cannot upload '+file+' to '+full_path+' to server: '+serv+' error: '+str(e.args))
logging('localhost', ' Cannot upload '+file+' to '+full_path+' to server: '+serv+' Error: '+str(e.args), haproxywi=1) logging('localhost', ' Cannot upload '+file+' to '+full_path+' to server: '+serv+' Error: '+str(e.args), haproxywi=1)
pass
try: try:
sftp.close() sftp.close()
@ -733,13 +732,13 @@ def upload(serv, path, file, **kwargs):
error = e.args error = e.args
logging('localhost', str(error[0]), haproxywi=1) logging('localhost', str(error[0]), haproxywi=1)
print('Cannot upload '+file+' to '+full_path+' to server: '+serv+' error: '+str(e.args)) print('Cannot upload '+file+' to '+full_path+' to server: '+serv+' error: '+str(e.args))
pass
return str(error) return str(error)
def upload_and_restart(serv, cfg, **kwargs): def upload_and_restart(serv, cfg, **kwargs):
import sql import sql
error = ""
if kwargs.get("nginx"): if kwargs.get("nginx"):
config_path = sql.get_setting('nginx_config_path') config_path = sql.get_setting('nginx_config_path')
@ -747,13 +746,11 @@ def upload_and_restart(serv, cfg, **kwargs):
else: else:
config_path = sql.get_setting('haproxy_config_path') config_path = sql.get_setting('haproxy_config_path')
tmp_file = sql.get_setting('tmp_config_path') + "/" + get_data('config') + ".cfg" tmp_file = sql.get_setting('tmp_config_path') + "/" + get_data('config') + ".cfg"
error = ""
try: try:
os.system("dos2unix "+cfg) os.system("dos2unix "+cfg)
except OSError: except OSError:
return 'Please install dos2unix' return 'Please install dos2unix'
pass
if kwargs.get("keepalived") == 1: if kwargs.get("keepalived") == 1:
if kwargs.get("just_save") == "save": if kwargs.get("just_save") == "save":
@ -816,7 +813,7 @@ def open_port_firewalld(cfg, serv, **kwargs):
try: try:
conf = open(cfg, "r") conf = open(cfg, "r")
except IOError: except IOError:
print('<div class="alert alert-danger">Can\'t read export config file</div>') print('<div class="alert alert-danger">Cannot read exported config file</div>')
firewalld_commands = ' &&' firewalld_commands = ' &&'
ports = '' ports = ''
@ -873,6 +870,8 @@ def check_haproxy_config(serv):
def show_log(stdout, **kwargs): def show_log(stdout, **kwargs):
i = 0 i = 0
out = '' out = ''
grep = ''
if kwargs.get('grep'): if kwargs.get('grep'):
import re import re
grep = kwargs.get('grep') grep = kwargs.get('grep')
@ -1022,8 +1021,8 @@ def ssh_command(serv, commands, **kwargs):
for command in commands: for command in commands:
try: try:
stdin, stdout, stderr = ssh.exec_command(command, get_pty=True) stdin, stdout, stderr = ssh.exec_command(command, get_pty=True)
except Exception: except Exception as e:
continue logging('localhost', ' ' + str(e), haproxywi=1)
if kwargs.get("ip") == "1": if kwargs.get("ip") == "1":
show_ip(stdout) show_ip(stdout)
@ -1045,12 +1044,12 @@ def ssh_command(serv, commands, **kwargs):
if line: if line:
print("<div class='alert alert-warning'>"+line+"</div>") print("<div class='alert alert-warning'>"+line+"</div>")
logging('localhost', ' '+line, haproxywi=1) logging('localhost', ' '+line, haproxywi=1)
try: try:
ssh.close() ssh.close()
except Exception: except Exception:
logging('localhost', ' '+str(ssh), haproxywi=1) logging('localhost', ' '+str(ssh), haproxywi=1)
return "error: "+str(ssh) return "error: "+str(ssh)
pass
def subprocess_execute(cmd): def subprocess_execute(cmd):

View File

@ -1,28 +1,27 @@
{% 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;
align: center; align: center;
text-align: center; text-align: center;
} }
th, tr, td { th, tr, td {
width: 6%; width: 6%;
min-width: 3%; min-width: 3%;
padding: 10px; padding: 10px;
} }
</style> </style>
<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 == '0' %} {% if services == '0' %}
<center> <div style="text-align: center;">
<br /> <br />
<h3>You do not have installed Metrics service. <h3>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> 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 Metrics service</h3> how to install Metrics service</h3>
</center> </div>
{% else %} {% else %}
{% if servers|length == 0 %} {% if servers|length == 0 %}
{% include 'include/getstarted.html' %} {% include 'include/getstarted.html' %}
@ -31,8 +30,8 @@ th, tr, td {
<tr class="overviewHead"> <tr class="overviewHead">
<th colspan=13 style="background-color: #d1ecf1"> <th colspan=13 style="background-color: #d1ecf1">
<span id="table_metrics_head" style="position: absolute;margin-left: 43%;">Metrics</span> <span id="table_metrics_head" style="position: absolute;margin-left: 43%;">Metrics</span>
<span id="en_table_metric" class="add switcher_table_metric" title="Enable showing Average table"></span> <span id="en_table_metric" class="plus switcher_table_metric" title="Enable display of the table of averages"></span>
<span id="dis_table_metric" class="minus switcher_table_metric" title="Disable showing Average table"></span> <span id="dis_table_metric" class="minus switcher_table_metric" title="Disable display of the table of averages"></span>
</th> </th>
</tr> </tr>
</table> </table>
@ -54,13 +53,18 @@ th, tr, td {
<div class="chart-container"> <div class="chart-container">
<canvas id="{{s.0}}" role="img"></canvas> <canvas id="{{s.0}}" role="img"></canvas>
</div> </div>
<div id="http_metrics_div" class="chart-container">
<canvas id="http_{{s.0}}" role="img"></canvas>
</div>
{% endfor %} {% endfor %}
<script> <script>
function showMetrics() { function showMetrics() {
let metrics = new Promise( let metrics = new Promise(
(resolve, reject) => { (resolve, reject) => {
removeData();
{% for s in servers %} {% for s in servers %}
getChartData('{{s.0}}') getChartData('{{s.0}}')
getHttpChartData('{{s.0}}')
{% endfor %} {% endfor %}
if (localStorage.getItem('table_metrics') == 0 || localStorage.getItem('table_metrics') === null) { if (localStorage.getItem('table_metrics') == 0 || localStorage.getItem('table_metrics') === null) {
$('#dis_table_metric').css('display', 'none'); $('#dis_table_metric').css('display', 'none');
@ -79,6 +83,7 @@ th, tr, td {
(resolve, reject) => { (resolve, reject) => {
{% for s in servers %} {% for s in servers %}
getChartData('{{s.0}}') getChartData('{{s.0}}')
getHttpChartData('{{s.0}}')
{% endfor %} {% endfor %}
}); });
metrics.then(); metrics.then();

View File

@ -253,10 +253,11 @@
<td> <td>
{% if prometheus|int() >= 1 %} {% if prometheus|int() >= 1 %}
<span class="serverUp server-status" title="running {{ prometheus }} process"></span> <span class="serverUp server-status" title="running {{ prometheus }} process"></span>
<a href="http://{{host}}:9090" target="_blank" title="Open Prometheus" class="logs_link">Prometheus</a>
{% else %} {% else %}
<span class="serverNone server-status" title="The service does not started or does not installed"></span> <span class="serverNone server-status" title="The service does not started or does not installed"></span>
Prometheus
{% endif %} {% endif %}
Prometheus
</td> </td>
{% endif %} {% endif %}
<td {% if role != 1 %}class="padding10 first-collumn-wi"{%endif%}> <td {% if role != 1 %}class="padding10 first-collumn-wi"{%endif%}>

View File

@ -70,7 +70,7 @@
font-family: "Font Awesome 5 Solid"; font-family: "Font Awesome 5 Solid";
content: "\f24d"; content: "\f24d";
} }
.add .fa-clone { .add .fa-clone, .plus .fa-plus {
color: var(--green-color); color: var(--green-color);
} }
.add-proxy::before { .add-proxy::before {
@ -273,6 +273,11 @@
font-family: "Font Awesome 5 Solid"; font-family: "Font Awesome 5 Solid";
content: "\f068"; content: "\f068";
} }
.plus::before {
display: none;
font-family: "Font Awesome 5 Solid";
content: "\f067";
}
.row-down::after { .row-down::after {
display: none; display: none;
font-family: "Font Awesome 5 Solid"; font-family: "Font Awesome 5 Solid";

View File

@ -500,7 +500,6 @@ ul{
} }
.menu li:last-child a, .menu li .v_menu li:last-child a{ .menu li:last-child a, .menu li .v_menu li:last-child a{
border-radius: 0 0 3px 3px; border-radius: 0 0 3px 3px;
border-bottom: 0;
} }
.menu li .v_menu li a{ .menu li .v_menu li a{
border-radius: 0; border-radius: 0;
@ -1200,7 +1199,7 @@ label {
} }
.switcher_table_metric { .switcher_table_metric {
margin-left: 99%; margin-left: 99%;
color: #a0d100; color: var(--green-color);
cursor: pointer; cursor: pointer;
} }
.geoip_country_code { .geoip_country_code {
@ -1210,3 +1209,6 @@ label {
width: 110px; width: 110px;
height: 60px; height: 60px;
} }
.help_cursor {
cursor: help;
}