mirror of https://github.com/Aidaho12/haproxy-wi
parent
b0169331ce
commit
2caca917b7
68
app/funct.py
68
app/funct.py
|
@ -47,10 +47,30 @@ def get_data(type):
|
|||
return now_utc.strftime(fmt)
|
||||
|
||||
|
||||
def get_user_group(**kwargs):
|
||||
import sql
|
||||
import http.cookies
|
||||
try:
|
||||
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
||||
user_group_id = cookie.get('group')
|
||||
user_group_id1 = user_group_id.value
|
||||
groups = sql.select_groups(id=user_group_id1)
|
||||
for g in groups:
|
||||
if g[0] == int(user_group_id1):
|
||||
if kwargs.get('id'):
|
||||
user_group = g[0]
|
||||
else:
|
||||
user_group = g[1]
|
||||
except:
|
||||
user_group = ''
|
||||
|
||||
return user_group
|
||||
|
||||
def logging(serv, action, **kwargs):
|
||||
import sql
|
||||
import http.cookies
|
||||
log_path = get_config_var('main', 'log_path')
|
||||
user_group = get_user_group()
|
||||
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
||||
|
||||
if not os.path.exists(log_path):
|
||||
|
@ -67,16 +87,6 @@ def logging(serv, action, **kwargs):
|
|||
except:
|
||||
login = ''
|
||||
|
||||
try:
|
||||
user_group_id = cookie.get('group')
|
||||
user_group_id1 = user_group_id.value
|
||||
groups = sql.select_groups(id=user_group_id1)
|
||||
for g in groups:
|
||||
if g[0] == int(user_group_id1):
|
||||
user_group = g[1]
|
||||
except:
|
||||
user_group = ''
|
||||
|
||||
if kwargs.get('alerting') == 1:
|
||||
mess = get_data('date_in_log') + action + "\n"
|
||||
log = open(log_path + "/checker-"+get_data('logs')+".log", "a")
|
||||
|
@ -290,6 +300,7 @@ def diff_config(oldcfg, cfg):
|
|||
import sql
|
||||
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
||||
log_path = get_config_var('main', 'log_path')
|
||||
user_group = get_user_group()
|
||||
diff = ""
|
||||
date = get_data('date_in_log')
|
||||
cmd="/bin/diff -ub %s %s" % (oldcfg, cfg)
|
||||
|
@ -300,16 +311,6 @@ def diff_config(oldcfg, cfg):
|
|||
except:
|
||||
login = ''
|
||||
|
||||
try:
|
||||
user_group_id = cookie.get('group')
|
||||
user_group_id1 = user_group_id.value
|
||||
groups = sql.select_groups(id=user_group_id1)
|
||||
for g in groups:
|
||||
if g[0] == int(user_group_id1):
|
||||
user_group = g[1]
|
||||
except:
|
||||
user_group = ''
|
||||
|
||||
output, stderr = subprocess_execute(cmd)
|
||||
|
||||
for line in output:
|
||||
|
@ -859,18 +860,7 @@ def show_haproxy_log(serv, rows=10, waf='0', grep=None, hour='00', minut='00', h
|
|||
|
||||
return show_log(output, grep=grep)
|
||||
elif service == 'internal':
|
||||
import http.cookies
|
||||
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
||||
|
||||
try:
|
||||
user_group_id = cookie.get('group')
|
||||
user_group_id1 = user_group_id.value
|
||||
groups = sql.select_groups(id=user_group_id1)
|
||||
for g in groups:
|
||||
if g[0] == int(user_group_id1):
|
||||
user_group = g[1]
|
||||
except:
|
||||
user_group = ''
|
||||
user_group = get_user_group()
|
||||
|
||||
if user_group != '' and user_group != 'All':
|
||||
user_grep = "|grep 'group: " + user_group + "'"
|
||||
|
@ -1133,16 +1123,22 @@ def get_users_params(**kwargs):
|
|||
return user, user_id, role, token, servers
|
||||
|
||||
|
||||
def check_group(group, role_id):
|
||||
def check_user_group(**kwargs):
|
||||
import http.cookies
|
||||
import os
|
||||
import sql
|
||||
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
||||
user_id = cookie.get('uuid')
|
||||
id = sql.get_user_id_by_uuid(user_id.value)
|
||||
if sql.select_user_groups(id, check_id=group) or role_id == 1:
|
||||
user_uuid = cookie.get('uuid')
|
||||
group = cookie.get('group')
|
||||
group_id = group.value
|
||||
user_id = sql.get_user_id_by_uuid(user_uuid.value)
|
||||
|
||||
if sql.check_user_group(user_id, group_id):
|
||||
return True
|
||||
else:
|
||||
logging('localhost', ' has tried to actions in not own group ', haproxywi=1, login=1)
|
||||
print('Atata!')
|
||||
sys.exit()
|
||||
return False
|
||||
|
||||
|
||||
|
|
|
@ -267,7 +267,6 @@ if form.getvalue('list_select_id') is not None:
|
|||
|
||||
|
||||
if form.getvalue('list_id_for_delete') is not None:
|
||||
import http.cookies
|
||||
haproxy_sock_port = sql.get_setting('haproxy_sock_port')
|
||||
lists_path = sql.get_setting('lists_path')
|
||||
fullpath = funct.get_config_var('main', 'fullpath')
|
||||
|
@ -275,10 +274,7 @@ if form.getvalue('list_id_for_delete') is not None:
|
|||
ip = form.getvalue('list_ip_for_delete')
|
||||
list_id = form.getvalue('list_id_for_delete')
|
||||
list_name = form.getvalue('list_name')
|
||||
|
||||
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
||||
user_group = cookie.get('group')
|
||||
user_group = user_group.value
|
||||
user_group = funct.get_user_group(id=1)
|
||||
|
||||
cmd = "sed -i 's!%s$!!' %s/%s/%s/%s && sed -i '/^$/d' %s/%s/%s/%s" % (ip, fullpath, lists_path, user_group, list_name, fullpath, lists_path, user_group, list_name)
|
||||
output, stderr = funct.subprocess_execute(cmd)
|
||||
|
@ -297,17 +293,13 @@ if form.getvalue('list_id_for_delete') is not None:
|
|||
|
||||
|
||||
if form.getvalue('list_ip_for_add') is not None:
|
||||
import http.cookies
|
||||
haproxy_sock_port = sql.get_setting('haproxy_sock_port')
|
||||
lists_path = sql.get_setting('lists_path')
|
||||
fullpath = funct.get_config_var('main', 'fullpath')
|
||||
ip = form.getvalue('list_ip_for_add')
|
||||
list_id = form.getvalue('list_id_for_add')
|
||||
list_name = form.getvalue('list_name')
|
||||
|
||||
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
||||
user_group = cookie.get('group')
|
||||
user_group = user_group.value
|
||||
user_group = funct.get_user_group(id=1)
|
||||
|
||||
cmd = 'echo "%s" >> %s/%s/%s/%s' % (ip, fullpath, lists_path, user_group, list_name)
|
||||
output, stderr = funct.subprocess_execute(cmd)
|
||||
|
@ -676,13 +668,12 @@ if form.getvalue('viewlogs') is not None:
|
|||
minut = form.getvalue('minut')
|
||||
hour1 = form.getvalue('hour1')
|
||||
minut1 = form.getvalue('minut1')
|
||||
out = funct.show_haproxy_log(serv=viewlog, rows=rows, waf='0', grep=grep, hour=hour, minut=minut, hour1=hour1, minut1=minut1, service='internal')
|
||||
if funct.check_user_group():
|
||||
out = funct.show_haproxy_log(serv=viewlog, rows=rows, waf='0', grep=grep, hour=hour, minut=minut, hour1=hour1, minut1=minut1, service='internal')
|
||||
print(out)
|
||||
|
||||
|
||||
if serv is not None and act == "showMap":
|
||||
#from datetime import datetime
|
||||
#from pytz import timezone
|
||||
import networkx as nx
|
||||
import matplotlib
|
||||
matplotlib.use('Agg')
|
||||
|
@ -1555,7 +1546,7 @@ if form.getvalue('newuser') is not None:
|
|||
group = form.getvalue('newgroupuser')
|
||||
role_id = sql.get_role_id_by_name(role)
|
||||
|
||||
if funct.check_group(group, role_id):
|
||||
if funct.check_user_group():
|
||||
if funct.is_admin(level=role_id):
|
||||
if sql.add_user(new_user, email, password, role, activeuser):
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
|
@ -1592,7 +1583,7 @@ if form.getvalue('updateuser') is not None:
|
|||
group = form.getvalue('usergroup')
|
||||
role_id = sql.get_role_id_by_name(role)
|
||||
|
||||
if funct.check_group(group, role_id):
|
||||
if funct.check_user_group():
|
||||
if funct.is_admin(level=role_id):
|
||||
sql.update_user(new_user, email, role, id, activeuser)
|
||||
funct.logging(new_user, ' has updated user ', haproxywi=1, login=1)
|
||||
|
@ -1727,10 +1718,7 @@ if form.getvalue('updategroup') is not None:
|
|||
|
||||
|
||||
if form.getvalue('new_ssh'):
|
||||
import http.cookies
|
||||
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
||||
group = cookie.get('group')
|
||||
user_group = group.value
|
||||
user_group = funct.get_user_group()
|
||||
name = form.getvalue('new_ssh')
|
||||
name = name + '_' + user_group
|
||||
enable = form.getvalue('ssh_enable')
|
||||
|
@ -1804,10 +1792,7 @@ if form.getvalue('updatessh'):
|
|||
|
||||
|
||||
if form.getvalue('ssh_cert'):
|
||||
import http.cookies
|
||||
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
||||
group = cookie.get('group')
|
||||
user_group = group.value
|
||||
user_group = funct.get_user_group()
|
||||
name = form.getvalue('name')
|
||||
name = name + '_' + user_group
|
||||
|
||||
|
@ -1924,10 +1909,7 @@ if form.getvalue('getcurrentusergroup') is not None:
|
|||
|
||||
|
||||
if form.getvalue('newsmon') is not None:
|
||||
import http.cookies
|
||||
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
||||
user_group = cookie.get('group')
|
||||
user_group = user_group.value
|
||||
user_group = funct.get_user_group(id=1)
|
||||
server = form.getvalue('newsmon')
|
||||
port = form.getvalue('newsmonport')
|
||||
enable = form.getvalue('newsmonenable')
|
||||
|
@ -1963,22 +1945,17 @@ if form.getvalue('newsmon') is not None:
|
|||
|
||||
|
||||
if form.getvalue('smondel') is not None:
|
||||
import http.cookies
|
||||
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
||||
user_group = cookie.get('group')
|
||||
user_group = user_group.value
|
||||
user_group = funct.get_user_group(id=1)
|
||||
id = form.getvalue('smondel')
|
||||
|
||||
if sql.delete_smon(id, user_group):
|
||||
print('Ok')
|
||||
funct.logging('SMON', ' Has been delete server from SMON ', haproxywi=1, login=1)
|
||||
if funct.check_user_group():
|
||||
if sql.delete_smon(id, user_group):
|
||||
print('Ok')
|
||||
funct.logging('SMON', ' Has been delete server from SMON ', haproxywi=1, login=1)
|
||||
|
||||
|
||||
if form.getvalue('showsmon') is not None:
|
||||
import http.cookies
|
||||
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
||||
user_group = cookie.get('group')
|
||||
user_group = user_group.value
|
||||
user_group = funct.get_user_group(id=1)
|
||||
sort = form.getvalue('sort')
|
||||
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
#!/usr/bin/env python3
|
||||
import funct
|
||||
import sql
|
||||
import http.cookies
|
||||
import os
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
env = Environment(extensions=["jinja2.ext.do"],loader=FileSystemLoader('templates/'), autoescape=True)
|
||||
template = env.get_template('servers.html')
|
||||
|
@ -15,9 +13,7 @@ try:
|
|||
user, user_id, role, token, servers = funct.get_users_params()
|
||||
ldap_enable = sql.get_setting('ldap_enable')
|
||||
grafana, stderr = funct.subprocess_execute("service grafana-server status |grep Active |awk '{print $1}'")
|
||||
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
||||
group = cookie.get('group')
|
||||
user_group = group.value
|
||||
user_group = funct.get_user_group(id=1)
|
||||
settings = sql.get_setting('', all=1)
|
||||
except Exception as e:
|
||||
pass
|
||||
|
|
11
app/smon.py
11
app/smon.py
|
@ -15,24 +15,21 @@ funct.check_login()
|
|||
|
||||
try:
|
||||
user, user_id, role, token, servers = funct.get_users_params()
|
||||
import http.cookies
|
||||
import os
|
||||
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
||||
group = cookie.get('group')
|
||||
user_group = group.value
|
||||
user_group = funct.get_user_group(id=1)
|
||||
cmd = "systemctl status smon |grep Active |awk '{print $2}'"
|
||||
smon_status, stderr = funct.subprocess_execute(cmd)
|
||||
except:
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
if action == 'add':
|
||||
smon = sql.select_smon(user_group=user_group,action='add')
|
||||
smon = sql.select_smon(user_group,action='add')
|
||||
funct.page_for_admin(level=2)
|
||||
title = "SMON Admin"
|
||||
else:
|
||||
smon = sql.smon_list(user_group)
|
||||
title = "SMON Dashboard"
|
||||
|
||||
|
||||
template = template.render(h2 = 1, title = title,
|
||||
autoreœfresh = 1,
|
||||
role = role,
|
||||
|
|
70
app/sql.py
70
app/sql.py
|
@ -386,20 +386,13 @@ def select_user_groups(id, **kwargs):
|
|||
sql = """select user_group_id from user_groups where user_id = '%s' """ % id
|
||||
if kwargs.get("limit") is not None:
|
||||
sql = """select user_group_id from user_groups where user_id = '%s' limit 1 """ % id
|
||||
if kwargs.get("check_id") is not None:
|
||||
sql = """select * from user_groups where user_id='%s' and user_group_id = '%s' """ % (id, kwargs.get("check_id"))
|
||||
|
||||
try:
|
||||
cur.execute(sql)
|
||||
except sqltool.Error as e:
|
||||
funct.out_error(e)
|
||||
else:
|
||||
if kwargs.get("check_id") is not None:
|
||||
for g in cur.fetchall():
|
||||
if g[0]:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
elif kwargs.get("limit") is not None:
|
||||
if kwargs.get("limit") is not None:
|
||||
for g in cur.fetchall():
|
||||
return g[0]
|
||||
else:
|
||||
|
@ -408,6 +401,27 @@ def select_user_groups(id, **kwargs):
|
|||
con.close()
|
||||
|
||||
|
||||
def check_user_group(user_id, group_id):
|
||||
con, cur = get_cur()
|
||||
sql = """select * from user_groups where user_id='%s' and user_group_id = '%s' """ % (user_id, group_id)
|
||||
try:
|
||||
cur.execute(sql)
|
||||
except sqltool.Error as e:
|
||||
funct.out_error(e)
|
||||
print(str(e))
|
||||
else:
|
||||
for g in cur.fetchall():
|
||||
#print(str(g[0]))
|
||||
if g[0] != '':
|
||||
return True
|
||||
else:
|
||||
#print('Atata!')
|
||||
return False
|
||||
|
||||
cur.close()
|
||||
con.close()
|
||||
|
||||
|
||||
def select_user_groups_with_names(id, **kwargs):
|
||||
con, cur = get_cur()
|
||||
if kwargs.get("all") is not None:
|
||||
|
@ -734,7 +748,7 @@ def get_dick_permit(**kwargs):
|
|||
if kwargs.get('keepalived'):
|
||||
nginx = "and keepalived = 1"
|
||||
|
||||
if select_user_groups(user, check_id=grp):
|
||||
if funct.check_user_group():
|
||||
con, cur = get_cur()
|
||||
if grp == '1':
|
||||
sql = """ select * from servers where enable = 1 %s %s %s order by pos""" % (disable, type_ip, nginx)
|
||||
|
@ -1422,11 +1436,10 @@ def select_servers_metrics(uuid, **kwargs):
|
|||
import http.cookies
|
||||
import os
|
||||
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
||||
user_id = cookie.get('uuid')
|
||||
group = cookie.get('group')
|
||||
group = group.value
|
||||
id = get_user_id_by_uuid(user_id.value)
|
||||
if select_user_groups(id, check_id=group):
|
||||
|
||||
if funct.check_user_group():
|
||||
if group == '1':
|
||||
sql = """ select ip from servers where enable = 1 and metrics = '1' """
|
||||
else:
|
||||
|
@ -1446,11 +1459,10 @@ def select_table_metrics(uuid):
|
|||
import http.cookies
|
||||
import os
|
||||
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
||||
user_id = cookie.get('uuid')
|
||||
group = cookie.get('group')
|
||||
group = group.value
|
||||
id = get_user_id_by_uuid(user_id.value)
|
||||
if select_user_groups(id, check_id=group):
|
||||
|
||||
if funct.check_user_group():
|
||||
if group == '1':
|
||||
groups = ""
|
||||
else:
|
||||
|
@ -1690,18 +1702,20 @@ def update_setting(param, val):
|
|||
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
||||
group = cookie.get('group')
|
||||
user_group = group.value
|
||||
con, cur = get_cur()
|
||||
sql = """update `settings` set `value` = '%s' where param = '%s' and `group` = '%s' """ % (val, param, user_group)
|
||||
try:
|
||||
cur.execute(sql)
|
||||
con.commit()
|
||||
return True
|
||||
except sqltool.Error as e:
|
||||
funct.out_error(e)
|
||||
con.rollback()
|
||||
return False
|
||||
cur.close()
|
||||
con.close()
|
||||
|
||||
if funct.check_user_group():
|
||||
con, cur = get_cur()
|
||||
sql = """update `settings` set `value` = '%s' where param = '%s' and `group` = '%s' """ % (val, param, user_group)
|
||||
try:
|
||||
cur.execute(sql)
|
||||
con.commit()
|
||||
return True
|
||||
except sqltool.Error as e:
|
||||
funct.out_error(e)
|
||||
con.rollback()
|
||||
return False
|
||||
cur.close()
|
||||
con.close()
|
||||
|
||||
|
||||
def get_ver():
|
||||
|
|
|
@ -133,7 +133,7 @@
|
|||
<td></td>
|
||||
</tr>
|
||||
{% for ssh in sshs %}
|
||||
{% if ssh.5|string() == group %}
|
||||
{% if ssh.5|string() == group|string() %}
|
||||
<tr style="width: 50%;" id="ssh-table-{{ssh.0}}" class="{{ loop.cycle('odd', 'even') }}">
|
||||
<td class="first-collumn">
|
||||
{% set id = 'ssh_name-' + ssh.0|string() %}
|
||||
|
|
Loading…
Reference in New Issue