python之psutil模塊詳解(Linux)--小白博客
阿新 • • 發佈:2019-03-23
terminal 程序 date dir task ipython sin 利用 eth
1.簡單介紹
psutil是一個跨平臺的庫(http://code.google.com/p/psutil/),能夠輕松的實現獲取系統運行的進程和系統利用率(CPU、內存、磁盤、網絡等)信息。它主要應用於系統監控,分析和限制系統資源及進程的管理。能實現同等命令行工具提供的功能,如ps、top、lsof、netstat、ifconfig、who、df、kill、free、nice、ionice、iostat、iotop、uptime、pidof、tty、taskset、pmap等。
2.安裝
pip3 install psutil
3.基本使用
3.1 cpu相關
In [1]: import psutil In [2]: psutil.cpu_times()#使用cpu_times獲取cpu的完整信息 Out[2]: scputimes(user=769.84, nice=2.78, system=387.68, idle=83791.98, iowait=479.84, irq=0.0, softirq=0.81, steal=0.0, guest=0.0, guest_nice=0.0) In [3]: psutil.cpu_count()#獲取cpu的邏輯個數 Out[3]: 1 In [4]: psutil.cpu_times_percent()#獲取cpu的所有邏輯信息 Out[4]: scputimes(user=0.7, nice=0.0, system=0.5, idle=98.4, iowait=0.4, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
3.2內存相關
In [5]: psutil.virtual_memory()#獲取內存的所有信息 Out[5]: svmem(total=1041309696, available=697958400, percent=33.0, used=176611328, free=91947008, active=516075520, inactive=323096576, buffers=102719488, cached=670031872, shared=12873728) In [7]: psutil.virtual_memory().total Out[7]: 1041309696 In [8]: psutil.virtual_memory().used Out[8]: 176553984 In [9]: psutil.virtual_memory().free Out[9]: 91901952 In [13]: psutil.swap_memory()#交換分區相關 Out[13]: sswap(total=0, used=0, free=0, percent=0, sin=0, sout=0)
3.3磁盤相關
In [14]: psutil.disk_partitions()#獲取磁盤的詳細信息 Out[14]: [sdiskpart(device=‘/dev/vda1‘, mountpoint=‘/‘, fstype=‘ext3‘, opts=‘rw,noatime,data=ordered‘)] In [17]: psutil.disk_usage(‘/‘)#獲取分區的使用情況 Out[17]: sdiskusage(total=21002579968, used=2223321088, free=17705578496, percent=11.2) In [18]: psutil.disk_io_counters()#獲取磁盤總的io個數,讀寫信息 Out[18]: sdiskio(read_count=25365, write_count=118754, read_bytes=391898112, write_bytes=3048738816, read_time=343585, write_time=10775463, read_merged_count=107, write_merged_count=583537, busy_time=623556) 補充說明下: read_count(讀IO數) write_count(寫IO數) read_bytes(讀IO字節數) write_bytes(寫IO字節數) read_time(磁盤讀時間) write_time(磁盤寫時間)
3.4網絡信息
In [19]: psutil.net_io_counters()#獲取網絡總信息 Out[19]: snetio(bytes_sent=24172706, bytes_recv=168785879, packets_sent=163657, packets_recv=442827, errin=0, errout=0, dropin=0, dropout=0) In [20]: psutil.net_io_counters(pernic=True)#獲取每個網絡接口的信息 Out[20]: {‘eth0‘: snetio(bytes_sent=24177750, bytes_recv=168797166, packets_sent=163685, packets_recv=442948, errin=0, errout=0, dropin=0, dropout=0), ‘lo‘: snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0)}
3.5其它信息
In [21]: psutil.users()#返回當前登錄系統的用戶信息 Out[21]: [suser(name=‘root‘, terminal=‘pts/0‘, host=‘X.X.X.X‘, started=1492844672.0)] In [22]: psutil.boot_time()#獲取開機時間 Out[22]: 1492762895.0 In [23]: import datetime#轉換成你能看懂的時間 In [24]: datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S") Out[24]: ‘2017-04-21 16:21:35‘
4.系統進程的管理方法
[root@VM_46_121_centos ~]# ps -ef | grep ipython#這裏首先我用ps獲取ipython進程號 root 2407 2365 0 16:50 pts/1 00:00:00 grep --color=auto ipython root 29735 26143 0 16:01 pts/0 00:00:07 /usr/local/bin/python3.5 /usr/local/bin/ipython In [27]: psutil.Process(29735) Out[27]: <psutil.Process(pid=29735, name=‘ipython‘) at 139986824457744> In [28]: p=psutil.Process(29735)#實例化一個進程對象,參數為ipython這個進程的PID In [29]: p.name()#獲得進程名 Out[29]: ‘ipython‘ In [31]: p.exe()#獲得進程的bin路徑 Out[31]: ‘/usr/local/bin/python3.5‘ In [32]: p.cwd()獲得進程工作目錄的絕對路徑 Out[32]: ‘/usr/local/lib/python3.5/site-packages/psutil-5.2.2-py3.5.egg-info‘ In [33]: p.status()#獲得進程狀態 Out[33]: ‘running‘ In [34]: p.create_time()#獲得進程創建的時間,時間戳格式 Out[34]: 1492848093.13 In [45]: datetime.datetime.fromtimestamp(p.create_time()).strftime("%Y-%m-%d %H:%M:%S") Out[45]: ‘2017-04-22 16:01:33‘ In [35]: p.uids()#獲取進程的uid信息 Out[35]: puids(real=0, effective=0, saved=0) In [36]: p.gids()#獲取進程的gid信息 Out[36]: pgids(real=0, effective=0, saved=0) In [37]: p.cpu_times()#獲取進程的cpu的時間信息 Out[37]: pcputimes(user=9.53, system=0.34, children_user=0.0, children_system=0.0) In [38]: p.cpu_affinity()#獲取進程的cpu親和度 Out[38]: [0] In [39]: p.memory_percent()#獲取進程的內存利用率 Out[39]: 6.187014703452833 In [40]: p.memory_info()#獲取進程的內存rss,vms信息 Out[40]: pmem(rss=64425984, vms=304410624, shared=4755456, text=2465792, lib=0, data=201437184, dirty=0) In [41]: p.io_counters()#獲取進程的io信息 Out[41]: pio(read_count=6915, write_count=6246, read_bytes=73728, write_bytes=1658880, read_chars=9329720, write_chars=1797027) In [43]: p.num_threads()獲取進程開啟的線程數 Out[43]: 1 popen類的使用:獲取用戶啟動的應用程序的進程信息 In [55]: from subprocess import PIPE In [50]: p1=psutil.Popen(["/usr/bin/python","-c","print(‘hello‘)"], stdout=PIPE) In [51]: p1.name() Out[51]: ‘python‘ In [52]: p1.username() Out[52]: ‘root‘ In [53]: p1.communicate() Out[53]: (b‘hello\n‘, None) In [54]: p.cpu_times() Out[54]: pcputimes(user=13.11, system=0.52, children_user=0.01, children_system=0.0)
python之psutil模塊詳解(Linux)--小白博客