monitorconfig
This commit is contained in:
@@ -18,6 +18,10 @@ from modules.device.routes import (
|
||||
device_bp
|
||||
)
|
||||
|
||||
from modules.monitor_config.routes import (
|
||||
monitor_config_bp
|
||||
)
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# Register Global Exception Handlers
|
||||
@@ -39,6 +43,11 @@ app.register_blueprint(
|
||||
url_prefix="/api/devices"
|
||||
)
|
||||
|
||||
app.register_blueprint(
|
||||
monitor_config_bp,
|
||||
url_prefix="/api/devices"
|
||||
)
|
||||
|
||||
# @app.route("/")
|
||||
# def home():
|
||||
# return {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from config.database import get_connection, release_connection
|
||||
from modules.monitor_config.repository import insert_default_monitor_config_db
|
||||
|
||||
def _row_to_dict(row):
|
||||
"""
|
||||
@@ -136,6 +137,7 @@ def insert_device(data):
|
||||
"""
|
||||
Tạo mới một thiết bị và cấu hình mặc định (MonitorConfig & AlertConfig)
|
||||
trong cùng một database transaction.
|
||||
Trả về: tuple (device_dict, monitor_config_dict)
|
||||
"""
|
||||
conn = get_connection()
|
||||
cur = None
|
||||
@@ -163,19 +165,8 @@ def insert_device(data):
|
||||
device_row = cur.fetchone()
|
||||
device_id = device_row[0]
|
||||
|
||||
# 2. Thêm cấu hình giám sát mặc định (MonitorConfig) cho thiết bị vừa tạo
|
||||
# enable_ping mặc định bật True để thực hiện Ping giám sát
|
||||
cur.execute("""
|
||||
INSERT INTO monitor_config (device_id, enable_ping, ping_count, ping_timeout, ping_interval, enable_snmp)
|
||||
VALUES (%s, %s, %s, %s, %s, %s)
|
||||
""", (
|
||||
device_id,
|
||||
True, # enable_ping
|
||||
3, # ping_count
|
||||
5, # ping_timeout (giây)
|
||||
60, # ping_interval (giây)
|
||||
False # enable_snmp
|
||||
))
|
||||
# 2. Thêm cấu hình giám sát mặc định (MonitorConfig) bằng repository chuyên biệt
|
||||
monitor_config = insert_default_monitor_config_db(device_id, cursor=cur)
|
||||
|
||||
# 3. Thêm cấu hình cảnh báo mặc định (AlertConfig) cho thiết bị vừa tạo
|
||||
cur.execute("""
|
||||
@@ -205,7 +196,7 @@ def insert_device(data):
|
||||
# Commit toàn bộ transaction
|
||||
conn.commit()
|
||||
|
||||
return _row_to_dict(full_row)
|
||||
return _row_to_dict(full_row), monitor_config
|
||||
|
||||
except Exception:
|
||||
conn.rollback()
|
||||
|
||||
@@ -62,12 +62,12 @@ def create_device_service(data):
|
||||
if existing_ip:
|
||||
raise DeviceIPAlreadyExistsException(data["ip_address"])
|
||||
|
||||
# 4. Insert DB
|
||||
new_device = insert_device(data)
|
||||
# 4. Insert DB (trả về thiết bị và cấu hình giám sát mặc định vừa tạo)
|
||||
new_device, monitor_config = insert_device(data)
|
||||
|
||||
# 5. Kích hoạt Job giám sát trên Background Scheduler
|
||||
# Truyền kèm thông tin cấu hình mặc định (enable_ping=True, v.v...)
|
||||
add_device_monitoring_job(new_device["id"], None)
|
||||
# Truyền kèm cấu hình giám sát mặc định thay vì None để Scheduler lập lịch
|
||||
add_device_monitoring_job(new_device["id"], monitor_config)
|
||||
|
||||
return new_device
|
||||
|
||||
|
||||
@@ -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