Python实现监控系统

要求:

对(系统、内存、硬盘、用户)进行实时监控

主函数(run.py)
import random
import socket
import platform
import psutil
from flask import Flask, render_template
from datetime import datetime
import getpass

app=Flask(__name__)
app.config['SECRET_KEY']=random._urandom(24)

@app.route('/')
def sysinfo():
    boot_time=psutil.boot_time()
    boot_time=datetime.fromtimestamp(boot_time)
    now_time=datetime.now()
    delta_time=now_time-boot_time
    delta_time=str(delta_time).split('.')[0]
    return render_template('sysinfo.html',
                           master=socket.gethostname(),
                           system=platform.system(),
                           machine=platform.machine(),
                           version=platform.version(),
                           architecture=platform.architecture(),
                           now_time=now_time,
                           boot_time=boot_time,
                           delta_time=delta_time
                           )
@app.route('/cpu/')
def cpu():
    C_times=psutil.cpu_times()
    return render_template('cpu.html',
                           Phy_cpu=psutil.cpu_count(logical=False),
                           Log_cpu=psutil.cpu_count(),
                           user=C_times.user,
                           system=C_times.system,
                           nice=C_times.nice,
                           iowait=C_times.iowait,
                           )

@app.route('/memory/')
def memory():
    info=psutil.virtual_memory()
    return render_template('memory.html',
                           total=str(round(info.total / 1024 / 1024 / 1024)),
                           available=str(round(info.available / 1024 / 1024 / 1024)),
                           percent=str(round(info.percent)),
                           used=str(round(info.used / 1024 / 1024 / 1024)),
                           free=str(round(info.free / 1024 / 1024 / 1024)),
                           buffers=str(round(info.buffers / 1024 / 1024)),
                           cached=str(round(info.cached / 1024 / 1024 / 1024))
                           )


@app.route('/disk/')
def disk():
    disk_info=psutil.disk_partitions()
    info1=disk_info[0]
    content1=psutil.disk_usage(info1.mountpoint)
    info2=disk_info[1]
    content2= psutil.disk_usage(info2.mountpoint)
    info3=disk_info[2]
    content3 = psutil.disk_usage(info3.mountpoint)
    info4=disk_info[3]
    content4 = psutil.disk_usage(info4.mountpoint)
    info5=disk_info[4]
    content5 = psutil.disk_usage(info5.mountpoint)
    return render_template('disk.html',
                           device1=info1.device,
                           mountpoint1=info1.mountpoint,
                           total1=str(round(content1.total /1024 /1024 /1024)),
                           used1=str(round(content1.used /1024 /1024 /1024)),
                           percent1=str(round(content1.percent)),
                           free1=str(round(content1.free /1024 /1024 /1024)),
                           fstype1=info1.fstype,
                           opts1=info1.opts,
                           device2=info2.device,
                           mountpoint2=info2.mountpoint,
                           total2=str(round(content2.total / 1024 / 1024 / 1024)),
                           used2=str(round(content2.used / 1024 / 1024 / 1024)),
                           percent2=str(round(content2.percent)),
                           free2=str(round(content2.free / 1024 / 1024 / 1024)),
                           fstype2=info2.fstype,
                           opts2=info2.opts,
                           device3=info3.device,
                           mountpoint3=info3.mountpoint,
                           total3=str(round(content3.total / 1024 / 1024 / 1024)),
                           used3=str(round(content3.used / 1024 / 1024 / 1024)),
                           percent3=str(round(content3.percent)),
                           free3=str(round(content3.free / 1024 / 1024 / 1024)),
                           fstype3=info3.fstype,
                           opts3=info3.opts,
                           device4=info4.device,
                           mountpoint4=info4.mountpoint,
                           total4=str(round(content4.total / 1024 / 1024 / 1024)),
                           used4=str(round(content4.used / 1024 / 1024 / 1024)),
                           percent4=str(round(content4.percent)),
                           free4=str(round(content4.free / 1024 / 1024 / 1024)),
                           fstype4=info4.fstype,
                           opts4=info4.opts,
                           device5=info5.device,
                           mountpoint5=info5.mountpoint,
                           total5=str(round(content5.total / 1024 / 1024 / 1024)),
                           used5=str(round(content5.used / 1024 / 1024 / 1024)),
                           percent5=str(round(content5.percent)),
                           free5=str(round(content5.free / 1024 / 1024 / 1024)),
                           fstype5=info5.fstype,
                           opts5=info5.opts,
                           )

@app.route('/user/')
def user():
    return render_template('user.html',
                           user=getpass.getuser(),
                           master=socket.gethostname(),
                           now_time=datetime.now()
                           )

app.run()

以下为templates中html

模版页面(base.html)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../static/css/bootstrap.min.css">
    <link rel="stylesheet" href="../static/css/main.css">
    <script href="js/bootstrap.min.js"></script>
</head>
<body>

