声明:本人对自动化运维技术比较感兴趣,于8月购买了一本为名《Python自动化运维技术与最佳实践》的书籍,准备将自己的学习过程记录如下,仅供学习之用。
环境介绍:
操作系统:redhat6.4 64位
python版本:2.6.6
一、源码方式安装psutil
下载地址:https://pypi.python.org/pypi/psutil
我是下载psutil3.0这个版本(psutil-3.0.0.tar.gz )
[root@localhost opt]# tar -xvf psutil-3.0.0.tar.gz
[root@localhost opt]# cd psutil-3.0.0
[root@localhost psutil-3.0.0]# python setup.py install
二、获取系统性能信息
(1)cpu信息
[root@localhost python]# python
Python 2.6.6 (r266:84292, Oct 12 2012, 14:23:48)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> psutil.cpu_times()
scputimes(user=11.65, nice=0.0, system=38.359999999999999, idle=977.19000000000005, iowait=15.48, irq=0.67000000000000004, softirq=1.97, steal=0.0, guest=0.0)
>>> psutil.cpu_times().user #获取单项数据信息,如用户user的CPU时间比
11.69
>>> psutil.cpu_count() #获取CPU的逻辑个数,默认logical=True
1
>>> psutil.cpu_count(logical=False) #获取CPU的物理个数
(2)内存信息
>>> psutil.virtual_memory() #使用psutil.virtual_memory方法获取内存完整信息
svmem(total=3154972672L, available=2967654400L, percent=5.9000000000000004, used=394760192L, free=2760212480L, active=124850176, inactive=165478400, buffers=29954048L, cached=177487872)
>>> psutil.virtual_memory().total #获取内存总数
3154972672L
>>> psutil.virtual_memory().free #获取空闲内存数
2760085504L
>>> psutil.virtual_memory().buffers #获取缓冲使用数
30068736L
#获取SWAP分区信息
>>> psutil.swap_memory()
sswap(total=3221217280L, used=0L, free=3221217280L, percent=0.0, sin=0, sout=0)
(3)磁盘信息
>>> psutil.disk_partitions() #获取磁盘完整信息
[sdiskpart(device='/dev/sda3', mountpoint='/', fstype='ext4', opts='rw'), sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='ext4', opts='rw'), sdiskpart(device='/dev/sdc1', mountpoint='/data', fstype='ext4', opts='rw')]
>>> psutil.disk_usage('/') #获取分区的使用情况
sdiskusage(total=17437650944, used=12692647936, free=3859218432, percent=72.799999999999997)
>>> psutil.disk_io_counters() #获取硬盘总的IO个数、读写信息
sdiskio(read_count=5616, write_count=2090, read_bytes=210427392, write_bytes=17358848, read_time=294074, write_time=24870)
>>> psutil.disk_io_counters(perdisk=True) #perdisk=True参数获取单个分区IO个数、读写信息
{'sdc1': sdiskio(read_count=560, write_count=1, read_bytes=2271744, write_bytes=4096, read_time=299, write_time=41), 'sdb1': sdiskio(read_count=313, write_count=0, read_bytes=1240064, write_bytes=0, read_time=159, write_time=0), 'sda2': sdiskio(read_count=363, write_count=0, read_bytes=1486848, write_bytes=0, read_time=989, write_time=0), 'sda3': sdiskio(read_count=3944, write_count=2108, read_bytes=203645952, write_bytes=17469440, read_time=289724, write_time=24761), 'sda1': sdiskio(read_count=436, write_count=3, read_bytes=1782784, write_bytes=12288, read_time=2903, write_time=86)}
(4)网络信息
>>> psutil.net_io_counters() #获取网络总的IO信息、默认pernic=False
snetio(bytes_sent=412708, bytes_recv=281914, packets_sent=6402, packets_recv=3333, errin=0, errout=0, dropin=0, dropout=0)
>>> psutil.net_io_counters(pernic=True) #获取每个网络接口的IO信息
{'lo': snetio(bytes_sent=174280, bytes_recv=174280, packets_sent=2151, packets_recv=2151, errin=0, errout=0, dropin=0, dropout=0), 'eth0': snetio(bytes_sent=251908, bytes_recv=118562, packets_sent=4406, packets_recv=1305, errin=0, errout=0, dropin=0, dropout=0)}
>>> psutil.net_if_stats() #获取每个网络接口的状态
{'lo': snicstats(isup=True, duplex=0, speed=0, mtu=16436), 'eth0': snicstats(isup=True, duplex=2, speed=1000, mtu=1500)}
>>> psutil.net_if_addrs() #获取每个网络接口的地址信息
{'lo': [snic(family=2, address='127.0.0.1', netmask='255.0.0.0', broadcast='127.0.0.1'), snic(family=10, address='::1', netmask='ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', broadcast=None), snic(family=17, address='00:00:00:00:00:00', netmask=None, broadcast='00:00:00:00:00:00')], 'lo:0': [snic(family=2, address='192.168.1.200', netmask='255.255.255.255', broadcast='192.168.1.200')], 'eth0': [snic(family=2, address='192.168.1.108', netmask='255.255.255.0', broadcast='192.168.1.255'), snic(family=10, address='fe80::20c:29ff:fe61:89ee%eth0', netmask='ffff:ffff:ffff:ffff::', broadcast=None), snic(family=17, address='00:0c:29:61:89:ee', netmask=None, broadcast='ff:ff:ff:ff:ff:ff')]}
(5)获取其他系统信息
>>> psutil.users() #返回当前登陆系统的用户信息
[suser(name='timesten', terminal='pts/0', host='192.168.1.5', started=1441249920.0), suser(name='root', terminal='pts/1', host='192.168.1.5', started=1441250048.0)]
>>> psutil.boot_time() #获取开机时间,以linux时间戳格式返回
1441249379.0
>>> import datetime
>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
'2015-09-03 11:02:59'