Skip to content

Commit

Permalink
v1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Aidaho12 committed Aug 14, 2024
1 parent cc5db1d commit d0f8ac4
Show file tree
Hide file tree
Showing 23 changed files with 825 additions and 221 deletions.
5 changes: 4 additions & 1 deletion app/api/v1/routes/rmon/routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from app.api.v1.routes.rmon import bp
from app.views.agent.views import AgentView, AgentsView
from app.views.check.views import (
CheckHttpView, CheckTcpView, CheckDnsView, CheckPingView, ChecksViewHttp, ChecksViewDns, ChecksViewTcp, ChecksViewPing
CheckHttpView, CheckTcpView, CheckDnsView, CheckPingView, ChecksViewHttp, ChecksViewDns, ChecksViewTcp,
ChecksViewPing, CheckSmtpView, ChecksViewSmtp
)


Expand All @@ -16,9 +17,11 @@ def register_api(view, endpoint, url, pk='check_id', pk_type='int'):
bp.add_url_rule('/checks/dns', view_func=ChecksViewDns.as_view('dns_checks'))
bp.add_url_rule('/checks/tcp', view_func=ChecksViewTcp.as_view('tcp_checks'))
bp.add_url_rule('/checks/ping', view_func=ChecksViewPing.as_view('ping_checks'))
bp.add_url_rule('/checks/smtp', view_func=ChecksViewSmtp.as_view('smtp_checks'))

register_api(AgentView, 'agent', '/agent', 'agent_id')
register_api(CheckHttpView, 'http_check', '/check/http', 'check_id')
register_api(CheckTcpView, 'tcp_check', '/check/tcp', 'check_id')
register_api(CheckPingView, 'ping_check', '/check/ping', 'check_id')
register_api(CheckDnsView, 'dns_check', '/check/dns', 'check_id')
register_api(CheckSmtpView, 'smtp_check', '/check/smtp', 'check_id')
8 changes: 0 additions & 8 deletions app/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ def delete_action_history_for_period():
history_sql.delete_action_history_for_period()


@scheduler.task('interval', id='delete_action_alerts_for_period', minutes=75, misfire_grace_time=None)
def delete_action_alerts_for_period():
app = scheduler.app
period = sql.get_setting('checker_keep_history_range')
with app.app_context():
history_sql.delete_alert_history(period, 'RMON')


@scheduler.task('interval', id='delete_old_logs', hours=1, misfire_grace_time=None)
def delete_old_logs():
app = scheduler.app
Expand Down
26 changes: 21 additions & 5 deletions app/modules/db/db_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,18 +218,19 @@ class Meta:
constraints = [SQL('UNIQUE (name, port)')]


class Alerts(BaseModel):
class RMONAlertsHistory(BaseModel):
id = AutoField()
name = CharField()
message = CharField()
level = CharField()
ip = CharField()
rmon_id = ForeignKeyField(SMON, on_delete='Cascade')
port = IntegerField()
user_group = IntegerField(constraints=[SQL('DEFAULT 1')])
service = CharField()
date = DateTimeField(default=datetime.now)

class Meta:
table_name = 'alerts'
primary_key = False
table_name = 'rmon_alerts_history'


class ActionHistory(BaseModel):
Expand Down Expand Up @@ -318,6 +319,21 @@ class Meta:
primary_key = False


class SmonSMTPCheck(BaseModel):
smon_id = ForeignKeyField(SMON, on_delete='Cascade', unique=True)
ip = CharField()
port = IntegerField()
username = CharField()
password = CharField()
use_tls = IntegerField(constraints=[SQL('DEFAULT 1')])
interval = IntegerField(constraints=[SQL('DEFAULT 120')])
agent_id = IntegerField(constraints=[SQL('DEFAULT 1')])

class Meta:
table_name = 'smon_smtp_check'
primary_key = False


class SmonHttpCheck(BaseModel):
smon_id = ForeignKeyField(SMON, on_delete='Cascade', unique=True)
url = CharField()
Expand Down Expand Up @@ -400,5 +416,5 @@ def create_tables():
conn.create_tables(
[Groups, User, Server, Role, Telegram, Slack, UserGroups, Setting, Cred, Version, ActionHistory,
SystemInfo, UserName, PD, SmonHistory, SmonAgent, SmonTcpCheck, SmonHttpCheck, SmonPingCheck, SmonDnsCheck, RoxyTool,
SmonStatusPage, SmonStatusPageCheck, SMON, Alerts, SmonGroup, MM]
SmonStatusPage, SmonStatusPageCheck, SMON, SmonGroup, MM, RMONAlertsHistory, SmonSMTPCheck]
)
26 changes: 13 additions & 13 deletions app/modules/db/history.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
from app.modules.db.db_model import ActionHistory, Alerts
from app.modules.db.db_model import ActionHistory, RMONAlertsHistory
from app.modules.db.sql import get_setting
from app.modules.db.common import out_error
import app.modules.roxy_wi_tools as roxy_wi_tools


