python实验13 Python监测服务器

寸阳分阴须爱惜,休负春色与时光。这篇文章主要讲述python实验13 Python监测服务器相关的知识,希望能为你提供帮助。
实验背景企业的服务器不管是对内部员?还是对外提供服务,服务器的状态是?个?常重要的信号,?旦出现cpu使?率过?,或是内存不?的情况,都要运维?员及时去处理调试的,以免影响到提供服务。
实验目的【python实验13 Python监测服务器】现使?python对?个服务器进?监测,每隔?段时间(具体看企业的需求),定时执?脚本,实时记录服务器cpu使?率,内存使?情况,磁盘使?情况,服务器的?络信息(实时上传/下载速度)。?旦cpu,内存,磁盘使?率超过百分之80,?即像管理员的邮箱发送告警信息。
实验脚本

import time
import socket
import platform
import psutil
from apscheduler.schedulers.blocking import BlockingScheduler
import smtplib
from email6.mime.multipart import MIMEMultipart
from email6.mime.text import MIMEText
import datetime
import os
import sys

def main():
print(----------------------系统信息--------------------------)
os_name = platform.platform()#获取操作系统名称及版本号
pc_name = platform.node()#获取计算机(服务器)的网络名称
processor = platform.processor()#获取计算机(服务器)处理器的信息
processor_bit = platform.architecture()[0]#获取操作系统的位数
myname = socket.gethostname()#socket方式获取服务器名称
myaddr = socket.gethostbyname(myname)#socket方式获取服务器IP地址

print(f系统信息:os_name)
print(f机器名称:pc_name)
print(f处理器:processor)
print(f处理器位数:processor_bit)
print(fIP地址:myaddr)

#用户信息
users_count=len(psutil.users())#系统用户的个数
users_list=(u.name for u in psutil.users())#系统用户名
print(f当前用户数量:users_count用户名:users_list)

#时间
now_time = time.strftime(%Y-%m-%d %H:%M:%S, time.localtime())#strftime()将localtime()返回本地时间的对象转换为格式化的时间字符串
boot_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(psutil.boot_time()))) #在localtime内添加参数,获得设备上次的开启时间,psutil.boot_time()查询上次开机时间的时间戳,我们只取整数部分
print(f系统启动时间:boot_time)
print(f系统当前时间:now_time)

print(----------------------cpu监控--------------------------)
cpuper=psutil.cpu_percent()#获取cpu的使用率
if cpuper> =80:#CPU的使用率> =80
with open(服务器异常报告.txt + datetime.date.today().isoformat(),a+) as f:# datetime.date.today().isoformat()返回一个如 "YYYY-MM-DD"格式的日期字符串(今天)
f.write(fnow_time_警告!服务器cpu利用率超过80%!)
print(fcpu利用率:cpuper%)

print(----------------------内存监控 -------------------------)
#内存使用情况
mem=psutil.virtual_memory()#获取内存信息
mem_total_G=str(int(mem.total / (1024.0 * 1024.0 * 1024.0))) + "G"#默认单位byte,换算单位为GB,总内存
mem_used_G = str(int(mem.used / (1024.0 * 1024.0 * 1024.0))) + "G"#已经使用过的内存
mem_available_G = str(int(mem.available / (1024.0 * 1024.0 * 1024.0))) + "G"#可用的内存
mem_free_G = str(int(mem.free / (1024.0 * 1024.0 * 1024.0))) + "G"#剩余的物理内存
print(f内存总容量:mem_total_G已使用内存:mem_used_G可用内存:mem_available_G剩余物理内存:mem_free_G内存使用率:mem.percent%)
if mem.percent> =80:
with open(服务器异常报告.txt + datetime.date.today().isoformat(),a+) as f:
f.write(fnow_time_警告!服务器内存利用率超过80%!)

print(----------------------网络监控 --------------------------)
net_msg = psutil.net_io_counters() #获取网络总的io情况,单位都是Byte字节
bytes_sent, bytes_recv = net_msg.bytes_sent, net_msg.bytes_recv#sent是上传,recv是下载的
time.sleep(3)#3s之后再次获取网络I/O接口数据量,以便求3s内的平均上传下载速度
net_msg = psutil.net_io_counters()
bytes_sent2, bytes_recv2 = net_msg.bytes_sent, net_msg.bytes_recv
sent_speed = (bytes_sent2 - bytes_sent) / 3
sent_speed = str(round((sent_speed / 1048576), 2)) + " MB/s" if sent_speed > = 1048576 else str(round((sent_speed / 1024), 2)) + " KB/s"#round(x,2)对x进行四舍五入,保留两位小数.这里if来判断流量是否大于1MB/s.小于1MB就不转换成MB转换成KB/s
recv_speed = (bytes_recv2 - bytes_recv) / 3
recv_speed = str(round((recv_speed / 1048576), 2)) + " MB/s" if recv_speed > = 1048576 else str(round(recv_speed / 1024, 2)) + " KB/s"
print(f"网络实时I/O(3s内)\\n上传速度:sent_speed\\n下载速度:recv_speed")

print(----------------------磁盘信息--------------------------)

disk_partitons = psutil.disk_partitions() #获取服务器所有磁盘分区的简略信息,返回一个列表

for disk in disk_partitons: #遍历每一个磁盘
o = psutil.disk_usage(disk.mountpoint)#mountpoint变量是每个磁盘,disk_usage来获取磁盘的空间使用情况
path = disk.device#disk.device表示每个磁盘的名字(路径)
total = str(int(o.total / (

    推荐阅读