原本這周打算基於glaphite來對系統服務進行監控,可是中間一直有個地方沒有搞明白,因此至於glaphite的web頁面監控可能要推遲到下週與朋友相見!python
今天就和你們聊聊一個很是不錯的Python下系統信息模塊pustilweb
簡介緩存
psutil是一個跨平臺庫,主要應用於系統監控,分析和限制系統資源及進程的管理;網絡
能夠輕鬆的獲取以下系統信息:https://pypi.python.org/pypi/psutilapp
CPUsocket
內存ide
磁盤性能
網絡ui
進程spa
安裝
# wget https://pypi.python.org/packages/source/p/psutil/psutil-2.2.0.tar.gz --no-check-certificate # tar xf psutil-2.2.0.tar.gz # cd psutil-2.2.0 # python setup.py install
獲取系統性能信息
(1)CPU信息
User Time:執行用戶進程的時間百分比
System Time:執行內核進程和中斷的時間百分比
Wait IO:因爲IO等待而使CPU處於idle(空閒)狀態的時間百分比
idle:CPU處於idle狀態的時間百分比
先來看看psutil模塊中對cpu有哪些方法可使用
>>> import psutil >>> psutil.cpu_ (鍵盤tab鍵 命令補全) psutil.cpu_count( psutil.cpu_percent( psutil.cpu_times( psutil.cpu_times_percent(
分述psutil模塊中cpu方法的使用
#獲取CPU的物理個數 >>> psutil.cpu_count(logical=False) #獲取CPU的邏輯個數,默認logical=True >>> psutil.cpu_count() #獲取cpu負載的百分比 >>> psutil.cpu_percent() 0.10000000000000001 #獲取cpu的完整信息,須要顯示全部邏輯cpu信息 >>> psutil.cpu_times() scputimes(user=3.2999999999999998, nice=0.0, system=16.329999999999998, idle=8322.0400000000009, iowait=21.93, irq=0.46999999999999997, softirq=0.54000000000000004, steal=0.0, guest=0.0) #獲取單項數據信息,如用戶user的CPU時間比 >>> psutil.cpu_times().user 3.2999999999999998 #時間間隔,是否以列表形式輸出 >>> psutil.cpu_times_percent(interval=2,percpu=True) [scputimes(user=0.0, nice=0.0, system=0.0, idle=100.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0)]
(2)內存信息
內存總數total
已使用的內存數used
空閒內存數free
緩衝區使用數buffers
緩存使用數cache
交換分區使用數swap
先來看看psutil模塊中對memory有哪些方法可使用
>>> mem = psutil.virtual_memory() >>> mem.(鍵盤tab鍵 命令補全) mem.active mem.buffers mem.count mem.inactive mem.percent mem.used mem.available mem.cached mem.free mem.index mem.total
分述psutil模塊中memory方法的使用
#獲取獲取內存完整信息 >>> mem svmem(total=1960640512L, available=1861410816L, percent=5.0999999999999996, used=158261248L, free=1802379264L, active=55451648, inactive=35868672, buffers=13492224L, cached=45539328) #獲取內存總數 >>> mem.total 1960640512L #獲取使用內存 >>> mem.used 158261248L #獲取空閒內存 >>> mem.free 1802379264L #獲取swap分區信息 >>> psutil.swap_memory() sswap(total=4160741376L, used=0L, free=4160741376L, percent=0.0, sin=0, sout=0)
(3)硬盤信息
利用率
IO
先來看看psutil模塊中對disk有哪些方法可使用
>>> psutil.dis psutil.disk_io_counters psutil.disk_partitions psutil.disk_usage
分述psutil模塊中disk方法的使用
#獲取磁盤的完整信息 >>> psutil.disk_partitions() [sdiskpart(device='/dev/mapper/VolGroup-lv_root', mountpoint='/', fstype='ext4', opts='rw'), sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='ext4', opts='rw')] #獲取分區的使用狀況 >>> psutil.disk_usage('/') sdiskusage(total=16521789440, used=1433333760, free=14249177088, percent=8.6999999999999993) >>> psutil.disk_usage('/boot') sdiskusage(total=507744256, used=33196032, free=448333824, percent=6.5) #獲取硬盤總的IO數 >>> psutil.disk_io_counters() sdiskio(read_count=7295, write_count=7190, read_bytes=116201472, write_bytes=38065152, read_time=123500, write_time=33952) #獲取單個分區IO個數 讀寫信息 >>> psutil.disk_io_counters(perdisk=True) {'dm-1': sdiskio(read_count=297, write_count=0, read_bytes=1216512, write_bytes=0, read_time=534, write_time=0), 'sda2': sdiskio(read_count=2462, write_count=2543, read_bytes=56902656, write_bytes=19034112, read_time=29130, write_time=11733), 'dm-0': sdiskio(read_count=3859, write_count=4647, read_bytes=55251968, write_bytes=19034112, read_time=93096, write_time=22216), 'sda1': sdiskio(read_count=677, write_count=6, read_bytes=2830336, write_bytes=21504, read_time=740, write_time=5)}
(4)網絡信息
發送字節數
接收字節數
發送數據包數
接收數據包數
先來看看psutil模塊中對network有哪些方法可使用
>>> psutil.net psutil.net_connections psutil.net_io_counters psutil.network_io_counters
分述psutil模塊中nework方法的使用
#獲取網絡總的IO信息,默認pernic=False >>> psutil.net_io_counters() snetio(bytes_sent=910776, bytes_recv=13499265, packets_sent=8757, packets_recv=15556, errin=0, errout=0, dropin=0, dropout=0) #獲取每一個網絡接口的IO信息 >>> psutil.net_io_counters(pernic=True) {'lo': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'eth2': snetio(bytes_sent=911296, bytes_recv=13405208, packets_sent=8761, packets_recv=14795, errin=0, errout=0, dropin=0, dropout=0), 'eth1': snetio(bytes_sent=1788, bytes_recv=98301, packets_sent=18, packets_recv=803, errin=0, errout=0, dropin=0, dropout=0)} #獲取全部接口的網卡信息 >>> psutil.net_connections() [sconn(fd=3, family=2, type=1, laddr=('0.0.0.0', 22), raddr=(), status='LISTEN', pid=1110), sconn(fd=12, family=2, type=1, laddr=('127.0.0.1', 25), raddr=(), status='LISTEN', pid=1186), sconn(fd=3, family=2, type=1, laddr=('192.168.10.26', 22), raddr=('192.168.10.1', 54342), status='ESTABLISHED', pid=1794), sconn(fd=4, family=10, type=1, laddr=('::', 22), raddr=(), status='LISTEN', pid=1110), sconn(fd=13, family=10, type=1, laddr=('::1', 25), raddr=(), status='LISTEN', pid=1186), sconn(fd=6, family=2, type=2, laddr=('0.0.0.0', 68), raddr=(), status='NONE', pid=1745), sconn(fd=6, family=2, type=2, laddr=('0.0.0.0', 68), raddr=(), status='NONE', pid=1643)] #獲取指定接口的網卡信息 >>> psutil.net_connections(kind='inet4') [sconn(fd=3, family=2, type=1, laddr=('0.0.0.0', 22), raddr=(), status='LISTEN', pid=1110), sconn(fd=12, family=2, type=1, laddr=('127.0.0.1', 25), raddr=(), status='LISTEN', pid=1186), sconn(fd=3, family=2, type=1, laddr=('192.168.10.26', 22), raddr=('192.168.10.1', 54342), status='ESTABLISHED', pid=1794), sconn(fd=6, family=2, type=2, laddr=('0.0.0.0', 68), raddr=(), status='NONE', pid=1745), sconn(fd=6, family=2, type=2, laddr=('0.0.0.0', 68), raddr=(), status='NONE', pid=1643)]
(5)其它系統信息
用戶登陸
開機時間
具體操做列子以下:
#獲取當前登陸系統的用戶信息 >>> psutil.users() [suser(name='root', terminal='tty1', host='', started=1427459328.0), suser(name='root', terminal='pts/0', host='192.168.10.1', started=1427459456.0), suser(name='root', terminal='pts/1', host='192.168.10.1', started=1427459456.0), suser(name='root', terminal='pts/2', host='192.168.10.1', started=1427459584.0)] >>> len(psutil.users()) 4 #獲取系統開機時間並格式化輸出 >>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime('%Y-%m-%d %H:%M:%S') '2015-03-27 20:27:36'
系統進程管理方法
(1)進程信心
進程PID
單個進程名稱
路徑
狀態
系統資源利用率
>>> import psutil >>> psutil.pids() 列出全部進程的PID [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 37, 38, 40, 41, 71, 199, 203, 218, 219, 220, 325, 327, 344, 345, 364, 417, 569, 764, 765, 817, 1042, 1058, 1070, 1110, 1186, 1194, 1197, 1209, 1211, 1213, 1215, 1217, 1219, 1228, 1229, 1231, 1298, 1643, 1745, 1794, 1796, 1829, 1846, 2004, 2039, 2154, 2175] >>> p = psutil.Process(2154) 實例化一個Process對象,參數爲一個進程的PID >>> p.name() 進程名 'python' >>> p.exe() 進程bin路徑 '/usr/bin/python' >>> p.cwd() 進程工做目錄絕對路徑 '/root' >>> p.status() 進程狀態 'running' >>> p.create_time() 進程建立時間、時間戳格式 1427469528.49 >>> p.uids() 進程uid信息 puids(real=0, effective=0, saved=0) >>> p.gids() 進程gid信息 pgids(real=0, effective=0, saved=0) >>> p.cpu_times() 進程CPU時間信息 pcputimes(user=0.059999999999999998, system=0.050000000000000003) >>> p.cpu_affinity() get進程CPU親和度 [0] >>> p.memory_percent() 進程內存利用率 0.43265534645853532 >>> p.memory_info() 進程內存rss、vms信息 pmem(rss=8486912, vms=185946112) >>> p.io_counters() 進程IO信息,包括讀寫IO數及字節數 pio(read_count=2206, write_count=1286, read_bytes=40960, write_bytes=0) >>> p.connections() 返回打開進程socket的namedutples列表 [] >>> p.num_threads() 進程開啓的線程數 1
(2)popen類的使用
psutil提供了popen類的做用是獲取用戶啓動的應用程序進程信息,以便跟蹤程序進程的運行狀態。
#定義 >>> import psutil >>> from subprocess import PIPE >>> p = psutil.Popen(['/usr/bin/python','-c','print("hello world.")'],stdout=PIPE) #獲取 >>> p.name() 'python' >>> p.username() 'root' >>> p.communicate() ('hello world.\n', None) >>> p.cpu_times() pcputimes(user=0.01, system=0.0)
補充:2015-03-28
psutil大小單位通常都採用字節,查看起來很不舒服!那麼咱們就用雅觀的方式來顯示內存的信息
首先定義單位換算字典 unit = {'b':1, 'k':2**10, 'm':2**20, 'g':2**30} 統計內存的總數並換算單位 >>> import psutil >>> mem = psutil.virtual_memory() >>> mem.total/unit['m'] #以M的單位顯示,並保留整數 1869L >>> mem.total/unit['g'] #以G的單位顯示,並保留整數 1L >>> print '%.2f' %(float(mem.total)/unit['m']) #保留兩位小數 1869.81 >>> print '%.2f' %(float(mem.total)/unit['g']) #保留兩位小數 1.83
若是朋友看過我前篇博文IT資產管理,可能還會記得那篇蒐集主機硬件信息都是基於讀取文件的方式進行截取或匹配等手段來獲取信息,用起來確實有點麻煩,若是朋友看明白了這篇文章,可能用那種方式10行完成的代碼用這個模塊2行就能夠實現了。這就是psutil的強大之處。
所謂萬世追究其根源,psutil的強大無非就是在底層封裝了一些庫,咱們直接調用就能夠了,實質上也是經過讀取文件的形式來獲取的。
好了,今天的psutil就先到這裏,若是你們對psutil感興趣,更多的功能能夠參考psutil官方文檔