Skip to content

Commit

Permalink
v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Aidaho12 committed Apr 13, 2024
1 parent 94c328a commit 9467f44
Show file tree
Hide file tree
Showing 30 changed files with 649 additions and 201 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

app/static/.DS_Store
app/static/.DS_Store
2 changes: 1 addition & 1 deletion app/create_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def update_db_v_3_4_5_22():

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

Expand Down
4 changes: 2 additions & 2 deletions app/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
@app.before_request
def check_login():
if request.endpoint not in (
'login_page', 'static', 'main.show_roxywi_version', 'smon.show_smon_status_page',
'smon.smon_history_statuses', 'smon.agent_get_checks', 'smon.get_check_status'
'login_page', 'static', 'main.show_roxywi_version', 'smon.show_smon_status_page', 'smon.smon_history_statuses_avg',
'smon.smon_history_statuses', 'smon.agent_get_checks', 'smon.get_check_status', 'smon.smon_history_metric'
):
try:
user_params = roxywi_common.get_users_params()
Expand Down
14 changes: 12 additions & 2 deletions app/modules/db/db_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,16 @@ class Meta:
primary_key = False


class SmonGroup(BaseModel):
id = AutoField()
name = CharField()
user_group = IntegerField(constraints=[SQL('DEFAULT 1')])

class Meta:
table_name = 'smon_groups'
constraints = [SQL('UNIQUE (name, user_group)')]


class SMON(BaseModel):
id = AutoField()
name = CharField(null=True)
Expand All @@ -211,7 +221,7 @@ class SMON(BaseModel):
desc = CharField(null=True)
response_time = CharField(null=True)
time_state = DateTimeField(constraints=[SQL('DEFAULT "0000-00-00 00:00:00"')])
group = CharField(null=True)
group_id = IntegerField(null=True)
http = CharField(null=True)
body_status = IntegerField(constraints=[SQL('DEFAULT 1')])
telegram_channel_id = IntegerField(null=True)
Expand Down Expand Up @@ -412,5 +422,5 @@ def create_tables():
conn.create_tables(
[User, Server, Role, Telegram, Slack, UUID, Token, ApiToken, Groups, UserGroups, Setting, Cred, Version, ActionHistory,
SystemInfo, UserName, PD, SmonHistory, SmonAgent, SmonTcpCheck, SmonHttpCheck, SmonPingCheck, SmonDnsCheck, RoxyTool,
SmonStatusPage, SmonStatusPageCheck, SMON, Alerts]
SmonStatusPage, SmonStatusPageCheck, SMON, Alerts, SmonGroup]
)
46 changes: 41 additions & 5 deletions app/modules/db/smon.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

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, SmonGroup
)
from app.modules.db.common import out_error
import app.modules.roxy_wi_tools as roxy_wi_tools

Expand Down Expand Up @@ -212,10 +214,10 @@ def select_one_smon(smon_id: int, check_id: int) -> tuple:
return query_res


def insert_smon(name, enable, group, desc, telegram, slack, pd, user_group, check_type):
def insert_smon(name, enable, group_id, desc, telegram, slack, pd, user_group, check_type):
try:
last_id = SMON.insert(
name=name, en=enable, desc=desc, group=group, telegram_channel_id=telegram, slack_channel_id=slack,
name=name, en=enable, desc=desc, group_id=group_id, telegram_channel_id=telegram, slack_channel_id=slack,
pd_channel_id=pd, user_group=user_group, status='3', check_type=check_type
).execute()
except Exception as e:
Expand Down Expand Up @@ -329,9 +331,9 @@ def delete_smon(smon_id, user_group):

def smon_list(user_group):
if user_group == 1:
query = (SMON.select().order_by(SMON.group))
query = (SMON.select().order_by(SMON.group_id))
else:
query = (SMON.select().where(SMON.user_group == user_group).order_by(SMON.group))
query = (SMON.select().where(SMON.user_group == user_group).order_by(SMON.group_id))

try:
query_res = query.execute()
Expand Down Expand Up @@ -658,3 +660,37 @@ def delete_smon_history():
query.execute()
except Exception as e:
out_error(e)


def select_checks_for_agent(agent_id: int, check_type: str) -> dict:
select_query = {
'http': SmonHttpCheck.select(SmonHttpCheck, SMON).join(SMON).where(SmonHttpCheck.agent_id == agent_id).objects(),
'tcp': SmonTcpCheck.select(SmonTcpCheck, SMON).join(SMON).where(SmonTcpCheck.agent_id == agent_id).objects(),
'dns': SmonDnsCheck.select(SmonDnsCheck, SMON).join(SMON).where(SmonDnsCheck.agent_id == agent_id).objects(),
'ping': SmonPingCheck.select(SmonPingCheck, SMON).join(SMON).where(SmonPingCheck.agent_id == agent_id).objects()
}
try:
return select_query[check_type].execute()
except Exception as e:
out_error(e)


