要求:
对(系统、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)),