Skip to content

Commit

Permalink
v1.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Aidaho12 committed Aug 17, 2024
1 parent d0f8ac4 commit 1f4579f
Show file tree
Hide file tree
Showing 30 changed files with 643 additions and 259 deletions.
2 changes: 2 additions & 0 deletions app/api/v1/routes/rmon/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
CheckHttpView, CheckTcpView, CheckDnsView, CheckPingView, ChecksViewHttp, ChecksViewDns, ChecksViewTcp,
ChecksViewPing, CheckSmtpView, ChecksViewSmtp
)
from app.views.check.status_page_views import StatusPageView


def register_api(view, endpoint, url, pk='check_id', pk_type='int'):
Expand All @@ -25,3 +26,4 @@ def register_api(view, endpoint, url, pk='check_id', pk_type='int'):
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')
register_api(StatusPageView, 'status_page', '/status-page', 'page_id')
22 changes: 20 additions & 2 deletions app/create_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ def update_db_v_1_0_7_1():
def update_db_v_1_1():
try:
migrate(
migrator.rename_column('smon_status_pages', 'style', 'custom_style'),
migrator.rename_column('smon_status_pages', 'desc', 'description'),
migrator.rename_column('telegram', 'groups', 'group_id'),
migrator.rename_column('slack', 'groups', 'group_id'),
migrator.rename_column('mattermost', 'groups', 'group_id'),
Expand All @@ -187,17 +189,32 @@ def update_db_v_1_1():
migrator.rename_column('cred', 'groups', 'group_id'),
)
except Exception as e:
if e.args[0] == 'no such column: "groups"' or str(e) == '(1060, no such column: "groups")':
if e.args[0] == 'no such column: "style"' or str(e) == '(1060, no such column: "style")':
print("Updating... DB has been updated to version 1.1")
elif e.args[0] == "'bool' object has no attribute 'sql'":
print("Updating... DB has been updated to version 1.1")
else:
print("An error occurred:", e)


def update_db_v_1_1_2():
try:
migrate(
migrator.add_column('smon_smtp_check', 'ignore_ssl_error', IntegerField(default=0)),
migrator.add_column_default('smon_smtp_check', 'ignore_ssl_error', 0),
migrator.add_column('smon_http_check', 'ignore_ssl_error', IntegerField(default=0)),
migrator.add_column_default('smon_http_check', 'ignore_ssl_error', 0),
)
except Exception as e:
if e.args[0] == 'duplicate column name: ignore_ssl_error' or str(e) == '(1060, "Duplicate column name \'ignore_ssl_error\'")':
print('Updating... DB has been updated to version 1.1.2')
else:
print("An error occurred:", e)


def update_ver():
try:
Version.update(version='1.1.0').execute()
Version.update(version='1.1.2').execute()
except Exception:
print('Cannot update version')

Expand All @@ -219,6 +236,7 @@ def update_all():
update_db_v_1_0_7()
update_db_v_1_0_7_1()
update_db_v_1_1()
update_db_v_1_1_2()


if __name__ == "__main__":
Expand Down
50 changes: 50 additions & 0 deletions app/modules/common/common_classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from typing import Union

from flask import g

import app.modules.db.server as server_sql
import app.modules.roxywi.common as roxywi_common
from app.modules.roxywi.class_models import ServerRequest, GroupQuery, CredRequest, ChannelRequest, StatusPageRequest
from app.middleware import get_user_params


class SupportClass:
def __init__(self, is_id=True):
self.is_id = is_id

@get_user_params()
def return_server_ip_or_id(self, server_id: Union[int, str]) -> Union[int, str]:
if isinstance(server_id, str):
try:
server = server_sql.get_server_by_ip(server_id)
except Exception as e:
raise e
else:
try:
server = server_sql.get_server_by_id(server_id)
except Exception as e:
raise e
try:
roxywi_common.is_user_has_access_to_group(g.user_params['user_id'], server.group_id)
except Exception as e:
roxywi_common.handler_exceptions_for_json_data(e, '')

if self.is_id:
return server.server_id
else:
return server.ip

@staticmethod
@get_user_params()
def return_group_id(body: Union[ServerRequest, CredRequest, GroupQuery, ChannelRequest, StatusPageRequest]):
if body.group_id:
if g.user_params['role'] == 1:
return body.group_id
else:
try:
roxywi_common.is_user_has_access_to_group(g.user_params['user_id'], body.group_id)
return body.group_id
except Exception:
return int(g.user_params['group_id'])
else:
return int(g.user_params['group_id'])
6 changes: 4 additions & 2 deletions app/modules/db/db_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ class SmonSMTPCheck(BaseModel):
use_tls = IntegerField(constraints=[SQL('DEFAULT 1')])
interval = IntegerField(constraints=[SQL('DEFAULT 120')])
agent_id = IntegerField(constraints=[SQL('DEFAULT 1')])
ignore_ssl_error = IntegerField(constraints=[SQL('DEFAULT 0')])

