monitorconfig
This commit is contained in:
@@ -42,6 +42,47 @@ def _row_to_dict(row):
|
||||
}
|
||||
|
||||
|
||||
# ============================================
|
||||
# INSERT DEFAULT: Tạo cấu hình giám sát mặc định cho thiết bị mới
|
||||
# ============================================
|
||||
def insert_default_monitor_config_db(device_id, cursor=None):
|
||||
"""
|
||||
Thêm cấu hình giám sát mặc định cho thiết bị mới.
|
||||
Nếu có cursor được truyền vào (từ transaction của device), dùng chung cursor đó.
|
||||
Nếu không, tự tạo connection riêng.
|
||||
"""
|
||||
sql = """
|
||||
INSERT INTO monitor_config (device_id, enable_ping, ping_count, ping_timeout, ping_interval, enable_snmp)
|
||||
VALUES (%s, %s, %s, %s, %s, %s)
|
||||
RETURNING id, device_id, enable_ping, ping_count, ping_timeout, ping_interval,
|
||||
enable_snmp, snmp_version, snmp_community, snmp_port, snmp_interval,
|
||||
snmp_timeout, snmp_custom_oids, created, modified
|
||||
"""
|
||||
# Mặc định bật Ping giám sát (enable_ping=True), ping_count=3, timeout=5s, interval=60s
|
||||
params = (device_id, True, 3, 5, 60, False)
|
||||
|
||||
if cursor:
|
||||
cursor.execute(sql, params)
|
||||
row = cursor.fetchone()
|
||||
return _row_to_dict(row)
|
||||
else:
|
||||
conn = get_connection()
|
||||
cur = None
|
||||
try:
|
||||
cur = conn.cursor()
|
||||
cur.execute(sql, params)
|
||||
row = cur.fetchone()
|
||||
conn.commit()
|
||||
return _row_to_dict(row)
|
||||
except Exception:
|
||||
conn.rollback()
|
||||
raise
|
||||
finally:
|
||||
if cur:
|
||||
cur.close()
|
||||
release_connection(conn)
|
||||
|
||||
|
||||
# ============================================
|
||||
# FIND BY DEVICE ID: Lấy cấu hình giám sát của một thiết bị
|
||||
# ============================================
|
||||
|
||||
27
backend/modules/monitor_config/routes.py
Normal file
27
backend/modules/monitor_config/routes.py
Normal file
@@ -0,0 +1,27 @@
|
||||
# pyrefly: ignore [missing-import]
|
||||
from flask import Blueprint
|
||||
|
||||
from modules.monitor_config.controller import (
|
||||
get_monitor_config,
|
||||
update_monitor_config,
|
||||
test_connection
|
||||
)
|
||||
|
||||
# Khởi tạo Blueprint cho Module Monitor Config
|
||||
# Blueprint này sẽ được đăng ký dưới prefix /api/devices trong app.py
|
||||
monitor_config_bp = Blueprint("monitor_config", __name__)
|
||||
|
||||
# Đăng ký các endpoints cấu hình giám sát thiết bị:
|
||||
# Luồng đi:
|
||||
# 1. Client gửi request tương ứng tới endpoint.
|
||||
# 2. Flask định tuyến (routing) request dựa trên method và url path.
|
||||
# 3. Chuyển tiếp request cho hàm xử lý tương ứng trong controller.py.
|
||||
|
||||
# GET /api/devices/<device_id>/monitor-config -> Lấy cấu hình giám sát
|
||||
monitor_config_bp.route("/<device_id>/monitor-config", methods=["GET"])(get_monitor_config)
|
||||
|
||||
# PUT /api/devices/<device_id>/monitor-config -> Cập nhật cấu hình giám sát
|
||||
monitor_config_bp.route("/<device_id>/monitor-config", methods=["PUT"])(update_monitor_config)
|
||||
|
||||
# POST /api/devices/<device_id>/monitor-config/test -> Kiểm tra kết nối (Ping/SNMP)
|
||||
monitor_config_bp.route("/<device_id>/monitor-config/test", methods=["POST"])(test_connection)
|
||||
@@ -22,13 +22,24 @@ import platform
|
||||
|
||||
from modules.monitor_config.repository import (
|
||||
find_monitor_config_by_device_id,
|
||||
update_monitor_config_db
|
||||
update_monitor_config_db,
|
||||
insert_default_monitor_config_db
|
||||
)
|
||||
from modules.monitor_config.exceptions import MonitorConfigNotFoundException
|
||||
from modules.device.repository import find_device_by_id
|
||||
from modules.device.exceptions import DeviceNotFoundException
|
||||
from scheduler.scheduler import reschedule_device_monitoring_job
|
||||
|
||||
|
||||
# ============================================
|
||||
# CREATE DEFAULT: Tạo cấu hình giám sát mặc định (Service Layer)
|
||||
# ============================================
|
||||
def create_default_monitor_config_service(device_id, cursor=None):
|
||||
"""
|
||||
Tạo cấu hình giám sát mặc định cho thiết bị mới.
|
||||
"""
|
||||
return insert_default_monitor_config_db(device_id, cursor)
|
||||
|
||||
# ============================================
|
||||
# Dynamic Import: icmplib và pysnmp
|
||||
# ============================================
|
||||
|
||||
Reference in New Issue
Block a user