系统性能信息模块篇psutil之获取系统性能信息
聲明:本人對自動化運維技術比較感興趣,于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'
轉載于:https://blog.51cto.com/718693/1691088
總結
以上是生活随笔為你收集整理的系统性能信息模块篇psutil之获取系统性能信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中使用tab键进行提示(py
- 下一篇: http的学习