1:安装psutil
#wgethttps://pypi.python.org/packages/source/p/psutil/psutil-2.0.0.tar.gz--no-check-certificate
#tar zxvf psutil-2.0.0.tar.gz
#cd psutil-2.0.0
#python setup.py install
2.获取系统性能信息
(1)CPU信息
>>>import psutil
>>>psutil.cpu_times()
scputimes(user=135.42,nice=0.48, system=80.67, idle=693.5, iowait=124.58, irq=0.01, softirq=3.54,steal=0.0, guest=0.0, guest_nice=0.0)
>>>psutil.cpu_times().user #获取单项数据信息,如用户user的CPU时间比
147.07
>>>psutil.cpu_count() #获取CPU的逻辑个数,默认Llogical=True4
2
>>>psutil.cpu_count(logical=False) #获取CPU的物理个数
2
(2)内存信息
>>>import psutil
>>>mem = psutil.virtual_memory() #使用psutil.virtual_memory获取内存完整信息
>>>mem
svmem(total=1029111808L,available=513196032L, percent=50.1, used=962969600L, free=66142208L,active=579526656, inactive=239161344, buffers=78082048L, cached=368971776)
>>>mem.total #获取内存总数
1029111808L
>>>mem.free #获取空闲内存数
66142208L
>>>psutil.swap_memory() #获取swap分区信息
sswap(total=922742784L,used=5111808L, free=917630976L, percent=0.6, sin=45056, sout=5132288)
(3)磁盘信息
>>>psutil.disk_partitions() #使用psutil.disk_partitions方法获取磁盘完整信息
[sdiskpart(device='/dev/sda1',mountpoint='/', fstype='ext4',opts='rw,relatime,errors=remount-ro,data=ordered'),sdiskpart(device='/dev/sr0', mountpoint='/media/cdrom0', fstype='iso9660',opts='ro,nosuid,nodev,noexec,relatime')]
>>>psutil.disk_usage('/') #使用psutil.disk_usage方法获取分区的使用情况
sdiskusage(total=20091629568,used=10586230784, free=8461176832, percent=52.7)
>>>psutil.disk_io_counters() #获取硬盘的IO总数,读写信息
sdiskio(read_count=28282,write_count=2216, read_bytes=580173824, write_bytes=54259712, read_time=447236,write_time=777976)
>>>psutil.disk_io_counters(perdisk=True) #参数perdisk=True获取单个分区IO个数,读写信息
{'fd0':sdiskio(read_count=0, write_count=0, read_bytes=0, write_bytes=0, read_time=0,write_time=0), 'sr0': sdiskio(read_count=33, write_count=0, read_bytes=92160,write_bytes=0, read_time=160, write_time=0), 'sda5': sdiskio(read_count=118,write_count=20, read_bytes=1097728, write_bytes=5132288, read_time=248,write_time=880), 'sda2': sdiskio(read_count=2, write_count=0, read_bytes=2048,write_bytes=0, read_time=16, write_time=0), 'sda1': sdiskio(read_count=28129,write_count=2196, read_bytes=578981888, write_bytes=49127424, read_time=446812,write_time=777096)}
(4)网络信息
>>>psutil.net_io_counters() #获取网络的IO信息,默认pernic=False
snetio(bytes_sent=14974,bytes_recv=226027, packets_sent=132, packets_recv=202, errin=0, errout=0,dropin=0, dropout=0)
>>>psutil.net_io_counters(pernic=True) # pernic=True输出每个网络接口的IO信息
{'lo':snetio(bytes_sent=1200, bytes_recv=1200, packets_sent=20, packets_recv=20,errin=0, errout=0, dropin=0, dropout=0), 'eth0': snetio(bytes_sent=13774,bytes_recv=224827, packets_sent=112, packets_recv=182, errin=0, errout=0,dropin=0, dropout=0)}
(5)其他系统信息
>>>psutil.users() #返回当前登录系统的用户信息
[suser(name='root',terminal=':0', host=':0', started=1482396928.0)]
>>>import psutil,datetime
>>>psutil.boot_time() #获取开机时间,以Linux时间格式返回
1482396866.0
>>>datetime.datetime.fromtimestamp(psutil.boot_time()) #系统开机时间
datetime.datetime(2016,12, 22, 16, 54, 26)