<nav class="navbar navbar-default">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="index.html"></a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li><a href="#">Sys Info<span class="sr-only">(current)</span></a></li>
                <li><a href="/">系统</a></li>
                <li><a href="/cpu/">CPU</a></li>
                <li><a href="/memory/">内存</a></li>
                <li><a href="/disk/">硬盘</a></li>
                <li><a href="/user/">用户</a></li>
            </ul>

        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>
<div class="col-xs-1">
</div>

{% block content %}

{% endblock %}

<div class="col-xs-1">
</div>
</body>
</html>
系统页面(sysinfo.html)
{% extends 'base.html' %}

{% block content %}
<div class="col-xs-9">
    <table class="table table-bordered">
      <tr >
          <th width="400">主机名</th>
          <th>{{master}}</th>
      </tr>
        <tr>
          <th>内核名称</th>
          <th>{{system}}</th>
      </tr>
        <tr>
          <th>发行版本号</th>
          <th>{{machine}}</th>
      </tr>
        <tr>
          <th>内核版本</th>
          <th>{{version}}</th>
      </tr>
        <tr>
          <th>系统构架</th>
          <th>{{architecture}}</th>
      </tr>
        <tr>
          <th>现在时间</th>
          <th>{{now_time}}</th>
      </tr>
        <tr>
          <th>开机时间</th>
          <th>{{boot_time}}</th>
      </tr>
        <tr>
          <th>运行时间</th>
          <th>{{delta_time}}</th>
      </tr>

    </table>
    </div>
{% endblock %}
内存页面(memory.html):
{% extends 'base.html' %}

{% block content %}
<div class="col-xs-9">
    <table class="table table-bordered">
      <tr >
          <th width="400">内存大小</th>
          <th>{{total}}G</th>
      </tr>
        <tr>
          <th>可用内存</th>
          <th>{{available}}G</th>
      </tr>
        <tr>
          <th>内存占用</th>
          <th>{{percent}}%</th>
      </tr>
        <tr>
          <th>以用内存</th>
          <th>{{used}}G</th>
      </tr>
        <tr>
          <th>空闲内存</th>
          <th>{{free}}G</th>
      </tr>
        <tr>
          <th>buffers</th>
          <th>{{buffers}}M</th>
      </tr>
        <tr>
          <th>cached</th>
          <th>{{cached}}G</th>
      </tr>
    </table>
    </div>
{% endblock %}
硬盘页面(disk.html)
{% extends 'base.html' %}

{% block content %}
<div class="col-xs-9">
    <table class="table table-bordered">
      <tr >
          <th width="300">设备</th>
          <th width="300">挂载点</th>
          <th width="300">容量</th>
          <th width="300">以用</th>
          <th width="300">剩余</th>
          <th width="300">类型</th>
          <th width="300">选项</th>
      </tr>
        <tr>
          <th>{{device1}}</th>
          <th>{{mountpoint1}}</th>
          <th>{{total1}}G</th>
          <th>{{used1}}G({{percent1}}%)</th>
          <th>{{free1}}G</th>
          <th>{{fstype1}}</th>
          <th>{{opts1}}</th>
      </tr>
        <tr>
          <th>{{device2}}</th>
          <th>{{mountpoint2}}</th>
          <th>{{total2}}G</th>
          <th>{{used2}}G({{percent2}}%)</th>
          <th>{{free2}}G</th>
          <th>{{fstype2}}</th>
          <th>{{opts2}}</th>
      </tr>
        <tr>
          <th>{{device3}}</th>
          <th>{{mountpoint3}}</th>
          <th>{{total3}}G</th>
          <th>{{used3}}G({{percent3}}%)</th>
          <th>{{free3}}G</th>
          <th>{{fstype3}}</th>
          <th>{{opts3}}</th>
      </tr>
        <tr>
          <th>{{device4}}</th>
          <th>{{mountpoint4}}</th>
          <th>{{total4}}G</th>
          <th>{{used4}}G({{percent4}}%)</th>
          <th>{{free4}}G</th>
          <th>{{fstype4}}</th>
          <th>{{opts4}}</th>
      </tr>
        <tr>
          <th>{{device5}}</th>
          <th>{{mountpoint5}}</th>
          <th>{{total5}}G</th>
          <th>{{used5}}G({{percent5}}%)</th>
          <th>{{free5}}G</th>
          <th>{{fstype5}}</th>
          <th>{{opts5}}</th>
      </tr>
    </table>
    </div>
{% endblock %}
用户页面(user.html)
{% extends 'base.html' %}

{% block content %}
<div class="col-xs-9">
    <table class="table table-bordered">
      <tr >
          <th width="300">用户名</th>
          <th width="300">终端</th>
          <th width="300">主机名</th>
          <th width="300">登录时间</th>
      </tr>
        <tr>
          <th>{{user}}</th>
          <th>123</th>
            <th>{{master}}</th>
            <th>{{now_time}}</th>
      </tr>

    </table>
    </div>
{% endblock %}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值