class Meta:
table_name = 'smon_smtp_check'
Expand All @@ -344,6 +345,7 @@ class SmonHttpCheck(BaseModel):
agent_id = IntegerField(constraints=[SQL('DEFAULT 1')])
headers = CharField(null=True)
body_req = CharField(null=True)
ignore_ssl_error = IntegerField(constraints=[SQL('DEFAULT 0')])

class Meta:
table_name = 'smon_http_check'
Expand Down Expand Up @@ -380,9 +382,9 @@ class SmonStatusPage(BaseModel):
id = AutoField()
name = CharField()
slug = CharField(unique=True)
desc = CharField(null=True)
description = CharField(null=True)
group_id = IntegerField()
style = TextField(null=True)
custom_style = TextField(null=True)

class Meta:
table_name = 'smon_status_pages'
Expand Down
18 changes: 18 additions & 0 deletions app/modules/db/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ def get_server_group(server_ip):
return out_error(e)


def get_server_by_id(server_id: int) -> Server:
try:
return Server.get(Server.server_id == server_id)
except Server.DoesNotExist:
raise RoxywiResourceNotFound
except Exception as e:
return out_error(e)


def get_server_by_ip(server_ip: str) -> Server:
try:
return Server.get(Server.ip == server_ip)
except Server.DoesNotExist:
raise RoxywiResourceNotFound
except Exception as e:
return out_error(e)


def select_server_by_name(name):
try:
ip = Server.get(Server.hostname == name)
Expand Down
63 changes: 18 additions & 45 deletions app/modules/db/smon.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,43 +154,6 @@ 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()
# 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:
Expand Down Expand Up @@ -284,10 +247,10 @@ def insert_smon_ping(smon_id, hostname, packet_size, interval, agent_id):
out_error(e)


