mirror of https://github.com/Aidaho12/haproxy-wi
parent
4fa061df97
commit
c016f706f1
10
app/add.py
10
app/add.py
|
@ -15,12 +15,10 @@ print('Content-type: text/html\n')
|
|||
funct.check_login(service=1)
|
||||
funct.page_for_admin(level=3)
|
||||
|
||||
if (
|
||||
form.getvalue('mode') is None and
|
||||
form.getvalue('new_userlist') is None and
|
||||
form.getvalue('peers-name') is None and
|
||||
form.getvalue('generateconfig') is None
|
||||
):
|
||||
if all(v is None for v in [
|
||||
form.getvalue('mode'), form.getvalue('new_userlist'),
|
||||
form.getvalue('peers-name'), form.getvalue('generateconfig')
|
||||
]):
|
||||
try:
|
||||
user, user_id, role, token, servers, user_services = funct.get_users_params(haproxy=1)
|
||||
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
||||
|
|
288
app/funct.py
288
app/funct.py
|
@ -9,9 +9,11 @@ def is_ip_or_dns(server_from_request: str) -> str:
|
|||
ip_regex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
|
||||
dns_regex = "^(?!-)[A-Za-z0-9-]+([\\-\\.]{1}[a-z0-9]+)*\\.[A-Za-z]{2,6}$"
|
||||
try:
|
||||
if server_from_request in ('roxy-wi-checker', 'roxy-wi-keep_alive', 'roxy-wi-keep-alive', 'roxy-wi-metrics',
|
||||
'roxy-wi-portscanner', 'roxy-wi-smon', 'roxy-wi-socket', 'fail2ban', 'prometheus',
|
||||
'all', 'grafana-server', 'rabbitmq-server'):
|
||||
if server_from_request in (
|
||||
'roxy-wi-checker', 'roxy-wi-keep_alive', 'roxy-wi-keep-alive', 'roxy-wi-metrics',
|
||||
'roxy-wi-portscanner', 'roxy-wi-smon', 'roxy-wi-socket', 'fail2ban', 'prometheus',
|
||||
'all', 'grafana-server', 'rabbitmq-server'
|
||||
):
|
||||
return server_from_request
|
||||
if re.match(ip_regex, server_from_request):
|
||||
return server_from_request
|
||||
|
@ -20,7 +22,7 @@ def is_ip_or_dns(server_from_request: str) -> str:
|
|||
return server_from_request
|
||||
else:
|
||||
return ''
|
||||
except:
|
||||
except Exception:
|
||||
return ''
|
||||
|
||||
|
||||
|
@ -122,7 +124,7 @@ def logging(server_ip, action, **kwargs):
|
|||
log_path = get_config_var('main', 'log_path')
|
||||
try:
|
||||
user_group = get_user_group()
|
||||
except:
|
||||
except Exception:
|
||||
user_group = ''
|
||||
|
||||
if not os.path.exists(log_path):
|
||||
|
@ -142,7 +144,7 @@ def logging(server_ip, action, **kwargs):
|
|||
try:
|
||||
if len(login_name) > 1:
|
||||
login = kwargs.get('login')
|
||||
except:
|
||||
except Exception:
|
||||
login = ''
|
||||
|
||||
try:
|
||||
|
@ -164,15 +166,15 @@ def logging(server_ip, action, **kwargs):
|
|||
print(str(e))
|
||||
else:
|
||||
mess = get_data('date_in_log') + " " + action + " from " + ip + "\n"
|
||||
log = open(log_path + "/roxy-wi-"+get_data('logs')+".log", "a")
|
||||
log = open(log_path + "/roxy-wi-" + get_data('logs') + ".log", "a")
|
||||
elif kwargs.get('provisioning') == 1:
|
||||
mess = get_data('date_in_log') + " from " + ip + " user: " + login + ", group: " + user_group + ", " + \
|
||||
action + "\n"
|
||||
log = open(log_path + "/provisioning-"+get_data('logs')+".log", "a")
|
||||
log = open(log_path + "/provisioning-" + get_data('logs') + ".log", "a")
|
||||
else:
|
||||
mess = get_data('date_in_log') + " from " + ip + " user: " + login + ", group: " + user_group + ", " + \
|
||||
action + " for: " + server_ip + "\n"
|
||||
log = open(log_path + "/config_edit-"+get_data('logs')+".log", "a")
|
||||
log = open(log_path + "/config_edit-" + get_data('logs') + ".log", "a")
|
||||
|
||||
if kwargs.get('keep_history'):
|
||||
keep_action_history(kwargs.get('service'), action, server_ip, login, ip)
|
||||
|
@ -259,7 +261,7 @@ def slack_send_mess(mess, **kwargs):
|
|||
client = WebClient(token=slack_token)
|
||||
|
||||
try:
|
||||
client.chat_postMessage(channel='#'+channel_name, text=mess)
|
||||
client.chat_postMessage(channel='#' + channel_name, text=mess)
|
||||
except SlackApiError as e:
|
||||
print('error: ' + str(e))
|
||||
logging('localhost', str(e), haproxywi=1)
|
||||
|
@ -360,13 +362,13 @@ def return_ssh_keys_path(server_ip, **kwargs):
|
|||
ssh_enable = sshs.enable
|
||||
ssh_user_name = sshs.username
|
||||
ssh_user_password = sshs.password
|
||||
ssh_key_name = full_path+'/keys/%s.pem' % sshs.name
|
||||
ssh_key_name = full_path + '/keys/%s.pem' % sshs.name
|
||||
else:
|
||||
for sshs in sql.select_ssh(serv=server_ip):
|
||||
ssh_enable = sshs.enable
|
||||
ssh_user_name = sshs.username
|
||||
ssh_user_password = sshs.password
|
||||
ssh_key_name = full_path+'/keys/%s.pem' % sshs.name
|
||||
ssh_key_name = full_path + '/keys/%s.pem' % sshs.name
|
||||
|
||||
return ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name
|
||||
|
||||
|
@ -421,8 +423,10 @@ def get_config(server_ip, cfg, **kwargs):
|
|||
|
||||
if kwargs.get("keepalived") or kwargs.get("service") == 'keepalived':
|
||||
config_path = "/etc/keepalived/keepalived.conf"
|
||||
elif (kwargs.get("nginx") or kwargs.get("service") == 'nginx' or
|
||||
kwargs.get("apache") or kwargs.get("service") == 'apache'):
|
||||
elif (
|
||||
kwargs.get("nginx") or kwargs.get("service") == 'nginx'
|
||||
or kwargs.get("apache") or kwargs.get("service") == 'apache'
|
||||
):
|
||||
config_path = kwargs.get('config_file_name')
|
||||
elif kwargs.get("waf") or kwargs.get("service") == 'waf':
|
||||
config_path = sql.get_setting('haproxy_dir') + '/waf/rules/' + kwargs.get("waf_rule_file")
|
||||
|
@ -479,7 +483,7 @@ def diff_config(oldcfg, cfg, **kwargs):
|
|||
diff += date + " user: " + login + ", group: " + user_group + " " + line + "\n"
|
||||
|
||||
try:
|
||||
log = open(log_path + "/config_edit-"+get_data('logs')+".log", "a")
|
||||
log = open(log_path + "/config_edit-" + get_data('logs') + ".log", "a")
|
||||
log.write(diff)
|
||||
log.close()
|
||||
except IOError:
|
||||
|
@ -489,20 +493,22 @@ def diff_config(oldcfg, cfg, **kwargs):
|
|||
|
||||
def get_remote_sections(server_ip: str, service: str) -> str:
|
||||
import sql
|
||||
remote_dir = service+'_dir'
|
||||
remote_dir = service + '_dir'
|
||||
config_dir = sql.get_setting(remote_dir)
|
||||
config_dir = return_nice_path(config_dir)
|
||||
if service == 'nginx':
|
||||
section_name = 'server_name'
|
||||
commands = [
|
||||
'sudo grep {} {}* -R |grep -v \'${}\|#\'|awk \'{{print $1, $3}}\''.format(section_name, config_dir,
|
||||
section_name)]
|
||||
'sudo grep {} {}* -R |grep -v \'${}\|#\'|awk \'{{print $1, $3}}\''.format(
|
||||
section_name, config_dir, section_name
|
||||
)]
|
||||
|
||||
elif service == 'apache':
|
||||
section_name = 'ServerName'
|
||||
commands = [
|
||||
'sudo grep {} {}*/*.conf -R |grep -v \'${}\|#\'|awk \'{{print $1, $3}}\''.format(section_name, config_dir,
|
||||
section_name)]
|
||||
'sudo grep {} {}*/*.conf -R |grep -v \'${}\|#\'|awk \'{{print $1, $3}}\''.format(
|
||||
section_name, config_dir, section_name
|
||||
)]
|
||||
|
||||
backends = ssh_command(server_ip, commands)
|
||||
|
||||
|
@ -520,18 +526,10 @@ def get_sections(config, **kwargs):
|
|||
if find_ip:
|
||||
return_config.append(find_ip[0])
|
||||
else:
|
||||
if line.startswith(('global',
|
||||
'listen',
|
||||
'frontend',
|
||||
'backend',
|
||||
'cache',
|
||||
'defaults',
|
||||
'#HideBlockStart',
|
||||
'#HideBlockEnd',
|
||||
'peers',
|
||||
'resolvers',
|
||||
'userlist',
|
||||
'http-errors')):
|
||||
if line.startswith((
|
||||
'global', 'listen', 'frontend', 'backend', 'cache', 'defaults', '#HideBlockStart',
|
||||
'#HideBlockEnd', 'peers', 'resolvers', 'userlist', 'http-errors'
|
||||
)):
|
||||
line = line.strip()
|
||||
return_config.append(line)
|
||||
|
||||
|
@ -551,18 +549,10 @@ def get_section_from_config(config, section):
|
|||
record = True
|
||||
continue
|
||||
if record:
|
||||
if line.startswith(('global',
|
||||
'listen',
|
||||
'frontend',
|
||||
'backend',
|
||||
'cache',
|
||||
'defaults',
|
||||
'#HideBlockStart',
|
||||
'#HideBlockEnd',
|
||||
'peers',
|
||||
'resolvers',
|
||||
'userlist',
|
||||
'http-errors')):
|
||||
if line.startswith((
|
||||
'global', 'listen', 'frontend', 'backend', 'cache', 'defaults', '#HideBlockStart',
|
||||
'#HideBlockEnd', 'peers', 'resolvers', 'userlist', 'http-errors'
|
||||
)):
|
||||
record = False
|
||||
end_line = index
|
||||
end_line = end_line - 1
|
||||
|
@ -621,7 +611,7 @@ def get_backends_from_config(server_ip, backends=''):
|
|||
except Exception as e:
|
||||
logging('localhost', str(e), haproxywi=1)
|
||||
try:
|
||||
cfg = configs_dir + server_ip + "-" + get_data('config') + '.'+format_cfg
|
||||
cfg = configs_dir + server_ip + "-" + get_data('config') + '.' + format_cfg
|
||||
except Exception:
|
||||
logging('localhost', ' Cannot generate cfg path', haproxywi=1)
|
||||
return
|
||||
|
@ -667,7 +657,7 @@ def get_stick_table(table):
|
|||
def show_installation_output(error, output, service):
|
||||
if error and "WARNING" not in error:
|
||||
logging('localhost', error, haproxywi=1)
|
||||
print('error: '+error)
|
||||
print('error: ' + error)
|
||||
return False
|
||||
else:
|
||||
for l in output:
|
||||
|
@ -722,11 +712,13 @@ def install_haproxy(server_ip, **kwargs):
|
|||
|
||||
syn_flood_protect = '1' if kwargs.get('syn_flood') == "1" else ''
|
||||
|
||||
commands = ["chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv +
|
||||
" SOCK_PORT=" + hap_sock_p + " STAT_PORT=" + stats_port + " STAT_FILE="+server_state_file + " DOCKER=" + docker +
|
||||
" SSH_PORT=" + ssh_port + " STATS_USER=" + stats_user + " CONT_NAME=" + container_name + " HAP_DIR=" + haproxy_dir +
|
||||
" STATS_PASS='" + stats_password + "' HAPVER=" + haproxy_ver + " SYN_FLOOD=" + syn_flood_protect +
|
||||
" HOST=" + server_ip + " USER=" + ssh_user_name + " PASS='" + ssh_user_password + "' KEY=" + ssh_key_name]
|
||||
commands = [
|
||||
"chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv +
|
||||
" SOCK_PORT=" + hap_sock_p + " STAT_PORT=" + stats_port + " STAT_FILE=" + server_state_file + " DOCKER=" + docker +
|
||||
" SSH_PORT=" + ssh_port + " STATS_USER=" + stats_user + " CONT_NAME=" + container_name + " HAP_DIR=" + haproxy_dir +
|
||||
" STATS_PASS='" + stats_password + "' HAPVER=" + haproxy_ver + " SYN_FLOOD=" + syn_flood_protect +
|
||||
" HOST=" + server_ip + " USER=" + ssh_user_name + " PASS='" + ssh_user_password + "' KEY=" + ssh_key_name
|
||||
]
|
||||
|
||||
output, error = subprocess_execute(commands[0])
|
||||
if server_for_installing:
|
||||
|
@ -764,9 +756,11 @@ def waf_install(server_ip):
|
|||
else:
|
||||
proxy_serv = ''
|
||||
|
||||
commands = ["chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv + " HAPROXY_PATH=" + haproxy_dir +
|
||||
" VERSION='" + ver + "' SSH_PORT=" + ssh_port + " HOST=" + server_ip +
|
||||
" USER=" + ssh_user_name + " PASS='" + ssh_user_password + "' KEY=" + ssh_key_name]
|
||||
commands = [
|
||||
"chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv + " HAPROXY_PATH=" + haproxy_dir +
|
||||
" VERSION='" + ver + "' SSH_PORT=" + ssh_port + " HOST=" + server_ip +
|
||||
" USER=" + ssh_user_name + " PASS='" + ssh_user_password + "' KEY=" + ssh_key_name
|
||||
]
|
||||
|
||||
output, error = subprocess_execute(commands[0])
|
||||
|
||||
|
@ -811,10 +805,12 @@ def install_nginx(server_ip, **kwargs):
|
|||
|
||||
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 + "' SSH_PORT=" + ssh_port + " CONFIG_PATH=" + config_path + " CONT_NAME=" + container_name +
|
||||
" STAT_PORT=" + stats_port + " STAT_PAGE=" + stats_page+" SYN_FLOOD=" + syn_flood_protect + " DOCKER=" + docker + " nginx_dir=" + nginx_dir +
|
||||
" HOST=" + server_ip + " USER=" + ssh_user_name + " PASS='" + ssh_user_password + "' KEY=" + ssh_key_name]
|
||||
commands = [
|
||||
"chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv + " STATS_USER=" + stats_user +
|
||||
" STATS_PASS='" + stats_password + "' SSH_PORT=" + ssh_port + " CONFIG_PATH=" + config_path + " CONT_NAME=" + container_name +
|
||||
" STAT_PORT=" + stats_port + " STAT_PAGE=" + stats_page + " SYN_FLOOD=" + syn_flood_protect + " DOCKER=" + docker + " nginx_dir=" + nginx_dir +
|
||||
" HOST=" + server_ip + " USER=" + ssh_user_name + " PASS='" + ssh_user_password + "' KEY=" + ssh_key_name
|
||||
]
|
||||
|
||||
output, error = subprocess_execute(commands[0])
|
||||
if server_for_installing:
|
||||
|
@ -878,7 +874,7 @@ def upload(server_ip, path, file, **kwargs):
|
|||
except Exception as e:
|
||||
error = str(e.args)
|
||||
logging('localhost', error, haproxywi=1)
|
||||
print(' Cannot upload '+file+' to '+full_path+' to server: '+server_ip+' error: ' + error)
|
||||
print(' Cannot upload ' + file + ' to ' + full_path + ' to server: ' + server_ip + ' error: ' + error)
|
||||
return error
|
||||
|
||||
try:
|
||||
|
@ -886,7 +882,7 @@ def upload(server_ip, path, file, **kwargs):
|
|||
except Exception as e:
|
||||
error = str(e.args)
|
||||
logging('localhost', error, haproxywi=1)
|
||||
print('Cannot upload '+file+' to '+full_path+' to server: '+server_ip+' error: ' + error)
|
||||
print('Cannot upload ' + file + ' to ' + full_path + ' to server: ' + server_ip + ' error: ' + error)
|
||||
return error
|
||||
|
||||
try:
|
||||
|
@ -967,7 +963,7 @@ def upload_and_restart(server_ip, cfg, **kwargs):
|
|||
login = 1
|
||||
|
||||
try:
|
||||
os.system("dos2unix "+cfg)
|
||||
os.system("dos2unix " + cfg)
|
||||
except OSError:
|
||||
return 'Please install dos2unix'
|
||||
|
||||
|
@ -1073,26 +1069,19 @@ def master_slave_upload_and_restart(server_ip, cfg, just_save, **kwargs):
|
|||
masters = sql.is_master(server_ip)
|
||||
for master in masters:
|
||||
if master[0] is not None:
|
||||
error = upload_and_restart(master[0],
|
||||
cfg,
|
||||
just_save=just_save,
|
||||
nginx=kwargs.get('nginx'),
|
||||
apache=kwargs.get('apache'),
|
||||
config_file_name=kwargs.get('config_file_name'),
|
||||
slave=1)
|
||||
error = upload_and_restart(
|
||||
master[0], cfg, just_save=just_save, nginx=kwargs.get('nginx'),
|
||||
apache=kwargs.get('apache'), config_file_name=kwargs.get('config_file_name'), slave=1
|
||||
)
|
||||
|
||||
if kwargs.get('login'):
|
||||
login = kwargs.get('login')
|
||||
else:
|
||||
login = ''
|
||||
error = upload_and_restart(server_ip,
|
||||
cfg,
|
||||
just_save=just_save,
|
||||
nginx=kwargs.get('nginx'),
|
||||
apache=kwargs.get('apache'),
|
||||
config_file_name=kwargs.get('config_file_name'),
|
||||
oldcfg=kwargs.get('oldcfg'),
|
||||
login=login)
|
||||
error = upload_and_restart(
|
||||
server_ip, cfg, just_save=just_save, nginx=kwargs.get('nginx'), apache=kwargs.get('apache'),
|
||||
config_file_name=kwargs.get('config_file_name'), oldcfg=kwargs.get('oldcfg'), login=login
|
||||
)
|
||||
|
||||
return error
|
||||
|
||||
|
@ -1116,7 +1105,7 @@ def open_port_firewalld(cfg, server_ip, **kwargs):
|
|||
listen = listen.split(";")[0]
|
||||
try:
|
||||
listen = int(listen)
|
||||
ports += str(listen)+' '
|
||||
ports += str(listen) + ' '
|
||||
firewalld_commands += ' sudo firewall-cmd --zone=public --add-port=%s/tcp --permanent -q &&' % str(listen)
|
||||
except Exception:
|
||||
pass
|
||||
|
@ -1132,7 +1121,7 @@ def open_port_firewalld(cfg, server_ip, **kwargs):
|
|||
try:
|
||||
bind = int(bind)
|
||||
firewalld_commands += ' sudo firewall-cmd --zone=public --add-port=%s/tcp --permanent -q &&' % str(bind)
|
||||
ports += str(bind)+' '
|
||||
ports += str(bind) + ' '
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
|
@ -1192,9 +1181,9 @@ def show_log(stdout, **kwargs):
|
|||
if kwargs.get("html") != 0:
|
||||
i = i + 1
|
||||
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"
|
||||
out += '<div class="'+line_class+'">' + line + '</div>'
|
||||
out += '<div class="' + line_class + '">' + line + '</div>'
|
||||
else:
|
||||
out += line
|
||||
|
||||
|
@ -1216,9 +1205,9 @@ def show_finding_in_config(stdout: str, **kwargs) -> str:
|
|||
for line in stdout:
|
||||
i = i + 1
|
||||
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 = "line" if '--' in line else "line3"
|
||||
out += '<div class="'+line_class+'">' + line + '</div>'
|
||||
out += '<div class="' + line_class + '">' + line + '</div>'
|
||||
|
||||
out += '<div class="line">--</div>'
|
||||
|
||||
|
@ -1229,8 +1218,8 @@ def show_haproxy_log(serv, rows=10, waf='0', grep=None, hour='00', minut='00', h
|
|||
import sql
|
||||
exgrep = form.getvalue('exgrep')
|
||||
log_file = form.getvalue('file')
|
||||
date = hour+':'+minut
|
||||
date1 = hour1+':'+minut1
|
||||
date = hour + ':' + minut
|
||||
date1 = hour1 + ':' + minut1
|
||||
cmd = ''
|
||||
|
||||
if grep is not None:
|
||||
|
@ -1273,11 +1262,11 @@ def show_haproxy_log(serv, rows=10, waf='0', grep=None, hour='00', minut='00', h
|
|||
apache_log_path = sql.get_setting('apache_log_path')
|
||||
|
||||
if serv == 'roxy-wi.access.log':
|
||||
cmd = "sudo cat {}| awk -F\"/|:\" '$3>\"{}:00\" && $3<\"{}:00\"' |tail -{} {} {}".format(apache_log_path+"/"+serv, date, date1, rows, grep_act, exgrep_act)
|
||||
cmd = "sudo cat {}| awk -F\"/|:\" '$3>\"{}:00\" && $3<\"{}:00\"' |tail -{} {} {}".format(apache_log_path + "/" + serv, date, date1, rows, grep_act, exgrep_act)
|
||||
elif serv == 'roxy-wi.error.log':
|
||||
cmd = "sudo cat {}| awk '$4>\"{}:00\" && $4<\"{}:00\"' |tail -{} {} {}".format(apache_log_path+"/"+serv, date, date1, rows, grep_act, exgrep_act)
|
||||
cmd = "sudo cat {}| awk '$4>\"{}:00\" && $4<\"{}:00\"' |tail -{} {} {}".format(apache_log_path + "/" + serv, date, date1, rows, grep_act, exgrep_act)
|
||||
elif serv == 'fail2ban.log':
|
||||
cmd = "sudo cat {}| awk -F\"/|:\" '$3>\"{}:00\" && $3<\"{}:00\"' |tail -{} {} {}".format("/var/log/"+serv, date, date1, rows, grep_act, exgrep_act)
|
||||
cmd = "sudo cat {}| awk -F\"/|:\" '$3>\"{}:00\" && $3<\"{}:00\"' |tail -{} {} {}".format("/var/log/" + serv, date, date1, rows, grep_act, exgrep_act)
|
||||
|
||||
output, stderr = subprocess_execute(cmd)
|
||||
|
||||
|
@ -1318,9 +1307,9 @@ def haproxy_wi_log(**kwargs):
|
|||
selects = get_files(log_path, format="log")
|
||||
for key, value in selects:
|
||||
if kwargs.get('with_date'):
|
||||
log_file = kwargs.get('file')+get_data('logs')+".log"
|
||||
log_file = kwargs.get('file') + get_data('logs') + ".log"
|
||||
else:
|
||||
log_file = kwargs.get('file')+".log"
|
||||
log_file = kwargs.get('file') + ".log"
|
||||
if log_file == value:
|
||||
return key
|
||||
else:
|
||||
|
@ -1330,18 +1319,18 @@ def haproxy_wi_log(**kwargs):
|
|||
group_grep = '|grep "group: ' + user_group + '"'
|
||||
else:
|
||||
group_grep = ''
|
||||
cmd = "find "+log_path+"/roxy-wi-* -type f -exec stat --format '%Y :%y %n' '{}' \; | sort -nr | cut -d: -f2- | head -1 |awk '{print $4}' |xargs tail"+group_grep+"|sort -r"
|
||||
cmd = "find " + log_path + "/roxy-wi-* -type f -exec stat --format '%Y :%y %n' '{}' \; | sort -nr | cut -d: -f2- | head -1 |awk '{print $4}' |xargs tail" + group_grep + "|sort -r"
|
||||
try:
|
||||
output, stderr = subprocess_execute(cmd)
|
||||
return output
|
||||
except:
|
||||
except Exception:
|
||||
return ''
|
||||
|
||||
|
||||
def show_ip(stdout):
|
||||
for line in stdout:
|
||||
if "Permission denied" in line:
|
||||
print('error: '+line)
|
||||
print('error: ' + line)
|
||||
else:
|
||||
print(line)
|
||||
|
||||
|
@ -1395,8 +1384,8 @@ def ssh_command(server_ip, commands, **kwargs):
|
|||
|
||||
for line in stderr.read().decode(encoding='UTF-8'):
|
||||
if line:
|
||||
print("<div class='alert alert-warning'>"+line+"</div>")
|
||||
logging('localhost', ' '+line, haproxywi=1)
|
||||
print("<div class='alert alert-warning'>" + line + "</div>")
|
||||
logging('localhost', ' ' + line, haproxywi=1)
|
||||
|
||||
|
||||
def subprocess_execute(cmd):
|
||||
|
@ -1415,7 +1404,7 @@ def show_backends(server_ip, **kwargs):
|
|||
cmd = 'echo "show backend" |nc %s %s' % (server_ip, hap_sock_p)
|
||||
output, stderr = subprocess_execute(cmd)
|
||||
if stderr:
|
||||
logging('localhost', ' '+stderr, haproxywi=1)
|
||||
logging('localhost', ' ' + stderr, haproxywi=1)
|
||||
if kwargs.get('ret'):
|
||||
ret = list()
|
||||
else:
|
||||
|
@ -1442,7 +1431,7 @@ def get_files(dir=get_config_var('configs', 'haproxy_save_configs_dir'), format=
|
|||
file = set()
|
||||
return_files = set()
|
||||
i = 0
|
||||
for files in sorted(glob.glob(os.path.join(dir, '*.'+format+'*'))):
|
||||
for files in sorted(glob.glob(os.path.join(dir, '*.' + format + '*'))):
|
||||
if format == 'log':
|
||||
file += [(i, files.split('/')[5])]
|
||||
else:
|
||||
|
@ -1471,10 +1460,12 @@ def get_remote_files(server_ip: str, config_dir: str, file_format: str):
|
|||
|
||||
|
||||
def return_nice_path(return_path: str) -> str:
|
||||
if ('nginx' not in return_path and
|
||||
'haproxy' not in return_path and
|
||||
'apache2' not in return_path and
|
||||
'httpd' not in return_path):
|
||||
if (
|
||||
'nginx' not in return_path
|
||||
and 'haproxy' not in return_path
|
||||
and 'apache2' not in return_path
|
||||
and 'httpd' not in return_path
|
||||
):
|
||||
return 'error: The path must contain the name of the service. Check it in Roxy-WI settings'
|
||||
if return_path[-1] != '/':
|
||||
return_path += '/'
|
||||
|
@ -1499,7 +1490,7 @@ def check_new_version(**kwargs):
|
|||
res = ''
|
||||
|
||||
if kwargs.get('service'):
|
||||
last_ver = '_'+kwargs.get('service')
|
||||
last_ver = '_' + kwargs.get('service')
|
||||
else:
|
||||
last_ver = ''
|
||||
|
||||
|
@ -1508,12 +1499,12 @@ def check_new_version(**kwargs):
|
|||
try:
|
||||
if proxy is not None and proxy != '' and proxy != 'None':
|
||||
proxy_dict = {"https": proxy, "http": proxy}
|
||||
response = requests.get('https://roxy-wi.org/update.py?last_ver'+last_ver+'=1', timeout=1, proxies=proxy_dict)
|
||||
requests.get('https://roxy-wi.org/update.py?ver_send='+current_ver, timeout=1, proxies=proxy_dict)
|
||||
response_status = requests.get('https://roxy-wi.org/update.py?user_name='+user_name, timeout=1, proxies=proxy_dict)
|
||||
response = requests.get('https://roxy-wi.org/update.py?last_ver' + last_ver + '=1', timeout=1, proxies=proxy_dict)
|
||||
requests.get('https://roxy-wi.org/update.py?ver_send=' + current_ver, timeout=1, proxies=proxy_dict)
|
||||
response_status = requests.get('https://roxy-wi.org/update.py?user_name=' + user_name, timeout=1, proxies=proxy_dict)
|
||||
else:
|
||||
response = requests.get('https://roxy-wi.org/update.py?last_ver'+last_ver+'=1', timeout=1)
|
||||
requests.get('https://roxy-wi.org/update.py?ver_send='+current_ver, timeout=1)
|
||||
response = requests.get('https://roxy-wi.org/update.py?last_ver' + last_ver + '=1', timeout=1)
|
||||
requests.get('https://roxy-wi.org/update.py?ver_send=' + current_ver, timeout=1)
|
||||
response_status = requests.get('https://roxy-wi.org/update.py?user_name=' + user_name, timeout=1)
|
||||
|
||||
res = response.content.decode(encoding='UTF-8')
|
||||
|
@ -1521,10 +1512,10 @@ def check_new_version(**kwargs):
|
|||
status = response_status.content.decode(encoding='UTF-8')
|
||||
status = status.split(' ')
|
||||
sql.update_user_status(status[0], status[1].strip(), status[2].strip())
|
||||
except:
|
||||
except Exception:
|
||||
pass
|
||||
except requests.exceptions.RequestException as e:
|
||||
logging('localhost', ' '+str(e), haproxywi=1)
|
||||
logging('localhost', ' ' + str(e), haproxywi=1)
|
||||
|
||||
return res
|
||||
|
||||
|
@ -1582,7 +1573,7 @@ def get_users_params(**kwargs):
|
|||
user_id = sql.get_user_id_by_uuid(user_uuid.value)
|
||||
user_services = sql.select_user_services(user_id)
|
||||
token = sql.get_token(user_uuid.value)
|
||||
except:
|
||||
except Exception:
|
||||
print('<meta http-equiv="refresh" content="0; url=/app/login.py">')
|
||||
|
||||
if kwargs.get('virt') and kwargs.get('haproxy'):
|
||||
|
@ -1639,7 +1630,7 @@ def check_is_server_in_group(server_ip):
|
|||
|
||||
def check_service(server_ip, service_name):
|
||||
server_ip = is_ip_or_dns(server_ip)
|
||||
commands = ["systemctl is-active "+service_name]
|
||||
commands = ["systemctl is-active " + service_name]
|
||||
return ssh_command(server_ip, commands)
|
||||
|
||||
|
||||
|
@ -1664,16 +1655,18 @@ def get_services_status():
|
|||
import distro
|
||||
services = []
|
||||
is_in_docker = is_docker()
|
||||
services_name = {'roxy-wi-checker': 'Checker backends master service',
|
||||
'roxy-wi-keep_alive': 'Auto start service',
|
||||
'roxy-wi-metrics': 'Metrics master service',
|
||||
'roxy-wi-portscanner': 'Port scanner service',
|
||||
'roxy-wi-smon': 'Simple monitoring network ports',
|
||||
'roxy-wi-socket': 'Socket service',
|
||||
'prometheus': 'Prometheus service',
|
||||
'grafana-server': 'Grafana service',
|
||||
'fail2ban': 'Fail2ban service',
|
||||
'rabbitmq-server': 'Message broker service'}
|
||||
services_name = {
|
||||
'roxy-wi-checker': 'Checker backends master service',
|
||||
'roxy-wi-keep_alive': 'Auto start service',
|
||||
'roxy-wi-metrics': 'Metrics master service',
|
||||
'roxy-wi-portscanner': 'Port scanner service',
|
||||
'roxy-wi-smon': 'Simple monitoring network ports',
|
||||
'roxy-wi-socket': 'Socket service',
|
||||
'prometheus': 'Prometheus service',
|
||||
'grafana-server': 'Grafana service',
|
||||
'fail2ban': 'Fail2ban service',
|
||||
'rabbitmq-server': 'Message broker service'
|
||||
}
|
||||
for s, v in services_name.items():
|
||||
if is_in_docker:
|
||||
cmd = "sudo supervisorctl status " + s + "|awk '{print $2}'"
|
||||
|
@ -1799,10 +1792,12 @@ def get_system_info(server_ip: str) -> bool:
|
|||
size = str(size) + 'Gb'
|
||||
fs = volume_info['configuration']['mount.fstype']
|
||||
state = volume_info['configuration']['state']
|
||||
disks[volume_name] = {'mount_point': mount_point,
|
||||
'size': size,
|
||||
'fs': fs,
|
||||
'state': state}
|
||||
disks[volume_name] = {
|
||||
'mount_point': mount_point,
|
||||
'size': size,
|
||||
'fs': fs,
|
||||
'state': state
|
||||
}
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
@ -1813,8 +1808,10 @@ def get_system_info(server_ip: str) -> bool:
|
|||
if s['class'] == 'network':
|
||||
if 'children' in s:
|
||||
for net in s['children']:
|
||||
network[net['logicalname']] = {'description': net['description'],
|
||||
'mac': net['serial']}
|
||||
network[net['logicalname']] = {
|
||||
'description': net['description'],
|
||||
'mac': net['serial']
|
||||
}
|
||||
if s['class'] == 'storage':
|
||||
for p, pval in s.items():
|
||||
if isinstance(pval, list):
|
||||
|
@ -1828,10 +1825,12 @@ def get_system_info(server_ip: str) -> bool:
|
|||
size = str(size) + 'Gb'
|
||||
fs = volume_info['configuration']['mount.fstype']
|
||||
state = volume_info['configuration']['state']
|
||||
disks[volume_name] = {'mount_point': mount_point,
|
||||
'size': size,
|
||||
'fs': fs,
|
||||
'state': state}
|
||||
disks[volume_name] = {
|
||||
'mount_point': mount_point,
|
||||
'size': size,
|
||||
'fs': fs,
|
||||
'state': state
|
||||
}
|
||||
for z, n in s.items():
|
||||
if isinstance(n, list):
|
||||
for y in n:
|
||||
|
@ -1865,10 +1864,12 @@ def get_system_info(server_ip: str) -> bool:
|
|||
size = str(size) + 'Gb'
|
||||
fs = q['configuration']['mount.fstype']
|
||||
state = q['configuration']['state']
|
||||
disks[volume_name] = {'mount_point': mount_point,
|
||||
'size': size,
|
||||
'fs': fs,
|
||||
'state': state}
|
||||
disks[volume_name] = {
|
||||
'mount_point': mount_point,
|
||||
'size': size,
|
||||
'fs': fs,
|
||||
'state': state
|
||||
}
|
||||
except Exception as e:
|
||||
print(e)
|
||||
except Exception:
|
||||
|
@ -1888,7 +1889,8 @@ def get_system_info(server_ip: str) -> bool:
|
|||
'mount_point': mount_point,
|
||||
'size': size,
|
||||
'fs': fs,
|
||||
'state': state}
|
||||
'state': state
|
||||
}
|
||||
except Exception:
|
||||
pass
|
||||
for w in o['children']:
|
||||
|
@ -1904,7 +1906,8 @@ def get_system_info(server_ip: str) -> bool:
|
|||
'mount_point': mount_point,
|
||||
'size': size,
|
||||
'fs': fs,
|
||||
'state': state}
|
||||
'state': state
|
||||
}
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
|
@ -1925,7 +1928,8 @@ def get_system_info(server_ip: str) -> bool:
|
|||
'mount_point': mount_point,
|
||||
'size': size,
|
||||
'fs': fs,
|
||||
'state': state}
|
||||
'state': state
|
||||
}
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
|
@ -1956,10 +1960,12 @@ def send_message_to_rabbit(message: str, **kwargs) -> None:
|
|||
rabbit_queue = sql.get_setting('rabbitmq_queue')
|
||||
|
||||
credentials = pika.PlainCredentials(rabbit_user, rabbit_password)
|
||||
parameters = pika.ConnectionParameters(rabbit_host,
|
||||
rabbit_port,
|
||||
rabbit_vhost,
|
||||
credentials)
|
||||
parameters = pika.ConnectionParameters(
|
||||
rabbit_host,
|
||||
rabbit_port,
|
||||
rabbit_vhost,
|
||||
credentials
|
||||
)
|
||||
|
||||
connection = pika.BlockingConnection(parameters)
|
||||
channel = connection.channel()
|
||||
|
|
|
@ -109,7 +109,7 @@ for s in servers:
|
|||
container_name = sql.get_setting('nginx_container_name')
|
||||
cmd = [
|
||||
"docker exec -it " + container_name + " /usr/sbin/nginx -v 2>&1|awk '{print $3}' && "
|
||||
"docker ps -a -f name="+container_name+" --format '{{.Status}}'|tail -1 && ps ax |grep nginx:"
|
||||
"docker ps -a -f name=" + container_name + " --format '{{.Status}}'|tail -1 && ps ax |grep nginx:"
|
||||
"|grep -v grep |wc -l"
|
||||
]
|
||||
try:
|
||||
|
|
235
app/sql.py
235
app/sql.py
|
@ -22,8 +22,10 @@ def out_error(error):
|
|||
def add_user(user, email, password, role, activeuser, group):
|
||||
if password != 'aduser':
|
||||
try:
|
||||
User.insert(username=user, email=email, password=funct.get_hash(password), role=role, activeuser=activeuser,
|
||||
groups=group).execute()
|
||||
User.insert(
|
||||
username=user, email=email, password=funct.get_hash(password), role=role, activeuser=activeuser,
|
||||
groups=group
|
||||
).execute()
|
||||
except Exception as e:
|
||||
out_error(e)
|
||||
return False
|
||||
|
@ -31,8 +33,9 @@ def add_user(user, email, password, role, activeuser, group):
|
|||
return True
|
||||
else:
|
||||
try:
|
||||
User.insert(username=user, email=email, role=role, ldap_user=ldap_user, activeuser=activeuser,
|
||||
groups=group).execute()
|
||||
User.insert(
|
||||
username=user, email=email, role=role, ldap_user=ldap_user, activeuser=activeuser, groups=group
|
||||
).execute()
|
||||
except Exception as e:
|
||||
out_error(e)
|
||||
return False
|
||||
|
@ -301,22 +304,16 @@ def update_hapwi_server(server_id, alert, metrics, active, service_name):
|
|||
out_error(e)
|
||||
|
||||
|
||||
def update_server(hostname, group, typeip, enable, master, server_id, cred, port, desc, haproxy, nginx, apache,
|
||||
firewall, protected):
|
||||
def update_server(
|
||||
hostname, group, typeip, enable, master, server_id, cred, port, desc, haproxy, nginx, apache,
|
||||
firewall, protected
|
||||
):
|
||||
try:
|
||||
server_update = Server.update(hostname=hostname,
|
||||
groups=group,
|
||||
type_ip=typeip,
|
||||
enable=enable,
|
||||
master=master,
|
||||
cred=cred,
|
||||
port=port,
|
||||
desc=desc,
|
||||
haproxy=haproxy,
|
||||
nginx=nginx,
|
||||
apache=apache,
|
||||
firewall_enable=firewall,
|
||||
protected=protected).where(Server.server_id == server_id)
|
||||
server_update = Server.update(
|
||||
hostname=hostname, groups=group, type_ip=typeip, enable=enable, master=master, cred=cred,
|
||||
port=port, desc=desc, haproxy=haproxy, nginx=nginx, apache=apache, firewall_enable=firewall,
|
||||
protected=protected
|
||||
).where(Server.server_id == server_id)
|
||||
server_update.execute()
|
||||
except Exception as e:
|
||||
out_error(e)
|
||||
|
@ -340,16 +337,15 @@ def select_users(**kwargs):
|
|||
elif kwargs.get("id") is not None:
|
||||
query = User.select().where(User.user_id == kwargs.get("id"))
|
||||
elif kwargs.get("group") is not None:
|
||||
query = (User.
|
||||
select(User, UserGroups, Case(0, [((
|
||||
User.last_login_date >= funct.get_data('regular', timedelta_minutes_minus=15)
|
||||
), 0)], 1).
|
||||
alias('last_login')).
|
||||
join(UserGroups, on=(User.user_id == UserGroups.user_id)).
|
||||
where(UserGroups.user_group_id == kwargs.get("group"))
|
||||
)
|
||||
query = (User.select(
|
||||
User, UserGroups, Case(
|
||||
0, [((User.last_login_date >= funct.get_data('regular', timedelta_minutes_minus=15)), 0)], 1
|
||||
).alias('last_login')
|
||||
).join(UserGroups, on=(User.user_id == UserGroups.user_id)).where(
|
||||
UserGroups.user_group_id == kwargs.get("group")
|
||||
))
|
||||
else:
|
||||
query = User.select(User,Case(0, [(
|
||||
query = User.select(User, Case(0, [(
|
||||
(
|
||||
User.last_login_date >= funct.get_data('regular', timedelta_minutes_minus=15)
|
||||
), 0)], 1).alias('last_login')).order_by(User.user_id)
|
||||
|
@ -382,7 +378,7 @@ def select_user_groups(user_id, **kwargs):
|
|||
def check_user_group(user_id, group_id):
|
||||
try:
|
||||
query_res = UserGroups.get((UserGroups.user_id == user_id) & (UserGroups.user_group_id == group_id))
|
||||
except:
|
||||
except Exception:
|
||||
return False
|
||||
else:
|
||||
if query_res.user_id != '':
|
||||
|
@ -393,14 +389,13 @@ def check_user_group(user_id, group_id):
|
|||
|
||||
def select_user_groups_with_names(user_id, **kwargs):
|
||||
if kwargs.get("all") is not None:
|
||||
query = (UserGroups
|
||||
.select(UserGroups.user_group_id, UserGroups.user_id, Groups.name)
|
||||
.join(Groups, on=(UserGroups.user_group_id == Groups.group_id)))
|
||||
query = (UserGroups.select(
|
||||
UserGroups.user_group_id, UserGroups.user_id, Groups.name
|
||||
).join(Groups, on=(UserGroups.user_group_id == Groups.group_id)))
|
||||
else:
|
||||
query = (UserGroups
|
||||
.select(UserGroups.user_group_id, Groups.name)
|
||||
.join(Groups, on=(UserGroups.user_group_id == Groups.group_id))
|
||||
.where(UserGroups.user_id == user_id))
|
||||
query = (UserGroups.select(
|
||||
UserGroups.user_group_id, Groups.name
|
||||
).join(Groups, on=(UserGroups.user_group_id == Groups.group_id)).where(UserGroups.user_id == user_id))
|
||||
try:
|
||||
query_res = query.execute()
|
||||
except Exception as e:
|
||||
|
@ -583,12 +578,10 @@ def write_api_token(user_token, group_id, user_role, user_name):
|
|||
token_ttl = int(get_setting('token_ttl'))
|
||||
|
||||
try:
|
||||
ApiToken.insert(token=user_token,
|
||||
user_name=user_name,
|
||||
user_group_id=group_id,
|
||||
user_role=user_role,
|
||||
create_date=funct.get_data('regular'),
|
||||
expire_date=funct.get_data('regular', timedelta=token_ttl)).execute()
|
||||
ApiToken.insert(
|
||||
token=user_token, user_name=user_name, user_group_id=group_id, user_role=user_role,
|
||||
create_date=funct.get_data('regular'), expire_date=funct.get_data('regular', timedelta=token_ttl)
|
||||
).execute()
|
||||
except Exception as e:
|
||||
out_error(e)
|
||||
|
||||
|
@ -603,10 +596,9 @@ def get_api_token(token):
|
|||
|
||||
|
||||
def get_user_id_by_api_token(token):
|
||||
query = (User
|
||||
.select(User.user_id)
|
||||
.join(ApiToken, on=(ApiToken.user_name == User.username))
|
||||
.where(ApiToken.token == token))
|
||||
query = (User.select(User.user_id).join(ApiToken, on=(
|
||||
ApiToken.user_name == User.username
|
||||
)).where(ApiToken.token == token))
|
||||
try:
|
||||
query_res = query.execute()
|
||||
except Exception as e:
|
||||
|
@ -634,7 +626,7 @@ def get_token(uuid):
|
|||
try:
|
||||
for i in query_res:
|
||||
return i.token
|
||||
except:
|
||||
except Exception:
|
||||
return ''
|
||||
|
||||
|
||||
|
@ -642,7 +634,7 @@ def delete_uuid(uuid):
|
|||
try:
|
||||
query = UUID.delete().where(UUID.uuid == uuid)
|
||||
query.execute()
|
||||
except:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
|
@ -711,10 +703,9 @@ def get_user_id_by_username(username: str):
|
|||
|
||||
|
||||
def get_user_role_by_uuid(uuid):
|
||||
query = (Role.select(Role.role_id)
|
||||
.join(User, on=(Role.name == User.role))
|
||||
.join(UUID, on=(User.user_id == UUID.user_id))
|
||||
.where(UUID.uuid == uuid))
|
||||
query = (Role.select(Role.role_id).join(User, on=(Role.name == User.role)).join(UUID, on=(
|
||||
User.user_id == UUID.user_id
|
||||
)).where(UUID.uuid == uuid))
|
||||
try:
|
||||
query_res = query.execute()
|
||||
except Exception as e:
|
||||
|
@ -804,7 +795,7 @@ def get_dick_permit(**kwargs):
|
|||
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
||||
group = cookie.get('group')
|
||||
grp = group.value
|
||||
except Exception as e:
|
||||
except Exception:
|
||||
print('<meta http-equiv="refresh" content="0; url=/app/login.py">')
|
||||
return
|
||||
if kwargs.get('token'):
|
||||
|
@ -842,13 +833,9 @@ def get_dick_permit(**kwargs):
|
|||
try:
|
||||
if mysql_enable == '1':
|
||||
if grp == '1' and not only_group:
|
||||
sql = """ select * from `servers` where {} {} {} {} {} {} {} order by `pos` asc""".format(disable,
|
||||
type_ip,
|
||||
nginx,
|
||||
haproxy,
|
||||
keepalived,
|
||||
apache,
|
||||
ip)
|
||||
sql = """ select * from `servers` where {} {} {} {} {} {} {} order by `pos` asc""".format(
|
||||
disable, type_ip, nginx, haproxy, keepalived, apache, ip
|
||||
)
|
||||
else:
|
||||
sql = """ select * from `servers` where `groups` = {group} and ({disable}) {type_ip} {ip} {haproxy} {nginx} {keepalived} {apache} order by `pos` asc
|
||||
""".format(
|
||||
|
@ -862,8 +849,10 @@ def get_dick_permit(**kwargs):
|
|||
)
|
||||
else:
|
||||
sql = """ select * from servers where groups = '{group}' and ({disable}) {type_ip} {ip} {haproxy} {nginx} {keepalived} {apache} order by pos
|
||||
""".format(group=grp, disable=disable, type_ip=type_ip, ip=ip, haproxy=haproxy, nginx=nginx,
|
||||
keepalived=keepalived, apache=apache)
|
||||
""".format(
|
||||
group=grp, disable=disable, type_ip=type_ip, ip=ip, haproxy=haproxy, nginx=nginx,
|
||||
keepalived=keepalived, apache=apache
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
print(str(e))
|
||||
|
@ -977,8 +966,10 @@ def select_backups(**kwargs):
|
|||
|
||||
|
||||
def update_backup(server, rserver, rpath, backup_type, time, cred, description, backup_id):
|
||||
backup_update = Backup.update(server=server, rhost=rserver, rpath=rpath, backup_type=backup_type, time=time,
|
||||
cred=cred, description=description).where(Backup.id == backup_id)
|
||||
backup_update = Backup.update(
|
||||
server=server, rhost=rserver, rpath=rpath, backup_type=backup_type, time=time,
|
||||
cred=cred, description=description
|
||||
).where(Backup.id == backup_id)
|
||||
try:
|
||||
backup_update.execute()
|
||||
except Exception as e:
|
||||
|
@ -1002,7 +993,7 @@ def delete_backups(backup_id):
|
|||
def check_exists_backup(server):
|
||||
try:
|
||||
backup = Backup.get(Backup.server == server)
|
||||
except:
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
if backup.id is not None:
|
||||
|
@ -1270,14 +1261,11 @@ def select_waf_servers_metrics(uuid):
|
|||
else:
|
||||
if user_group.groups == '1':
|
||||
query = Waf.select(Server.ip).join(Server, on=(Waf.server_id == Server.server_id)).where(
|
||||
(Server.enable == 1) &
|
||||
(Waf.metrics == 1)
|
||||
(Server.enable == 1) & (Waf.metrics == 1)
|
||||
)
|
||||
else:
|
||||
query = Waf.select(Server.ip).join(Server, on=(Waf.server_id == Server.server_id)).where(
|
||||
(Server.enable == 1) &
|
||||
(Waf.metrics == 1) &
|
||||
(Server.groups == user_group.groups)
|
||||
(Server.enable == 1) & (Waf.metrics == 1) & (Server.groups == user_group.groups)
|
||||
)
|
||||
try:
|
||||
query_res = query.execute()
|
||||
|
@ -1844,7 +1832,7 @@ def select_table_metrics():
|
|||
def get_setting(param, **kwargs):
|
||||
try:
|
||||
user_group = funct.get_user_group(id=1)
|
||||
except:
|
||||
except Exception:
|
||||
user_group = ''
|
||||
|
||||
if user_group == '' or param == 'lists_path' or param == 'ssl_local_path':
|
||||
|
@ -1909,9 +1897,7 @@ def select_roles():
|
|||
def select_alert(**kwargs):
|
||||
if kwargs.get("group") is not None:
|
||||
query = Server.select(Server.ip).where(
|
||||
(Server.alert == 1) &
|
||||
(Server.enable == 1) &
|
||||
(Server.groups == kwargs.get('group'))
|
||||
(Server.alert == 1) & (Server.enable == 1) & (Server.groups == kwargs.get('group'))
|
||||
)
|
||||
else:
|
||||
query = Server.select(Server.ip).where((Server.alert == 1) & (Server.enable == 1))
|
||||
|
@ -1926,9 +1912,8 @@ def select_alert(**kwargs):
|
|||
def select_all_alerts(**kwargs):
|
||||
if kwargs.get("group") is not None:
|
||||
query = Server.select(Server.ip).where(
|
||||
((Server.alert == 1) | (Server.nginx_alert == 1)) &
|
||||
(Server.enable == 1) &
|
||||
(Server.groups == kwargs.get('group')))
|
||||
((Server.alert == 1) | (Server.nginx_alert == 1)) & (Server.enable == 1) & (Server.groups == kwargs.get('group'))
|
||||
)
|
||||
else:
|
||||
query = Server.select(Server.ip).where(((Server.alert == 1) | (Server.nginx_alert == 1)) & (Server.enable == 1))
|
||||
try:
|
||||
|
@ -1942,9 +1927,8 @@ def select_all_alerts(**kwargs):
|
|||
def select_nginx_alert(**kwargs):
|
||||
if kwargs.get("group") is not None:
|
||||
query = Server.select(Server.ip).where(
|
||||
(Server.nginx_alert == 1) &
|
||||
(Server.enable == 1) &
|
||||
(Server.groups == kwargs.get('group')))
|
||||
(Server.nginx_alert == 1) & (Server.enable == 1) & (Server.groups == kwargs.get('group'))
|
||||
)
|
||||
else:
|
||||
query = Server.select(Server.ip).where((Server.nginx_alert == 1) & (Server.enable == 1))
|
||||
try:
|
||||
|
@ -1958,9 +1942,8 @@ def select_nginx_alert(**kwargs):
|
|||
def select_apache_alert(**kwargs):
|
||||
if kwargs.get("group") is not None:
|
||||
query = Server.select(Server.ip).where(
|
||||
(Server.apache_alert == 1) &
|
||||
(Server.enable == 1) &
|
||||
(Server.groups == kwargs.get('group')))
|
||||
(Server.apache_alert == 1) & (Server.enable == 1) & (Server.groups == kwargs.get('group'))
|
||||
)
|
||||
else:
|
||||
query = Server.select(Server.ip).where((Server.apache_alert == 1) & (Server.enable == 1))
|
||||
try:
|
||||
|
@ -1974,9 +1957,8 @@ def select_apache_alert(**kwargs):
|
|||
def select_keepalived_alert(**kwargs):
|
||||
if kwargs.get("group") is not None:
|
||||
query = Server.select(Server.ip).where(
|
||||
(Server.keepalived_alert == 1) &
|
||||
(Server.enable == 1) &
|
||||
(Server.groups == kwargs.get('group')))
|
||||
(Server.keepalived_alert == 1) & (Server.enable == 1) & (Server.groups == kwargs.get('group'))
|
||||
)
|
||||
else:
|
||||
query = Server.select(Server.ip).where((Server.keepalived_alert == 1) & (Server.enable == 1))
|
||||
|
||||
|
@ -2136,14 +2118,14 @@ def check_token_exists(token):
|
|||
return True
|
||||
else:
|
||||
return False
|
||||
except:
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def insert_smon(server, port, enable, proto, uri, body, group, desc, telegram, slack, user_group):
|
||||
try:
|
||||
http = proto + ':' + uri
|
||||
except:
|
||||
except Exception:
|
||||
http = ''
|
||||
|
||||
try:
|
||||
|
@ -2173,7 +2155,7 @@ def select_smon(user_group, **kwargs):
|
|||
if kwargs.get('ip'):
|
||||
try:
|
||||
http = kwargs.get('proto') + ':' + kwargs.get('uri')
|
||||
except:
|
||||
except Exception:
|
||||
http = ''
|
||||
sql = """select id, ip, port, en, http, body, telegram_channel_id, `desc`, `group`, user_group, slack_channel_id from smon
|
||||
where ip='%s' and port='%s' and http='%s' and body='%s' %s
|
||||
|
@ -2454,7 +2436,7 @@ def is_cloud():
|
|||
sql = """ select * from cloud_uuid """
|
||||
try:
|
||||
cursor.execute(sql)
|
||||
except:
|
||||
except Exception:
|
||||
return ""
|
||||
else:
|
||||
for cl_uuid in cursor.fetchall():
|
||||
|
@ -2464,7 +2446,7 @@ def is_cloud():
|
|||
def return_firewall(serv):
|
||||
try:
|
||||
query_res = Server.get(Server.ip == serv).firewall_enable
|
||||
except:
|
||||
except Exception:
|
||||
return False
|
||||
else:
|
||||
return True if query_res == 1 else False
|
||||
|
@ -2486,7 +2468,7 @@ def insert_port_scanner_settings(server_id, user_group_id, enabled, notify, hist
|
|||
server_id=server_id, user_group_id=user_group_id, enabled=enabled, notify=notify, history=history
|
||||
).execute()
|
||||
return True
|
||||
except:
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
|
@ -2589,7 +2571,7 @@ def delete_ports(serv):
|
|||
def insert_port_scanner_history(serv, port, port_status, service_name):
|
||||
try:
|
||||
PortScannerHistory.insert(
|
||||
serv=serv, port=port, status=port_status, service_name=service_name,date=funct.get_data('regular')
|
||||
serv=serv, port=port, status=port_status, service_name=service_name, date=funct.get_data('regular')
|
||||
).execute()
|
||||
except Exception as e:
|
||||
out_error(e)
|
||||
|
@ -2597,8 +2579,8 @@ def insert_port_scanner_history(serv, port, port_status, service_name):
|
|||
|
||||
def delete_alert_history(keep_interval: int, service: str):
|
||||
query = Alerts.delete().where(
|
||||
(Alerts.date < funct.get_data('regular', timedelta_minus=keep_interval)) &
|
||||
(Alerts.service == service))
|
||||
(Alerts.date < funct.get_data('regular', timedelta_minus=keep_interval)) & (Alerts.service == service)
|
||||
)
|
||||
try:
|
||||
query.execute()
|
||||
except Exception as e:
|
||||
|
@ -2640,7 +2622,7 @@ def add_provider_aws(provider_name, provider_group, provider_key, provider_secre
|
|||
try:
|
||||
ProvidersCreds.insert(
|
||||
name=provider_name, type='aws', group=provider_group, key=provider_key, secret=provider_secret,
|
||||
create_date=funct.get_data('regular'),edit_date=funct.get_data('regular')
|
||||
create_date=funct.get_data('regular'), edit_date=funct.get_data('regular')
|
||||
).execute()
|
||||
return True
|
||||
except Exception as e:
|
||||
|
@ -2793,9 +2775,7 @@ def select_do_server(server_id):
|
|||
def update_provisioning_server_status(status, user_group_id, name, provider_id, **kwargs):
|
||||
if kwargs.get('update_ip'):
|
||||
query = ProvisionedServers.update(status=status, IP=kwargs.get('update_ip')).where(
|
||||
(ProvisionedServers.name == name) &
|
||||
(ProvisionedServers.group_id == user_group_id) &
|
||||
(ProvisionedServers.provider_id == provider_id)
|
||||
(ProvisionedServers.name == name) & (ProvisionedServers.group_id == user_group_id) & (ProvisionedServers.provider_id == provider_id)
|
||||
)
|
||||
else:
|
||||
query = ProvisionedServers.update(status=status).where(
|
||||
|
@ -2811,9 +2791,9 @@ def update_provisioning_server_status(status, user_group_id, name, provider_id,
|
|||
|
||||
def update_provisioning_server_gcore_name(name, template_name, user_group_id, provider_id):
|
||||
query = ProvisionedServers.update(name_template=template_name).where(
|
||||
(ProvisionedServers.name == name) &
|
||||
(ProvisionedServers.group_id == user_group_id) &
|
||||
(ProvisionedServers.provider_id == provider_id)
|
||||
(ProvisionedServers.name == name)
|
||||
& (ProvisionedServers.group_id == user_group_id)
|
||||
& (ProvisionedServers.provider_id == provider_id)
|
||||
)
|
||||
try:
|
||||
query.execute()
|
||||
|
@ -2823,9 +2803,9 @@ def update_provisioning_server_gcore_name(name, template_name, user_group_id, pr
|
|||
|
||||
def update_provisioning_server_error(status, user_group_id, name, provider_id):
|
||||
query = ProvisionedServers.update(last_error=status).where(
|
||||
(ProvisionedServers.name == name) &
|
||||
(ProvisionedServers.group_id == user_group_id) &
|
||||
(ProvisionedServers.provider_id == provider_id)
|
||||
(ProvisionedServers.name == name)
|
||||
& (ProvisionedServers.group_id == user_group_id)
|
||||
& (ProvisionedServers.provider_id == provider_id)
|
||||
)
|
||||
try:
|
||||
query.execute()
|
||||
|
@ -2923,7 +2903,7 @@ def select_provisioned_servers(**kwargs):
|
|||
def select_aws_provider(provider_id):
|
||||
try:
|
||||
query_res = ProvidersCreds.get(ProvidersCreds.id == provider_id)
|
||||
except:
|
||||
except Exception:
|
||||
return ""
|
||||
else:
|
||||
return query_res.key, query_res.secret
|
||||
|
@ -2932,7 +2912,7 @@ def select_aws_provider(provider_id):
|
|||
def select_gcore_provider(provider_id):
|
||||
try:
|
||||
query_res = ProvidersCreds.get(ProvidersCreds.id == provider_id)
|
||||
except:
|
||||
except Exception:
|
||||
return ""
|
||||
else:
|
||||
return query_res.key, query_res.secret
|
||||
|
@ -2941,7 +2921,7 @@ def select_gcore_provider(provider_id):
|
|||
def select_do_provider(provider_id):
|
||||
try:
|
||||
query_res = ProvidersCreds.get(ProvidersCreds.id == provider_id)
|
||||
except:
|
||||
except Exception:
|
||||
return ""
|
||||
else:
|
||||
return query_res.key
|
||||
|
@ -2983,7 +2963,7 @@ def update_aws_provider(new_name, new_key, new_secret, provider_id):
|
|||
def is_serv_protected(serv):
|
||||
try:
|
||||
query_res = Server.get(Server.ip == serv)
|
||||
except:
|
||||
except Exception:
|
||||
return ""
|
||||
else:
|
||||
return True if query_res.protected else False
|
||||
|
@ -3031,9 +3011,10 @@ def select_service_settings(server_id: int, service: str) -> str:
|
|||
|
||||
def select_docker_service_settings(server_id: int, service: str) -> str:
|
||||
query = ServiceSetting.select().where(
|
||||
(ServiceSetting.server_id == server_id) &
|
||||
(ServiceSetting.service == service) &
|
||||
(ServiceSetting.setting == 'dockerized'))
|
||||
(ServiceSetting.server_id == server_id)
|
||||
& (ServiceSetting.service == service)
|
||||
& (ServiceSetting.setting == 'dockerized')
|
||||
)
|
||||
try:
|
||||
query_res = query.execute()
|
||||
except Exception as e:
|
||||
|
@ -3044,8 +3025,9 @@ def select_docker_service_settings(server_id: int, service: str) -> str:
|
|||
|
||||
def select_docker_services_settings(service: str) -> str:
|
||||
query = ServiceSetting.select().where(
|
||||
(ServiceSetting.service == service) &
|
||||
(ServiceSetting.setting == 'dockerized'))
|
||||
(ServiceSetting.service == service)
|
||||
& (ServiceSetting.setting == 'dockerized')
|
||||
)
|
||||
try:
|
||||
query_res = query.execute()
|
||||
except Exception as e:
|
||||
|
@ -3057,9 +3039,10 @@ def select_docker_services_settings(service: str) -> str:
|
|||
def select_service_setting(server_id: int, service: str, setting: str) -> str:
|
||||
try:
|
||||
result = ServiceSetting.get(
|
||||
(ServiceSetting.server_id == server_id) &
|
||||
(ServiceSetting.service == service) &
|
||||
(ServiceSetting.setting == setting)).value
|
||||
(ServiceSetting.server_id == server_id)
|
||||
& (ServiceSetting.service == service)
|
||||
& (ServiceSetting.setting == setting)
|
||||
).value
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
|
@ -3121,8 +3104,8 @@ def select_action_history_by_user_id(user_id: int):
|
|||
|
||||
def select_action_history_by_server_id_and_service(server_id: int, service: str):
|
||||
query = ActionHistory.select().where(
|
||||
(ActionHistory.server_id == server_id) &
|
||||
(ActionHistory.service == service)
|
||||
(ActionHistory.server_id == server_id)
|
||||
& (ActionHistory.service == service)
|
||||
)
|
||||
try:
|
||||
query_res = query.execute()
|
||||
|
@ -3150,8 +3133,8 @@ def insert_config_version(server_id: int, user_id: int, service: str, local_path
|
|||
def select_config_version(server_ip: str, service: str) -> str:
|
||||
server_id = select_server_id_by_ip(server_ip)
|
||||
query = ConfigVersion.select().where(
|
||||
(ConfigVersion.server_id == server_id) &
|
||||
(ConfigVersion.service == service)
|
||||
(ConfigVersion.server_id == server_id)
|
||||
& (ConfigVersion.service == service)
|
||||
)
|
||||
try:
|
||||
query_res = query.execute()
|
||||
|
@ -3163,8 +3146,8 @@ def select_config_version(server_ip: str, service: str) -> str:
|
|||
|
||||
def delete_config_version(service: str, local_path: str):
|
||||
query_res = ConfigVersion.delete().where(
|
||||
(ConfigVersion.service == service) &
|
||||
(ConfigVersion.local_path == local_path)
|
||||
(ConfigVersion.service == service)
|
||||
& (ConfigVersion.local_path == local_path)
|
||||
)
|
||||
try:
|
||||
query_res.execute()
|
||||
|
@ -3179,9 +3162,9 @@ def select_remote_path_from_version(server_ip: str, service: str, local_path: st
|
|||
server_id = select_server_id_by_ip(server_ip)
|
||||
try:
|
||||
query_res = ConfigVersion.get(
|
||||
(ConfigVersion.server_id == server_id) &
|
||||
(ConfigVersion.service == service) &
|
||||
(ConfigVersion.local_path == local_path)
|
||||
(ConfigVersion.server_id == server_id)
|
||||
& (ConfigVersion.service == service)
|
||||
& (ConfigVersion.local_path == local_path)
|
||||
).remote_path
|
||||
except Exception as e:
|
||||
out_error(e)
|
||||
|
|
|
@ -27,7 +27,7 @@ if configver:
|
|||
|
||||
try:
|
||||
user, user_id, role, token, servers, user_services = funct.get_users_params(disable=1)
|
||||
except:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if service == 'keepalived':
|
||||
|
@ -78,8 +78,9 @@ if serv is not None and form.getvalue('del') is not None:
|
|||
os.remove(os.path.join(configs_dir, form.getvalue(get)))
|
||||
try:
|
||||
file.add(form.getvalue(get) + "<br />")
|
||||
funct.logging(serv, "Version of config has been deleted: %s" % form.getvalue(get), login=1,
|
||||
keep_history=1, service=service)
|
||||
funct.logging(
|
||||
serv, "Version of config has been deleted: %s" % form.getvalue(get), login=1, keep_history=1, service=service
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except OSError as e:
|
||||
|
|
|
@ -65,22 +65,9 @@ if form.getvalue('type') is None:
|
|||
selects.append(['roxy-wi.error.log', 'error.log'])
|
||||
selects.append(['roxy-wi.access.log', 'access.log'])
|
||||
|
||||
output_from_parsed_template = template.render(h2=1,
|
||||
autorefresh=1,
|
||||
title="View internal logs",
|
||||
role=role,
|
||||
user=user,
|
||||
serv=serv,
|
||||
select_id="viewlogs",
|
||||
selects=selects,
|
||||
rows=rows,
|
||||
grep=grep,
|
||||
exgrep=exgrep,
|
||||
hour=hour,
|
||||
hour1=hour1,
|
||||
minut=minut,
|
||||
minut1=minut1,
|
||||
page=page,
|
||||
user_services=user_services,
|
||||
token=token)
|
||||
print(output_from_parsed_template)
|
||||
rendered_template = template.render(
|
||||
h2=1, autorefresh=1, title="View internal logs", role=role, user=user, serv=serv, select_id="viewlogs",
|
||||
selects=selects, rows=rows, grep=grep, exgrep=exgrep, hour=hour, hour1=hour1, minut=minut,
|
||||
minut1=minut1, page=page, user_services=user_services, token=token
|
||||
)
|
||||
print(rendered_template)
|
||||
|
|
|
@ -1,25 +1,19 @@
|
|||
#!/usr/bin/env python3
|
||||
import funct
|
||||
import sql
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
env = Environment(loader=FileSystemLoader('templates/'), autoescape=True)
|
||||
template = env.get_template('viewstats.html')
|
||||
form = funct.form
|
||||
serv = form.getvalue('serv')
|
||||
service = form.getvalue('service')
|
||||
|
||||
serv = form.getvalue('serv')
|
||||
service = form.getvalue('service')
|
||||
|
||||
print('Content-type: text/html\n')
|
||||
funct.check_login()
|
||||
|
||||
if service == 'nginx':
|
||||
if funct.check_login(service=2):
|
||||
title = 'NGINX stats page'
|
||||
else:
|
||||
if funct.check_login(service=1):
|
||||
title = 'HAProxy stats page'
|
||||
|
||||
try:
|
||||
user, user_id, role, token, servers, user_services = funct.get_users_params(virt=1, haproxy=1)
|
||||
|
||||
|
||||
if serv is None:
|
||||
first_serv = servers
|
||||
for i in first_serv:
|
||||
|
@ -28,18 +22,20 @@ try:
|
|||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
output_from_parsed_template = template.render(h2=1,
|
||||
autorefresh=1,
|
||||
title=title,
|
||||
role=role,
|
||||
user=user,
|
||||
onclick="showStats()",
|
||||
select_id="serv",
|
||||
selects=servers,
|
||||
serv=serv,
|
||||
service=service,
|
||||
user_services=user_services,
|
||||
token=token)
|
||||
print(output_from_parsed_template)
|
||||
if service == 'nginx':
|
||||
if funct.check_login(service=2):
|
||||
title = 'NGINX stats page'
|
||||
servers = sql.get_dick_permit(nginx=1)
|
||||
elif service == 'apache':
|
||||
if funct.check_login(service=4):
|
||||
title = 'Apache stats page'
|
||||
servers = sql.get_dick_permit(apache=1)
|
||||
else:
|
||||
if funct.check_login(service=1):
|
||||
title = 'HAProxy stats page'
|
||||
|
||||
rendered_template = template.render(
|
||||
h2=1, autorefresh=1, title=title, role=role, user=user, onclick="showStats()", select_id="serv",
|
||||
selects=servers, serv=serv, service=service, user_services=user_services, token=token
|
||||
)
|
||||
print(rendered_template)
|
||||
|
|
Loading…
Reference in New Issue