diff --git a/app/modules/db/smon.py b/app/modules/db/smon.py index 1282695d..c5c8dc64 100644 --- a/app/modules/db/smon.py +++ b/app/modules/db/smon.py @@ -4,7 +4,7 @@ from peewee import fn from app.modules.db.db_model import SmonAgent, Server, SMON, SmonTcpCheck, SmonHttpCheck, SmonDnsCheck, SmonPingCheck, SmonHistory, SmonStatusPageCheck, SmonStatusPage from app.modules.db.sql import out_error, get_setting -import modules.roxy_wi_tools as roxy_wi_tools +import app.modules.roxy_wi_tools as roxy_wi_tools time_zone = get_setting('time_zone') get_date = roxy_wi_tools.GetDate(time_zone) @@ -636,3 +636,10 @@ def select_body_status(smon_id): out_error(e) else: return query_res + + +def count_agents() -> int: + try: + return SmonAgent.select().count() + except Exception as e: + out_error(e) diff --git a/app/modules/tools/smon.py b/app/modules/tools/smon.py index 1b4dfe0e..cf226c69 100644 --- a/app/modules/tools/smon.py +++ b/app/modules/tools/smon.py @@ -7,27 +7,32 @@ import app.modules.roxywi.common as roxywi_common def create_smon(json_data, user_group, show_new=1) -> bool: - name = common.checkAjaxInput(json_data['name']) - hostname = common.checkAjaxInput(json_data['ip']) - port = common.checkAjaxInput(json_data['port']) - enable = common.checkAjaxInput(json_data['enabled']) - url = common.checkAjaxInput(json_data['url']) - body = common.checkAjaxInput(json_data['body']) - group = common.checkAjaxInput(json_data['group']) - desc = common.checkAjaxInput(json_data['desc']) - telegram = common.checkAjaxInput(json_data['tg']) - slack = common.checkAjaxInput(json_data['slack']) - pd = common.checkAjaxInput(json_data['pd']) - resolver = common.checkAjaxInput(json_data['resolver']) - record_type = common.checkAjaxInput(json_data['record_type']) - packet_size = common.checkAjaxInput(json_data['packet_size']) - http_method = common.checkAjaxInput(json_data['http_method']) - interval = common.checkAjaxInput(json_data['interval']) - agent_id = common.checkAjaxInput(json_data['agent_id']) - check_type = common.checkAjaxInput(json_data['check_type']) + try: + name = common.checkAjaxInput(json_data['name']) + hostname = common.checkAjaxInput(json_data['ip']) + port = common.checkAjaxInput(json_data['port']) + enable = common.checkAjaxInput(json_data['enabled']) + url = common.checkAjaxInput(json_data['url']) + body = common.checkAjaxInput(json_data['body']) + group = common.checkAjaxInput(json_data['group']) + desc = common.checkAjaxInput(json_data['desc']) + telegram = common.checkAjaxInput(json_data['tg']) + slack = common.checkAjaxInput(json_data['slack']) + pd = common.checkAjaxInput(json_data['pd']) + resolver = common.checkAjaxInput(json_data['resolver']) + record_type = common.checkAjaxInput(json_data['record_type']) + packet_size = common.checkAjaxInput(json_data['packet_size']) + http_method = common.checkAjaxInput(json_data['http_method']) + interval = common.checkAjaxInput(json_data['interval']) + agent_id = common.checkAjaxInput(json_data['agent_id']) + check_type = common.checkAjaxInput(json_data['check_type']) + except Exception as e: + roxywi_common.handle_exceptions(e, 'SMON server', 'Cannot parse check parameters') if agent_id == '': raise Exception('warning: Select an Agent first') + else: + agent_ip = smon_sql.select_server_ip_by_agent_id(agent_id) if check_type == 'tcp': try: @@ -53,15 +58,14 @@ def create_smon(json_data, user_group, show_new=1) -> bool: smon_sql.insert_smon_dns(last_id, hostname, port, resolver, record_type, interval, agent_id) if last_id: - roxywi_common.logging('SMON', f' A new server {name} to SMON has been add ', roxywi=1, login=1) + roxywi_common.logging('SMON', f'A new server {name} to SMON has been add', roxywi=1, login=1) try: api_path = f'check/{last_id}' check_json = create_check_json(json_data) - server_ip = smon_sql.select_server_ip_by_agent_id(agent_id) - smon_agent.send_post_request_to_agent(agent_id, server_ip, api_path, check_json) + smon_agent.send_post_request_to_agent(agent_id, agent_ip, api_path, check_json) except Exception as e: - roxywi_common.logging('SMON', f'Cannot add check to the agent {server_ip}: {e}', roxywi=1, login=1) + roxywi_common.logging('SMON', f'Cannot add check to the agent {agent_ip}: {e}', roxywi=1, login=1) if last_id and show_new: return last_id @@ -124,7 +128,7 @@ def update_smon(smon_id, json_data) -> str: is_edited = smon_sql.update_smonDns(smon_id, hostname, port, resolver, record_type, interval, agent_id) if is_edited: - roxywi_common.logging('SMON', f' The SMON server {name} has been update ', roxywi=1, login=1) + roxywi_common.logging('SMON', f'The SMON server {name} has been update', roxywi=1, login=1) try: api_path = f'check/{smon_id}' check_json = create_check_json(json_data) @@ -175,7 +179,7 @@ def delete_smon(smon_id, user_group) -> str: server_ip = smon_sql.get_agent_ip_by_id(agent_id) smon_agent.delete_check(agent_id, server_ip, smon_id) except Exception as e: - roxywi_common.handle_exceptions(e, 'Roxy-WI server', f'Cannot delete check', roxywi=1, login=1) + roxywi_common.handle_exceptions(e, 'Roxy-WI server', 'Cannot delete check', roxywi=1, login=1) try: if smon_sql.delete_smon(smon_id, user_group): roxywi_common.logging('SMON', ' The server from SMON has been delete ', roxywi=1, login=1) diff --git a/app/modules/tools/smon_agent.py b/app/modules/tools/smon_agent.py index 41d354d9..5819392e 100644 --- a/app/modules/tools/smon_agent.py +++ b/app/modules/tools/smon_agent.py @@ -4,7 +4,7 @@ import requests import app.modules.db.sql as sql import app.modules.db.smon as smon_sql import app.modules.common.common as common -import app.modules.roxywi.common as common_roxywi +import app.modules.roxywi.common as roxywi_common from app.modules.service.installation import run_ansible @@ -28,6 +28,15 @@ def generate_agent_inc(server_ip: str, action: str, agent_uuid: uuid) -> object: return inv, server_ips +def check_agent_limit(): + user_subscription = roxywi_common.return_user_subscription() + count_agents = smon_sql.count_agents() + if user_subscription['user_plan'] == 'user' and count_agents >= 1: + raise Exception('error: You have reached limit for Home plan') + elif user_subscription['user_plan'] == 'company' and count_agents >= 5: + raise Exception('error: You have reached limit for Enterprise plan') + + def add_agent(data) -> int: name = common.checkAjaxInput(data.get("name")) server_id = int(data.get("server_id")) @@ -35,19 +44,20 @@ def add_agent(data) -> int: desc = common.checkAjaxInput(data.get("desc")) enabled = int(data.get("enabled")) agent_uuid = str(uuid.uuid4()) + check_agent_limit() try: inv, server_ips = generate_agent_inc(server_ip, 'install', agent_uuid) run_ansible(inv, server_ips, 'smon_agent') except Exception as e: - common_roxywi.handle_exceptions(e, server_ip, 'Cannot install SMON agent', roxywi=1, login=1) + roxywi_common.handle_exceptions(e, server_ip, 'Cannot install SMON agent', roxywi=1, login=1) try: last_id = smon_sql.add_agent(name, server_id, desc, enabled, agent_uuid) - common_roxywi.logging(server_ip, 'A new SMON agent has been created', roxywi=1, login=1, keep_history=1, service='SMON') + roxywi_common.logging(server_ip, 'A new SMON agent has been created', roxywi=1, login=1, keep_history=1, service='SMON') return last_id except Exception as e: - common_roxywi.handle_exceptions(e, 'Roxy-WI server', 'Cannot create Agent', roxywi=1, login=1) + roxywi_common.handle_exceptions(e, 'Roxy-WI server', 'Cannot create Agent', roxywi=1, login=1) def delete_agent(agent_id: int): @@ -57,7 +67,7 @@ def delete_agent(agent_id: int): inv, server_ips = generate_agent_inc(server_ip, 'uninstall', agent_uuid) run_ansible(inv, server_ips, 'smon_agent') except Exception as e: - common_roxywi.handle_exceptions(e, server_ip, 'Cannot uninstall SMON agent', roxywi=1, login=1) + roxywi_common.handle_exceptions(e, server_ip, 'Cannot uninstall SMON agent', roxywi=1, login=1) def update_agent(json_data): @@ -69,7 +79,7 @@ def update_agent(json_data): try: smon_sql.update_agent(agent_id, name, desc, enabled) except Exception as e: - common_roxywi.handle_exceptions(e, 'Roxy-WI server', f'Cannot update SMON agent: {agent_id}', roxywi=1, login=1) + roxywi_common.handle_exceptions(e, 'Roxy-WI server', f'Cannot update SMON agent: {agent_id}', roxywi=1, login=1) def get_agent_headers(agent_id: int) -> dict: @@ -110,11 +120,11 @@ def delete_check(agent_id: int, server_ip: str, check_id: int) -> bytes: req = requests.delete(f'http://{server_ip}:{agent_port}/check/{check_id}', headers=headers, timeout=5) return req.content except requests.exceptions.HTTPError as e: - common_roxywi.logging(server_ip, f'error: Cannot delete check from agent: http error {e}', roxywi=1, login=1) + roxywi_common.logging(server_ip, f'error: Cannot delete check from agent: http error {e}', roxywi=1, login=1) except requests.exceptions.ConnectTimeout: - common_roxywi.logging(server_ip, 'error: Cannot delete check from agent: connection timeout', roxywi=1, login=1) + roxywi_common.logging(server_ip, 'error: Cannot delete check from agent: connection timeout', roxywi=1, login=1) except requests.exceptions.ConnectionError: - common_roxywi.logging(server_ip, 'error: Cannot delete check from agent: connection error', roxywi=1, login=1) + roxywi_common.logging(server_ip, 'error: Cannot delete check from agent: connection error', roxywi=1, login=1) except Exception as e: raise Exception(f'error: Cannot delete check from Agent {server_ip}: {e}') @@ -195,16 +205,16 @@ def send_checks(agent_id: int) -> None: try: send_tcp_checks(agent_id, server_ip) except Exception as e: - raise Exception(f'{e}') + roxywi_common.logging(f'Agent ID: {agent_id}', f'error: Cannot send TCP checks: {e}', roxywi=1) try: send_ping_checks(agent_id, server_ip) except Exception as e: - raise Exception(f'{e}') + roxywi_common.logging(f'Agent ID: {agent_id}', f'error: Cannot send Ping checks: {e}', roxywi=1) try: send_dns_checks(agent_id, server_ip) except Exception as e: - raise Exception(f'{e}') + roxywi_common.logging(f'Agent ID: {agent_id}', f'error: Cannot send DNS checks: {e}', roxywi=1) try: send_http_checks(agent_id, server_ip) except Exception as e: - raise Exception(f'{e}') + roxywi_common.logging(f'Agent ID: {agent_id}', f'error: Cannot send HTTP checks: {e}', roxywi=1) diff --git a/app/templates/statsview.html b/app/templates/statsview.html index 6cdd4fdc..ef4eabd0 100644 --- a/app/templates/statsview.html +++ b/app/templates/statsview.html @@ -30,7 +30,7 @@