您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在Python中使用psutil監(jiān)控服務(wù)器,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
服務(wù)器的監(jiān)控通過(guò)安裝一些常用的監(jiān)控軟件之外,有時(shí)也需要運(yùn)行一些shell或Python腳本;shell下可以使用系統(tǒng)自帶的ps/free/top/df等shell命令,Python可以調(diào)用subprocess等模塊來(lái)運(yùn)行shell命令,不過(guò)這么做就比較麻煩。這里有一個(gè)比較好用的第三方模塊:psutil。
psutil是一個(gè)跨平臺(tái)的庫(kù),用于在Python中檢索有關(guān)運(yùn)行進(jìn)程和系統(tǒng)利用率(CPU,內(nèi)存,磁盤,網(wǎng)絡(luò),傳感器)的信息。它主要用于系統(tǒng)監(jiān)視,分析,限制進(jìn)程資源和運(yùn)行進(jìn)程的管理。它實(shí)現(xiàn)了UNIX命令行工具提供的許多功能,例如:ps,top,lsof,netstat,ifconfig,who,df,kill,free,nice,ionice,iostat,iotop,uptime,pidof,tty,taskset,pmap。 psutil目前支持以下平臺(tái):
Linux
Windows
OSX,
FreeBSD, OpenBSD, NetBSD
Sun Solaris
AIX
...
等裝有Python2.6至3.6的32-bit和64-bit架構(gòu). 也可以在PyPy上運(yùn)行。
安裝
文中示例均在Python版本3.6環(huán)境下運(yùn)行;
# pip3 install psutil
常用模塊
獲取psutil版本信息
In [1]: import psutil In [2]: psutil.version_info Out[2]: (5, 4, 3
獲取CPU信息
In [3]: psutil.cpu_count() # 邏輯CPU核數(shù) Out[3]: 4 In [4]: psutil.cpu_count(logical=False) # 物理CPU核數(shù) Out[4]: 2 In [5]: psutil.cpu_times() # CPU的用戶、系統(tǒng)、空閑時(shí)間 Out[5]: scputimes(user=240773.0, nice=0.0, system=96416.32, idle=1161930.41) In [9]: psutil.cpu_percent(percpu=True) # 獲取每個(gè)CPU的使用率,類似TOP命令 Out[9]: [43.3, 22.0, 42.0, 23.0] In [10]: top = [psutil.cpu_percent(interval=i, percpu=True) for i in range(10)] #設(shè)置每秒刷新時(shí)間間隔,統(tǒng)計(jì)十次的結(jié)果 In [11]: top Out[11]: [[40.8, 19.7, 38.5, 20.7], [25.7, 5.9, 13.0, 5.0], [35.0, 15.6, 30.0, 14.4], [23.7, 7.0, 18.3, 7.4], [38.5, 17.0, 34.2, 17.5], [37.2, 19.6, 36.3, 20.0], [29.6, 16.6, 28.8, 16.8], [37.7, 19.0, 35.4, 18.7], [30.8, 16.3, 26.9, 16.5], [44.2, 27.9, 41.5, 28.6]]
獲取內(nèi)存信息
In [13]: psutil.virtual_memory() #獲取內(nèi)存統(tǒng)計(jì)數(shù)據(jù),單位bytes,我這里8G內(nèi)存 Out[13]: svmem(total=8589934592, available=1891045376, percent=78.0, used=6053986304, free=15130624, active=1878392832, inactive=1875914752, wired=2299678720) In [14]: psutil.swap_memory() # 獲取swap的統(tǒng)計(jì)數(shù)據(jù) Out[14]: sswap(total=2147483648, used=1340866560, free=806617088, percent=62.4, sin=126090076160, sout=3524710400)
獲取磁盤信息
In [17]: psutil.disk_partitions() #獲取磁盤分區(qū)信息 Out[17]: [sdiskpart(device='/dev/disk1', mountpoint='/', fstype='hfs', opts='rw,local,rootfs,dovolfs,journaled,multilabel')] In [20]: psutil.disk_usage('/') # 獲取分區(qū)使用情況,這里使用了25.4% Out[20]: sdiskusage(total=499055067136, used=126482944000, free=372309979136, percent=25.4) In [22]: psutil.disk_io_counters() #磁盤IO情況 Out[22]: sdiskio(read_count=7364142, write_count=6510641, read_bytes=282106464256, write_bytes=261763244544, read_time=2608778, write_time=1095259)
獲取網(wǎng)絡(luò)信息
In [23]: psutil.net_if_stats() # 獲取網(wǎng)卡接口狀態(tài) Out[23]: {'awdl0': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1484), 'bridge0': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1500), 'en0': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1500), 'en1': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=0, mtu=1500), 'en2': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=0, mtu=1500), 'gif0': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1280), 'lo0': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=16384), 'p2p0': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=2304), 'stf0': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1280), 'utun0': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=2000), 'utun1': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1352)} In [25]: psutil.net_if_stats().get("en0") #獲取單個(gè)網(wǎng)卡en0的狀態(tài) Out[25]: snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1500) In [26]: psutil.net_if_addrs() # 獲取所有網(wǎng)卡的地址信息 Out[26]: {'awdl0': [snic(family=<AddressFamily.AF_LINK: 18>, address='36:7d:f3:80:6e:4e', netmask=None, broadcast=None, ptp=None), snic(family=<AddressFamily.AF_INET6: 30>, address='fe80::347d:f3ff:fe80:6e4e%awdl0', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None)], 'bridge0': [snic(family=<AddressFamily.AF_LINK: 18>, address='4a:00:02:c0:33:70', netmask=None, broadcast=None, ptp=None)], 'en0': [snic(family=<AddressFamily.AF_INET: 2>, address='192.168.0.101', netmask='255.255.255.0', broadcast='192.168.0.255', ptp=None), snic(family=<AddressFamily.AF_LINK: 18>, address='ac:bc:32:91:32:8b', netmask=None, broadcast=None, ptp=None), snic(family=<AddressFamily.AF_INET6: 30>, address='fe80::1476:ce7e:210a:2e32%en0', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None)], 'en1': [snic(family=<AddressFamily.AF_LINK: 18>, address='4a:00:02:c0:33:70', netmask=None, broadcast=None, ptp=None)], 'en2': [snic(family=<AddressFamily.AF_LINK: 18>, address='4a:00:02:c0:33:71', netmask=None, broadcast=None, ptp=None)], 'lo0': [snic(family=<AddressFamily.AF_INET: 2>, address='127.0.0.1', netmask='255.0.0.0', broadcast=None, ptp=None), snic(family=<AddressFamily.AF_INET6: 30>, address='::1', netmask='ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', broadcast=None, ptp=None), snic(family=<AddressFamily.AF_INET6: 30>, address='fe80::1%lo0', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None)], 'p2p0': [snic(family=<AddressFamily.AF_LINK: 18>, address='0e:bc:32:91:32:8b', netmask=None, broadcast=None, ptp=None)], 'utun0': [snic(family=<AddressFamily.AF_INET6: 30>, address='fe80::583c:77a0:6b93:b045%utun0', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None)], 'utun1': [snic(family=<AddressFamily.AF_INET: 2>, address='10.5.200.244', netmask=None, broadcast=None, ptp='10.5.200.244')]} # 獲取en0網(wǎng)卡的地址, 這里包括mac和ipv6地址 In [40]: for addr in psutil.net_if_addrs().get("en0"): ...: print(addr.address) 192.168.0.101 ac:bc:32:91:32:8b fe80::1476:ce7e:210a:2e32%en0 In [43]: psutil.net_io_counters() # 獲取網(wǎng)絡(luò)讀寫字節(jié)/包的個(gè)數(shù) Out[43]: snetio(bytes_sent=174614221, bytes_recv=586279725, packets_sent=863903, packets_recv=873583, errin=0, errout=0, dropin=0, dropout=0) In [45]: psutil.net_connections() # 獲取網(wǎng)絡(luò)連接信息,注意這里需要root權(quán)限。
獲取進(jìn)程信息:
In [46]: psutil.pids() # 獲取所有進(jìn)程ID In [47]: psutil.Process(61) # 獲取指定PID的進(jìn)程信息 Out[47]: psutil.Process(pid=61, name='dsAccessService', started='2018-02-26 09:57:04') In [49]: psutil.Process(45573).exe() # 獲取進(jìn)程的exe路徑 Out[49]: '/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python' In [50]: psutil.Process(45573).name() # 獲取進(jìn)程名稱 Out[50]: 'Python' In [52]: psutil.Process(45573).cmdline() # 獲取進(jìn)程啟動(dòng)的命令 Out[52]: ['/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python', '/usr/local/bin/ptipython'] In [56]: psutil.Process(45573).num_threads() # 獲取進(jìn)程的線程數(shù)量 Out[56]: 3 In [57]: psutil.Process(45573).environ() # 獲取進(jìn)程的環(huán)境變量信息
上述內(nèi)容就是怎么在Python中使用psutil監(jiān)控服務(wù)器,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。