def get_smon_group_by_name(user_group: int, name: str) -> int:
try:
return SmonGroup.select().where((SmonGroup.name == name) & (SmonGroup.user_group == user_group)).get().id
except Exception as e:
return 0


def add_smon_group(user_group: int, name: str) -> int:
try:
return SmonGroup.insert(name=name, user_group=user_group).on_conflict('replace').execute()
except Exception as e:
out_error(e)


def select_smon_groups(user_group: int) -> object:
try:
return SmonGroup.select().where(SmonGroup.user_group == user_group)
except Exception as e:
out_error(e)
26 changes: 25 additions & 1 deletion app/modules/roxywi/nettools.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import json

import whois
from flask import Response, stream_with_context

import app.modules.server.ssh as mod_ssh
import modules.server.server as server_mod
import app.modules.server.server as server_mod


def ping_from_server(server_from: str, server_to: str, action: str) -> Response:
Expand Down Expand Up @@ -106,3 +109,24 @@ def nslookup_from_server(server_from: str, dns_name: str, record_type: str) -> s
count_string += 1

return output1


def whois_check(domain_name: str) -> str:
try:
whois_data = json.loads(str(whois.whois(domain_name)))
except Exception as e:
return f'error: Cannot get whois from {domain_name}: {e}'

output = (f'<b>Domain name:</b> {whois_data["domain_name"]}<br />'
f'<b>Registrar:</b> {whois_data["registrar"]} <br />'
f'<b>Creation date:</b> {whois_data["creation_date"]} <br />'
f'<b>Expiration date:</b> {whois_data["expiration_date"]} <br />'
f'<b>Name servers:</b> {whois_data["name_servers"]} <br />'
f'<b>Status:</b> {whois_data["status"]} <br />')

if 'emails' in whois_data:
output += f'<b>Emails:</b> {whois_data["emails"]} <br />'
if 'org' in whois_data:
output += f'<b>Organization:</b> {whois_data["org"]} <br />'

return output
21 changes: 13 additions & 8 deletions app/modules/tools/smon.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ def create_smon(json_data, user_group, show_new=1) -> bool:
if int(packet_size) < 16:
raise Exception('RMON error: a packet size cannot be less than 16')

last_id = smon_sql.insert_smon(name, enable, group, desc, telegram, slack, pd, user_group, check_type)
if group:
smon_group_id = smon_sql.get_smon_group_by_name(user_group, group)
if not smon_group_id:
smon_group_id = smon_sql.add_smon_group(user_group, group)
else:
smon_group_id = None

last_id = smon_sql.insert_smon(name, enable, smon_group_id, desc, telegram, slack, pd, user_group, check_type)

if check_type == 'ping':
smon_sql.insert_smon_ping(last_id, hostname, packet_size, interval, agent_id)
Expand Down Expand Up @@ -168,13 +175,6 @@ def create_check_json(json_data: dict) -> dict:
return check_json


def show_smon(sort: str) -> str:
user_group = roxywi_common.get_user_group(id=1)
lang = roxywi_common.get_user_lang_for_flask()

return render_template('ajax/smon/smon_dashboard.html', smon=smon_sql.smon_list(user_group), sort=sort, lang=lang, update=1)


def delete_smon(smon_id, user_group) -> str:
try:
agent_id = smon_sql.get_agent_id_by_check_id(smon_id)
Expand Down Expand Up @@ -313,3 +313,8 @@ def check_checks_limit():
raise Exception('error: You have reached limit for Home plan')
elif user_subscription['user_plan'] == 'enterprise' and count_checks >= 100:
raise Exception('error: You have reached limit for Enterprise plan')


def get_check_id_by_name(name: str) -> int:
checking_types = {'tcp': '1', 'http': '2', 'ping': '4', 'dns': '5'}
return checking_types[name]
26 changes: 24 additions & 2 deletions app/routes/main/routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import sys
import pytz

from flask import render_template, request, session, g, abort
from flask import render_template, request, session, g, abort, send_from_directory, jsonify
from flask_login import login_required

sys.path.insert(0, "/var/www/rmon/app")
Expand Down Expand Up @@ -77,6 +78,13 @@ def make_session_permanent():
session.permanent = True


@app.route('/favicon.ico')
def favicon():
print(app.root_path, 'static')
return send_from_directory(os.path.join(app.root_path, 'static'),
'images/favicon/favicon.ico', mimetype='image/vnd.microsoft.icon')


@bp.route('/nettools')
@login_required
@get_user_params(1)
Expand All @@ -86,21 +94,24 @@ def nettools():

