From 498153ef1ab6786533e11535d490b927e12ee7ac Mon Sep 17 00:00:00 2001 From: Pavel Loginov Date: Mon, 17 Oct 2022 13:22:58 +0300 Subject: [PATCH] v6.2.2.0 Changelog: https://roxy-wi.org/changelog#6_2_2 --- .gitignore | 1 + app/create_db.py | 2 +- app/funct.py | 157 +++---- app/options.py | 438 ++++++++---------- app/scripts/ansible/roles/apache_exporter.yml | 24 + .../roles/apache_exporter/defaults/main.yml | 8 + .../roles/apache_exporter/handlers/main.yml | 7 + .../roles/apache_exporter/tasks/main.yml | 81 ++++ .../roles/apache_exporter/tasks/preflight.yml | 7 + .../templates/apache_exporter.service.j2 | 20 + .../roles/apache_exporter/vars/main.yml | 10 + app/scripts/install_apache_exporter.sh | 54 +++ app/templates/ajax/bin_bout.html | 37 +- app/templates/hapservers.html | 11 +- app/templates/include/mon_installation.html | 31 ++ config_other/requirements_deb.txt | 1 + inc/overview.js | 22 + inc/users.js | 68 ++- 18 files changed, 618 insertions(+), 361 deletions(-) create mode 100644 app/scripts/ansible/roles/apache_exporter.yml create mode 100644 app/scripts/ansible/roles/apache_exporter/defaults/main.yml create mode 100644 app/scripts/ansible/roles/apache_exporter/handlers/main.yml create mode 100644 app/scripts/ansible/roles/apache_exporter/tasks/main.yml create mode 100644 app/scripts/ansible/roles/apache_exporter/tasks/preflight.yml create mode 100644 app/scripts/ansible/roles/apache_exporter/templates/apache_exporter.service.j2 create mode 100644 app/scripts/ansible/roles/apache_exporter/vars/main.yml create mode 100644 app/scripts/install_apache_exporter.sh diff --git a/.gitignore b/.gitignore index d59a138b..ae60cac3 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ roxy-wi.db configs keys log +.DS_Store diff --git a/app/create_db.py b/app/create_db.py index 28f05f13..f66ef491 100644 --- a/app/create_db.py +++ b/app/create_db.py @@ -961,7 +961,7 @@ def update_db_v_6_2_1(): except Exception as e: print("An error occurred:", e) else: - print("Updating... DB has been updated to version 6.2.1.0") + print("Updating... DB has been updated to version 6.2.2.0") def update_ver(): diff --git a/app/funct.py b/app/funct.py index 328f57ef..a5b63be4 100644 --- a/app/funct.py +++ b/app/funct.py @@ -5,6 +5,8 @@ import re import json import http.cookies +import distro + import modules.roxy_wi_tools as roxy_wi_tools get_config_var = roxy_wi_tools.GetConfigVar() @@ -115,7 +117,6 @@ def get_user_group(**kwargs) -> str: def logging(server_ip: str, action: str, **kwargs) -> None: import sql - import distro login = '' cur_date = get_data('logs') @@ -335,13 +336,10 @@ def page_for_admin(level=1) -> None: return -def return_ssh_keys_path(server_ip: str, **kwargs): +def return_ssh_keys_path(server_ip: str, **kwargs) -> dict: import sql lib_path = get_config_var.get_config_var('main', 'lib_path') - ssh_enable = '' - ssh_user_name = '' - ssh_user_password = '' - ssh_key_name = '' + ssh_settings = {} if kwargs.get('id'): sshs = sql.select_ssh(id=kwargs.get('id')) @@ -349,26 +347,24 @@ def return_ssh_keys_path(server_ip: str, **kwargs): sshs = sql.select_ssh(serv=server_ip) for ssh in sshs: - ssh_enable = ssh.enable - ssh_user_name = ssh.username - ssh_user_password = ssh.password - ssh_key_name = f'{lib_path}/keys/{ssh.name}.pem' + ssh_settings.setdefault('enabled', ssh.enable) + ssh_settings.setdefault('user', ssh.username) + ssh_settings.setdefault('password', ssh.password) + ssh_settings.setdefault('key', f'{lib_path}/keys/{ssh.name}.pem') - return ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name + ssh_port = [str(server[10]) for server in sql.select_servers(server=server_ip)] + ssh_settings.setdefault('port', ssh_port[0]) + + return ssh_settings def ssh_connect(server_ip): - import sql from modules import ssh_connection - ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = return_ssh_keys_path(server_ip) - servers = sql.select_servers(server=server_ip) - ssh_port = 22 + ssh_settings = return_ssh_keys_path(server_ip) - for server in servers: - ssh_port = server[10] - - ssh = ssh_connection.SshConnection(server_ip, ssh_port, ssh_user_name, ssh_user_password, ssh_enable, ssh_key_name) + ssh = ssh_connection.SshConnection(server_ip, ssh_settings['port'], ssh_settings['user'], + ssh_settings['password'], ssh_settings['enabled'], ssh_settings['key']) return ssh @@ -629,15 +625,8 @@ def install_haproxy(server_ip, **kwargs): haproxy_ver = kwargs.get('hapver') server_for_installing = kwargs.get('server') docker = kwargs.get('docker') - ssh_port = 22 - ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = return_ssh_keys_path(server_ip) - - if ssh_enable == 0: - ssh_key_name = '' - - servers = sql.select_servers(server=server_ip) - for server in servers: - ssh_port = str(server[10]) + proxy_serv = '' + ssh_settings = return_ssh_keys_path(server_ip) os.system("cp scripts/%s ." % script) @@ -646,17 +635,15 @@ def install_haproxy(server_ip, **kwargs): if proxy is not None and proxy != '' and proxy != 'None': proxy_serv = proxy - else: - proxy_serv = '' 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 + f"chmod +x {script} && ./{script} PROXY={proxy_serv} SOCK_PORT={hap_sock_p} STAT_PORT={stats_port} " + f"STAT_FILE={server_state_file} DOCKER={docker} SSH_PORT={ssh_settings['port']} STATS_USER={stats_user} " + f"CONT_NAME={container_name} HAP_DIR={haproxy_dir} STATS_PASS='{stats_password}' HAPVER={haproxy_ver} " + f"SYN_FLOOD={syn_flood_protect} HOST={server_ip} USER={ssh_settings['user']} PASS='{ssh_settings['password']}' " + f"KEY={ssh_settings['key']}" ] output, error = subprocess_execute(commands[0]) @@ -673,7 +660,7 @@ def install_haproxy(server_ip, **kwargs): sql.insert_or_update_service_setting(server_id, 'haproxy', 'dockerized', '1') sql.insert_or_update_service_setting(server_id, 'haproxy', 'restart', '1') - os.system("rm -f %s" % script) + os.remove(script) def waf_install(server_ip): @@ -683,23 +670,18 @@ def waf_install(server_ip): haproxy_dir = sql.get_setting('haproxy_dir') ver = check_haproxy_version(server_ip) service = ' WAF' - ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = return_ssh_keys_path(server_ip) - ssh_port = '22' - - if ssh_enable == 0: - ssh_key_name = '' + proxy_serv = '' + ssh_settings = return_ssh_keys_path(server_ip) os.system("cp scripts/%s ." % script) if proxy is not None and proxy != '' and proxy != 'None': proxy_serv = proxy - 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 + f"chmod +x {script} && ./{script} PROXY={proxy_serv} HAPROXY_PATH={haproxy_dir} VERSION='{ver}' " + f"SSH_PORT={ssh_settings['port']} HOST={server_ip} USER={ssh_settings['user']} PASS='{ssh_settings['password']}' " + f"KEY={ssh_settings['key']}" ] output, error = subprocess_execute(commands[0]) @@ -708,7 +690,7 @@ def waf_install(server_ip): sql.insert_waf_metrics_enable(server_ip, "0") sql.insert_waf_rules(server_ip) - os.system("rm -f %s" % script) + os.remove(script) def waf_nginx_install(server_ip): @@ -717,23 +699,17 @@ def waf_nginx_install(server_ip): proxy = sql.get_setting('proxy') nginx_dir = sql.get_setting('nginx_dir') service = ' WAF' - ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = return_ssh_keys_path(server_ip) - ssh_port = '22' - - if ssh_enable == 0: - ssh_key_name = '' + proxy_serv = '' + ssh_settings = return_ssh_keys_path(server_ip) os.system("cp scripts/%s ." % script) if proxy is not None and proxy != '' and proxy != 'None': proxy_serv = proxy - else: - proxy_serv = '' commands = [ - "chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv + " NGINX_PATH=" + nginx_dir - + " SSH_PORT=" + ssh_port + " HOST=" + server_ip - + " USER=" + ssh_user_name + " PASS='" + ssh_user_password + "' KEY=" + ssh_key_name + f"chmod +x {script} && ./{script} PROXY={proxy_serv} NGINX_PATH={nginx_dir} SSH_PORT={ssh_settings['port']} " + f"HOST={server_ip} USER={ssh_settings['user']} PASS='{ssh_settings['password']}' KEY={ssh_settings['key']}" ] output, error = subprocess_execute(commands[0]) @@ -742,7 +718,7 @@ def waf_nginx_install(server_ip): sql.insert_nginx_waf_rules(server_ip) sql.insert_waf_nginx_server(server_ip) - os.system("rm -f %s" % script) + os.remove(script) def install_nginx(server_ip, **kwargs): @@ -758,30 +734,21 @@ def install_nginx(server_ip, **kwargs): proxy = sql.get_setting('proxy') docker = kwargs.get('docker') container_name = sql.get_setting('nginx_container_name') - ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = return_ssh_keys_path(server_ip) - ssh_port = '22' - - if ssh_enable == 0: - ssh_key_name = '' + proxy_serv = '' + ssh_settings = return_ssh_keys_path(server_ip) os.system("cp scripts/%s ." % script) if proxy is not None and proxy != '' and proxy != 'None': proxy_serv = proxy - else: - proxy_serv = '' - - servers = sql.select_servers(server=server_ip) - for server in servers: - ssh_port = str(server[10]) 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 + f"chmod +x {script} && ./{script} PROXY={proxy_serv} STATS_USER={stats_user} STATS_PASS='{stats_password}' " + f"SSH_PORT={ssh_settings['port']} CONFIG_PATH={config_path} CONT_NAME={container_name} STAT_PORT={stats_port} " + f"STAT_PAGE={stats_page} SYN_FLOOD={syn_flood_protect} DOCKER={docker} nginx_dir={nginx_dir} HOST={server_ip} " + f"USER={ssh_settings['user']} PASS='{ssh_settings['password']}' KEY={ssh_settings['key']}" ] output, error = subprocess_execute(commands[0]) @@ -797,11 +764,10 @@ def install_nginx(server_ip, **kwargs): sql.insert_or_update_service_setting(server_id, 'nginx', 'dockerized', '1') sql.insert_or_update_service_setting(server_id, 'nginx', 'restart', '1') - os.system("rm -f %s" % script) + os.remove(script) def update_roxy_wi(service): - import distro restart_service = '' if distro.id() == 'ubuntu': @@ -1273,8 +1239,7 @@ def show_roxy_log( serv = value break else: - print('Haha') - return + return 'Haha' if serv == 'backup.log': awk_column = 2 @@ -1286,35 +1251,35 @@ def show_roxy_log( return show_log(output, grep=grep) -def roxy_wi_log(**kwargs) -> str: +def roxy_wi_log(**kwargs) -> list: log_path = get_config_var.get_config_var('main', 'log_path') if kwargs.get('log_id'): selects = get_files(log_path, "log") for key, value in selects: - log_file = kwargs.get('file') + ".log" + log_file = f"{kwargs.get('file')}.log" if log_file == value: return key else: user_group_id = get_user_group(id=1) if user_group_id != 1: user_group = get_user_group() - group_grep = '|grep "group: ' + user_group + '"' + group_grep = f'|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 = f"find {log_path}/roxy-wi-* -type f -exec stat --format '%Y :%y %n' '{{}}' \; | sort -nr | cut -d: -f2- " \ + f"| head -1 |awk '{{print $4}}' |xargs tail {group_grep}|sort -r" try: output, stderr = subprocess_execute(cmd) return output except Exception: - return '' + return [''] def show_ip(stdout): for line in stdout: if "Permission denied" in line: - print('error: ' + line) + print(f'error: {line}') else: print(line) @@ -1447,6 +1412,7 @@ def return_nice_path(return_path: str) -> str: and 'keepalived' 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 += '/' @@ -1582,6 +1548,8 @@ def get_users_params(**kwargs): servers = sql.get_dick_permit(disable=0) elif kwargs.get('haproxy'): servers = sql.get_dick_permit(haproxy=1) + elif kwargs.get('service'): + servers = sql.get_dick_permit(service=kwargs.get('service')) else: servers = sql.get_dick_permit() @@ -1642,7 +1610,7 @@ def check_service(server_ip, service_name): return ssh_command(server_ip, commands) -def get_service_version(server_ip, service_name): +def get_service_version(server_ip: str, service_name: str) -> str: server_ip = is_ip_or_dns(server_ip) if service_name == 'haproxy_exporter': commands = ["/opt/prometheus/exporters/haproxy_exporter --version 2>&1 |head -1|awk '{print $3}'"] @@ -1650,6 +1618,8 @@ def get_service_version(server_ip, service_name): commands = ["/opt/prometheus/exporters/nginx_exporter 2>&1 |head -1 |awk -F\"=\" '{print $2}'|awk '{print $1}'"] elif service_name == 'node_exporter': commands = ["node_exporter --version 2>&1 |head -1|awk '{print $3}'"] + elif service_name == 'apache_exporter': + commands = ["/opt/prometheus/exporters/apache_exporter --version 2>&1 |head -1|awk '{print $3}'"] ver = ssh_command(server_ip, commands) @@ -1660,7 +1630,6 @@ def get_service_version(server_ip, service_name): def get_services_status(): - import distro services = [] is_in_docker = is_docker() services_name = { @@ -1678,10 +1647,12 @@ def get_services_status(): } for s, v in services_name.items(): if is_in_docker: - cmd = "sudo supervisorctl status " + s + "|awk '{print $2}'" + cmd = f"sudo supervisorctl status {s}|awk '{{print $2}}'" else: - cmd = "systemctl is-active %s" % s + cmd = f"systemctl is-active {s}" + status, stderr = subprocess_execute(cmd) + if s != 'roxy-wi-keep_alive': service_name = s.split('_')[0] if s == 'grafana-server': @@ -1695,9 +1666,9 @@ def get_services_status(): cmd = "prometheus --version 2>&1 |grep prometheus|awk '{print $3}'" else: if distro.id() == 'ubuntu': - cmd = "apt list --installed 2>&1 |grep " + service_name + "|awk '{print $2}'|sed 's/-/./'" + cmd = f"apt list --installed 2>&1 |grep {service_name}|awk '{{print $2}}'|sed 's/-/./'" else: - cmd = "rpm -q " + service_name + "|awk -F\"" + service_name + "\" '{print $2}' |awk -F\".noa\" '{print $1}' |sed 's/-//1' |sed 's/-/./'" + cmd = f"rpm -q {service_name}|awk -F\"{service_name}\" '{{print $2}}' |awk -F\".noa\" '{{print $1}}' |sed 's/-//1' |sed 's/-/./'" service_ver, stderr = subprocess_execute(cmd) try: @@ -2073,9 +2044,9 @@ def send_email(email_to: str, subject: str, message: str) -> None: smtp_obj.starttls() smtp_obj.login(mail_smtp_user, mail_smtp_password) smtp_obj.send_message(msg) - logging('localhost', 'An email has been sent to ' + email_to, haproxywi=1) + logging('localhost', f'An email has been sent to {email_to}', haproxywi=1) except Exception as e: - logging('localhost', 'error: unable to send email: ' + str(e), haproxywi=1) + logging('localhost', f'error: unable to send email: {e}', haproxywi=1) def send_email_to_server_group(subject: str, mes: str, group_id: int) -> None: @@ -2087,7 +2058,7 @@ def send_email_to_server_group(subject: str, mes: str, group_id: int) -> None: for user_email in users_email: send_email(user_email.email, subject, mes) except Exception as e: - logging('localhost', 'error: unable to send email: ' + str(e), haproxywi=1) + logging('localhost', f'error: unable to send email: {e}', haproxywi=1) def alert_routing( diff --git a/app/options.py b/app/options.py index e17bff51..a93e0b2f 100644 --- a/app/options.py +++ b/app/options.py @@ -5,6 +5,7 @@ import sys import json import http.cookies +import requests from jinja2 import Environment, FileSystemLoader import funct @@ -121,7 +122,7 @@ if serv and form.getvalue('ssl_cert'): except Exception as e: funct.logging('localhost', e.args[0], haproxywi=1) try: - os.system("mv %s %s" % (name, cert_local_dir)) + os.rename(name, cert_local_dir) except OSError as e: funct.logging('localhost', e.args[0], haproxywi=1) @@ -456,17 +457,17 @@ if form.getvalue('action_hap') is not None and serv is not None: if is_docker == '1': container_name = sql.get_setting('haproxy_container_name') - commands = ["sudo docker %s %s" % (action, container_name)] + commands = [f"sudo docker {action} {container_name}"] else: haproxy_enterprise = sql.select_service_setting(server_id, 'haproxy', 'haproxy_enterprise') if haproxy_enterprise == '1': haproxy_service_name = "hapee-2.0-lb" - commands = ["sudo systemctl %s %s" % (action, haproxy_service_name)] + commands = [f"sudo systemctl {action} {haproxy_service_name}"] funct.ssh_command(serv, commands) - funct.logging(serv, 'Service has been ' + action + 'ed', haproxywi=1, login=1, keep_history=1, + funct.logging(serv, f'Service has been {action}ed', haproxywi=1, login=1, keep_history=1, service='haproxy') - print("success: HAProxy has been %s" % action) + print(f"success: HAProxy has been {action}") else: print("error: Bad config, check please") @@ -651,17 +652,17 @@ if form.getvalue('show_userlists'): except Exception as e: funct.logging('localhost', str(e), haproxywi=1) try: - cfg = configs_dir + serv + "-" + funct.get_data('config') + '.' + format_file + cfg = f'{configs_dir}{serv}-{funct.get_data("config")}.{format_file}' except Exception as e: - funct.logging('localhost', ' Cannot generate a cfg path ' + str(e), haproxywi=1) + funct.logging('localhost', f' Cannot generate a cfg path {e}', haproxywi=1) try: error = funct.get_config(serv, cfg) except Exception as e: - funct.logging('localhost', ' Cannot download a config ' + str(e), haproxywi=1) + funct.logging('localhost', f' Cannot download a config {e}', haproxywi=1) try: sections = funct.get_userlists(cfg) except Exception as e: - funct.logging('localhost', ' Cannot get Userlists from the config file ' + str(e), haproxywi=1) + funct.logging('localhost', f' Cannot get Userlists from the config file {e}', haproxywi=1) sections = 'error: Cannot get Userlists' print(sections) @@ -704,20 +705,19 @@ if act == "overview": except Exception: waf_len = 0 - if haproxy == 1: - cmd = 'echo "show info" |nc %s %s -w 1|grep -e "Process_num"' % ( - serv2, sql.get_setting('haproxy_sock_port')) + if haproxy: + cmd = f'echo "show info" |nc {serv2} {sql.get_setting("haproxy_sock_port")} -w 1|grep -e "Process_num"' haproxy_process = funct.server_status(funct.subprocess_execute(cmd)) - if nginx == 1: - nginx_cmd = 'echo "something" |nc %s %s -w 1' % (serv2, sql.get_setting('nginx_stats_port')) + if nginx: + nginx_cmd = f'echo "something" |nc {serv2} {sql.get_setting("nginx_stats_port")} -w 1' nginx_process = funct.server_status(funct.subprocess_execute(nginx_cmd)) - if apache == 1: - apache_cmd = 'echo "something" |nc %s %s -w 1' % (serv2, sql.get_setting('apache_stats_port')) + if apache: + apache_cmd = f'echo "something" |nc {serv2} {sql.get_setting("apache_stats_port")} -w 1' apache_process = funct.server_status(funct.subprocess_execute(apache_cmd)) - if keepalived == 1: + if keepalived: command = ["ps ax |grep keepalived|grep -v grep|wc -l|tr -d '\n'"] try: keepalived_process = funct.ssh_command(serv2, command) @@ -893,8 +893,6 @@ if act == "overviewServers": ioloop.close() if form.getvalue('action'): - import requests - haproxy_user = sql.get_setting('stats_user') haproxy_pass = sql.get_setting('stats_password') stats_port = sql.get_setting('stats_port') @@ -919,8 +917,6 @@ if form.getvalue('action'): auth=(haproxy_user, haproxy_pass)) if serv is not None and act == "stats": - import requests - if form.getvalue('service') == 'nginx': haproxy_user = sql.get_setting('nginx_stats_user') haproxy_pass = sql.get_setting('nginx_stats_password') @@ -1004,7 +1000,7 @@ if form.getvalue('viewlogs') is not None: if funct.check_user_group(): out = funct.show_roxy_log(serv=viewlog, rows=rows, waf='0', grep=grep, hour=hour, minut=minut, hour1=hour1, minut1=minut1, service='internal') - print(out) + print(out) if serv is not None and act == "showMap": import networkx as nx @@ -1017,10 +1013,9 @@ if serv is not None and act == "showMap": haproxy_config_path = sql.get_setting('haproxy_config_path') hap_configs_dir = get_config.get_config_var('configs', 'haproxy_save_configs_dir') date = funct.get_data('config') - cfg = hap_configs_dir + serv + "-" + date + ".cfg" + cfg = f'{hap_configs_dir}{serv}-{date}.cfg' - print('
') - print("

