这段代码使用flask获取系统性能阅读,并使用每秒刷新一次的Chart将其绘制在Django网页上。我正在尝试将数据存储到postgresql中,但它不起作用。没有数据被插入到在数据库中创建的表中。
views.py
from django.shortcuts import render
import requests
from django.http import HttpResponse, request
from .models import Usage
def monitor(request):
url = "http://127.0.0.1:5000/stats"
data = requests.get(url)
print(data.text)
data2 = data.text
return render(request, "home.html", {'alldata': data2})
def cpu_monitor(request):
url = "http://127.0.0.1:5000/cpu"
cpu_data = requests.get(url)
cpu_data2 = cpu_data.json()
for key in cpu_data2:
cpu = cpu_data2['CPU']
time = cpu_data2['Time']
print(cpu)
print(time)
val = key
return render(request, "chart.html", {'data': val})
def cp(request):
url = "http://127.0.0.1:5000/cpu"
cpu_data = requests.get(url)
cpu_data2 = cpu_data.json()
for key in cpu_data2:
cpu = cpu_data2['CPU']
time = cpu_data2['Time']
print(cpu)
print(time)
val = [cpu]
return HttpResponse(val)
def mm(request):
url = "http://127.0.0.1:5000/memory"
memory_data = requests.get(url)
memory_data2 = memory_data.json()
for key in memory_data2:
memory = memory_data2['Memory Percentage']
time = memory_data2['Time']
print(memory)
print(time)
mem = [memory]
return HttpResponse(mem)
def dk(request):
url = "http://127.0.0.1:5000/disk"
disk_data = requests.get(url)
disk_data2 = disk_data.json()
for key in disk_data2:
disk = disk_data2['Used Partition']
time = disk_data2['Time']
print(disk)
print(time)
dsk = [disk]
return HttpResponse(dsk)
def dkf(request):
url = "http://127.0.0.1:5000/diskfree"
free_disk_data = requests.get(url)
free_disk_data2 = free_disk_data.json()
for key in free_disk_data2:
free_disk = free_disk_data2['Free Partition']
time = free_disk_data2['Time']
print(free_disk)
print(time)
dskf = [free_disk]
return HttpResponse(dskf)
def ib(request):
url = "http://127.0.0.1:5000/inb"
ib_data = requests.get(url)
ib_data2 = ib_data.json()
for key in ib_data2:
ibn = ib_data2['Received Bytes']
time = ib_data2['Time']
print(ibn)
print(time)
ibd = [ibn]
return HttpResponse(ibd)
def ob(request):
url = "http://127.0.0.1:5000/oub"
ob_data = requests.get(url)
ob_data2 = ob_data.json()
for key in ob_data2:
obn = ob_data2['Received Bytes']
time = ob_data2['Time']
print(obn)
print(time)
obd = [obn]
return HttpResponse(obd)
def insert():
sysMon = Usage
sysMon.cpu = cp(request)
sysMon.memory = mm(request)
sysMon.freeDisk = dk(request)
sysMon.usedDisk = dkf(request)
sysMon.inbound = ib(request)
sysMon.outbound = ob(request)
sysMon.save()
models.py
from django.db import models
class Usage(models.Model):
cpu = models.CharField(max_length=200, null=True)
memory = models.CharField(max_length=200, null=True)
freeDisk = models.CharField(max_length=200, null=True)
usedDisk = models.CharField(max_length=200, null=True)
inbound = models.CharField(max_length=200, null=True)
outbound = models.CharField(max_length=200, null=True)
任何帮助都将不胜感激。
1条答案
按热度按时间pw9qyyiw1#
了解每个函数的数据类型。视图函数返回HttpResponse类型的数据。应更改数据函数,使每个函数返回所需类型的数据,而不是返回HttpResponse。例如,mm函数应返回内存数据,在模型中为CharField(它是文本,限制为200个字符)。确保它返回文本,而不是Python列表,不是json,也不是HttpResponse。
然后调用一个视图函数,它将收集(来自所有数据函数)并在返回HttpResponse之前保存它。
还要注意的是,对于数据收集和保存到数据库,你不一定要使用视图(需要浏览器和人工加载页面),你可以创建一个Python脚本来加载Django模块,并从后端进行数据收集,例如定期从cron(Linux上)或Windows任务调度程序调用。