From 89192f6a8d49c466149ec48e424f6236ca0d4110 Mon Sep 17 00:00:00 2001 From: Aidaho12 Date: Mon, 6 Aug 2018 19:59:39 +0600 Subject: [PATCH] v2.8 --- app/metrics.py | 2 + app/scripts/install_haproxy.sh | 4 +- app/sql.py | 117 +++++++++++++++++++++++++++++++++ app/templates/metrics.html | 53 +++++++++++++++ image/haproxy-wi-metrics.jpeg | Bin 308573 -> 307414 bytes inc/users.js | 4 +- 6 files changed, 176 insertions(+), 4 deletions(-) diff --git a/app/metrics.py b/app/metrics.py index 9d9a4a41..4589c26a 100644 --- a/app/metrics.py +++ b/app/metrics.py @@ -17,6 +17,7 @@ try: user = sql.get_user_name_by_uuid(user_id.value) servers = sql.get_dick_permit() token = sql.get_token(user_id.value) + table_stat = sql.select_table_metrics(user_id.value) except: pass @@ -25,5 +26,6 @@ template = template.render(h2 = 1, title = "Metrics", role = sql.get_user_role_by_uuid(user_id.value), user = user, onclick = "metricsShow()", + table_stat = sql.select_table_metrics(user_id.value), token = token) print(template) \ No newline at end of file diff --git a/app/scripts/install_haproxy.sh b/app/scripts/install_haproxy.sh index 09a99ebb..d173a59c 100644 --- a/app/scripts/install_haproxy.sh +++ b/app/scripts/install_haproxy.sh @@ -15,14 +15,14 @@ fi if hash apt-get 2>/dev/null; then sudo apt-get install haproxy socat -y else - sudo wget http://cbs.centos.org/kojifiles/packages/haproxy/1.8.1/5.el7/x86_64/haproxy18-1.8.1-5.el7.x86_64.rpm + wget http://cbs.centos.org/kojifiles/packages/haproxy/1.8.1/5.el7/x86_64/haproxy18-1.8.1-5.el7.x86_64.rpm sudo yum install haproxy18-1.8.1-5.el7.x86_64.rpm -y fi if [ $? -eq 1 ] then sudo yum install wget socat -y > /dev/null - sudo wget http://cbs.centos.org/kojifiles/packages/haproxy/1.8.1/5.el7/x86_64/haproxy18-1.8.1-5.el7.x86_64.rpm + wget http://cbs.centos.org/kojifiles/packages/haproxy/1.8.1/5.el7/x86_64/haproxy18-1.8.1-5.el7.x86_64.rpm sudo yum install haproxy18-1.8.1-5.el7.x86_64.rpm -y fi if [ $? -eq 1 ] diff --git a/app/sql.py b/app/sql.py index a0eaa4ab..d558a285 100644 --- a/app/sql.py +++ b/app/sql.py @@ -707,6 +707,123 @@ def select_servers_metrics(uuid, **kwargs): cur.close() con.close() +def select_table_metrics(uuid): + con, cur = create_db.get_cur() + sql = """ select * from user where username = '%s' """ % get_user_name_by_uuid(uuid) + + try: + cur.execute(sql) + except sqltool.Error as e: + print("An error occurred:", e) + else: + for group in cur: + if group[5] == '1': + groups = "" + else: + groups = "and servers.groups like '%{group}%' ".format(group=group[5]) + sql = """ + select ip.ip, hostname, avg_sess_1h, avg_sess_24h, avg_sess_3d, max_sess_1h, max_sess_24h, max_sess_3d, avg_cur_1h, avg_cur_24h, avg_cur_3d, max_con_1h, max_con_24h, max_con_3d from + (select servers.ip from servers where metrics = 1 ) as ip, + + (select servers.ip, servers.hostname as hostname from servers left join metrics as metr on servers.ip = metr.serv where servers.metrics = 1 %s) as hostname, + + (select servers.ip,round(avg(metr.sess_rate), 1) as avg_sess_1h from servers + left join metrics as metr on metr.serv = servers.ip + where servers.metrics = 1 and + metr.date <= datetime('now', 'localtime') and metr.date >= datetime('now', '-1 hours', 'localtime') + group by servers.ip) as avg_sess_1h, + + (select servers.ip,round(avg(metr.sess_rate), 1) as avg_sess_24h from servers + left join metrics as metr on metr.serv = servers.ip + where servers.metrics = 1 and + metr.date <= datetime('now', 'localtime') and metr.date >= datetime('now', '-24 hours', 'localtime') + group by servers.ip) as avg_sess_24h, + + (select servers.ip,round(avg(metr.sess_rate), 1) as avg_sess_3d from servers + left join metrics as metr on metr.serv = servers.ip + where servers.metrics = 1 and + metr.date <= datetime('now', 'localtime') and metr.date >= datetime('now', '-3 days', 'localtime') + group by servers.ip ) as avg_sess_3d, + + (select servers.ip,max(metr.sess_rate) as max_sess_1h from servers + left join metrics as metr on metr.serv = servers.ip + where servers.metrics = 1 and + metr.date <= datetime('now', 'localtime') and metr.date >= datetime('now', '-1 hours', 'localtime') + group by servers.ip) as max_sess_1h, + + (select servers.ip,max(metr.sess_rate) as max_sess_24h from servers + left join metrics as metr on metr.serv = servers.ip + where servers.metrics = 1 and + metr.date <= datetime('now', 'localtime') and metr.date >= datetime('now', '-24 hours', 'localtime') + group by servers.ip) as max_sess_24h, + + (select servers.ip,max(metr.sess_rate) as max_sess_3d from servers + left join metrics as metr on metr.serv = servers.ip + where servers.metrics = 1 and + metr.date <= datetime('now', 'localtime') and metr.date >= datetime('now', '-3 days', 'localtime') + group by servers.ip ) as max_sess_3d, + + (select servers.ip,round(avg(metr.curr_con+metr.cur_ssl_con), 1) as avg_cur_1h from servers + left join metrics as metr on metr.serv = servers.ip + where servers.metrics = 1 and + metr.date <= datetime('now', 'localtime') and metr.date >= datetime('now', '-1 hours', 'localtime') + group by servers.ip) as avg_cur_1h, + + (select servers.ip,round(avg(metr.curr_con+metr.cur_ssl_con), 1) as avg_cur_24h from servers + left join metrics as metr on metr.serv = servers.ip + where servers.metrics = 1 and + metr.date <= datetime('now', 'localtime') and metr.date >= datetime('now', '-24 hours', 'localtime') + group by servers.ip) as avg_cur_24h, + + (select servers.ip,round(avg(metr.curr_con+metr.cur_ssl_con), 1) as avg_cur_3d from servers + left join metrics as metr on metr.serv = servers.ip + where servers.metrics = 1 and + metr.date <= datetime('now', 'localtime') and metr.date >= datetime('now', '-3 days', 'localtime') + group by servers.ip ) as avg_cur_3d, + + (select servers.ip,max(metr.curr_con) as max_con_1h from servers + left join metrics as metr on metr.serv = servers.ip + where servers.metrics = 1 and + metr.date <= datetime('now', 'localtime') and metr.date >= datetime('now', '-1 hours', 'localtime') + group by servers.ip) as max_con_1h, + + (select servers.ip,max(metr.curr_con) as max_con_24h from servers + left join metrics as metr on metr.serv = servers.ip + where servers.metrics = 1 and + metr.date <= datetime('now', 'localtime') and metr.date >= datetime('now', '-24 hours', 'localtime') + group by servers.ip) as max_con_24h, + + (select servers.ip,max(metr.curr_con) as max_con_3d from servers + left join metrics as metr on metr.serv = servers.ip + where servers.metrics = 1 and + metr.date <= datetime('now', 'localtime') and metr.date >= datetime('now', '-3 days', 'localtime') + group by servers.ip ) as max_con_3d + + where ip.ip=hostname.ip + and ip.ip=avg_sess_1h.ip + and ip.ip=avg_sess_24h.ip + and ip.ip=avg_sess_3d.ip + and ip.ip=max_sess_1h.ip + and ip.ip=max_sess_24h.ip + and ip.ip=max_sess_3d.ip + and ip.ip=avg_cur_1h.ip + and ip.ip=avg_cur_24h.ip + and ip.ip=avg_cur_3d.ip + and ip.ip=max_con_1h.ip + and ip.ip=max_con_24h.ip + and ip.ip=max_con_3d.ip + + group by ip.ip""" % groups + try: + cur.execute(sql) + except sqltool.Error as e: + print('An error occurred: ' + e + ' X') + else: + return cur.fetchall() + cur.close() + con.close() + + def show_update_telegram(token, page): from jinja2 import Environment, FileSystemLoader env = Environment(loader=FileSystemLoader('templates/ajax')) diff --git a/app/templates/metrics.html b/app/templates/metrics.html index 5694f202..8bddec63 100644 --- a/app/templates/metrics.html +++ b/app/templates/metrics.html @@ -5,7 +5,60 @@ iframe { border: none; padding: 10px; } +table, th, tr, td { + border: 1px solid #ddd; + align: center; + text-align: center; +} +th, tr, td { + width: 6%; + padding: 10px; +} + + + + + + + + + + + + + + + + + + + + + + + + + + + {% for val in table_stat %} + + + + + + + + + + + + + + + + {% endfor %} +
Metrics
ServerAverage sessionsPeak sessionsAverage connectionsPeak connections
60 minutes24 hours3 days60 minutes24 hours3 days60 minutes24 hours3 days60 minutes24 hours3 days
{{ val.1 }}{{ val.2 }}{{ val.3 }}{{ val.4 }}{{ val.5 }}{{ val.6 }}{{ val.7 }}{{ val.8 }}{{ val.9 }}{{ val.10 }}{{ val.11 }}{{ val.12 }}{{ val.13 }}