Map from %s


" % serv) + print(f'

Map from {serv}

') error = funct.get_config(serv, cfg) if error: @@ -1028,7 +1023,8 @@ if serv is not None and act == "showMap": try: conf = open(cfg, "r") except IOError: - print('error: Can\'t read import config file') + print('error: Cannot read import config file') + sys.exit() G = nx.DiGraph() node = "" @@ -1090,8 +1086,8 @@ if serv is not None and act == "showMap": sections['backends'][node]['servers'][line_new[0]] = {line_new[0]: backend_server_port} except Exception: pass - - os.system("/bin/rm -f " + cfg) + conf.close() + os.remove(cfg) i, k, j = 0, 0, 0 backend_servers_len_dict = 1 @@ -1215,11 +1211,8 @@ if serv is not None and act == "showMap": except Exception as e: print(str(e)) - cmd = "rm -f " + os.path.dirname(os.getcwd()) + "/map*.png && mv map.png " + os.path.dirname( - os.getcwd()) + "/map" + date + ".png" - output, stderr = funct.subprocess_execute(cmd) - print(stderr) - print('map' % date) + os.system(f"rm -f {os.path.dirname(os.getcwd())}/map*.png && mv map.png {os.path.dirname(os.getcwd())}/map{date}.png") + print(f'map
') if form.getvalue('servaction') is not None: server_state_file = sql.get_setting('server_state_file') @@ -1227,22 +1220,23 @@ if form.getvalue('servaction') is not None: enable = funct.checkAjaxInput(form.getvalue('servaction')) backend = funct.checkAjaxInput(form.getvalue('servbackend')) - cmd = 'echo "{} {}" |sudo socat stdio {}'.format(enable, backend, haproxy_sock) + cmd = f'echo "{enable} {backend}" |sudo socat stdio {haproxy_sock}' if form.getvalue('save') == "on": - save_command = 'echo "show servers state" | sudo socat {} stdio > {}'.format(haproxy_sock, server_state_file) + save_command = f'echo "show servers state" | sudo socat {haproxy_sock} stdio > {server_state_file}' command = [cmd + ';' + save_command] else: command = [cmd] if enable != "show": - funct.logging(serv, 'Has been ' + enable + 'ed ' + backend, login=1, keep_history=1, service='haproxy') + funct.logging(serv, f'Has been {enable}ed {backend}', login=1, keep_history=1, service='haproxy') print( - '

You %s %s on HAProxy %s. Look it or Edit something else


' % ( - enable, backend, serv, serv)) + f'

You {enable} {backend} on HAProxy {serv}. Look it or Edit something else


') print(funct.ssh_command(serv, command, show_log="1")) - action = 'runtimeapi.py ' + enable + ' ' + backend + action = f'runtimeapi.py {enable} {backend}' funct.logging(serv, action) if act == "showCompareConfigs": @@ -1344,9 +1338,10 @@ if serv is not None and act == "configShow": is_serv_protected=is_serv_protected, is_restart=is_restart) print(template) + conf.close() if form.getvalue('configver') is None: - os.system("/bin/rm -f " + cfg) + os.remove(cfg) if act == 'configShowFiles': service = form.getvalue('service') @@ -1396,31 +1391,19 @@ if form.getvalue('master'): script = "install_keepalived.sh" proxy = sql.get_setting('proxy') keepalived_path_logs = sql.get_setting('keepalived_path_logs') - ssh_port = 22 - ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = funct.return_ssh_keys_path(master) - - if ssh_enable == 0: - ssh_key_name = '' - - servers = sql.select_servers(server=master) - for server in servers: - ssh_port = str(server[10]) + proxy_serv = '' + ssh_settings = funct.return_ssh_keys_path(master) if proxy is not None and proxy != '' and proxy != 'None': proxy_serv = proxy - else: - proxy_serv = '' - os.system("cp scripts/%s ." % script) + os.system(f"cp scripts/{script} .") commands = [ - "chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv + " SSH_PORT=" + ssh_port + " router_id=" + router_id - + " ETH=" + ETH + " IP=" + str( - IP) + " MASTER=MASTER" + " ETH_SLAVE=" + ETH_SLAVE + " keepalived_path_logs=" + keepalived_path_logs - + " RETURN_TO_MASTER=" + return_to_master + " SYN_FLOOD=" + syn_flood + " HOST=" + str( - master) + " HAPROXY=" + haproxy - + " NGINX=" + nginx + " USER=" + str(ssh_user_name) + " PASS='" + str(ssh_user_password) + "' KEY=" + str( - ssh_key_name) + f"chmod +x {script} && ./{script} PROXY={proxy_serv} SSH_PORT={ssh_settings['port']} router_id={router_id} " + f"ETH={ETH} IP={IP} MASTER=MASTER ETH_SLAVE={ETH_SLAVE} keepalived_path_logs={keepalived_path_logs} " + f"RETURN_TO_MASTER={return_to_master} SYN_FLOOD={syn_flood} HOST={master} HAPROXY={haproxy} NGINX={nginx} " + f"USER={ssh_settings['user']} PASS='{ssh_settings['password']}' KEY={ssh_settings['key']}" ] output, error = funct.subprocess_execute(commands[0]) @@ -1437,6 +1420,7 @@ if form.getvalue('master'): hostname + '-VIP', IP, group_id, '1', '1', '0', cred_id, ssh_port, 'VRRP IP for ' + master, haproxy, nginx, '0', firewall ) + os.remove(script) if form.getvalue('master_slave'): master = form.getvalue('master_slave') @@ -1451,36 +1435,25 @@ if form.getvalue('master_slave'): script = "install_keepalived.sh" proxy = sql.get_setting('proxy') keepalived_path_logs = sql.get_setting('keepalived_path_logs') - ssh_port = 22 - ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = funct.return_ssh_keys_path(slave) - - if ssh_enable == 0: - ssh_key_name = '' - - servers = sql.select_servers(server=slave) - for server in servers: - ssh_port = str(server[10]) + proxy_serv = '' + ssh_settings = funct.return_ssh_keys_path(slave) if proxy is not None and proxy != '' and proxy != 'None': proxy_serv = proxy - else: - proxy_serv = '' - os.system("cp scripts/%s ." % script) + os.system(f"cp scripts/{script} .") commands = [ - "chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv + " SSH_PORT=" + ssh_port + " router_id=" + router_id - + " ETH=" + ETH + " IP=" + IP + " MASTER=BACKUP" + " ETH_SLAVE=" + ETH_SLAVE + " keepalived_path_logs=" + keepalived_path_logs - + " HAPROXY=" + HAPROXY + " NGINX=" + nginx + " HOST=" + str(slave) + " USER=" + str( - ssh_user_name) + " PASS='" + str(ssh_user_password) - + "' KEY=" + str(ssh_key_name) + f"chmod +x {script} && ./{script} PROXY={proxy_serv} SSH_PORT={ssh_settings['port']} router_id={router_id} ETH={ETH} " + f"IP={IP} MASTER=BACKUP ETH_SLAVE={ETH_SLAVE} keepalived_path_logs={keepalived_path_logs} HAPROXY={HAPROXY} " + f"NGINX={nginx} HOST={slave} USER={ssh_settings['user']} PASS='{ssh_settings['password']}' KEY={ssh_settings['key']}" ] output, error = funct.subprocess_execute(commands[0]) funct.show_installation_output(error, output, 'slave Keepalived') - os.system("rm -f %s" % script) + os.remove(script) sql.update_server_master(master, slave) sql.update_keepalived(slave) @@ -1496,34 +1469,25 @@ if form.getvalue('masteradd'): script = "install_keepalived.sh" proxy = sql.get_setting('proxy') keepalived_path_logs = sql.get_setting('keepalived_path_logs') - ssh_port = 22 - ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = funct.return_ssh_keys_path(master) - - if ssh_enable == 0: - ssh_key_name = '' + proxy_serv = '' + ssh_settings = funct.return_ssh_keys_path(master) if proxy is not None and proxy != '' and proxy != 'None': proxy_serv = proxy - else: - proxy_serv = '' - os.system("cp scripts/%s ." % script) - - servers = sql.select_servers(server=master) - for server in servers: - ssh_port = str(server[10]) + os.system(f"cp scripts/{script} .") commands = [ - "chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv + " SSH_PORT=" + ssh_port + " ETH=" + ETH - + " SLAVE_ETH=" + SLAVE_ETH + " keepalived_path_logs=" + keepalived_path_logs + " RETURN_TO_MASTER=" + return_to_master - + " IP=" + str(IP) + " MASTER=MASTER" + " RESTART=" + kp + " ADD_VRRP=1 HOST=" + str( - master) + " router_id=" + router_id - + " USER=" + str(ssh_user_name) + " PASS='" + str(ssh_user_password) + "' KEY=" + str(ssh_key_name) + f"chmod +x {script} && ./{script} PROXY={proxy_serv} SSH_PORT={ssh_settings['port']} ETH={ETH} SLAVE_ETH={SLAVE_ETH} " + f"keepalived_path_logs={keepalived_path_logs} RETURN_TO_MASTER={return_to_master} IP={IP} MASTER=MASTER " + f"RESTART={kp} ADD_VRRP=1 HOST={master} router_id={router_id} USER={ssh_settings['user']} " + f"PASS='{ssh_settings['password']}' KEY={ssh_settings['key']}" ] output, error = funct.subprocess_execute(commands[0]) funct.show_installation_output(error, output, 'master VRRP address') + os.remove(script) if form.getvalue('masteradd_slave'): master = form.getvalue('masteradd_slave') @@ -1536,36 +1500,25 @@ if form.getvalue('masteradd_slave'): script = "install_keepalived.sh" proxy = sql.get_setting('proxy') keepalived_path_logs = sql.get_setting('keepalived_path_logs') - ssh_port = 22 - ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = funct.return_ssh_keys_path(slave) - - if ssh_enable == 0: - ssh_key_name = '' + proxy_serv = '' + ssh_settings = funct.return_ssh_keys_path(slave) if proxy is not None and proxy != '' and proxy != 'None': proxy_serv = proxy - else: - proxy_serv = '' - os.system("cp scripts/%s ." % script) - - servers = sql.select_servers(server=slave) - for server in servers: - ssh_port = str(server[10]) + os.system(f"cp scripts/{script} .") commands = [ - "chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv - + " SSH_PORT=" + ssh_port + " ETH=" + ETH + " SLAVE_ETH=" + SLAVE_ETH + " keepalived_path_logs=" + keepalived_path_logs - + " IP=" + str(IP) + " MASTER=BACKUP" + " RESTART=" + kp + " ADD_VRRP=1 HOST=" + str( - slave) + " router_id=" + router_id - + " USER=" + str(ssh_user_name) + " PASS='" + str(ssh_user_password) + "' KEY=" + str(ssh_key_name) + f"chmod +x {script} && ./{script} PROXY={proxy_serv} SSH_PORT={ssh_settings['port']} ETH={ETH} SLAVE_ETH={SLAVE_ETH} " + f"keepalived_path_logs={keepalived_path_logs} IP={IP} MASTER=BACKUP RESTART={kp} ADD_VRRP=1 HOST={slave} " + f"router_id={router_id} USER={ssh_settings['user']} PASS='{ssh_settings['password']}' KEY={ssh_settings['key']}" ] output, error = funct.subprocess_execute(commands[0]) funct.show_installation_output(error, output, 'slave VRRP address') - os.system("rm -f %s" % script) + os.remove(script) if form.getvalue('master_slave_hap'): master = form.getvalue('master_slave_hap') @@ -1592,16 +1545,15 @@ if form.getvalue('master_slave_nginx'): if form.getvalue('install_grafana'): script = "install_grafana.sh" proxy = sql.get_setting('proxy') + proxy_serv = '' host = os.environ.get('HTTP_HOST', '') - os.system("cp scripts/%s ." % script) + os.system(f"cp scripts/{script} .") if proxy is not None and proxy != '' and proxy != 'None': proxy_serv = proxy - else: - proxy_serv = '' - commands = ["chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv] + commands = [f"chmod +x {script} && ./{script} PROXY={proxy_serv}"] output, error = funct.subprocess_execute(commands[0]) @@ -1623,7 +1575,7 @@ if form.getvalue('install_grafana'): print( f'success: Grafana and Prometheus servers were installed. You can find Grafana on http://{host}:3000
') - os.system("rm -f %s" % script) + os.remove(script) if form.getvalue('haproxy_exp_install'): serv = form.getvalue('haproxy_exp_install') @@ -1636,17 +1588,9 @@ if form.getvalue('haproxy_exp_install'): stats_password = sql.get_setting('stats_password') stat_page = sql.get_setting('stats_page') proxy = sql.get_setting('proxy') - ssh_port = 22 - ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = funct.return_ssh_keys_path(serv) + ssh_settings = funct.return_ssh_keys_path(serv) - if ssh_enable == 0: - ssh_key_name = '' - - servers = sql.select_servers(server=serv) - for server in servers: - ssh_port = str(server[10]) - - os.system("cp scripts/%s ." % script) + os.system(f"cp scripts/{script} .") if proxy is not None and proxy != '' and proxy != 'None': proxy_serv = proxy @@ -1654,93 +1598,76 @@ if form.getvalue('haproxy_exp_install'): proxy_serv = '' commands = [ - "chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv - + " STAT_PORT=" + str(stats_port) + " STAT_FILE=" + server_state_file - + " SSH_PORT=" + ssh_port + " STAT_PAGE=" + stat_page + " VER=" + ver + " EXP_PROM=" + ext_prom - + " STATS_USER=" + stats_user + " STATS_PASS='" + stats_password + "' HOST=" + serv - + " USER=" + ssh_user_name + " PASS='" + ssh_user_password + "' KEY=" + ssh_key_name + f"chmod +x {script} && ./{script} PROXY={proxy_serv} STAT_PORT={stats_port} STAT_FILE={server_state_file}" + f" SSH_PORT={ssh_settings['port']} STAT_PAGE={stat_page} VER={ver} EXP_PROM={ext_prom} STATS_USER={stats_user}" + f" STATS_PASS='{stats_password}' HOST={serv} USER={ssh_settings['user']} PASS='{ssh_settings['password']}' KEY={ssh_settings['key']}" ] output, error = funct.subprocess_execute(commands[0]) funct.show_installation_output(error, output, 'HAProxy exporter') - os.system("rm -f %s" % script) + os.remove(script) -if form.getvalue('nginx_exp_install'): - serv = form.getvalue('nginx_exp_install') - ver = form.getvalue('exporter_v') - ext_prom = form.getvalue('ext_prom') - script = "install_nginx_exporter.sh" - stats_user = sql.get_setting('nginx_stats_user') - stats_password = sql.get_setting('nginx_stats_password') - stats_port = sql.get_setting('nginx_stats_port') - stats_page = sql.get_setting('nginx_stats_page') +if form.getvalue('nginx_exp_install') or form.getvalue('apache_exp_install'): + if form.getvalue('nginx_exp_install'): + service = 'nginx' + elif form.getvalue('apache_exp_install'): + service = 'apache' + + serv = funct.is_ip_or_dns(form.getvalue('serv')) + ver = funct.checkAjaxInput(form.getvalue('exporter_v')) + ext_prom = funct.checkAjaxInput(form.getvalue('ext_prom')) + script = f"install_{service}_exporter.sh" + stats_user = sql.get_setting(f'{service}_stats_user') + stats_password = sql.get_setting(f'{service}_stats_password') + stats_port = sql.get_setting(f'{service}_stats_port') + stats_page = sql.get_setting(f'{service}_stats_page') proxy = sql.get_setting('proxy') - ssh_port = 22 - ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = funct.return_ssh_keys_path(serv) + proxy_serv = '' + ssh_settings = funct.return_ssh_keys_path(serv) - if ssh_enable == 0: - ssh_key_name = '' - - servers = sql.select_servers(server=serv) - for server in servers: - ssh_port = str(server[10]) - - os.system("cp scripts/%s ." % script) + os.system(f"cp scripts/{script} .") if proxy is not None and proxy != '' and proxy != 'None': proxy_serv = proxy - else: - proxy_serv = '' commands = [ - "chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv - + " STAT_PORT=" + str(stats_port) + " SSH_PORT=" + ssh_port + " STAT_PAGE=" + stats_page - + " STATS_USER=" + stats_user + " STATS_PASS='" + stats_password + "' HOST=" + serv + " VER=" + ver - + " EXP_PROM=" + ext_prom + " USER=" + ssh_user_name + " PASS='" + ssh_user_password + "' KEY=" + ssh_key_name + f"chmod +x {script} && ./{script} PROXY={proxy_serv} STAT_PORT={stats_port} SSH_PORT={ssh_settings['port']} STAT_PAGE={stats_page}" + f" STATS_USER={stats_user} STATS_PASS='{stats_password}' HOST={serv} VER={ver} EXP_PROM={ext_prom} USER={ssh_settings['user']} " + f" PASS='{ssh_settings['password']}' KEY={ssh_settings['key']}" ] output, error = funct.subprocess_execute(commands[0]) - funct.show_installation_output(error, output, 'Nginx exporter') + funct.show_installation_output(error, output, f'{service.title()} exporter') - os.system("rm -f %s" % script) + os.remove(script) if form.getvalue('node_exp_install'): - serv = form.getvalue('node_exp_install') - ver = form.getvalue('exporter_v') - ext_prom = form.getvalue('ext_prom') + serv = funct.is_ip_or_dns(form.getvalue('node_exp_install')) + ver = funct.checkAjaxInput(form.getvalue('exporter_v')) + ext_prom = funct.checkAjaxInput(form.getvalue('ext_prom')) script = "install_node_exporter.sh" proxy = sql.get_setting('proxy') - ssh_port = 22 - ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = funct.return_ssh_keys_path(serv) + proxy_serv = '' + ssh_settings = funct.return_ssh_keys_path(serv) - if ssh_enable == 0: - ssh_key_name = '' - - servers = sql.select_servers(server=serv) - for server in servers: - ssh_port = str(server[10]) - - os.system("cp scripts/%s ." % script) + os.system(f"cp scripts/{script} .") if proxy is not None and proxy != '' and proxy != 'None': proxy_serv = proxy - else: - proxy_serv = '' commands = [ - "chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv + " SSH_PORT=" + ssh_port - + " VER=" + ver + " EXP_PROM=" + ext_prom + " HOST=" + serv + " USER=" + ssh_user_name - + " PASS='" + ssh_user_password + "' KEY=" + ssh_key_name + f"chmod +x {script} && ./{script} PROXY={proxy_serv} SSH_PORT={ssh_settings['port']} VER={ver} EXP_PROM={ext_prom} " + f"HOST={serv} USER={ssh_settings['user']} PASS='{ssh_settings['password']}' KEY={ssh_settings['key']}" ] output, error = funct.subprocess_execute(commands[0]) funct.show_installation_output(error, output, 'Node exporter') - os.system("rm -f %s" % script) + os.remove(script) if form.getvalue('backup') or form.getvalue('deljob') or form.getvalue('backupupdate'): serv = form.getvalue('server') @@ -1753,8 +1680,7 @@ if form.getvalue('backup') or form.getvalue('deljob') or form.getvalue('backupup update = form.getvalue('backupupdate') description = form.getvalue('description') script = 'backup.sh' - ssh_port = 22 - ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = funct.return_ssh_keys_path('localhost', id=int(cred)) + ssh_settings = funct.return_ssh_keys_path('localhost', id=int(cred)) if deljob: time = '' @@ -1768,16 +1694,11 @@ if form.getvalue('backup') or form.getvalue('deljob') or form.getvalue('backupup print('warning: Backup job for %s already exists' % serv) sys.exit() - servers = sql.select_servers(server=serv) - for server in servers: - ssh_port = str(server[10]) - - os.system("cp scripts/%s ." % script) + os.system(f"cp scripts/{script} .") commands = [ - "chmod +x " + script + " && ./" + script + " HOST=" + rserver + " SERVER=" + serv - + " TYPE=" + backup_type + " SSH_PORT=" + ssh_port + " TIME=" + time - + " RPATH=" + rpath + " DELJOB=" + deljob + " USER=" + str(ssh_user_name) + " KEY=" + str(ssh_key_name) + f"chmod +x {script} && ./{script} HOST={rserver} SERVER={serv} TYPE={backup_type} SSH_PORT={ssh_settings['port']} " + f"TIME={time} RPATH={rpath} DELJOB={deljob} USER={ssh_settings['user']} KEY={ssh_settings['key']}" ] output, error = funct.subprocess_execute(commands[0]) @@ -1813,6 +1734,8 @@ if form.getvalue('backup') or form.getvalue('deljob') or form.getvalue('backupup print('Ok') funct.logging('backup ', ' a backup job for server ' + serv + ' has been updated', haproxywi=1, login=1) + os.remove(script) + if form.getvalue('git_backup'): server_id = form.getvalue('server') service_id = form.getvalue('git_service') @@ -1830,19 +1753,13 @@ if form.getvalue('git_backup'): service_name = sql.select_service_name_by_id(service_id).lower() service_config_dir = sql.get_setting(service_name + '_dir') script = 'git_backup.sh' - ssh_port = 22 - ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = funct.return_ssh_keys_path('localhost', id=int(cred)) + proxy_serv = '' + ssh_settings = funct.return_ssh_keys_path('localhost', id=int(cred)) - os.system("cp scripts/%s ." % script) + os.system(f"cp scripts/{script} .") if proxy is not None and proxy != '' and proxy != 'None': proxy_serv = proxy - else: - proxy_serv = '' - - servers = sql.select_servers(server=server_ip) - for server in servers: - ssh_port = str(server[10]) if repo is None or git_init == '0': repo = '' @@ -1850,10 +1767,9 @@ if form.getvalue('git_backup'): branch = 'main' commands = [ - "chmod +x " + script + " && ./" + script + " HOST=" + server_ip + " DELJOB=" + deljob - + " SERVICE=" + service_name + " INIT=" + git_init + " SSH_PORT=" + ssh_port + " PERIOD=" + period - + " REPO=" + repo + " BRANCH=" + branch + " CONFIG_DIR=" + service_config_dir - + " PROXY=" + proxy_serv + " USER=" + str(ssh_user_name) + " KEY=" + str(ssh_key_name) + f"chmod +x {script} && ./{script} HOST={server_ip} DELJOB={deljob} SERVICE={service_name} INIT={git_init} " + f"SSH_PORT={ssh_settings['port']} PERIOD={period} REPO={repo} BRANCH={branch} CONFIG_DIR={service_config_dir} " + f"PROXY={proxy_serv} USER={ssh_settings['user']} KEY={ssh_settings['key']}" ] output, error = funct.subprocess_execute(commands[0]) @@ -1887,7 +1803,7 @@ if form.getvalue('git_backup'): else: if sql.delete_git(form.getvalue('git_backup')): print('Ok') - os.system("rm -f %s" % script) + os.remove(script) if form.getvalue('install_nginx'): funct.install_nginx(form.getvalue('install_nginx'), docker=form.getvalue('docker')) @@ -1914,7 +1830,7 @@ if form.getvalue('update_roxy_wi'): 'roxy-wi-socket', 'roxy-wi-prometheus-exporter'] if service not in services: - print('error: ' + service + ' is not part of Roxy-WI') + print(f'error: {service} is not part of Roxy-WI') sys.exit() funct.update_roxy_wi(service) @@ -2094,7 +2010,7 @@ if form.getvalue('get_nginx_v'): if is_dockerized == '1': container_name = sql.get_setting('nginx_container_name') - cmd = ["docker exec -it " + container_name + " /usr/sbin/nginx -v 2>&1|awk '{print $3}'"] + cmd = [f"docker exec -it {container_name} /usr/sbin/nginx -v 2>&1|awk '{{print $3}}'"] else: cmd = ['sudo /usr/sbin/nginx -v'] print(funct.ssh_command(serv, cmd)) @@ -2108,44 +2024,45 @@ if form.getvalue('get_exporter_v'): if form.getvalue('bwlists'): lib_path = get_config.get_config_var('main', 'lib_path') - list_path = lib_path + "/" + sql.get_setting('lists_path') + "/" + form.getvalue('group') + "/" + form.getvalue( - 'color') + "/" + form.getvalue('bwlists') + color = funct.checkAjaxInput(form.getvalue('color')) + group = funct.checkAjaxInput(form.getvalue('group')) + bwlists = funct.checkAjaxInput(form.getvalue('bwlists')) + list_path = f"{lib_path}/{sql.get_setting('lists_path')}/{group}/{color}/{bwlist}" + try: file = open(list_path, "r") file_read = file.read() file.close() print(file_read) except IOError: - print('error: Cat\'n read ' + form.getvalue('color') + ' list , ') + print(f"error: Cannot read {color} list") if form.getvalue('bwlists_create'): - color = form.getvalue('color') + color = funct.checkAjaxInput(form.getvalue('color')) lib_path = get_config.get_config_var('main', 'lib_path') - list_name = form.getvalue('bwlists_create').split('.')[0] - list_name += '.lst' - list_path = lib_path + "/" + sql.get_setting('lists_path') + "/" + form.getvalue( - 'group') + "/" + color + "/" + list_name + list_name = f"{form.getvalue('bwlists_create').split('.')[0]}.lst" + list_path = f"{lib_path}/{sql.get_setting('lists_path')}/{form.getvalue('group')}/{color}/{list_name}" try: open(list_path, 'a').close() print('success: ') try: - funct.logging(serv, 'has been created ' + color + ' list ' + list_name, haproxywi=1, login=1) + funct.logging(serv, f'A new list {color} {list_name} has been created', haproxywi=1, login=1) except Exception: pass except IOError as e: - print('error: Cannot create new ' + color + ' list. %s , ' % e) + print(f'error: Cannot create a new {color} list. {e}, ') if form.getvalue('bwlists_save'): - color = form.getvalue('color') - bwlists_save = form.getvalue('bwlists_save') + color = funct.checkAjaxInput(form.getvalue('color')) + group = funct.checkAjaxInput(form.getvalue('group')) + bwlists_save = funct.checkAjaxInput(form.getvalue('bwlists_save')) lib_path = get_config.get_config_var('main', 'lib_path') - list_path = lib_path + "/" + sql.get_setting('lists_path') + "/" + form.getvalue( - 'group') + "/" + color + "/" + bwlists_save + list_path = f"{lib_path}/{sql.get_setting('lists_path')}/{group}/{color}/{bwlists_save}" try: with open(list_path, "w") as file: file.write(form.getvalue('bwlists_content')) except IOError as e: - print('error: Cannot save ' + color + ' list. %s , ' % e) + print(f'error: Cannot save {color} list. {e}') path = sql.get_setting('haproxy_dir') + "/" + color servers = [] @@ -2163,8 +2080,8 @@ if form.getvalue('bwlists_save'): servers.append(s[2]) for serv in servers: - funct.ssh_command(serv, ["sudo mkdir " + path]) - funct.ssh_command(serv, ["sudo chown $(whoami) " + path]) + funct.ssh_command(serv, [f"sudo mkdir {path}"]) + funct.ssh_command(serv, [f"sudo chown $(whoami) {path}"]) error = funct.upload(serv, path + "/" + bwlists_save, list_path, dir='fullpath') if error: @@ -2172,7 +2089,7 @@ if form.getvalue('bwlists_save'): else: print('success: Edited ' + color + ' list was uploaded to ' + serv + ' , ') try: - funct.logging(serv, 'has been edited the ' + color + ' list ' + bwlists_save, haproxywi=1, login=1) + funct.logging(serv, f'Has been edited the {color} list {bwlists_save}', haproxywi=1, login=1) except Exception: pass @@ -2184,9 +2101,9 @@ if form.getvalue('bwlists_save'): haproxy_service_name = "haproxy" if form.getvalue('bwlists_restart') == 'restart': - funct.ssh_command(serv, ["sudo systemctl restart " + haproxy_service_name]) + funct.ssh_command(serv, [f"sudo systemctl restart {haproxy_service_name}"]) elif form.getvalue('bwlists_restart') == 'reload': - funct.ssh_command(serv, ["sudo systemctl reload " + haproxy_service_name]) + funct.ssh_command(serv, [f"sudo systemctl reload {haproxy_service_name}"]) if form.getvalue('bwlists_delete'): color = form.getvalue('color') @@ -2939,13 +2856,11 @@ if form.getvalue('showBytes') is not None: bin_bout.append(cout[0]) env = Environment(loader=FileSystemLoader('templates'), autoescape=True) template = env.get_template('ajax/bin_bout.html') - template = template.render(bin_bout=bin_bout, serv=serv) + template = template.render(bin_bout=bin_bout, serv=serv, service='haproxy') print(template) if form.getvalue('nginxConnections'): - import requests - - serv = form.getvalue('nginxConnections') + serv = funct.is_ip_or_dns(form.getvalue('nginxConnections')) port = sql.get_setting('nginx_stats_port') user = sql.get_setting('nginx_stats_user') password = sql.get_setting('nginx_stats_password') @@ -2967,7 +2882,30 @@ if form.getvalue('nginxConnections'): template = template.render(bin_bout=bin_bout, serv=serv, service='nginx') print(template) else: - print('error: cannot connect to Nginx stat page') + print('error: cannot connect to NGINX stat page') + +if form.getvalue('apachekBytes'): + serv = funct.is_ip_or_dns(form.getvalue('apachekBytes')) + port = sql.get_setting('apache_stats_port') + user = sql.get_setting('apache_stats_user') + password = sql.get_setting('apache_stats_password') + page = sql.get_setting('apache_stats_page') + bin_bout = [] + url = f'http://{serv}:{port}/{page}?auto' + + r = requests.get(url, auth=(user, password)) + + if r.status_code == 200: + for line in r.text.split('\n'): + if 'ReqPerSec' in line or 'BytesPerSec' in line: + bin_bout.append(line.split(' ')[1]) + + env = Environment(loader=FileSystemLoader('templates')) + template = env.get_template('ajax/bin_bout.html') + template = template.render(bin_bout=bin_bout, serv=serv, service='apache') + print(template) + else: + print('error: cannot connect to Apache stat page') if form.getvalue('waf_rule_id'): enable = funct.checkAjaxInput(form.getvalue('waf_en')) @@ -3029,27 +2967,18 @@ if form.getvalue('lets_domain'): ssl_path = sql.get_setting('cert_path') haproxy_dir = sql.get_setting('haproxy_dir') script = "letsencrypt.sh" - ssh_port = "22" - ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = funct.return_ssh_keys_path(serv) + proxy_serv = '' + ssh_settings = funct.return_ssh_keys_path(serv) - if ssh_enable == 0: - ssh_key_name = '' - - servers = sql.select_servers(server=serv) - for server in servers: - ssh_port = str(server[10]) - - os.system("cp scripts/%s ." % script) + os.system(f"cp scripts/{script} .") if proxy is not None and proxy != '' and proxy != 'None': proxy_serv = proxy - else: - proxy_serv = '' commands = [ - "chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv + " haproxy_dir=" + haproxy_dir - + " DOMAIN=" + lets_domain + " EMAIL=" + lets_email + " SSH_PORT=" + ssh_port + " SSL_PATH=" + ssl_path - + " HOST=" + serv + " USER=" + ssh_user_name + " PASS='" + ssh_user_password + "' KEY=" + ssh_key_name + f"chmod +x {script} && ./{script} PROXY={proxy_serv} haproxy_dir={haproxy_dir} DOMAIN={lets_domain} " + f"EMAIL={lets_email} SSH_PORT={ssh_settings['port']} SSL_PATH={ssl_path} HOST={serv} USER={ ssh_settings['user']} " + f"PASS='{ssh_settings['password']}' KEY={ssh_settings['key']}" ] output, error = funct.subprocess_execute(commands[0]) @@ -3071,7 +3000,7 @@ if form.getvalue('lets_domain'): else: print('success: Certificate has been created') - os.system("rm -f %s" % script) + os.remove(script) if form.getvalue('uploadovpn'): name = funct.checkAjaxInput(form.getvalue('ovpnname')) @@ -3194,7 +3123,8 @@ if form.getvalue('geoip_install'): service = form.getvalue('geoip_service') proxy = sql.get_setting('proxy') maxmind_key = sql.get_setting('maxmind_key') - ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = funct.return_ssh_keys_path(serv) + proxy_serv = '' + ssh_settings = funct.return_ssh_keys_path(serv) if service in ('haproxy', 'nginx'): service_dir = funct.return_nice_path(sql.get_setting(f'{service}_dir')) @@ -3203,28 +3133,22 @@ if form.getvalue('geoip_install'): print('warning: select a server and service first') sys.exit() - if ssh_enable == 0: - ssh_key_name = '' - - ssh_port = [ str(server[10]) for server in sql.select_servers(server=serv) ] - if proxy is not None and proxy != '' and proxy != 'None': proxy_serv = proxy - else: - proxy_serv = '' os.system(f"cp scripts/{script} .") commands = [ - f"chmod +x {script} && ./{script} PROXY={proxy_serv} SSH_PORT={ssh_port[0]} UPDATE={geoip_update} maxmind_key={maxmind_key} " - f"service_dir={service_dir} HOST={serv} USER={ssh_user_name} PASS={ssh_user_password} KEY={ssh_key_name}" + f"chmod +x {script} && ./{script} PROXY={proxy_serv} SSH_PORT={ssh_settings['port']} UPDATE={geoip_update} " + f"maxmind_key={maxmind_key} service_dir={service_dir} HOST={serv} USER={ssh_settings['user']} " + f"PASS={ssh_settings['password']} KEY={ssh_settings['key']}" ] output, error = funct.subprocess_execute(commands[0]) funct.show_installation_output(error, output, 'GeoLite2 Database') - os.system("rm -f %s" % script) + os.remove(script) if form.getvalue('nettools_icmp_server_from'): server_from = form.getvalue('nettools_icmp_server_from') diff --git a/app/scripts/ansible/roles/apache_exporter.yml b/app/scripts/ansible/roles/apache_exporter.yml new file mode 100644 index 00000000..176c0d9b --- /dev/null +++ b/app/scripts/ansible/roles/apache_exporter.yml @@ -0,0 +1,24 @@ +- hosts: "{{ variable_host }}" + become: yes + become_method: sudo + tasks: + - name: Set SSH port + set_fact: + ansible_port: "{{SSH_PORT}}" + + - name: Open stat port for iptables + iptables: + chain: INPUT + destination_port: "9117" + jump: ACCEPT + protocol: tcp + ignore_errors: yes + + + roles: + - role: apache_exporter + environment: + http_proxy: "{{PROXY}}" + https_proxy: "{{PROXY}}" + vars: + apache_exporter_scrape_uri: 'http://{{STATS_USER}}:{{STATS_PASS}}@{{variable_host}}:{{STAT_PORT}}/{{STAT_PAGE}}' diff --git a/app/scripts/ansible/roles/apache_exporter/defaults/main.yml b/app/scripts/ansible/roles/apache_exporter/defaults/main.yml new file mode 100644 index 00000000..15aac73f --- /dev/null +++ b/app/scripts/ansible/roles/apache_exporter/defaults/main.yml @@ -0,0 +1,8 @@ +--- +apache_exporter_version: 0.6.0 +apache_exporter_telemetry_address: "0.0.0.0:9117" +apache_exporter_telemetry_endpoint: "/metrics" +apache_exporter_incecure: "false" +stats_page: "stats" +apache_exporter_scrape_uri: "http://{{host}}/{{stats_page}}?auto" +prometheus_exporter_dir: "/opt/prometheus/exporters" diff --git a/app/scripts/ansible/roles/apache_exporter/handlers/main.yml b/app/scripts/ansible/roles/apache_exporter/handlers/main.yml new file mode 100644 index 00000000..4559d88b --- /dev/null +++ b/app/scripts/ansible/roles/apache_exporter/handlers/main.yml @@ -0,0 +1,7 @@ +--- +- name: restart apache exporter + become: true + systemd: + daemon_reload: true + name: apache_exporter + state: restarted diff --git a/app/scripts/ansible/roles/apache_exporter/tasks/main.yml b/app/scripts/ansible/roles/apache_exporter/tasks/main.yml new file mode 100644 index 00000000..b03ce3ac --- /dev/null +++ b/app/scripts/ansible/roles/apache_exporter/tasks/main.yml @@ -0,0 +1,81 @@ +--- +- include: preflight.yml + +- name: Create the Apache Exporter group + become: true + group: + name: "{{ __apache_exporter_group }}" + state: present + system: true + +- name: Create the Apache Exporter user + become: true + user: + name: "{{ __apache_exporter_user }}" + groups: "{{ __apache_exporter_group }}" + append: true + shell: /usr/sbin/nologin + system: true + createhome: false + home: / + +- name: Distribution Directory + become: true + become_user: root + file: + path: "{{ prometheus_exporter_dir }}" + owner: root + group: root + mode: "u=rwx,go=rx" + state: directory + +- name: Download apache_exporter binary to local folder + get_url: + url: "https://github.com/Lusitaniae/apache_exporter/releases/download/v{{ apache_exporter_version }}/apache_exporter-{{ apache_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}.tar.gz" + dest: "/tmp/apache_exporter-{{ apache_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}.tar.gz" + checksum: "sha256:{{ __apache_exporter_checksum }}" + register: _download_binary + until: _download_binary is succeeded + retries: 5 + delay: 2 + delegate_to: localhost + check_mode: false + +- name: Unpack apache_exporter binary + unarchive: + src: "/tmp/apache_exporter-{{ apache_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}.tar.gz" + dest: "/tmp" + creates: "/tmp/apache_exporter-{{ apache_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}/apache_exporter" + delegate_to: localhost + check_mode: false + +- name: Propagate apache_exporter binaries + become: true + copy: + src: "/tmp/apache_exporter-{{ apache_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}/apache_exporter" + dest: "/opt/prometheus/exporters/apache_exporter" + mode: 0755 + owner: root + group: root + notify: + - restart apache exporter + + when: not ansible_check_mode +- name: Copy the apache_exporter systemd service file + become: true + template: + src: apache_exporter.service.j2 + dest: /etc/systemd/system/apache_exporter.service + owner: root + group: root + mode: 0644 + no_log: true + notify: + - restart apache exporter + +- name: Ensure apache_exporter is enabled on boot + become: true + systemd: + name: apache_exporter + state: started + enabled: true diff --git a/app/scripts/ansible/roles/apache_exporter/tasks/preflight.yml b/app/scripts/ansible/roles/apache_exporter/tasks/preflight.yml new file mode 100644 index 00000000..e1261121 --- /dev/null +++ b/app/scripts/ansible/roles/apache_exporter/tasks/preflight.yml @@ -0,0 +1,7 @@ +--- +- name: "Get checksum for {{ go_arch_map[ansible_architecture] | default(ansible_architecture) }} architecture" + set_fact: + __apache_exporter_checksum: "{{ item.split(' ')[0] }}" + with_items: + - "{{ lookup('url', 'https://github.com/Lusitaniae/apache_exporter/releases/download/v' + apache_exporter_version + '/sha256sums.txt', wantlist=True) | list }}" + when: "('linux-' + (go_arch_map[ansible_architecture] | default(ansible_architecture)) + '.tar.gz') in item" diff --git a/app/scripts/ansible/roles/apache_exporter/templates/apache_exporter.service.j2 b/app/scripts/ansible/roles/apache_exporter/templates/apache_exporter.service.j2 new file mode 100644 index 00000000..dacfe393 --- /dev/null +++ b/app/scripts/ansible/roles/apache_exporter/templates/apache_exporter.service.j2 @@ -0,0 +1,20 @@ +{{ ansible_managed | comment }} + +[Unit] +Description=Prometheus Apache Exporter +After=network.target + +[Service] +Type=simple +User={{ __apache_exporter_user }} +Group={{ __apache_exporter_group }} +ExecStart={{ prometheus_exporter_dir }}/apache_exporter \ + --telemetry.address {{ apache_exporter_telemetry_address|default("0.0.0.0:9117") }} \ + --scrape_uri {{ apache_exporter_scrape_uri|default("http://localhost/server-status/?auto") }} \ + --telemetry.endpoint {{ apache_exporter_telemetry_endpoint|default("/metrics") }} \ + +SyslogIdentifier=apache_exporter +Restart=always + +[Install] +WantedBy=multi-user.target diff --git a/app/scripts/ansible/roles/apache_exporter/vars/main.yml b/app/scripts/ansible/roles/apache_exporter/vars/main.yml new file mode 100644 index 00000000..221266da --- /dev/null +++ b/app/scripts/ansible/roles/apache_exporter/vars/main.yml @@ -0,0 +1,10 @@ +--- +go_arch_map: + i386: '386' + x86_64: 'amd64' + aarch64: 'arm64' + armv7l: 'armv7' + armv6l: 'armv6' + +__apache_exporter_user: apache-exp +__apache_exporter_group: apache-exp diff --git a/app/scripts/install_apache_exporter.sh b/app/scripts/install_apache_exporter.sh new file mode 100644 index 00000000..e5881473 --- /dev/null +++ b/app/scripts/install_apache_exporter.sh @@ -0,0 +1,54 @@ +#!/bin/bash +for ARGUMENT in "$@" +do + KEY=$(echo $ARGUMENT | cut -f1 -d=) + VALUE=$(echo $ARGUMENT | cut -f2 -d=) + + case "$KEY" in + PROXY) PROXY=${VALUE} ;; + HOST) HOST=${VALUE} ;; + USER) USER=${VALUE} ;; + PASS) PASS=${VALUE} ;; + KEY) KEY=${VALUE} ;; + VER) VER=${VALUE} ;; + EXP_PROM) EXP_PROM=${VALUE} ;; + STAT_PORT) STAT_PORT=${VALUE} ;; + STAT_PAGE) STAT_PAGE=${VALUE} ;; + STATS_USER) STATS_USER=${VALUE} ;; + STATS_PASS) STATS_PASS=${VALUE} ;; + SSH_PORT) SSH_PORT=${VALUE} ;; + *) + esac +done + +export ANSIBLE_HOST_KEY_CHECKING=False +export ANSIBLE_DISPLAY_SKIPPED_HOSTS=False +export ACTION_WARNINGS=False +export LOCALHOST_WARNING=False +export COMMAND_WARNINGS=False + +PWD=`pwd` +PWD=$PWD/scripts/ansible/ +echo "$HOST ansible_port=$SSH_PORT" > $PWD/$HOST + +if [[ $KEY == "" ]]; then + ansible-playbook $PWD/roles/apache_exporter.yml -e "ansible_user=$USER ansible_ssh_pass='$PASS' variable_host=$HOST PROXY=$PROXY STAT_PAGE=$STAT_PAGE STAT_PORT=$STAT_PORT STATS_USER=$STATS_USER STATS_PASS=$STATS_PASS SSH_PORT=$SSH_PORT apache_exporter_version=$VER" -i $PWD/$HOST +else + ansible-playbook $PWD/roles/apache_exporter.yml --key-file $KEY -e "ansible_user=$USER variable_host=$HOST PROXY=$PROXY STAT_PAGE=$STAT_PAGE STAT_PORT=$STAT_PORT STATS_USER=$STATS_USER STATS_PASS=$STATS_PASS SSH_PORT=$SSH_PORT apache_exporter_version=$VER" -i $PWD/$HOST +fi + +if [ $? -gt 0 ] +then + echo "error: Can't install Apache exporter

" + exit 1 +fi + +if [ "$EXP_PROM" == 0 ] +then + if ! sudo grep -Fxq " - $HOST:9117" /etc/prometheus/prometheus.yml; then + sudo echo " - $HOST:9117" | sudo tee -a /etc/prometheus/prometheus.yml > /dev/null + sudo systemctl reload prometheus 2>> /dev/null + fi +fi + +rm -f $PWD/$HOST diff --git a/app/templates/ajax/bin_bout.html b/app/templates/ajax/bin_bout.html index 94769091..e588d461 100644 --- a/app/templates/ajax/bin_bout.html +++ b/app/templates/ajax/bin_bout.html @@ -1,4 +1,4 @@ -{% if service != 'nginx' %} +{% if service == 'haproxy' %}
Bytes in @@ -39,6 +39,7 @@
{% endif %} +{% if service == 'nginx' or service == 'haproxy' %}
Current ses @@ -59,4 +60,36 @@
{{bin_bout.3}}
-
\ No newline at end of file +
+{% endif %} +{% if service == 'apache' %} +
+
+ ReqPerSec + + + + + +
+
+ {{bin_bout.0}} +
+
+ BytesPerSec +
+
+ {% set total = bin_bout.1|int / 1024 %} + {% set metric = 'Kb' %} + {% if total > 1000 %} + {% set total = total / 1024 %} + {% set metric = 'Mb' %} + {% endif %} + {% if total > 1000 %} + {% set total = total / 1024 %} + {% set metric = 'Gb' %} + {% endif %} + {{total|round(2)}} {{metric}} +
+
+{% endif %} diff --git a/app/templates/hapservers.html b/app/templates/hapservers.html index e42a4eec..f66e5a8e 100644 --- a/app/templates/hapservers.html +++ b/app/templates/hapservers.html @@ -68,11 +68,11 @@ function showMetrics() { {%- if service == 'haproxy' %} {%- for s in servers %} - {%- if s.8.0.9 %} - getChartData(server_ip) - getHttpChartData(server_ip) - getWafChartData(server_ip) - {% endif %} + {%- if s.8.0.9 %} + getChartData(server_ip) + getHttpChartData(server_ip) + getWafChartData(server_ip) + {% endif %} {% endfor %} {% endif %} showOverviewServer('{{s.1}}', server_ip, '{{s.0}}', '{{service}}'); @@ -82,6 +82,7 @@ getNginxChartData(server_ip) {% endif %} {%- elif service == 'apache' %} + showApachekBytes(server_ip) {% if s.8.0.27 %} getApacheChartData(server_ip) {% endif %} diff --git a/app/templates/include/mon_installation.html b/app/templates/include/mon_installation.html index c1c28ea0..3b13e51e 100644 --- a/app/templates/include/mon_installation.html +++ b/app/templates/include/mon_installation.html @@ -93,6 +93,37 @@ + + + + + + + + + + + + + + + + + +

Install Apache Exporter

Current installationAvailable VersionsExternal PrometheusServer
+ {% set values = dict() %} + {% set values = {'0.7.0':'0.7.0', '0.8.0':'0.8.0', '0.9.0':'0.9.0', '0.10.0':'0.10.0'} %} + {{ select('apacheexpver', values=values, selected='0.10.0') }} + {{ checkbox('apache_ext_prom', title="This exporter will be used by an external Prometheus. Also use this checkbox if you update the Exporter") }} + + + Install +
diff --git a/config_other/requirements_deb.txt b/config_other/requirements_deb.txt index aff1ff97..1ecfdbce 100644 --- a/config_other/requirements_deb.txt +++ b/config_other/requirements_deb.txt @@ -4,3 +4,4 @@ pyTelegramBotAPI>=3.6.3 slack-sdk>=3.4.0 distro>=1.2.0 retry>=0.9.2 +psutil>=5.9.1 diff --git a/inc/overview.js b/inc/overview.js index 9b5bfabf..9cac1533 100644 --- a/inc/overview.js +++ b/inc/overview.js @@ -518,6 +518,28 @@ function showNginxConnections(serv) { } } ); } +function showApachekBytes(serv) { + $.ajax( { + url: "options.py", + data: { + apachekBytes: serv, + token: $('#token').val() + }, + type: "POST", + beforeSend: function() { + $("#sessions").html(''); + }, + success: function( data ) { + data = data.replace(/\s+/g,' '); + if (data.indexOf('error:') != '-1') { + toastr.error(data); + } else { + $("#bin_bout").html(data); + $.getScript("/inc/fontawesome.min.js") + } + } + } ); +} function showUsersOverview() { $.ajax( { url: "options.py", diff --git a/inc/users.js b/inc/users.js index 4ae87032..5822d6a7 100644 --- a/inc/users.js +++ b/inc/users.js @@ -166,7 +166,8 @@ $( function() { $.ajax( { url: "options.py", data: { - nginx_exp_install: $('#nginx_exp_addserv').val(), + nginx_exp_install: 1, + serv: $('#nginx_exp_addserv').val(), exporter_v: $('#nginxexpver').val(), ext_prom: ext_prom, token: $('#token').val() @@ -180,7 +181,7 @@ $( function() { } else if (data.indexOf('success') != '-1' ){ toastr.clear(); toastr.success(data); - $('#cur_nginx_exp_ver').text('Nginx exporter is installed'); + $('#cur_nginx_exp_ver').text('NGINX exporter is installed'); $("#nginx_exp_addserv").trigger( "selectmenuchange" ); } else if (data.indexOf('Info') != '-1' ){ toastr.clear(); @@ -192,6 +193,43 @@ $( function() { } } ); }); + $('#apache_exp_install').click(function() { + $("#ajaxmon").html('') + $("#ajaxmon").html(wait_mess); + var ext_prom = 0; + if ($('#apache_ext_prom').is(':checked')) { + ext_prom = '1'; + } + $.ajax( { + url: "options.py", + data: { + apache_exp_install: 1, + serv: $('#apache_exp_addserv').val(), + exporter_v: $('#apacheexpver').val(), + ext_prom: ext_prom, + token: $('#token').val() + }, + type: "POST", + success: function( data ) { + data = data.replace(/\s+/g,' '); + $("#ajaxmon").html(''); + if (data.indexOf('error:') != '-1' || data.indexOf('FAILED') != '-1' || data.indexOf('UNREACHABLE') != '-1') { + toastr.error(data); + } else if (data.indexOf('success') != '-1' ){ + toastr.clear(); + toastr.success(data); + $('#cur_apache_exp_ver').text('Apache exporter is installed'); + $("#apache_exp_addserv").trigger( "selectmenuchange" ); + } else if (data.indexOf('Info') != '-1' ){ + toastr.clear(); + toastr.info(data); + } else { + toastr.clear(); + toastr.info(data); + } + } + } ); + }); $('#node_exp_install').click(function() { $("#ajaxmon").html('') $("#ajaxmon").html(wait_mess); @@ -315,7 +353,7 @@ $( function() { toastr.clear(); toastr.error(data); } else if(data == 'no' || data == '') { - $('#cur_nginx_exp_ver').text('Nginx exporter has not been installed'); + $('#cur_nginx_exp_ver').text('NGINX exporter has not been installed'); } else { $('#cur_nginx_exp_ver').text(data); $('#cur_nginx_exp_ver').css('font-weight', 'bold'); @@ -323,6 +361,29 @@ $( function() { } } ); }); + $( "#apache_exp_addserv" ).on('selectmenuchange',function() { + $.ajax( { + url: "options.py", + data: { + get_exporter_v: 'apache_exporter', + serv: $('#apache_exp_addserv option:selected').val(), + token: $('#token').val() + }, + type: "POST", + success: function( data ) { + data = data.replace(/^\s+|\s+$/g,''); + if (data.indexOf('error:') != '-1') { + toastr.clear(); + toastr.error(data); + } else if(data == 'no' || data == '') { + $('#cur_apache_exp_ver').text('Apache exporter has not been installed'); + } else { + $('#cur_apache_exp_ver').text(data); + $('#cur_apache_exp_ver').css('font-weight', 'bold'); + } + } + } ); + }); $( "#node_exp_addserv" ).on('selectmenuchange',function() { $.ajax( { url: "options.py", @@ -1060,6 +1121,7 @@ function addServer(dialog_id) { $('select:regex(id, backup-server)').append('').selectmenu("refresh"); $('select:regex(id, haproxy_exp_addserv)').append('').selectmenu("refresh"); $('select:regex(id, nginx_exp_addserv)').append('').selectmenu("refresh"); + $('select:regex(id, apache_exp_addserv)').append('').selectmenu("refresh"); $('select:regex(id, node_exp_addserv)').append('').selectmenu("refresh"); } }

Install Node Exporter