基于Django开发的服务器监控系统

基于Django开发的服务器监控系统

  • 项目要求
  • 项目展示
  • 核心代码
    • 客户端
    • 服务器端
  • 项目源码
  • 展示视频

项目要求

可以同时查看多个终端的运行状况信息(如处理器,内存,硬盘等)

项目展示

基于Django开发的服务器监控系统_第1张图片

基于Django开发的服务器监控系统

核心代码

客户端

调用psutil库,使用request每隔1s向服务器发送当前状态

import time
from datetime import datetime

import psutil
import requests

while True:
    cpu_num = psutil.cpu_count()
    cpu_percent = psutil.cpu_percent()

    memory_total = psutil.virtual_memory().total
    memory_ava = psutil.virtual_memory().available
    memory_per = psutil.virtual_memory().percent

    disk_total = psutil.disk_usage('/').total
    disk_free = psutil.disk_usage('/').free

    net_sent = psutil.net_io_counters().bytes_sent
    net_rec = psutil.net_io_counters().bytes_recv

    now = datetime.now()

    data = {'cpu_num': cpu_num, 'cpu_percent': cpu_percent, 'memory_total': memory_total, 'memory_ava': memory_ava,
            'memory_per': memory_per, 'disk_total': disk_total, 'disk_free': disk_free, 'net_sent': net_sent,
            'net_rec': net_rec, 'time': now}

    headers = {'Content-Type': 'application/json'}
    try:
        requests.post('http://192.168.12.1:8000/api', data, headers)
    except:
        print("error")
    time.sleep(1)

服务器端

创立一个api用于接收发送的信息,并存储到Django自带的数据库中
创立一个网页用于访问展示

from django.http import HttpResponse
from django.db import models
from django.shortcuts import render
import datetime
from .models import pc


# Create your views here.

def getMessage(request):
    if request.method == "POST":
        ip = ""
        if request.META.get('HTTP_X_FORWARDED_FOR'):
            ip = request.META.get("HTTP_X_FORWARDED_FOR")
        else:
            ip = request.META.get("REMOTE_ADDR")
        cpu_num = request.POST.get("cpu_num")
        cpu_percent = request.POST.get("cpu_percent")
        memory_total = request.POST.get("memory_total")
        memory_ava = request.POST.get("memory_ava")
        memory_per = request.POST.get("memory_per")
        disk_total = request.POST.get("disk_total")
        disk_free = request.POST.get("disk_free")
        net_sent = request.POST.get("net_sent")
        net_rec = request.POST.get("net_rec")
        time = request.POST.get("time")

        try:
            pc.objects.get(ip=ip)
        except:
            pc.objects.create(ip=ip, time=time, cpu_num=cpu_num, cpu_percent=cpu_percent, memory_total=memory_total,
                              memory_ava=memory_ava, memory_per=memory_per, disk_total=disk_total,
                              disk_free=disk_free,
                              net_sent=net_sent, net_rec=net_rec)
        else:
            pc.objects.filter(ip=ip).update(cpu_num=cpu_num, time=time, cpu_percent=cpu_percent,
                                            memory_total=memory_total,
                                            memory_ava=memory_ava, memory_per=memory_per, disk_total=disk_total,
                                            disk_free=disk_free,
                                            net_sent=net_sent, net_rec=net_rec)

        return HttpResponse("ok")


def home(request):
    pcs = pc.objects.all()

    text = ""
    num = 1

    for p in pcs:
        p.time = p.time + datetime.timedelta(hours=8)

        text += str(
            num) + " " + p.time.strftime(
            "%m-%d %H:%M:%S") + ' - ' + p.ip + ' - ' + p.cpu_num + ' - ' + p.cpu_percent + ' - ' + p.memory_total + ' - ' + p.memory_ava \
                + ' - ' + p.memory_per + ' - ' + p.disk_total + ' - ' + p.disk_free + ' - ' + p.net_sent + ' - ' + p.net_rec + " 
"
num += 1 return render(request, "home.html", {"text": text})
DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>服务器浏览title>
head>
<body>
<h2>服务器h2>
<h3>编号 - 时间 - ip - 处理器个数 - 处理器使用率 - 总内存 - 剩余内存 - 内存使用率 - 硬盘总空间 - 硬盘剩余空间 - 网络发送量 - 网络接收量h3>
{% autoescape off %}
<h3>{{ text }}h3>
{% endautoescape %}
<form method="post">
    {% csrf_token %}
    <input type="submit" value="刷新" name="save_button" style="width: 80px;height: 60px">
form>
<a>作者:zhj12399a>
body>
html>

项目源码

GitHub地址

展示视频

bilibili地址

你可能感兴趣的:(django,服务器,python)