Python psutil模塊

psutil是一個跨平臺庫(http://code.google.com/p/psutil/),能夠輕松實現(xiàn)獲取系統(tǒng)運行的進程和系統(tǒng)利用率(包括CPU、內(nèi)存、磁盤、網(wǎng)絡(luò)等)信息。它主要應(yīng)用于系統(tǒng)監(jiān)控,分析和限制系統(tǒng)資源及進程的管理。它實現(xiàn)了同等命令行工具提供的功能,如ps、top、lsof、netstat、ifconfig、who、df、kill、free、nice、ionice、iostat、iotop、uptime、pidof、tty、taskset、pmap等。目前支持32位和64位的Linux、Windows、OS X、FreeBSD和Sun Solaris等操作系統(tǒng)

一、下載安裝

下載地址:
https://pypi.python.org/pypi/psutil#downloads
下載最新的psutil-5.4.0.tar.gz到本地目錄,然后解壓安裝

tar -xzvf psutil-5.4.0.tar.gz
cd psutil-5.4.0
sudo python setup.py install

二、使用

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import psutil
print "CPU篇"
print "查看cpu所有信息",psutil.cpu_times()
print "查看cpu邏輯個數(shù):",psutil.cpu_count()
print "-"*50

print "內(nèi)存篇"
mem = psutil.virtual_memory()
print "系統(tǒng)內(nèi)存所有信息:",mem
print "物理內(nèi)存total值:",mem.total
print "物理內(nèi)存used值:",mem.used
print "物理內(nèi)存free值:",mem.free
print "-"*50

print "IO篇"
print psutil.disk_io_counters()
print "-"*50

print "網(wǎng)絡(luò)信息篇"
print "獲取網(wǎng)絡(luò)總IO信息:",psutil.net_io_counters()
print "輸出網(wǎng)絡(luò)每個接口信息:",psutil.net_io_counters(pernic=True)
print "-"*50

print "系統(tǒng)篇"
print "獲取當前系統(tǒng)用戶登錄信息:",psutil.users()
print "獲取開機時間:",psutil.boot_time()
print "-"*50

print "進程篇"
print "全部進程:",psutil.pids()
print "查看單個進程:"
p = psutil.Process(psutil.pids()[0])
print p.name()   #進程名
print p.exe()    #進程的bin路徑
print p.cwd()    #進程的工作目錄絕對路徑
print p.status()   #進程狀態(tài)
print p.create_time()  #進程創(chuàng)建時間
print p.uids()    #進程uid信息
print p.gids()    #進程的gid信息
print p.cpu_times()   #進程的cpu時間信息,包括user,system兩個cpu信息
print p.memory_percent()  #進程內(nèi)存利用率
print p.memory_info()    #進程內(nèi)存rss,vms信息
print p.num_threads()  #進程開啟的線程數(shù)

輸入:

CPU篇
查看cpu所有信息 scputimes(user=199778.34, nice=0.0, system=129090.97, idle=2147992.8)
查看cpu邏輯個數(shù): 4
--------------------------------------------------
內(nèi)存篇
系統(tǒng)內(nèi)存所有信息: svmem(total=8589934592L, available=2206568448L, percent=74.3, used=6914883584L, free=69648384L, active=2192187392L, inactive=2136920064L, wired=2585776128L)
物理內(nèi)存total值: 8589934592
物理內(nèi)存used值: 6914883584
物理內(nèi)存free值: 69648384
--------------------------------------------------
IO篇
sdiskio(read_count=16768800L, write_count=6494421L, read_bytes=490265200128L, write_bytes=427495276032L, read_time=8553809L, write_time=4596681L)
--------------------------------------------------
網(wǎng)絡(luò)信息篇
獲取網(wǎng)絡(luò)總IO信息: snetio(bytes_sent=2002375048L, bytes_recv=11383527651L, packets_sent=15315862L, packets_recv=17018710L, errin=0L, errout=0L, dropin=0L, dropout=0)
輸出網(wǎng)絡(luò)每個接口信息: {'gif0': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0), 'utun0': snetio(bytes_sent=268L, bytes_recv=0L, packets_sent=3L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0), 'en2': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0), 'en0': snetio(bytes_sent=1687986426L, bytes_recv=10973766141L, packets_sent=11555082L, packets_recv=12806110L, errin=0L, errout=0L, dropin=0L, dropout=0), 'en1': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0), 'lo0': snetio(bytes_sent=310665891L, bytes_recv=310665891L, packets_sent=3746872L, packets_recv=3746872L, errin=0L, errout=0L, dropin=0L, dropout=0), 'bridge0': snetio(bytes_sent=342L, bytes_recv=0L, packets_sent=1L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0), 'p2p0': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0), 'stf0': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0), 'awdl0': snetio(bytes_sent=3722121L, bytes_recv=99095619L, packets_sent=13904L, packets_recv=465728L, errin=0L, errout=0L, dropin=0L, dropout=0)}
--------------------------------------------------
系統(tǒng)篇
獲取當前系統(tǒng)用戶登錄信息: [suser(name='bestfei', terminal='console', host=None, started=1507604480.0, pid=94), suser(name='bestfei', terminal='ttys000', host=None, started=1507604864.0, pid=609), suser(name='bestfei', terminal='ttys001', host=None, started=1510044800.0, pid=33488), suser(name='bestfei', terminal='ttys002', host=None, started=1509589632.0, pid=24702), suser(name='bestfei', terminal='ttys004', host=None, started=1509535872.0, pid=24351)]
獲取開機時間: 1507604480.0
--------------------------------------------------
進程篇
全部進程: [33652, 33649, 33643, 33642, 33637, 33636, 33634, 33633, 33629, 33628, 33623, 33574, 33498, 33497, 33496, 33495, 33489, 33488, 33482, 33425, 33401, 33400, 33365, 33364, 33348, 33194, 33069, 33035, 33023, 33021, 33017, 33007, 33006, 33005, 33001, 32987, 32963, 32952, 32900, 32899, 32897, 32895, 32894, 32889, 32888, 32887, 32854, 32853, 32848, 32847, 32846, 32845, 32844, 32843, 32839, 32837, 32836, 32831, 32824, 32823, 31435, 31434, 31134, 31133, 31128, 31127, 31126, 31125, 31124, 31123, 31121, 31118, 31094, 31090, 31062, 31061, 31056, 31052, 31046, 31045, 31031, 31030, 31027, 31025, 31024, 31023, 31021, 31015, 31014, 31013, 31006, 31005, 31004, 31003, 31002, 31000, 30999, 30998, 30997, 30995, 30993, 30992, 30991, 30988, 30972, 30833, 30831, 30588, 30460, 29649, 29401, 29306, 29305, 29298, 29296, 29295, 28701, 28392, 28390, 28388, 25459, 25454, 24711, 24706, 24703, 24702, 24701, 24352, 24351, 23717, 23283, 23281, 23280, 23211, 23131, 22750, 22713, 22694, 21476, 21234, 21219, 21198, 21197, 21195, 21194, 19373, 19318, 16101, 13469, 11762, 11759, 10128, 9320, 5215, 4644, 4112, 3380, 2682, 2238, 2082, 1942, 1665, 1663, 808, 677, 620, 610, 609, 538, 537, 520, 440, 436, 414, 413, 411, 410, 408, 407, 405, 393, 389, 385, 377, 365, 364, 363, 361, 360, 359, 358, 357, 354, 352, 348, 337, 333, 332, 331, 330, 326, 322, 316, 309, 307, 305, 303, 298, 289, 286, 285, 284, 275, 272, 268, 264, 255, 254, 250, 249, 248, 239, 236, 234, 233, 232, 221, 210, 181, 180, 178, 176, 175, 174, 172, 168, 161, 159, 158, 157, 155, 135, 132, 108, 106, 104, 102, 101, 100, 99, 98, 96, 95, 94, 93, 90, 89, 88, 87, 85, 83, 81, 80, 79, 77, 76, 68, 60, 59, 57, 53, 47, 46, 45, 40, 39, 38, 36, 35, 1]
查看單個進程:
Python
/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
/Users/bestfei/Documents/fei/python
running
1510048067.4
puids(real=501, effective=501, saved=501)
puids(real=20, effective=20, saved=20)
pcputimes(user=0.036896284, system=0.030256296, children_user=0.0, children_system=0.0)
0.150680541992
pmem(rss=12951552L, vms=2524672000L, pfaults=5189, pageins=81)
1
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 安裝 下載 安裝 功能 psutil 是一個跨平臺庫,能夠輕松的實現(xiàn)獲取系統(tǒng)運行的進程和系統(tǒng)利用率(包括cpu、內(nèi)...
    SateZheng閱讀 4,268評論 1 5
  • Python處理Windows進程 psutil(Python system and process utilit...
    Zhaifg閱讀 22,904評論 0 13
  • linux資料總章2.1 1.0寫的不好抱歉 但是2.0已經(jīng)改了很多 但是錯誤還是無法避免 以后資料會慢慢更新 大...
    數(shù)據(jù)革命閱讀 13,367評論 2 33
  • 如果你想知道你的服務(wù)器正在做干什么,你就需要了解一些基本的命令,一旦你精通了這些命令,那你就是一個專業(yè)的 Linu...
    七寸知架構(gòu)閱讀 11,400評論 1 71
  • 大學(xué)的時候,幫朋友寫的操作系統(tǒng)調(diào)研的作業(yè),最近整理過去的文檔時候偶然發(fā)現(xiàn),遂作為博客發(fā)出來。 從串口驅(qū)動到Linu...
    free_will閱讀 7,708評論 7 59

友情鏈接更多精彩內(nèi)容