#!/usr/bin/env python3 # -*- coding: utf-8 -*- from fastapi import APIRouter, Request, Depends from database import get_db from sqlalchemy.orm import Session from models import * import json router = APIRouter() @router.post('/monitoring') async def mine(request: Request,db: Session = Depends(get_db)): data = await request.body() body = data.decode(encoding='utf-8') if len(body) > 0: body = json.loads(body) if "system_ip" in body:system_ip = body['system_ip'] else:system_ip='未知ip' if "system_uptime" in body:system_uptime = body['system_uptime'] else:system_uptime=0 if "cpu_cores" in body:cpu_cores = body['cpu_cores'] else:cpu_cores=0 if "cpu_count" in body:cpu_count = body['cpu_count'] else:cpu_count=0 if "cpu_freq" in body:cpu_freq = body['cpu_freq'] else:cpu_freq=0.0 if "cpu_usage" in body:cpu_usage = body['cpu_usage'] else:cpu_usage=0.0 if "memory_total" in body:memory_total = body['memory_total'] else:memory_total=0 if "memory_usage" in body:memory_usage = body['memory_usage'] else:memory_usage=0.0 if "disk1_name" in body:disk1_name = body['disk1_name'] else:disk1_name='未配置磁盘' if "disk1_total" in body:disk1_total = body['disk1_total'] else:disk1_total=0 if "disk1_usage" in body:disk1_usage = body['disk1_usage'] else:disk1_usage=0.0 if "disk1_read_speed" in body:disk1_read_speed = body['disk1_read_speed'] else:disk1_read_speed=0 if "disk1_write_speed" in body:disk1_write_speed = body['disk1_write_speed'] else:disk1_write_speed=0 if "disk2_name" in body:disk2_name = body['disk2_name'] else:disk2_name='未配置磁盘' if "disk2_total" in body:disk2_total = body['disk2_total'] else:disk2_total=0 if "disk2_usage" in body:disk2_usage = body['disk2_usage'] else:disk2_usage=0.0 if "disk2_read_speed" in body:disk2_read_speed = body['disk2_read_speed'] else:disk2_read_speed=0 if "disk2_write_speed" in body:disk2_write_speed = body['disk2_write_speed'] else:disk2_write_speed=0 monitor = monitorEntity( system_ip=system_ip, system_uptime = system_uptime, cpu_cores = cpu_cores, cpu_count = cpu_count, cpu_freq = cpu_freq, cpu_usage = cpu_usage, memory_total = memory_total, memory_usage = memory_usage, disk1_name = disk1_name, disk1_total = disk1_total, disk1_usage = disk1_usage, disk1_read_speed = disk1_read_speed, disk1_write_speed = disk1_write_speed, disk2_name = disk2_name, disk2_total = disk2_total, disk2_usage = disk2_usage, disk2_read_speed = disk2_read_speed, disk2_write_speed = disk2_write_speed ) db.add(monitor) db.commit() else: return None return { 'msg': '提交成功', 'code': 200 }