@bp.post('/nettools/<check>')
@login_required
def nettols_check(check):
def nettools_check(check):
server_from = common.checkAjaxInput(request.form.get('server_from'))
server_to = common.is_ip_or_dns(request.form.get('server_to'))
action = common.checkAjaxInput(request.form.get('nettools_action'))
port_to = common.checkAjaxInput(request.form.get('nettools_telnet_port_to'))
dns_name = common.checkAjaxInput(request.form.get('nettools_nslookup_name'))
dns_name = common.is_ip_or_dns(dns_name)
record_type = common.checkAjaxInput(request.form.get('nettools_nslookup_record_type'))
domain_name = common.is_ip_or_dns(request.form.get('nettools_whois_name'))

if check == 'icmp':
return nettools_mod.ping_from_server(server_from, server_to, action)
elif check == 'tcp':
return nettools_mod.telnet_from_server(server_from, server_to, port_to)
elif check == 'dns':
return nettools_mod.nslookup_from_server(server_from, dns_name, record_type)
elif check == 'whois':
return jsonify(nettools_mod.whois_check(domain_name))
else:
return 'error: Wrong check'

Expand Down Expand Up @@ -187,3 +198,14 @@ def scan_port(server_id, server_ip):
else:
lang = roxywi_common.get_user_lang_for_flask()
return render_template('ajax/scan_ports.html', ports=stdout, info=stdout1, lang=lang)


@bp.route('/cpu-ram-metrics/<server_id>')
@get_user_params()
def cpu_ram_metrics(server_id):
kwargs = {
'id': server_id,
'lang': g.user_params['lang']
}

return render_template('ajax/overviewServers.html', **kwargs)
23 changes: 20 additions & 3 deletions app/routes/smon/agent_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def agent():
'smon_status': tools_common.is_tool_active('rmon-server'),
}

return render_template('smon/agent.html', **kwargs)
return render_template('smon/agents.html', **kwargs)
elif request.method == 'POST':
data = request.get_json()
try:
Expand All @@ -48,6 +48,23 @@ def agent():
return 'ok'


@bp.get('/agent/<int:agent_id>')
@login_required
@get_user_params()
def get_agent(agent_id):
kwargs = {
'agents': smon_sql.get_agent(agent_id),
'lang': roxywi_common.get_user_lang_for_flask(),
'smon_status': tools_common.is_tool_active('rmon-server'),
'http_checks': smon_sql.select_checks_for_agent(agent_id, 'http'),
'tcp_checks': smon_sql.select_checks_for_agent(agent_id, 'tcp'),
'dns_checks': smon_sql.select_checks_for_agent(agent_id, 'dns'),
'ping_checks': smon_sql.select_checks_for_agent(agent_id, 'ping')
}

return render_template('smon/agent.html', **kwargs)


@bp.post('/agent/hello')
def agent_get_checks():
json_data = request.json
Expand Down Expand Up @@ -83,10 +100,10 @@ def get_agent_count():
return 'ok'


@bp.get('/agent/<int:agent_id>')
@bp.get('/agent/info/<int:agent_id>')
@login_required
@get_user_params()
def get_agent(agent_id):
def get_agent_info(agent_id):
try:
agent_data = smon_sql.get_agent(agent_id)
except Exception as e:
Expand Down
20 changes: 12 additions & 8 deletions app/routes/smon/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def smon_main_dashboard():
'smon': smon_sql.smon_list(group_id),
'agents': smon_sql.get_agents(group_id),
'group': group_id,
'smon_groups': smon_sql.select_smon_groups(group_id),
'smon_status': tools_common.is_tool_active('rmon-server'),
'telegrams': channel_sql.get_user_telegram_by_group(group_id),
'slacks': channel_sql.get_user_pd_by_group(group_id),
Expand Down Expand Up @@ -111,6 +112,17 @@ def smon_dashboard(smon_id, check_id):
return render_template('include/smon/smon_history.html', **kwargs)


@bp.get('/groups')
@login_required
@get_user_params()
def get_groups():
groups = smon_sql.select_smon_groups(g.user_params['group_id'])
smon_groups = ''
for group in groups:
smon_groups += f'{group.name}\n'
return smon_groups


@bp.get('/checks/count')
@login_required
def get_checks_count():
Expand Down Expand Up @@ -376,7 +388,6 @@ def smon_host_history(server_ip):


@bp.route('/history/metric/<int:dashboard_id>')
@login_required
def smon_history_metric(dashboard_id):
return jsonify(smon_mod.history_metrics(dashboard_id))

Expand All @@ -390,10 +401,3 @@ def smon_history_statuses(dashboard_id):
@login_required
def smon_history_cur_status(dashboard_id, check_id):
return smon_mod.history_cur_status(dashboard_id, check_id)


@bp.post('/refresh')
@login_required
def smon_show():
sort = common.checkAjaxInput(request.form.get('sort'))
return smon_mod.show_smon(sort)
Loading

0 comments on commit 9467f44

Please sign in to comment.