要求: 对(系统、CPU、内存、硬盘、用户)进行实时监控
主函数(run.py):
import random
import socket
import platform
import psutil
from flask import Flask, render_template
from datetime import datetime
import getpass
fla=Flask(__name__)
fla.config['SECRET_KEY']=random._urandom(24)
@fla.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
)
@fla.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,
)
@fla.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))
)
@fla.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,
)
@fla.route('/user/')
def user():
return render_template('user.html',
user=getpass.getuser(),
master=socket.gethostname(),
now_time=datetime.now()
)
fla.run()
模版页面(base.html):
Title
{% block content %}
{% endblock %}
系统页面(sysinfo.html):
{% extends 'base.html' %}
{% block content %}
主机名
{{master}}
内核名称
{{system}}
发行版本号
{{machine}}
内核版本
{{version}}
系统构架
{{architecture}}
现在时间
{{now_time}}
开机时间
{{boot_time}}
运行时间
{{delta_time}}
{% endblock %}
CPU页面(cpu.html):
{% extends 'base.html' %}
{% block content %}
物理CPU核心数
{{Phy_cpu}}
逻辑CPU核心数
{{Log_cpu}}
用户
{{user}}
系统
{{system}}
空闲
{{}}
iowait
{{iowait}}
{% endblock %}
内存页面(memory.html):
{% extends 'base.html' %}
{% block content %}
内存大小
{{total}}G
可用内存
{{available}}G
内存占用
{{percent}}%
以用内存
{{used}}G
空闲内存
{{free}}G
buffers
{{buffers}}M
cached
{{cached}}G
{% endblock %}
硬盘页面(disk.html):
{% extends 'base.html' %}
{% block content %}
设备
挂载点
容量
以用
剩余
类型
选项
{{device1}}
{{mountpoint1}}
{{total1}}G
{{used1}}G({{percent1}}%)
{{free1}}G
{{fstype1}}
{{opts1}}
{{device2}}
{{mountpoint2}}
{{total2}}G
{{used2}}G({{percent2}}%)
{{free2}}G
{{fstype2}}
{{opts2}}
{{device3}}
{{mountpoint3}}
{{total3}}G
{{used3}}G({{percent3}}%)
{{free3}}G
{{fstype3}}
{{opts3}}
{{device4}}
{{mountpoint4}}
{{total4}}G
{{used4}}G({{percent4}}%)
{{free4}}G
{{fstype4}}
{{opts4}}
{{device5}}
{{mountpoint5}}
{{total5}}G
{{used5}}G({{percent5}}%)
{{free5}}G
{{fstype5}}
{{opts5}}
{% endblock %}
用户页面(user.html):
{% extends 'base.html' %}
{% block content %}
用户名
终端
主机名
登录时间
{{user}}
123
{{master}}
{{now_time}}
{% endblock %}
执行结果: 系统页面:
CPU页面:
内存页面:
硬盘页面:
用户页面:
对系统中的这些信息主要用的还是(psutil
)函数