def insert_smon_smtp(smon_id, hostname, port, username, password, interval, agent_id):
def insert_smon_smtp(smon_id, hostname, port, username, password, interval, agent_id, ignore_ssl_error):
try:
SmonSMTPCheck.insert(
smon_id=smon_id, ip=hostname, port=port, username=username, password=password, interval=interval, agent_id=agent_id
smon_id=smon_id, ip=hostname, port=port, username=username, password=password, interval=interval, agent_id=agent_id, ignore_ssl_error=ignore_ssl_error
).on_conflict('replace').execute()
except Exception as e:
out_error(e)
Expand All @@ -309,10 +272,11 @@ def insert_smon_dns(smon_id: int, hostname: str, port: int, resolver: str, recor
out_error(e)


def insert_smon_http(smon_id, url, body, http_method, interval, agent_id, body_req, header_req, status_code):
def insert_smon_http(smon_id, url, body, http_method, interval, agent_id, body_req, header_req, status_code, ignore_ssl_error):
try:
SmonHttpCheck.insert(
smon_id=smon_id, url=url, body=body, method=http_method, interval=interval, agent_id=agent_id, body_req=body_req, headers=header_req, accepted_status_codes=status_code
smon_id=smon_id, url=url, body=body, method=http_method, interval=interval, agent_id=agent_id, body_req=body_req,
headers=header_req, accepted_status_codes=status_code, ignore_ssl_error=ignore_ssl_error
).on_conflict('replace').execute()
except Exception as e:
out_error(e)
Expand Down Expand Up @@ -365,9 +329,9 @@ def smon_list(user_group):
return query_res


def add_status_page(name: str, slug: str, desc: str, group_id: int, checks: list) -> int:
def add_status_page(name: str, slug: str, desc: str, group_id: int, checks: list, styles: str) -> int:
try:
last_id = SmonStatusPage.insert(name=name, slug=slug, group_id=group_id, desc=desc).execute()
last_id = SmonStatusPage.insert(name=name, slug=slug, group_id=group_id, description=desc, custom_style=styles).execute()
except Exception as e:
if 'Duplicate entry' in str(e):
raise Exception('error: The Slug is already taken, please enter another one')
Expand All @@ -378,9 +342,9 @@ def add_status_page(name: str, slug: str, desc: str, group_id: int, checks: list
return last_id


def edit_status_page(page_id: int, name: str, slug: str, desc: str) -> None:
def edit_status_page(page_id: int, name: str, slug: str, desc: str, styles: str) -> None:
try:
SmonStatusPage.update(name=name, slug=slug, desc=desc).where(SmonStatusPage.id == page_id).execute()
SmonStatusPage.update(name=name, slug=slug, description=desc, custom_style=styles).where(SmonStatusPage.id == page_id).execute()
except Exception as e:
out_error(e)

Expand Down Expand Up @@ -409,6 +373,15 @@ def select_status_pages(group_id: int):
return query_res


def select_status_page_with_group(page_id: int, group_id: int) -> SmonStatusPage:
try:
return SmonStatusPage.get((SmonStatusPage.group_id == group_id) & (SmonStatusPage.id == page_id))
except SmonStatusPage.DoesNotExist:
raise RoxywiResourceNotFound
except Exception as e:
out_error(e)


def select_status_page_by_id(page_id: int):
try:
query_res = SmonStatusPage.select().where(SmonStatusPage.id == page_id).execute()
Expand Down
11 changes: 11 additions & 0 deletions app/modules/roxywi/class_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class HttpCheckRequest(BaseCheckRequest):
body_req: Optional[str] = None
body: Optional[EscapedString] = None
accepted_status_codes: Annotated[int, Gt(99), Le(599)]
ignore_ssl_error: Optional[bool] = 0

@field_validator('http_method', mode='before')
@classmethod
Expand Down Expand Up @@ -124,6 +125,7 @@ class SmtpCheckRequest(BaseCheckRequest):
password: EscapedString
port: Annotated[int, Gt(1), Le(65535)] = 53
ip: Union[IPvAnyAddress, DomainName]
ignore_ssl_error: Optional[bool] = 0


class PingCheckRequest(BaseCheckRequest):
Expand Down Expand Up @@ -207,3 +209,12 @@ class ChannelRequest(BaseModel):
class SettingsRequest(BaseModel):
param: EscapedString
value: EscapedString


class StatusPageRequest(BaseModel):
name: EscapedString
slug: EscapedString
description: Optional[EscapedString] = None
custom_style: Optional[EscapedString] = None
checks: list
group_id: Optional[int] = None
11 changes: 8 additions & 3 deletions app/modules/roxywi/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import app.modules.db.server as server_sql
import app.modules.db.history as history_sql
import app.modules.roxy_wi_tools as roxy_wi_tools
from app.modules.roxywi.exception import RoxywiResourceNotFound, RoxywiCheckLimits
from app.modules.roxywi.exception import RoxywiResourceNotFound, RoxywiCheckLimits, RoxywiGroupMismatch
from app.modules.roxywi.class_models import ErrorResponse

get_config_var = roxy_wi_tools.GetConfigVar()
Expand Down Expand Up @@ -266,9 +266,14 @@ def handle_json_exceptions(ex: Exception, message: str, server_ip='RMON server')
return ErrorResponse(error=f'{message}: {ex}').model_dump(mode='json')


def is_user_has_access_to_group(user_id: int) -> None:
def is_user_has_access_to_its_group(user_id: int) -> None:
if not user_sql.check_user_group(user_id, g.user_params['group_id']) and g.user_params['role'] != 1:
raise Exception('User has no access to group')
raise RoxywiGroupMismatch


def is_user_has_access_to_group(user_id: int, group_id: int) -> None:
if not user_sql.check_user_group(user_id, group_id) and g.user_params['role'] != 1:
raise RoxywiGroupMismatch


def handler_exceptions_for_json_data(ex: Exception, main_ex_mes: str) -> tuple[dict, int]:
Expand Down
30 changes: 14 additions & 16 deletions app/modules/roxywi/roxy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import re
from packaging import version

import requests
from requests.adapters import HTTPAdapter
Expand Down Expand Up @@ -31,28 +32,25 @@ def check_ver():


def versions():
json_data = {
'need_update': 0
}
try:
current_ver = check_ver()
current_ver_without_dots = current_ver.split('.')
current_ver_without_dots = ''.join(current_ver_without_dots)
current_ver_without_dots = current_ver_without_dots.replace('\n', '')
current_ver_without_dots = int(current_ver_without_dots)
except Exception:
current_ver = "Cannot get current version"
current_ver_without_dots = 0
current_ver = roxy_sql.get_ver()
json_data['current_ver'] = roxy_sql.get_ver()
except Exception as e:
raise Exception(f'Cannot get current version: {e}')

try:
new_ver = check_new_version('rmon')
new_ver_without_dots = new_ver.split('.')
new_ver_without_dots = ''.join(new_ver_without_dots)
new_ver_without_dots = new_ver_without_dots.replace('\n', '')
new_ver_without_dots = int(new_ver_without_dots)
json_data['new_ver'] = new_ver
except Exception as e:
new_ver = "Cannot get a new version"
new_ver_without_dots = 0
roxywi_common.logging('RMON server', f' {e}')
raise Exception(f'Cannot get new version: {e}')

if version.parse(current_ver) < version.parse(new_ver):
json_data['need_update'] = 1

return current_ver, new_ver, current_ver_without_dots, new_ver_without_dots
return json_data


def check_new_version(service):
Expand Down
Loading

0 comments on commit 1f4579f

Please sign in to comment.