#磁盘使用率
disk = psutil.disk_partitions()
for i in disk:
print "磁盘:%s 分区格式:%s"%(i.device,i.fstype)
disk_use = psutil.disk_usage(i.device)
print "使用了:%sM,空闲:%sM,总共:%sM,使用率\033[1;31;42m%s%%\033[0m,"%(disk_use.used/1024/1024,disk_use.free/1024/1024,disk_use.total/1024/1024,disk_use.percent)
#网络使用率
1. 监控服务器cpu使用率
def cpu():
while True:
time.sleep(1)
cpu_liyonglv = psutil.cpu_percent()
print "当前cpu利用率:\033[1;31;42m%s%%\033[0m"%cpu_liyonglv
if cpu_liyonglv >15.0:
baojing()
def baojing():
i = 0
while i < 10 :
i += 1
time.sleep(0.5)
winsound.PlaySound("ALARM8",winsound.SND_ALIAS)
cpu()
2. 监控服务器内存使用率
memory = psutil.virtual_memory()
print memory.used
print memory.total
ab = float(memory.used)/float(memory.total)*100
print "%.2f%%"%ab
print psutil.swap_memory()
3. 监控服务器网卡使用率
#网络使用率
count = _io_counters()
print "发送字节数:\033[1;31;42m%s\033[0mbytes,接收字节数:\033[1;31;42m%s\033[0mbytes,发送包数:%s,接收包数%s"%(count.bytes_sent,count.bytes_recv,count.packets_sent,count.packets_recv)
users = psutil.users()
print "当前登录用户:",users[0].name
#时间
curent_time = psutil.boot_time()
curent_time_1 = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(curent_time))
print curent_time_1
4. 进程监控
脚本如下:
#读取进程pid,名称,可执行路径 pid = psutil.pids() for k,i in enumerate(pid): try: proc = psutil.Process(i) print k,i,"%.2f%%"%(proc.memory_percent()),"%",proc.name(),proc.exe() except psutil.AccessDenied : print "psutil.AccessDenied"