python 安裝psutil 來實現獲取系統信息
# yum -y install python*pip # yum -y groupinstall "Development Tools # yum -y install python34-devel.x86_64 # pip3 install --upgrade pip # pip3 install ipython # pip3 install psutil
1、ipython 中終端界面測試
[root@localhost ~]# ipython Python 3.4.5 (default, May 29 2017, 15:17:55) Type 'copyright', 'credits' or 'license' for more information IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: import psutil In [2]: mem = psutil.virtual_memory() In [3]: mem.total,mem.used Out[3]: (1033498624, 332750848)
(1)獲取系統性能信息
采集系統的基本性能包括CPU、內存、磁盤、網絡等等,可以完整描述當前系統的運行狀態及質量。而psutil已經封閉了這些方法
運維工程師可以根據自己的需求來調用
Linux系統的CPU利用率有以下幾個部分
1、User Time ,執行用戶進程的時間百分比;
2、System Time, 執行內核進程和中斷的時間百分比
3、Wait IO,由于IO等待而使CPU處于idle(空閑)狀態的時間百分比
4、Idle,CPU處于idle狀態的時間百分比
使用python中的psutil.cpu_times()方可以簡單地得到這些信息,同時也可以獲取 CPU的硬件相關信息,比如CPU的物理個數與邏輯個數,
eg:
In [5]: psutil.cpu_times() Out[5]: scputimes(user=137.75, nice=0.52, system=68.2, idle=5436.8, iowait=60.51, irq=0.0, softirq=2.1, steal=0.0, guest=0.0, guest_nice=0.0) In [6]: psutil.cpu_times().user Out[6]: 137.96 In [7]: psutil.cpu_count() ##獲取CPU的邏輯個數 Out[7]: 1 In [8]: psutil.cpu_count(logical=False) ##獲取CPU的物理個數 Out[8]: 1
(2)內存信息
Linux系統的內存信息利用率信息涉及total(內存總數)、used(已使用的內存數)、free(空閑內存數)、buffers(緩沖使用數)
cache(緩存使用數)、swap(交換分區使用數)等,分別使用psutil.virtual_memory()與psutil.swap_memory()方法獲取這些信息
操作如下
In [1]: import psutil In [2]: mem = psutil.virtual_memory() ##獲取內存完整信息 In [3]: mem Out[3]: svmem(total=1033498624, available=573415424, percent=44.5, used=288641024, free=77266944, active=339841024, inactive=378482688, buffers=0, cached=667590656, shared=7475200) In [4]: mem.total ##獲取內存總數 Out[4]: 1033498624 In [5]: mem.free ##獲取空閑內存數 Out[5]: 77266944 In [6]: psutil.swap_memory() ##獲取SWAP分區信息 Out[6]: sswap(total=2147479552, used=12288, free=2147467264, percent=0.0, sin=0, sout=12288)
(3)磁盤信息
在系統的所有磁盤信息中,我們更加關注硬盤的I/O信息,其中磁盤利用率使用psutil.disk_usage方法獲取。磁盤IO信息包括read_count(讀IO數)
write_count(寫IO數)、read_bytes(IO讀字節數)、write_bytes(IO寫字節數)、read_time(磁盤讀時間)、write_time(磁盤寫時間)等。這些IO信息可以
使用psutil.disk_io_counter()獲取。
操作如下
In [7]: psutil.disk_partitions() ##使用psutil.disk_partitions方法獲取磁盤完整信息 Out[7]: [sdiskpart(device='/dev/mapper/centos-root', mountpoint='/', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota'), sdiskpart(device='/dev/mapper/centos-home', mountpoint='/home', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota'), sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota')] In [8]: psutil.disk_usage('/') ##使用psutil.disk_usage方法獲取分區參數的使用情況 Out[8]: sdiskusage(total=53660876800, used=4668710912, free=48992165888, percent=8.7) In [9]: psutil.disk_io_counters()##使用psutil.disk_io_counters獲取硬盤總數的IO個數 Out[9]: sdiskio(read_count=35014, write_count=73927, read_bytes=923575296, write_bytes=2137012224, read_time=411040, write_time=9266640, read_merged_count=12, write_merged_count=17756, busy_time=271857) In [10]: psutil.disk_io_counters(perdisk=True) ##"perdisk=True"參數獲取單個分區IO個數 Out[10]: {'dm-0': sdiskio(read_count=15871, write_count=44823, read_bytes=456472576, write_bytes=1065395200, read_time=203757, write_time=7633124, read_merged_count=0, write_merged_count=0, busy_time=132981), 'dm-1': sdiskio(read_count=141, write_count=3, read_bytes=1298432, write_bytes=12288, read_time=213, write_time=4195, read_merged_count=0, write_merged_count=0, busy_time=2406), 'dm-2': sdiskio(read_count=464, write_count=6, read_bytes=1353728, write_bytes=2098176, read_time=566, write_time=22, read_merged_count=0, write_merged_count=0, busy_time=568), 'fd0': sdiskio(read_count=0, write_count=0, read_bytes=0, write_bytes=0, read_time=0, write_time=0, read_merged_count=0, write_merged_count=0, busy_time=0), 'sda1': sdiskio(read_count=1953, write_count=2057, read_bytes=4658176, write_bytes=2110464, read_time=1880, write_time=454, read_merged_count=0, write_merged_count=0, busy_time=2248), 'sda2': sdiskio(read_count=16574, write_count=27068, read_bytes=459612160, write_bytes=1067505664, read_time=204457, write_time=1628869, read_merged_count=12, write_merged_count=17758, busy_time=133511), 'sr0': sdiskio(read_count=11, write_count=0, read_bytes=180224, write_bytes=0, read_time=167, write_time=0, read_merged_count=0, write_merged_count=0, busy_time=167)}
(4)網絡信息
系統的網絡信息的與磁盤的IO類似,涉及幾個關鍵點,包括bytes_sent(發送字節數)、bytes_recv=28220119(接收字節數)、packets_sent=200978(發送數據包數)、
packets_recv=212672(接收數據包數)等。這些網終信息使用psutil.net_io_counters()方法獲取。
操作如下
In [11]: psutil.net_io_counters() ##使用psutil.net_io_counters獲取網絡總的IO信息,默認pernic=False
Out[11]: snetio(bytes_sent=3123740, bytes_recv=157671880, packets_sent=33813, packets_recv=220535, errin=0, errout=0, dropin=0, dropout=0)
(5)其他系統信息
psutil模塊還支持獲取用戶登錄,開機時間等信息
操作如下
In [12]: psutil.users() ##使用psutil.users 方法當當登錄系統的用戶信息。 Out[12]: [suser(name='root', terminal='tty1', host='', started=1498524288.0), suser(name='root', terminal='pts/2', host='10.2.84.143', started=1498545280.0)] In [13]: psutil.boot_time() Out[13]: 1498524250.0
2、系統進程管理方法
獲得當前系統的進程信息,可以讓運維人員得知應用程序的狀態,包括進程的啟動時間、查看或設置CPU親和度、內存使用率、IO信息
socket連接、線程數等,這些信息可以呈現出指定進程是否存活、資源利用情況,為開發人員的代碼優化、問題定位提供良好的數據參考
(1)進程信息
psutil模塊在獲取進程信息方面也提供了很好的支持,包括使用psutil.pids()方法獲取所有進程的PID,使用psutil.Process()方法獲取單
個進程的名稱、路徑、狀態、系統資源利用率等信息。
操作如下
In [19]: import psutil In [20]: psutil.pids() ##列出所有進程的PID Out[20]: [1, 2, 3, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...... In [21]: p = psutil.Process(7509) ##實例化一個Process對象,參數為一個進程PID In [22]: p.name ##進程名 Out[22]: <bound method Process.name of <psutil.Process(pid=7509, name='crond') at 140135402646272>> In [23]: p.exe() ##進程bin路徑 Out[23]: '/usr/sbin/crond' In [24]: p.cwd() ##進程工作目錄絕對路徑 Out[24]: '/' In [25]: p.status() ##進程狀態 Out[25]: 'sleeping' In [26]: p.create_time() ##進程創建時間,時間戳格式 Out[26]: 1498540040.44 In [27]: p.uids() ##進程uid信息 Out[27]: puids(real=0, effective=0, saved=0) In [28]: p.gids() ##進程gid信息 Out[28]: pgids(real=0, effective=0, saved=0) In [29]: p.cpu_times() ##進程CPU時間信息,包括user、system兩個CPU時間 Out[29]: pcputimes(user=0.0, system=0.49, children_user=0.21, children_system=0.68) In [30]: p.cpu_affinity() ##get進程CPU親和度,如要設置進程CPU親和度,將CPU號作為參數即可 Out[30]: [0] In [31]: p.memory_percent() ##進程內存利用率 Out[31]: 0.16843757307218243 In [32]: p.memory_info() ##進程在內 rss\vms信息 Out[32]: pmem(rss=1740800, vms=129363968, shared=1093632, text=61440, lib=0, data=1347584, dirty=0) In [33]: p.io_counters() ##進程IO信息,包括讀寫IO數及字節數 Out[33]: pio(read_count=9719, write_count=934, read_bytes=937984, write_bytes=258048, read_chars=5607895, write_chars=111727) In [34]: p.connections() ##返回打開進程socket的namedutples列表,包括fs\family\laddr等信息 Out[34]: [] In [35]: p.num_threads() ##進程開啟的線程數 Out[35]: 1
(2)popen類的使用
psutil提供的popen類的作用是獲取用戶啟動的應用程序進程信息,以便跟蹤程序進程的運行狀態。
操作如下
In [49]: p = psutil.Popen(["/usr/bin/python3", "-c" ,"print('hello')"],stdout=PIPE) In [45]: p.name() Out[45]: 'python' In [46]: p.username() Out[46]: 'root' In [47]: p.communicate() Out[47]: (b'hello\n', None) In [50]: p.cpu_times() Out[50]: pcputimes(user=0.01, system=0.01, children_user=0.0, children_system=0.0)
原創文章,作者:renjin,如若轉載,請注明出處:http://www.www58058.com/78714