SMON agent fixes
pull/375/head
Aidaho 2024-02-24 20:45:43 +03:00
parent 447739c644
commit ee0bd7d0de
5 changed files with 79 additions and 39 deletions

View File

@ -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.db_model import SmonAgent, Server, SMON, SmonTcpCheck, SmonHttpCheck, SmonDnsCheck, SmonPingCheck, SmonHistory, SmonStatusPageCheck, SmonStatusPage
from app.modules.db.sql import out_error, get_setting 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') time_zone = get_setting('time_zone')
get_date = roxy_wi_tools.GetDate(time_zone) get_date = roxy_wi_tools.GetDate(time_zone)
@ -636,3 +636,10 @@ def select_body_status(smon_id):
out_error(e) out_error(e)
else: else:
return query_res return query_res
def count_agents() -> int:
try:
return SmonAgent.select().count()
except Exception as e:
out_error(e)

View File

@ -7,6 +7,7 @@ import app.modules.roxywi.common as roxywi_common
def create_smon(json_data, user_group, show_new=1) -> bool: def create_smon(json_data, user_group, show_new=1) -> bool:
try:
name = common.checkAjaxInput(json_data['name']) name = common.checkAjaxInput(json_data['name'])
hostname = common.checkAjaxInput(json_data['ip']) hostname = common.checkAjaxInput(json_data['ip'])
port = common.checkAjaxInput(json_data['port']) port = common.checkAjaxInput(json_data['port'])
@ -25,9 +26,13 @@ def create_smon(json_data, user_group, show_new=1) -> bool:
interval = common.checkAjaxInput(json_data['interval']) interval = common.checkAjaxInput(json_data['interval'])
agent_id = common.checkAjaxInput(json_data['agent_id']) agent_id = common.checkAjaxInput(json_data['agent_id'])
check_type = common.checkAjaxInput(json_data['check_type']) 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 == '': if agent_id == '':
raise Exception('warning: Select an Agent first') raise Exception('warning: Select an Agent first')
else:
agent_ip = smon_sql.select_server_ip_by_agent_id(agent_id)
if check_type == 'tcp': if check_type == 'tcp':
try: 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) smon_sql.insert_smon_dns(last_id, hostname, port, resolver, record_type, interval, agent_id)
if last_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: try:
api_path = f'check/{last_id}' api_path = f'check/{last_id}'
check_json = create_check_json(json_data) 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, agent_ip, api_path, check_json)
smon_agent.send_post_request_to_agent(agent_id, server_ip, api_path, check_json)
except Exception as e: 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: if last_id and show_new:
return last_id 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) is_edited = smon_sql.update_smonDns(smon_id, hostname, port, resolver, record_type, interval, agent_id)
if is_edited: 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: try:
api_path = f'check/{smon_id}' api_path = f'check/{smon_id}'
check_json = create_check_json(json_data) 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) server_ip = smon_sql.get_agent_ip_by_id(agent_id)
smon_agent.delete_check(agent_id, server_ip, smon_id) smon_agent.delete_check(agent_id, server_ip, smon_id)
except Exception as e: 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: try:
if smon_sql.delete_smon(smon_id, user_group): if smon_sql.delete_smon(smon_id, user_group):
roxywi_common.logging('SMON', ' The server from SMON has been delete ', roxywi=1, login=1) roxywi_common.logging('SMON', ' The server from SMON has been delete ', roxywi=1, login=1)

View File

@ -4,7 +4,7 @@ import requests
import app.modules.db.sql as sql import app.modules.db.sql as sql
import app.modules.db.smon as smon_sql import app.modules.db.smon as smon_sql
import app.modules.common.common as common 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 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 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: def add_agent(data) -> int:
name = common.checkAjaxInput(data.get("name")) name = common.checkAjaxInput(data.get("name"))
server_id = int(data.get("server_id")) server_id = int(data.get("server_id"))
@ -35,19 +44,20 @@ def add_agent(data) -> int:
desc = common.checkAjaxInput(data.get("desc")) desc = common.checkAjaxInput(data.get("desc"))
enabled = int(data.get("enabled")) enabled = int(data.get("enabled"))
agent_uuid = str(uuid.uuid4()) agent_uuid = str(uuid.uuid4())
check_agent_limit()
try: try:
inv, server_ips = generate_agent_inc(server_ip, 'install', agent_uuid) inv, server_ips = generate_agent_inc(server_ip, 'install', agent_uuid)
run_ansible(inv, server_ips, 'smon_agent') run_ansible(inv, server_ips, 'smon_agent')
except Exception as e: 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: try:
last_id = smon_sql.add_agent(name, server_id, desc, enabled, agent_uuid) 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 return last_id
except Exception as e: 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): 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) inv, server_ips = generate_agent_inc(server_ip, 'uninstall', agent_uuid)
run_ansible(inv, server_ips, 'smon_agent') run_ansible(inv, server_ips, 'smon_agent')
except Exception as e: 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): def update_agent(json_data):
@ -69,7 +79,7 @@ def update_agent(json_data):
try: try:
smon_sql.update_agent(agent_id, name, desc, enabled) smon_sql.update_agent(agent_id, name, desc, enabled)
except Exception as e: 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: 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) req = requests.delete(f'http://{server_ip}:{agent_port}/check/{check_id}', headers=headers, timeout=5)
return req.content return req.content
except requests.exceptions.HTTPError as e: 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: 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: 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: except Exception as e:
raise Exception(f'error: Cannot delete check from Agent {server_ip}: {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: try:
send_tcp_checks(agent_id, server_ip) send_tcp_checks(agent_id, server_ip)
except Exception as e: 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: try:
send_ping_checks(agent_id, server_ip) send_ping_checks(agent_id, server_ip)
except Exception as e: 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: try:
send_dns_checks(agent_id, server_ip) send_dns_checks(agent_id, server_ip)
except Exception as e: 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: try:
send_http_checks(agent_id, server_ip) send_http_checks(agent_id, server_ip)
except Exception as e: 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)

View File

@ -30,7 +30,7 @@
<div id="ajax" style="margin-left: 15px; margin-right: 5px;"></div> <div id="ajax" style="margin-left: 15px; margin-right: 5px;"></div>
<script> <script>
if (cur_url[1]) { if (cur_url[2]) {
showStats(); showStats();
} }
async function wait() { async function wait() {

View File

@ -661,6 +661,22 @@ function deleteStatusPage(page_id) {
} }
}); });
} }
function checkAgentLimit() {
let return_value = false;
$.ajax({
url: '/app/smon/agent/count',
async: false,
success: function (data) {
data = data.replace(/\s+/g, ' ');
if (data.indexOf('error:') != '-1') {
toastr.error(data);
} else {
return_value = true;
}
}
});
return return_value;
}
function addAgentDialog(agent_id=0, edit=false) { function addAgentDialog(agent_id=0, edit=false) {
cleanAgentAddForm(); cleanAgentAddForm();
let tabel_title = $("#add-agent-page-overview").attr('title'); let tabel_title = $("#add-agent-page-overview").attr('title');
@ -669,6 +685,9 @@ function addAgentDialog(agent_id=0, edit=false) {
tabel_title = $("#add-agent-page-overview").attr('data-edit'); tabel_title = $("#add-agent-page-overview").attr('data-edit');
getAgentSettings(agent_id); getAgentSettings(agent_id);
} else { } else {
if (!checkAgentLimit()) {
return false;
}
getFreeServers(); getFreeServers();
} }
let dialogTable = $("#add-agent-page").dialog({ let dialogTable = $("#add-agent-page").dialog({