CPU,內存,磁盤,網卡信息獲取

近期在學習python,寫了個小腳本用於監控機器的運行狀態。python

具體代碼見下文,部分代碼參照了網上的資源。編輯器

# -*- coding: UTF8 -*-
# Author: Shane
# CreteTime: 2014-3-13
# Mail: ngy01112003@163.com
# Version: 1.0
"""
This Script provide the statics of system.
Inclue Cpu, Memory, Disk, Nic etc.
Based on the Windows xp sp3, python 2.7.6, psutil 1.2.1.
"""
import sys
import os
import datetime
import psutil
from psutil._compat import print_
#字節轉換
def bytes2human(n):
# http://code.activestate.com/recipes/578019
# >>> bytes2human(10000)
# '9.8K'
# >>> bytes2human(100001221)
# '95.4M'
symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
prefix = {}
for i, s in enumerate(symbols):
prefix[s] = 1 << (i + 1) * 10
for s in reversed(symbols):
if n >= prefix[s]:
value = float(n) / prefix[s]
return '%.1f%s' % (value, s)
return "%sB" % n
###監控CPU使用率###
def getCpuPer():
#獲取CPU使用率
perCPU = psutil.cpu_percent(interval=1)
print "CPU Usage:", int(perCPU), "%"
###監控內存###
def getMemStatus():
#獲取內存狀態
phymem = psutil.phymem_usage()
buffers = getattr(psutil, 'phymem_buffers', lambda: 0)()
cached = getattr(psutil, 'cached_phymem', lambda: 0)()
used = phymem.total - (phymem.free + buffers + cached)
line = "Memory Usage: %5s%%    Used:%s    Total:%s" % (
phymem.percent,
str(int(used / 1024 / 1024)) + "M",
str(int(phymem.total / 1024 / 1024)) + "M"
)
print_(line)
###監控磁盤信息###
def getPartitionStatus():
#獲取磁盤分區信息
pars = psutil.disk_partitions()
#print ""
print "\nDisk Partition Status:"
templ = "%-17s %8s %8s %8s %5s%% %9s  %s"       #定義打印格式
print_(templ % ("Partition", "Total", "Used", "Free", "Use", "Type",
"Mount"))                       #打印表頭
for par in pars:
if os.name == "nt":
if "cdrom" in par.opts or par.fstype == '':
continue
disk = psutil.disk_usage(par.mountpoint)
print_(templ % (                            #打印具體的信息
par.device,
bytes2human(disk.total),
bytes2human(disk.used),
bytes2human(disk.free),
int(disk.percent),
par.fstype,
par.mountpoint))
#獲取磁盤IO信息
diskIo = psutil.disk_io_counters()
print "\nDisk IO Status:"
templ = "%-7s %12s %12s %12s %12s %12s"       #定義打印格式
print_(templ % ("Read_Count", "Wriet_Count", "Read_Bytes",
"Write_Bytes", "Read_Time", "Write_Time"))
print_(templ % (diskIo.read_count,
diskIo.write_count,
bytes2human(diskIo.read_bytes),
bytes2human(diskIo.write_bytes),
int(diskIo.read_time/1000),
int(diskIo.write_time/1000)))
###獲取網卡狀態###
def getNicStatus():
nics = psutil.net_io_counters(pernic=True)
nicKeys = nics.keys()
print "\nNIC IO Status:"
templ = "%-30s %12s %17s %17s %17s %12s %12s  %9s %9s"       #定義打印格式
print_(templ % ("Nic Name","Bytes_Sent", "Bytes_Recv", "Packets_Sent", "Packets_Recv",
"ErrorIn", "ErrorOut","DropIn","DropOut"))
for name in nicKeys:
nicStatus = nics[name]
print_(templ % (name,
bytes2human(nicStatus.bytes_sent),
bytes2human(nicStatus.bytes_recv),
str(nicStatus.packets_sent),
nicStatus.packets_recv,
nicStatus.errin,
nicStatus.errout,
nicStatus.dropin,
nicStatus.dropout))
###主函數入口###
def main():
getLoginUser()
getCpuPer()
getMemStatus()
getPartitionStatus()
getNicStatus()
#入口
if __name__ == "__main__":
sys.exit(main())

 

51的代碼編輯器居然不支持縮進。python對代碼縮進但是有嚴格要求的哎!算了,我直接傳腳本好了。ide

相關文章
相關標籤/搜索