def alerts_history(service, user_group, **kwargs):
if kwargs.get('host'):
query = Alerts.select().where(
(Alerts.service == service) &
(Alerts.ip == kwargs.get('host')) &
(Alerts.user_group == user_group)
if kwargs.get('check_id'):
query = RMONAlertsHistory.select().where(
(RMONAlertsHistory.service == service) &
(RMONAlertsHistory.rmon_id == kwargs.get('check_id')) &
(RMONAlertsHistory.user_group == user_group)
)
else:
query = Alerts.select().where(Alerts.service == service)
query = RMONAlertsHistory.select().where(RMONAlertsHistory.service == service)
try:
return query.execute()
except Exception as e:
out_error(e)


def insert_alerts(user_group, level, ip, port, message, service):
def insert_alerts(check_id, user_group, level, check_name, port, message, service):
get_date = roxy_wi_tools.GetDate()
cur_date = get_date.return_date('regular')
try:
Alerts.insert(
user_group=user_group, message=message, level=level, ip=ip, port=port, service=service,
date=cur_date
RMONAlertsHistory.insert(
user_group=user_group, message=message, level=level, port=port, service=service,
date=cur_date, rmon_id=check_id, name=check_name
).execute()
except Exception as e:
out_error(e)
Expand All @@ -34,8 +34,8 @@ def insert_alerts(user_group, level, ip, port, message, service):
def delete_alert_history(keep_interval: int, service: str):
get_date = roxy_wi_tools.GetDate()
cur_date = get_date.return_date('regular', timedelta_minus=keep_interval)
query = Alerts.delete().where(
(Alerts.date < cur_date) & (Alerts.service == service)
query = RMONAlertsHistory.delete().where(
(RMONAlertsHistory.date < cur_date) & (RMONAlertsHistory.service == service)
)
try:
query.execute()
Expand Down
89 changes: 52 additions & 37 deletions app/modules/db/smon.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from peewee import fn

from app.modules.db.db_model import (
SmonAgent, Server, SMON, SmonTcpCheck, SmonHttpCheck, SmonDnsCheck, SmonPingCheck, SmonHistory, SmonStatusPageCheck, SmonStatusPage, SmonGroup
SmonAgent, Server, SMON, SmonTcpCheck, SmonHttpCheck, SmonDnsCheck, SmonPingCheck, SmonHistory, SmonStatusPageCheck,
SmonStatusPage, SmonGroup, SmonSMTPCheck
)
from app.modules.db.common import out_error
import app.modules.roxy_wi_tools as roxy_wi_tools
Expand Down Expand Up @@ -153,41 +154,49 @@ def select_server_ip_by_agent_id(agent_id: int) -> str:
out_error(e)


def select_en_smon_tcp(agent_id) -> object:
try:
return SmonTcpCheck.select(SmonTcpCheck, SMON).join_from(SmonTcpCheck, SMON).where((SMON.enabled == '1') & (SmonTcpCheck.agent_id == agent_id)).execute()
except Exception as e:
out_error(e)


def select_en_smon_ping(agent_id) -> object:
query = SmonPingCheck.select(SmonPingCheck, SMON).join_from(SmonPingCheck, SMON).where((SMON.enabled == '1') & (SmonPingCheck.agent_id == agent_id))
try:
query_res = query.execute()
except Exception as e:
out_error(e)
else:
return query_res


def select_en_smon_dns(agent_id) -> object:
query = SmonDnsCheck.select(SmonDnsCheck, SMON).join_from(SmonDnsCheck, SMON).where((SMON.enabled == '1') & (SmonDnsCheck.agent_id == agent_id))
try:
query_res = query.execute()
except Exception as e:
out_error(e)
else:
return query_res


def select_en_smon_http(agent_id) -> object:
query = SmonHttpCheck.select(SmonHttpCheck, SMON).join_from(SmonHttpCheck, SMON).where((SMON.enabled == '1') & (SmonHttpCheck.agent_id == agent_id))
try:
query_res = query.execute()
# def select_en_smon_tcp(agent_id) -> object:
# try:
# return SmonTcpCheck.select(SmonTcpCheck, SMON).join_from(SmonTcpCheck, SMON).where((SMON.enabled == '1') & (SmonTcpCheck.agent_id == agent_id)).execute()
# except Exception as e:
# out_error(e)
#
#
# def select_en_smon_ping(agent_id) -> object:
# query = SmonPingCheck.select(SmonPingCheck, SMON).join_from(SmonPingCheck, SMON).where((SMON.enabled == '1') & (SmonPingCheck.agent_id == agent_id))
# try:
# query_res = query.execute()
# except Exception as e:
# out_error(e)
# else:
# return query_res
#
#
# def select_en_smon_dns(agent_id) -> object:
# query = SmonDnsCheck.select(SmonDnsCheck, SMON).join_from(SmonDnsCheck, SMON).where((SMON.enabled == '1') & (SmonDnsCheck.agent_id == agent_id))
# try:
# query_res = query.execute()
# except Exception as e:
# out_error(e)
# else:
# return query_res
#
#
# def select_en_smon_http(agent_id) -> object:
# query = SmonHttpCheck.select(SmonHttpCheck, SMON).join_from(SmonHttpCheck, SMON).where((SMON.enabled == '1') & (SmonHttpCheck.agent_id == agent_id))
# try:
# query_res = query.execute()
# except Exception as e:
# out_error(e)
# else:
# return query_res


def select_en_smon(agent_id: int, check_type: str) -> Union[SmonTcpCheck, SmonPingCheck, SmonDnsCheck, SmonHttpCheck, SmonSMTPCheck]:
model = tool_common.get_model_for_check(check_type=check_type)
try:
return model.select(model, SMON).join_from(model, SMON).where((SMON.enabled == '1') & (model.agent_id == agent_id)).execute()
except Exception as e:
out_error(e)
else:
return query_res


def select_status(smon_id):
Expand Down Expand Up @@ -249,7 +258,6 @@ def insert_smon_history_http_metrics(date, **kwargs) -> None:

def select_one_smon(smon_id: int, check_type_id: int) -> tuple:
correct_model = tool_common.get_model_for_check(check_type_id=check_type_id)

try:
return correct_model.select(correct_model, SMON).join_from(correct_model, SMON).where(SMON.id == smon_id).execute()
except correct_model.DoesNotExist:
Expand All @@ -266,15 +274,22 @@ def insert_smon(name, enable, group_id, desc, telegram, slack, pd, mm, user_grou
).execute()
return last_id
except Exception as e:
print('error')
out_error(e)


def insert_smon_ping(smon_id, hostname, packet_size, interval, agent_id):
try:
SmonPingCheck.insert(smon_id=smon_id, ip=hostname, packet_size=packet_size, interval=interval, agent_id=agent_id).on_conflict('replace').execute()
except Exception as e:
print('123')
out_error(e)


def insert_smon_smtp(smon_id, hostname, port, username, password, interval, agent_id):
try:
SmonSMTPCheck.insert(
smon_id=smon_id, ip=hostname, port=port, username=username, password=password, interval=interval, agent_id=agent_id
).on_conflict('replace').execute()
except Exception as e:
out_error(e)


Expand Down
9 changes: 8 additions & 1 deletion app/modules/roxywi/class_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,16 @@ class DnsCheckRequest(BaseCheckRequest):
ip: Union[IPvAnyAddress, DomainName]


class SmtpCheckRequest(BaseCheckRequest):
username: EscapedString
password: EscapedString
port: Annotated[int, Gt(1), Le(65535)] = 53
ip: Union[IPvAnyAddress, DomainName]


class PingCheckRequest(BaseCheckRequest):
ip: Union[IPvAnyAddress, DomainName]
packet_size: Annotated[int, Le(16)]
packet_size: Annotated[int, Gt(16)]


class TcpCheckRequest(BaseCheckRequest):
Expand Down
8 changes: 6 additions & 2 deletions app/modules/tools/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import app.modules.db.roxy as roxy_sql
import app.modules.roxywi.roxy as roxywi_mod
import app.modules.server.server as server_mod
from app.modules.db.db_model import SmonTcpCheck, SmonHttpCheck, SmonDnsCheck, SmonPingCheck
from app.modules.db.db_model import SmonTcpCheck, SmonHttpCheck, SmonDnsCheck, SmonPingCheck, SmonSMTPCheck


def get_services_status(update_cur_ver=0):
Expand Down Expand Up @@ -95,19 +95,23 @@ def get_cur_tool_version(tool_name: str) -> str:
return roxy_sql.get_tool_cur_version(tool_name)


def get_model_for_check(check_type: str = None, check_type_id: int = None) -> Type[Union[SmonTcpCheck, SmonHttpCheck, SmonDnsCheck, SmonPingCheck]]:
def get_model_for_check(
check_type: str = None, check_type_id: int = None
) -> Type[Union[SmonTcpCheck, SmonHttpCheck, SmonDnsCheck, SmonPingCheck, SmonSMTPCheck]]:
if check_type:
check_models = {
'tcp': SmonTcpCheck,
'http': SmonHttpCheck,
'dns': SmonDnsCheck,
'ping': SmonPingCheck,
'smtp': SmonSMTPCheck,
}
elif check_type_id:
check_type = str(check_type_id)
check_models = {
'1': SmonTcpCheck,
'2': SmonHttpCheck,
'3': SmonSMTPCheck,
'5': SmonDnsCheck,
'4': SmonPingCheck,
}
Expand Down
Loading

0 comments on commit d0f8ac4

Please sign in to comment.