一、开发环境搭建和配置
#mac环境
brew install python@3.12
python3.12 --version
python3.12 -m pip install --upgrade pip
python3.12 -m pip install Django==5.1
python3.12 -m django --version
#用于检索系统信息和进程管理
python3.12 -m pip install psutil
#集成 pika 用于 RabbitMQ
python3.12 -m pip install pika
#要在 Docker 中安装 RabbitMQ 并开启 WebSocket 端口 15674
docker run -d --name rabbitmq \
-p 5672:5672 \
-p 15672:15672 \
-p 15674:15674 \
rabbitmq:3-management
#进入容器内部
docker exec -it rabbitmq bash
#在容器内部,启用 WebSocket 插件:
rabbitmq-plugins enable rabbitmq_web_stomp
二、构建django应用
# 创建 monitor-plat 项目
django-admin startproject operation
#项目结构
operation
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-313.pyc
│ ├── settings.cpython-313.pyc
│ ├── urls.cpython-313.pyc
│ └── wsgi.cpython-313.pyc
├── asgi.py
├── common
│ ├── __init__.py
│ ├── __pycache__
│ │ └── __init__.cpython-313.pyc
│ ├── enum
│ │ ├── EventTypesEnum.py
│ │ ├── OperatorTypeEnum.py
│ │ ├── ResponeCodeEnum.py
│ │ ├── __init__.py
│ │ └── __pycache__
│ │ ├── EventTypesEnum.cpython-313.pyc
│ │ ├── OperatorTypeEnum.cpython-313.pyc
│ │ ├── ResponeCodeEnum.cpython-313.pyc
│ │ └── __init__.cpython-313.pyc
│ ├── exception
│ │ ├── BusinessException.py
│ │ ├── __init__.py
│ │ └── __pycache__
│ │ ├── BusinessException.cpython-313.pyc
│ │ └── __init__.cpython-313.pyc
│ └── utils
│ ├── CommonResult.py
│ ├── PageUtils.py
│ ├── Serializers.py
│ ├── __init__.py
│ └── __pycache__
│ ├── CommonResult.cpython-313.pyc
│ ├── PageUtils.cpython-313.pyc
│ ├── Serializers.cpython-313.pyc
│ └── __init__.cpython-313.pyc
├── settings.py
├── urls.py
└── wsgi.py
log
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-313.pyc
│ ├── admin.cpython-313.pyc
│ ├── apps.cpython-313.pyc
│ ├── models.cpython-313.pyc
│ └── views.cpython-313.pyc
├── admin.py
├── apps.py
├── migrations
│ ├── 0001_initial.py
│ ├── __init__.py
│ └── __pycache__
│ ├── 0001_initial.cpython-313.pyc
│ └── __init__.cpython-313.pyc
├── models.py
├── tests.py
└── views.py
dbOperations
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-313.pyc
│ ├── admin.cpython-313.pyc
│ ├── apps.cpython-313.pyc
│ ├── models.cpython-313.pyc
│ └── views.cpython-313.pyc
├── admin.py
├── apps.py
├── migrations
│ ├── __init__.py
│ └── __pycache__
│ └── __init__.cpython-313.pyc
├── models.py
├── tests.py
└── views.py
user
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-313.pyc
│ ├── admin.cpython-313.pyc
│ ├── apps.cpython-313.pyc
│ ├── models.cpython-313.pyc
│ └── views.cpython-313.pyc
├── admin.py
├── apps.py
├── migrations
│ ├── 0001_initial.py
│ ├── __init__.py
│ └── __pycache__
│ ├── 0001_initial.cpython-313.pyc
│ └── __init__.cpython-313.pyc
├── models.py
├── tests.py
└── views.py
monitor
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-313.pyc
│ ├── admin.cpython-313.pyc
│ ├── apps.cpython-313.pyc
│ ├── models.cpython-313.pyc
│ ├── signals.cpython-313.pyc
│ ├── tasks.cpython-313.pyc
│ └── views.cpython-313.pyc
├── admin.py
├── apps.py
├── migrations
│ ├── 0001_initial.py
│ ├── __init__.py
│ └── __pycache__
│ ├── 0001_initial.cpython-313.pyc
│ └── __init__.cpython-313.pyc
├── models.py
├── signals.py
├── tasks.py
├── tests.py
└── views.py
三、监控,告警相关代码
monitor
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-313.pyc
│ ├── admin.cpython-313.pyc
│ ├── apps.cpython-313.pyc
│ ├── models.cpython-313.pyc
│ ├── signals.cpython-313.pyc
│ ├── tasks.cpython-313.pyc
│ └── views.cpython-313.pyc
#=========================tasks.py=========================
import logging
import multiprocessing
import pika
import json
import psutil
import time
from monitor.models import ServerSecurityEventLog
from operation import settings
from operation.common.enum.EventTypesEnum import EventTypesEnum
"""
监控服务器 性能
"""
def send_memory_data():
connection = None
channel = None
#获取 rabbitmq配置
rabbitmq_config = settings.RABBITMQ_CONFIG
try:
connection = pika.BlockingConnection(pika.ConnectionParameters(
host=rabbitmq_config['host'],
port=rabbitmq_config['port'],
virtual_host=rabbitmq_config['virtual_host'],
credentials=pika.PlainCredentials(rabbitmq_config['user'], rabbitmq_config['password'])
))
channel = connection.channel()
channel.queue_declare(queue='virtual_queue',durable=True)
print("连接成功")
logging.info("连接成功")
except pika.exceptions.AMQPConnectionError as e:
logging.error(f"无法连接到AMQP服务器: {e}")
return # 如果连接失败,直接返回,避免后续代码执行
try:
while True:
cpu_usage = psutil.cpu_percent(interval=1)
cup_num = multiprocessing.cpu_count()
memory_usage = psutil.virtual_memory()
use_memory_usage = round(memory_usage.percent / (1024 * 1024 * 1024), 2) # 获取已使用的内存
memory_usage_total = round(memory_usage.total / (1024 * 1024 * 1024), 2) # 获取总内存
free_usage = round(memory_usage.active / (1024 * 1024 * 1024), 2) # 获取 空闲内存
disk_usage = psutil.disk_usage('/') # 获取根目录的磁盘使用情况
disk_usage_total = round(disk_usage.total / (1024 * 1024 * 1024), 2) # 获取磁盘总容量
free_disk_usage = round(disk_usage.free / (1024 * 1024 * 1024), 2) # 获取磁盘剩余容量
network_io = psutil.net_io_counters()
#告警逻辑 保存event事件
save_serverSecurity_event(cpu_usage, memory_usage.percent, disk_usage.percent, network_io)
memory_usage_data = {
'cpu_usage': cpu_usage, # CPU的使用率。百分比
'cup_num': cup_num,
'use_memory_usage': use_memory_usage, # 内存的使用情况。通以字节(bytes)或更常见的单位(如MB、GB)表示
'memory_usage_total': memory_usage_total,
'free_usage': free_usage, # 空闲内存
'disk_usage': disk_usage, # 磁盘的使用情况。通常以字节(bytes)
'disk_usage_total': disk_usage_total, # 获取磁盘总容量
'free_disk_usage': free_disk_usage, # 获取磁盘剩余容量
'network_io': network_io # 表示网络输入/输出的情况 通常以字节(bytes)
}
# 发送rabbitmq
channel.basic_publish(
exchange='',
routing_key='virtual_queue',
body=json.dumps(memory_usage_data)
)
logging.info("数据发送成功")
time.sleep(5) # 每5秒发送一次
except Exception as e:
logging.error(f"数据发送失败: {e}")
finally:
if connection and connection.is_open:
connection.close()
"""
保存告警逻辑
"""
def save_serverSecurity_event(cpu_usage,memory_usage,disk_usage,network_io):
logging.info("保存告警逻辑=====================================")
# 告警逻辑
if (cpu_usage > settings.CPU_THRESHOLD or
memory_usage > settings.MEMORY_THRESHOLD or
disk_usage > settings.DISK_THRESHOLD or
network_io.packets_recv == settings.PACKET_RECV_THRESHOLD or
network_io.packets_sent == settings.PACKETS_SENT_THRESHOLD):
event_type = None
event_details = ""
# 当cpu使用率大于90%时 记录cpu
if cpu_usage > settings.CPU_THRESHOLD:
logging.info(f'CPU使用率超过90%')
event_type = EventTypesEnum.CPU_OVERLOAD.value
event_details = f'CPU使用率超过90%: {cpu_usage}%'
elif memory_usage > settings.MEMORY_THRESHOLD:
logging.info(f'内存使用率超过80%')
event_type = EventTypesEnum.MEMORY_OVERLOAD.value
event_details = f'内存使用率超过80%: 1.15MB%'
elif disk_usage > settings.DISK_THRESHOLD:
logging.info(f'磁盘使用率超过90%')
event_type = EventTypesEnum.DISK_OVERLOAD.value
event_details = f'磁盘使用率超过90%: {disk_usage}%'
elif network_io.packets_recv == settings.PACKET_RECV_THRESHOLD or network_io.packets_sent == settings.PACKETS_SENT_THRESHOLD:
logging.info(f'网络异常,接收或发送的数据包为0')
event_type = EventTypesEnum.NETWORK_ANOMALY.value
event_details = '网络异常,接收或发送的数据包为0'
# 保存告警信息到数据库
ServerSecurityEventLog.objects.create(
event_type=event_type,
event_details=event_details
)
#========================signals.py=========================
"""
处理告警逻辑:
"""
import logging
import time
from datetime import datetime
from django.core.mail import send_mail
from django.db.models.signals import post_save
from django.dispatch import receiver
from monitor.models import ServerSecurityEventLog
from operation import settings
"""
发送告警邮件
"""
# 设置告警邮件的发送频率,单位为秒
ALERT_EMAIL_INTERVAL = 60 * 5 # 5分钟
# 上次发送告警邮件的时间
last_alert_email_time = 0
@receiver(post_save, sender=ServerSecurityEventLog)
def send_alert_email(sender, instance, **kwargs):
#global 关键字: 在函数内部使用 global 关键字声明变量时,表示该变量是全局变量,
# 而不是函数内部的局部变量。这样可以在函数内部修改全局变量的值。
global last_alert_email_time
#获取当前时间
current_time = time.time()
if current_time - last_alert_email_time >= ALERT_EMAIL_INTERVAL:
logging.info("发送告警邮件====================")
subject = '服务器告警'
# 格式化时间为目标格式
formatted_time = instance.event_time.strftime("%Y-%m-%d %H:%M:%S")
message = (f'服务器状态异常,请立即检查!\n'
f'事件类型: {instance.event_type}\n'
f'事件详情: {instance.event_details}\n'
f'触发时间: {formatted_time}\n')
from_email = settings.DEFAULT_FROM_EMAIL
recipient_list = ['xxxxxxxx@qq.com']
try:
send_mail(subject, message, from_email, recipient_list)
logging.info("邮件发送成功")
last_alert_email_time = current_time # 更新上次发送告警邮件的时间
except Exception as e:
logging.error(f"发送邮件失败 {e}")
#======================apps.py======================
from django.apps import AppConfig
class MonitorConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'monitor'
"""
ready 方法: 这是 AppConfig 类中的一个特殊方法,在 Django 应用启动时会被调用。通常用于执行一些初始化操作
"""
def ready(self):
# 注册信号 导入 monitor.signals 模块,用于注册 Django 信号。信号是 Django 中一种用于在特定事件发生时触发操作的机制。
#@receiver(post_save, sender=ServerSecurityEventLog) 再做ServerSecurityEventLog 保存操作时 会 触发操作的机制。
import monitor.signals
# 导入任务函数: 从 tasks 模块中导入 send_memory_data 函数。
from .tasks import send_memory_data
#创建并启动线程: 使用 threading.Thread 创建一个新线程,
#并将 send_memory_data 函数作为目标函数传递给线程。然后调用 thread.start() 启动线程。
from threading import Thread
thread = Thread(target=send_memory_data)
thread.start()
#==========================models.py==========================
from django.db import models
# Create your models here.
#告警 models
class ServerSecurityEventLog(models.Model):
EVENT_TYPES = (
('cpu_overload', 'CPU过载'),
('memory_overload', '内存过载'),
('disk_overload', '磁盘过载'),
('network_anomaly', '网络异常'),
)
event_type = models.CharField(max_length=50, choices=EVENT_TYPES)
event_time = models.DateTimeField(auto_now_add=True)
event_details = models.TextField()
is_resolved = models.BooleanField(default=False)
def __str__(self):
return f"{self.event_type} - {self.event_time}"
class Meta:
db_table = 'server_security_events_log' # 指定表名
def to_dict(self):
return {
'id': self.id,
'event_type':self.event_type,
'event_details':self.event_details,
'event_time':self.event_time,
'is_resolved':self.is_resolved
}
#=========================views.py=========================
#用于前端展示
import json
import multiprocessing
import psutil
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from monitor.models import ServerSecurityEventLog
from operation.common.enum.ResponeCodeEnum import ResponseCodeEnum
from operation.common.exception.BusinessException import BusinessException
from operation.common.utils.CommonResult import CommonResult
from operation.common.utils.PageUtils import paginate_queryset
def getSystemInfo(request):
try:
if request.method != "GET":
raise BusinessException(ResponseCodeEnum.METHOD_ERROR.message,ResponseCodeEnum.METHOD_ERROR.code)
cpu_usage = psutil.cpu_percent(interval=1)
cup_count = multiprocessing.cpu_count()
memory_usage = psutil.virtual_memory()
use_memory_usage = round(memory_usage.percent/(1024 * 1024 * 1024),2) #获取已使用的内存
memory_usage_total = round(memory_usage.total/(1024 * 1024 * 1024),2) #获取总内存
free_usage = round(memory_usage.active/(1024 * 1024 * 1024),2) #获取 空闲内存存
disk_usage = psutil.disk_usage('/') #获取根目录的磁盘使用情况
disk_usage_total = round(disk_usage.total/(1024 * 1024 * 1024),2) #获取磁盘总容量
free_disk_usage = round(disk_usage.free /(1024 * 1024 * 1024),2) #获取磁盘剩余容量
network_io = psutil.net_io_counters()
data = {
'cpu_usage': cpu_usage, #CPU的使用率。百分比
'cup_count':cup_count,
'use_memory_usage': use_memory_usage, #内存的使用情况。通以字节(bytes)或更常见的单位(如MB、GB)表示
'memory_usage_total':memory_usage_total,
'free_usage':free_usage, #空闲内存
'disk_usage': disk_usage, #磁盘的使用情况。通常以字节(bytes)
'disk_usage_total':disk_usage_total, #获取磁盘总容量
'free_disk_usage':free_disk_usage, #获取磁盘剩余容量
'network_io': network_io #表示网络输入/输出的情况 通常以字节(bytes)
}
return JsonResponse(CommonResult.success_data(data), json_dumps_params={'ensure_ascii': False})
except BusinessException as e:
return JsonResponse(CommonResult.error(e.code,e.message), json_dumps_params={'ensure_ascii': False})
"""
获取告警列表
"""
@csrf_exempt
def getAlarmList(request):
if request.method == 'POST':
json_data = request.body
data = json.loads(json_data)
page = data.get('page', 1)
page_size = data.get('page_size', 10)
serverSecurityEventLogs = ServerSecurityEventLog.objects.all().order_by('event_time')
event_list, pagination_info = paginate_queryset(serverSecurityEventLogs, page, page_size)
event_lists = [ServerSecurityEventLog.to_dict() for ServerSecurityEventLog in event_list]
return JsonResponse(CommonResult.success_pagination(event_lists, pagination_info), json_dumps_params={'ensure_ascii': False})
else:
return JsonResponse(CommonResult.success_data(), json_dumps_params={'ensure_ascii': False})
#==============================EventTypesEnum工具类==============================
##############################EventTypesEnum##############################
"""
('cpu_overload', 'CPU过载'),
('memory_overload', '内存过载'),
('disk_overload', '磁盘过载'),
('network_anomaly', '网络异常'),
"""
from enum import Enum
class EventTypesEnum(Enum):
CPU_OVERLOAD = ('cpu_overload', 'CPU过载')
MEMORY_OVERLOAD = ('memory_overload', '内存过载')
DISK_OVERLOAD = ('disk_overload', '磁盘过载')
NETWORK_ANOMALY = ('network_anomaly', '网络异常')
"""
Enum 的成员值不能直接是元组
可以使用 Enum 的 __new__ 方法来自定义枚举成员的创建过程。
__value: 私有变量
__value__: 双下划线前后缀通常用于定义Python的特殊方法(也称为魔术方法)。这些方法有特定的用途,例如 __init__ 用于初始化对象,__str__ 用于定义对象的字符串表示等。
_value_: 双下划线前后缀通常用于避免与Python的特殊方法或内置函数命名冲突。这种命名方式并不常见,通常是为了避免与Python的保留字或特殊方法名冲突。
_value:单个下划线前缀通常用于表示一个变量是“内部使用”的。虽然Python没有真正的私有变量,但这种命名约定告诉其他开发者这个变量不应该被直接访问。
"""
def __new__(cls,code,message):
obj = object.__new__(cls)
obj._value_ = code
obj.message = message
return obj
@property
def get_message(self):
return self.message
##############################OperatorTypeEnum##############################
import logging
from enum import Enum
"""
操作类别(0其它 1后台用户 2手机端用户) other,backend,mobile
"""
from enum import Enum
class OperatorTypeEnum(Enum):
# 元组
OPERATOR_TYPE_OTHER = (0, "其它")
OPERATOR_TYPE_BACKEND = (1, "后台用户")
OPERATOR_TYPE_MOBILE = (2, "手机端用户")
# def __init__(self, code, value):
# self.code = code
# self.message = value
# logging.info("执行OperatorTypeEnum 初始化 code %s,value %s================"%(self.code,self.value))
"""
cls:表示类的本身(以便在类方法中访问枚举类的成员变量。)
self:表示实例
"""
@classmethod
def get_value_by_id(cls,code):
for operator in cls:
if operator.value[0] == code:
return operator.value[1]
return None
##############################ResponseCodeEnum##############################
from enum import Enum
class ResponseCodeEnum(Enum):
SUCCESS = (200, "操作成功!")
PARAMS_ERROR = (400, "参数解析失败,请核对参数!")
UNAUTHORIZED = (401, "未认证(签名错误)")
FORBIDDEN = (402, "请求错误") # 返回失败业务公共code
MEDIA_TYPE_ERROR = (403, "不支持的媒体异常,请核对contentType!")
URL_REQ_NULL = (404, "请求路径不存在")
METHOD_ERROR = (405, "不支持当前请求方法,请核对请求方法!")
INTERNAL_SERVER_ERROR = (500, "服务器内部错误!") # 系统异常公共code
NULL_POINTER_ERROR = (600, "请核对必填字段是否为空!")
NUMBER_FORMAT_ERROR = (601, "数据类型不一致!")
PARAMS_TYPE_ERROR = (602, "参数类型错误!")
TIMEOUT_ERROR = (603, "token失效连接超时!")
TIMEOUT_EXPIRE_ERROR = (604, "token登录过期!")
TOKEN_ILLEGAL = (605, "非法token!")
USER_LOGIN_FAIL = (1001,"用户登录失败!")
def __init__(self, code, message):
self.code = code
self.message = message
#@property 是 Python 中用于将类的方法转换为属性访问的装饰器。
# 使用 @property 装饰器,你可以像访问属性一样访问方法,而不需要调用它
@property
def status_code(self):
return self.code
@property
def error_message(self):
return self.message
#<class 'enum.EnumType'> 枚举类
# print(type(ResponseCodeEnum))
#<enum 'ResponseCodeEnum'> SUCCESS 是 ResponseCodeEnum 枚举类的一个实例。
# print(type(ResponseCodeEnum.SUCCESS))
##############################BusinessException##############################
class BusinessException(Exception):
def __init__(self,message,code):
self.message = message
self.code = code
##############################CommonResult##############################
from typing import Any, Optional
from operation.common.enum.ResponeCodeEnum import ResponseCodeEnum
class CommonResult:
"""
res: ResponseCodeEnum 这个定义表示 res 参数的类型是 ResponseCodeEnum,
即 res 必须是 ResponseCodeEnum 的枚举值之一。这种定义方式有助于提高代码的类型安全性和可读性。
"""
def __init__(self, res: ResponseCodeEnum, data: Any, pagination: Optional[dict] = None):
self.ResponseCodeEnum = ResponseCodeEnum
self.data = data
self.pagination = pagination
"""
表态方法定义 直接通过 CommonResult.success 类名.静态方法名
@staticmethod 不能访问或修改类或实例的属性。
"""
@staticmethod
def success_pagination(data: Any, pagination: Optional[dict] = None):
return {
'code': ResponseCodeEnum.SUCCESS.status_code,
'message': ResponseCodeEnum.SUCCESS.message,
'data': data, # 将 QuerySet 序列化为 JSON,
'pagination': pagination
}
@staticmethod
def error(code,message):
return {
'code': code,
'message': message,
'data': None, # 将 QuerySet 序列化为 JSON,
'pagination': None
}
@staticmethod
def success_data(data: Any):
return {
'code': ResponseCodeEnum.SUCCESS.status_code,
'message': ResponseCodeEnum.SUCCESS.message,
'data': data, # 将 QuerySet 序列化为 JSON,
}
##############################PageUtils##############################
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
def paginate_queryset(queryset, page, page_size):
"""
分页封装函数
:param queryset: 查询集
:param page: 页码
:param page_size: 每页记录数
:return: 分页后的数据和分页信息
"""
paginator = Paginator(queryset, page_size)
try:
page_obj = paginator.page(page)
except PageNotAnInteger:
page_obj = paginator.page(1)
except EmptyPage:
page_obj = paginator.page(paginator.num_pages)
pagination_info = {
'total_pages': paginator.num_pages,
'current_page': page_obj.number,
'has_next': page_obj.has_next(),
'has_previous': page_obj.has_previous(),
'next_page_number': page_obj.next_page_number() if page_obj.has_next() else None,
'previous_page_number': page_obj.previous_page_number() if page_obj.has_previous() else None,
'total': paginator.count
}
return page_obj.object_list, pagination_info
##############################Serializer##############################
# db_operations/serializers.py
from rest_framework import serializers
class SQLExecutionSerializer(serializers.Serializer):
sql = serializers.CharField()
四、 settings.py配置
#数据库 连接配置 mysql
DATABASES = {
'default': {
# 数据库引擎
'ENGINE': 'django.db.backends.mysql',
# 'ENGINE': 'sqlalchemy.create_engine',
# 数据库名字
'NAME': 'xxx',
# 数据库用户名
'USER': 'xx',
# 数据库密码
'PASSWORD': 'xx',
# 数据库主机地址
'HOST': 'xxx.xxx.xxx.xxx',
# 数据库端口号
'PORT': 'xxx'
}
}
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'root': {
'handlers': ['console'],
'level': 'INFO',
},
'loggers': {
'django': {
'handlers': ['console'],
'level': 'INFO',
'propagate': False,
},
},
}
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'log',
'monitor',
'user',
'dbOperations'
]
# 告警 邮箱配置 需要开通 smtp 权限 在qq邮箱中 配置
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.qq.com' # QQ邮箱的SMTP服务器
EMAIL_PORT = 465 # QQ邮箱的SMTP端口
EMAIL_USE_SSL = True # 使用SSL加密
EMAIL_HOST_USER = 'xx' # 你的QQ邮箱
EMAIL_HOST_PASSWORD = 'xx' # 你的QQ邮箱密码或授权码
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
# RabbitMQ 配置
RABBITMQ_CONFIG = {
'host': 'xx.xx.xx.xx',
'port': xx,
'virtual_host': '/',
'user': 'xx',
'password': 'xx',
}
五、 创建和应用迁移
#创建数据库迁移文件并应用
python manage.py makemigrations
python manage.py migrate
六、 注册前端请求路由
from django.contrib import admin
from django.urls import path
from log import views as log_views
from monitor import views as monitor_views
from user import views as user_views
from dbOperations import views as db_views
"""
说明:
这个路径将 /admin/ 开头的 URL 映射到 Django 自带的管理后台。
admin.site.urls 是 Django 管理后台的 URL 配置,包含了管理后台的所有功能和页面。
这个路径将 /log/ 开头的 URL 映射到 log 应用中的 getLogList 视图函数。
name='log' 为这个 URL 路径定义了一个名称,可以在模板或其他地方通过这个名称来引用这个 URL。
"""
urlpatterns = [
path('admin/', admin.site.urls),
path('log/getLogList', log_views.getLogList, name='log_list'),
path('log/getDebugLogList', log_views.getDebugLogList,name='debug_log_list'),
path('monitor/getSystemInfo', monitor_views.getSystemInfo,name='system_info'),
path('monitor/getAlarmList', monitor_views.getAlarmList, name='alarm_list'),
path('user/login',user_views.login, name='login'),
path('dbOperations/executeSQLView', db_views.executeSQLView,name='execute_sql_view')
]
七、 启动项目
#打开终端 cd 到根目录
python manage.py runserver
八、 效果
#启动 前端 项目
npm run dev
#==========================前端部分代码 alarmPage.vue==========================
<template>
<div class="app-container">
<el-card shadow="always">
<el-form ref="searchForm" :inline="true" :model="searchMap" style="margin-top: 20px">
<el-form-item>
<el-button type="primary" icon="el-icon-search" @click="searchLog('searchForm')">搜索</el-button>
<el-button type="primary" icon="el-icon-clear" @click="resetForm('searchForm')">重置</el-button>
</el-form-item>
</el-form>
</el-card>
<el-card shadow="always">
<div>
<el-table ref="multipleTable" v-loading="listLoading" :data="alarmListDto" border fit highlight-current-row style="width: 100%;" class="tb-edit">
<el-table-column label="操作ID" prop="id" width="180px" align="center" />
<el-table-column label="事件类型" prop="event_type" width="380px" align="center" />
<el-table-column label="事件详情" prop="event_details" width="380px" align="center" />
<!-- <el-table-column label="是否已解决" prop="is_resolved" width="180px" align="center" /> -->
<el-table-column label="触发时间" prop="event_time" width="180px" align="center" />
</el-table>
</div>
<div class="block">
<el-pagination :current-page="currentPage" :page-sizes="[5, 10, 15, 20]" :page-size="5" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
</div>
</el-card>
</div>
</template>
<script>
import {
getAlarmList
} from '@/api/monitors/monitors-request'
export default {
data() {
return {
total: 0,
currentPage: 1,
searchMap: {},
listLoading: true,
params: {
page: 1, // 当前页码
page_size: 5 // 每页显示数目
},
alarmListDto: [],
error: ''
}
},
mounted() {
this.getAlarmList()
},
methods: {
handleSizeChange(val) {
console.log(`每页 ${val} 条`)
this.params.page_size = val
this.getAlarmList()
},
handleCurrentChange(val) {
console.log(`当前页: ${val}`)
this.params.page = val
this.getAlarmList()
},
// 重置功能,element ui 提供的功能
resetForm(formName) {
console.log(this.$refs[formName].resetFields)
this.$refs[formName].resetFields()
this.getAlarmList()
},
searchLog(formName) {
console.log(this.searchMap)
this.getAlarmList()
},
getAlarmList() {
this.dataLoading = true
console.log('请求参数:' + this.params)
getAlarmList(this.params).then((res) => {
console.log('响应:', res.data.data)
alert(JSON.stringify(res.data.data))
this.alarmListDto = res.data.data
this.total = res.data.pagination.total
setTimeout(() => { // 超过指定超时时间 关闭查询的转圈的loading加载动画
this.listLoading = false
}, 1.5 * 1000)
})
}
}
}
</script>
<style>
</style>