upload project source code
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from app.common.response import SuccessResponse
|
||||
from app.core.dependencies import AuthPermission
|
||||
from app.core.logger import log
|
||||
from app.core.router_class import OperationLogRoute
|
||||
|
||||
from .service import ServerService
|
||||
|
||||
|
||||
ServerRouter = APIRouter(route_class=OperationLogRoute, prefix="/server", tags=["服务器监控"])
|
||||
|
||||
@ServerRouter.get(
|
||||
'/info',
|
||||
summary="查询服务器监控信息",
|
||||
description="查询服务器监控信息",
|
||||
dependencies=[Depends(AuthPermission(["module_monitor:server:query"]))]
|
||||
)
|
||||
async def get_monitor_server_info_controller() -> JSONResponse:
|
||||
"""
|
||||
查询服务器监控信息
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含服务器监控信息的JSON响应。
|
||||
"""
|
||||
result_dict = await ServerService.get_server_monitor_info_service()
|
||||
log.info(f'获取服务器监控信息成功: {result_dict}')
|
||||
|
||||
return SuccessResponse(data=result_dict, msg='获取服务器监控信息成功')
|
||||
@@ -0,0 +1,79 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
|
||||
class CpuInfoSchema(BaseModel):
|
||||
"""CPU信息模型"""
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
cpu_num: int = Field(description="CPU核心数")
|
||||
used: float = Field(ge=0, le=100, description="CPU用户使用率(%)")
|
||||
sys: float = Field(ge=0, le=100, description="CPU系统使用率(%)")
|
||||
free: float = Field(ge=0, le=100, description="CPU空闲率(%)")
|
||||
|
||||
|
||||
class MemoryInfoSchema(BaseModel):
|
||||
"""内存信息模型"""
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
total: str = Field(description="内存总量")
|
||||
used: str = Field(description="已用内存")
|
||||
free: str = Field(description="剩余内存")
|
||||
usage: float = Field(ge=0, le=100, description="使用率(%)")
|
||||
|
||||
|
||||
class SysInfoSchema(BaseModel):
|
||||
"""系统信息模型"""
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
computer_ip: str = Field(description="服务器IP")
|
||||
computer_name: str = Field(description="服务器名称")
|
||||
os_arch: str = Field(description="系统架构")
|
||||
os_name: str = Field(description="操作系统")
|
||||
user_dir: str = Field(description="项目路径")
|
||||
|
||||
|
||||
class PyInfoSchema(BaseModel):
|
||||
"""Python运行信息模型"""
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
name: str = Field(description="Python名称")
|
||||
version: str = Field(description="Python版本")
|
||||
start_time: str = Field(description="启动时间")
|
||||
run_time: str = Field(description="运行时长")
|
||||
home: str = Field(description="安装路径")
|
||||
memory_used: str = Field(description="内存占用")
|
||||
memory_usage: float = Field(ge=0, le=100, description="内存使用率(%)")
|
||||
memory_total: str = Field(description="总内存")
|
||||
memory_free: str = Field(description="剩余内存")
|
||||
|
||||
|
||||
class DiskInfoSchema(BaseModel):
|
||||
"""磁盘信息模型"""
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
dir_name: str = Field(description="磁盘路径")
|
||||
sys_type_name: str = Field(description="文件系统类型")
|
||||
type_name: str = Field(description="磁盘类型")
|
||||
total: str = Field(description="总容量")
|
||||
used: str = Field(description="已用容量")
|
||||
free: str = Field(description="可用容量")
|
||||
usage: float = Field(ge=0, le=100, description="使用率(%)")
|
||||
|
||||
|
||||
class ServerMonitorSchema(BaseModel):
|
||||
"""服务器监控信息模型"""
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
cpu: CpuInfoSchema = Field(description="CPU信息")
|
||||
mem: MemoryInfoSchema = Field(description="内存信息")
|
||||
py: PyInfoSchema = Field(description="Python运行信息")
|
||||
sys: SysInfoSchema = Field(description="系统信息")
|
||||
disks: list[DiskInfoSchema] = Field(default_factory=list, description="磁盘信息")
|
||||
@@ -0,0 +1,164 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import platform
|
||||
import psutil
|
||||
import socket
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
from app.utils.common_util import bytes2human
|
||||
|
||||
from .schema import (
|
||||
CpuInfoSchema,
|
||||
MemoryInfoSchema,
|
||||
PyInfoSchema,
|
||||
ServerMonitorSchema,
|
||||
DiskInfoSchema,
|
||||
SysInfoSchema
|
||||
)
|
||||
|
||||
|
||||
class ServerService:
|
||||
"""服务监控模块服务层"""
|
||||
|
||||
@classmethod
|
||||
async def get_server_monitor_info_service(cls) -> dict:
|
||||
"""
|
||||
获取服务器监控信息
|
||||
|
||||
返回:
|
||||
- Dict: 包含服务器监控信息的字典。
|
||||
"""
|
||||
return ServerMonitorSchema(
|
||||
cpu=cls._get_cpu_info(),
|
||||
mem=cls._get_memory_info(),
|
||||
sys=cls._get_system_info(),
|
||||
py=cls._get_python_info(),
|
||||
disks=cls._get_disk_info()
|
||||
).model_dump()
|
||||
|
||||
@classmethod
|
||||
def _get_cpu_info(cls) -> CpuInfoSchema:
|
||||
"""
|
||||
获取CPU信息
|
||||
|
||||
返回:
|
||||
- CpuInfoSchema: CPU信息模型。
|
||||
"""
|
||||
cpu_times = psutil.cpu_times_percent()
|
||||
cpu_num=psutil.cpu_count(logical=True)
|
||||
if not cpu_num:
|
||||
cpu_num = 1
|
||||
return CpuInfoSchema(
|
||||
cpu_num=cpu_num,
|
||||
used=cpu_times.user,
|
||||
sys=cpu_times.system,
|
||||
free=cpu_times.idle
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _get_memory_info(cls) -> MemoryInfoSchema:
|
||||
"""
|
||||
获取内存信息
|
||||
|
||||
返回:
|
||||
- MemoryInfoSchema: 内存信息模型。
|
||||
"""
|
||||
memory = psutil.virtual_memory()
|
||||
return MemoryInfoSchema(
|
||||
total=bytes2human(memory.total),
|
||||
used=bytes2human(memory.used),
|
||||
free=bytes2human(memory.free),
|
||||
usage=memory.percent
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _get_system_info(cls) -> SysInfoSchema:
|
||||
"""
|
||||
获取系统信息
|
||||
|
||||
返回:
|
||||
- SysInfoSchema: 系统信息模型。
|
||||
"""
|
||||
hostname = socket.gethostname()
|
||||
return SysInfoSchema(
|
||||
computer_ip=socket.gethostbyname(hostname),
|
||||
computer_name=platform.node(),
|
||||
os_arch=platform.machine(),
|
||||
os_name=platform.platform(),
|
||||
user_dir=str(Path.cwd())
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _get_python_info(cls) -> PyInfoSchema:
|
||||
"""
|
||||
获取Python解释器信息
|
||||
|
||||
返回:
|
||||
- PyInfoSchema: Python解释器信息模型。
|
||||
"""
|
||||
current_process = psutil.Process()
|
||||
memory = psutil.virtual_memory()
|
||||
process_memory = current_process.memory_info()
|
||||
|
||||
start_time = current_process.create_time()
|
||||
run_time = ServerService._calculate_run_time(start_time)
|
||||
|
||||
return PyInfoSchema(
|
||||
name=current_process.name(),
|
||||
version=platform.python_version(),
|
||||
start_time=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start_time)),
|
||||
run_time=run_time,
|
||||
home=str(Path(current_process.exe())),
|
||||
memory_total=bytes2human(memory.available),
|
||||
memory_used=bytes2human(process_memory.rss),
|
||||
memory_free=bytes2human(memory.available - process_memory.rss),
|
||||
memory_usage=round((process_memory.rss / memory.available) * 100, 2)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _get_disk_info(cls) -> list[DiskInfoSchema]:
|
||||
"""
|
||||
获取磁盘信息
|
||||
|
||||
返回:
|
||||
- list[DiskInfoSchema]: 磁盘信息模型列表。
|
||||
"""
|
||||
disk_info = []
|
||||
for partition in psutil.disk_partitions():
|
||||
try:
|
||||
# 使用mountpoint而不是device来获取磁盘使用情况
|
||||
usage = psutil.disk_usage(partition.mountpoint)
|
||||
mount_point = str(Path(partition.mountpoint))
|
||||
disk_info.append(
|
||||
DiskInfoSchema(
|
||||
dir_name=mount_point, # 使用mountpoint替代device
|
||||
sys_type_name=partition.fstype,
|
||||
type_name=f'本地固定磁盘({mount_point})',
|
||||
total=bytes2human(usage.total),
|
||||
used=bytes2human(usage.used),
|
||||
free=bytes2human(usage.free),
|
||||
usage=usage.percent # 直接使用数字而不是字符串
|
||||
)
|
||||
)
|
||||
except (PermissionError, FileNotFoundError):
|
||||
# 明确指定可能的异常
|
||||
continue
|
||||
return disk_info
|
||||
|
||||
@classmethod
|
||||
def _calculate_run_time(cls,start_time: float) -> str:
|
||||
"""
|
||||
计算运行时间
|
||||
|
||||
参数:
|
||||
- start_time (float): 进程启动时间(时间戳)
|
||||
|
||||
返回:
|
||||
- str: 格式化后的运行时间字符串(例如:"1天2小时3分钟")
|
||||
"""
|
||||
difference = time.time() - start_time
|
||||
days = int(difference // (24 * 60 * 60))
|
||||
hours = int((difference % (24 * 60 * 60)) // (60 * 60))
|
||||
minutes = int((difference % (60 * 60)) // 60)
|
||||
return f'{days}天{hours}小时{minutes}分钟'
|
||||
Reference in New Issue
Block a user