2023-06-03 15:04:22 +00:00
|
|
|
import json
|
|
|
|
|
2023-05-03 05:46:32 +00:00
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
|
|
|
|
import modules.db.sql as sql
|
|
|
|
import modules.common.common as common
|
|
|
|
import modules.roxywi.common as roxywi_common
|
|
|
|
|
|
|
|
form = common.form
|
|
|
|
|
|
|
|
|
|
|
|
def create_smon() -> None:
|
|
|
|
user_group = roxywi_common.get_user_group(id=1)
|
2023-06-03 15:04:22 +00:00
|
|
|
name = common.checkAjaxInput(form.getvalue('newsmonname'))
|
|
|
|
hostname = common.checkAjaxInput(form.getvalue('newsmon'))
|
2023-05-03 05:46:32 +00:00
|
|
|
port = common.checkAjaxInput(form.getvalue('newsmonport'))
|
|
|
|
enable = common.checkAjaxInput(form.getvalue('newsmonenable'))
|
2023-06-03 15:04:22 +00:00
|
|
|
url = common.checkAjaxInput(form.getvalue('newsmonurl'))
|
2023-05-03 05:46:32 +00:00
|
|
|
body = common.checkAjaxInput(form.getvalue('newsmonbody'))
|
|
|
|
group = common.checkAjaxInput(form.getvalue('newsmongroup'))
|
|
|
|
desc = common.checkAjaxInput(form.getvalue('newsmondescription'))
|
|
|
|
telegram = common.checkAjaxInput(form.getvalue('newsmontelegram'))
|
|
|
|
slack = common.checkAjaxInput(form.getvalue('newsmonslack'))
|
|
|
|
pd = common.checkAjaxInput(form.getvalue('newsmonpd'))
|
2023-06-03 15:04:22 +00:00
|
|
|
check_type = common.checkAjaxInput(form.getvalue('newsmonchecktype'))
|
2023-06-16 03:51:26 +00:00
|
|
|
resolver = common.checkAjaxInput(form.getvalue('newsmonresserver'))
|
|
|
|
record_type = common.checkAjaxInput(form.getvalue('newsmondns_record_type'))
|
2023-06-03 15:04:22 +00:00
|
|
|
|
|
|
|
if check_type == 'tcp':
|
|
|
|
try:
|
|
|
|
port = int(port)
|
|
|
|
except Exception:
|
2023-06-16 03:51:26 +00:00
|
|
|
print('SMON error: port must be a number')
|
2023-06-03 15:04:22 +00:00
|
|
|
return None
|
|
|
|
if port > 65535 or port < 0:
|
|
|
|
print('SMON error: port must be 0-65535')
|
|
|
|
return None
|
|
|
|
|
|
|
|
last_id = sql.insert_smon(name, enable, group, desc, telegram, slack, pd, user_group, check_type)
|
|
|
|
|
|
|
|
if check_type == 'ping':
|
|
|
|
sql.insert_smon_ping(last_id, hostname)
|
|
|
|
elif check_type == 'tcp':
|
|
|
|
sql.insert_smon_tcp(last_id, hostname, port)
|
|
|
|
elif check_type == 'http':
|
|
|
|
sql.insert_smon_http(last_id, url, body)
|
2023-06-16 03:51:26 +00:00
|
|
|
elif check_type == 'dns':
|
|
|
|
sql.insert_smon_dns(last_id, hostname, port, resolver, record_type)
|
2023-05-03 05:46:32 +00:00
|
|
|
|
|
|
|
if last_id:
|
|
|
|
lang = roxywi_common.get_user_lang()
|
|
|
|
smon = sql.select_smon_by_id(last_id)
|
|
|
|
pds = sql.get_user_pd_by_group(user_group)
|
|
|
|
slacks = sql.get_user_slack_by_group(user_group)
|
|
|
|
telegrams = sql.get_user_telegram_by_group(user_group)
|
2023-06-03 15:04:22 +00:00
|
|
|
smon_service = sql.select_smon_check_by_id(last_id, check_type)
|
2023-05-03 05:46:32 +00:00
|
|
|
env = Environment(loader=FileSystemLoader('templates'), autoescape=True)
|
|
|
|
template = env.get_template('ajax/show_new_smon.html')
|
2023-06-03 15:04:22 +00:00
|
|
|
template = template.render(smon=smon, telegrams=telegrams, slacks=slacks, pds=pds, lang=lang, check_type=check_type,
|
|
|
|
smon_service=smon_service)
|
2023-05-03 05:46:32 +00:00
|
|
|
print(template)
|
2023-06-03 15:04:22 +00:00
|
|
|
roxywi_common.logging('SMON', f' A new server {name} to SMON has been add ', roxywi=1, login=1)
|
2023-05-03 05:46:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def update_smon() -> None:
|
|
|
|
smon_id = common.checkAjaxInput(form.getvalue('id'))
|
2023-06-03 15:04:22 +00:00
|
|
|
name = common.checkAjaxInput(form.getvalue('updateSmonName'))
|
2023-05-03 05:46:32 +00:00
|
|
|
ip = common.checkAjaxInput(form.getvalue('updateSmonIp'))
|
|
|
|
port = common.checkAjaxInput(form.getvalue('updateSmonPort'))
|
|
|
|
en = common.checkAjaxInput(form.getvalue('updateSmonEn'))
|
2023-06-03 15:04:22 +00:00
|
|
|
url = common.checkAjaxInput(form.getvalue('updateSmonUrl'))
|
2023-05-03 05:46:32 +00:00
|
|
|
body = common.checkAjaxInput(form.getvalue('updateSmonBody'))
|
|
|
|
telegram = common.checkAjaxInput(form.getvalue('updateSmonTelegram'))
|
|
|
|
slack = common.checkAjaxInput(form.getvalue('updateSmonSlack'))
|
|
|
|
pd = common.checkAjaxInput(form.getvalue('updateSmonPD'))
|
|
|
|
group = common.checkAjaxInput(form.getvalue('updateSmonGroup'))
|
|
|
|
desc = common.checkAjaxInput(form.getvalue('updateSmonDesc'))
|
2023-06-03 15:04:22 +00:00
|
|
|
check_type = common.checkAjaxInput(form.getvalue('check_type'))
|
2023-06-16 03:51:26 +00:00
|
|
|
resolver = common.checkAjaxInput(form.getvalue('updateSmonResServer'))
|
|
|
|
record_type = common.checkAjaxInput(form.getvalue('updateSmonRecordType'))
|
2023-06-03 15:04:22 +00:00
|
|
|
is_edited = False
|
|
|
|
|
|
|
|
if check_type == 'tcp':
|
|
|
|
try:
|
|
|
|
port = int(port)
|
|
|
|
except Exception:
|
|
|
|
print('SMON error: port must number')
|
|
|
|
return None
|
|
|
|
if port > 65535 or port < 0:
|
|
|
|
print('SMON error: port must be 0-65535')
|
|
|
|
return None
|
2023-05-03 05:46:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
roxywi_common.check_user_group()
|
|
|
|
try:
|
2023-06-03 15:04:22 +00:00
|
|
|
if sql.update_smon(smon_id, name, telegram, slack, pd, group, desc, en):
|
|
|
|
if check_type == 'http':
|
|
|
|
is_edited = sql.update_smonHttp(smon_id, url, body)
|
|
|
|
elif check_type == 'tcp':
|
|
|
|
is_edited = sql.update_smonTcp(smon_id, ip, port)
|
|
|
|
elif check_type == 'ping':
|
2023-06-16 03:51:26 +00:00
|
|
|
is_edited = sql.update_smonPing(smon_id, ip)
|
|
|
|
elif check_type == 'dns':
|
|
|
|
is_edited = sql.update_smonDns(smon_id, ip, port, resolver, record_type)
|
2023-06-03 15:04:22 +00:00
|
|
|
|
|
|
|
if is_edited:
|
|
|
|
print("Ok")
|
|
|
|
roxywi_common.logging('SMON', f' The SMON server {name} has been update ', roxywi=1, login=1)
|
2023-05-03 05:46:32 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
|
|
|
|
|
|
|
|
def show_smon() -> None:
|
|
|
|
user_group = roxywi_common.get_user_group(id=1)
|
|
|
|
lang = roxywi_common.get_user_lang()
|
|
|
|
sort = common.checkAjaxInput(form.getvalue('sort'))
|
|
|
|
env = Environment(loader=FileSystemLoader('templates'), autoescape=True)
|
|
|
|
template = env.get_template('ajax/smon_dashboard.html')
|
|
|
|
template = template.render(smon=sql.smon_list(user_group), sort=sort, lang=lang, update=1)
|
|
|
|
print(template)
|
|
|
|
|
|
|
|
|
|
|
|
def delete_smon() -> None:
|
|
|
|
user_group = roxywi_common.get_user_group(id=1)
|
|
|
|
smon_id = common.checkAjaxInput(form.getvalue('smondel'))
|
|
|
|
|
|
|
|
if roxywi_common.check_user_group():
|
|
|
|
try:
|
|
|
|
if sql.delete_smon(smon_id, user_group):
|
|
|
|
print('Ok')
|
|
|
|
roxywi_common.logging('SMON', ' The server from SMON has been delete ', roxywi=1, login=1)
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2023-06-03 15:04:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def history_metrics(server_id: int, check_id: int) -> None:
|
|
|
|
metric = sql.select_smon_history(server_id, check_id)
|
|
|
|
|
|
|
|
metrics = {'chartData': {}}
|
|
|
|
metrics['chartData']['labels'] = {}
|
|
|
|
labels = ''
|
|
|
|
curr_con = ''
|
|
|
|
|
|
|
|
for i in reversed(metric):
|
|
|
|
labels += f'{i.date.time()},'
|
|
|
|
curr_con += f'{i.response_time},'
|
|
|
|
|
|
|
|
metrics['chartData']['labels'] = labels
|
|
|
|
metrics['chartData']['curr_con'] = curr_con
|
|
|
|
|
|
|
|
print(json.dumps(metrics))
|