Python – psutil (Disk , NetWork)

Loading

測試環境為 CentOS8 (虛擬機)

Python 的 psutil (python system and process utilities) 套件是一個可以跨平台讀取系統 關於 CPU 與 memory 請參考 – https://benjr.tw/105251 , disks 與 network (這邊說明) , sensors 資訊與使用率工具.

安裝 psutil 套件.

[root@localhost ~]# pip install psutil
[root@localhost ~]# python3
Python 3.6.8 (default, Sep 21 2021, 20:17:36)
[GCC 8.4.1 20200928 (Red Hat 8.4.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.

匯入 psutil 套件.

>>> import psutil

Disk

不知道是因為虛擬機的關係部分值沒有,這邊直接看 https://pypi.org/project/psutil/ 的範例.

檢視磁區分割狀態.

>>> psutil.disk_partitions()
[sdiskpart(device='/dev/sda1', mountpoint='/', fstype='ext4', opts='rw,nosuid', maxfile=255, maxpath=4096),
 sdiskpart(device='/dev/sda2', mountpoint='/home', fstype='ext', opts='rw', maxfile=255, maxpath=4096)]

檢視磁區使用率,需指定 mountpoint .

>>> psutil.disk_usage('/')
sdiskusage(total=21378641920, used=4809781248, free=15482871808, percent=22.5)

檢視硬碟 I/O 狀態,這邊使用 perdisk 參數為 False,如需看個別磁碟狀況可以使用 True .

>>> psutil.disk_io_counters(perdisk=False)
sdiskio(read_count=719566, write_count=1082197, read_bytes=18626220032, write_bytes=24081764352, read_time=5023392, write_time=63199568, read_merged_count=619166, write_merged_count=812396, busy_time=4523412)
  1. read_count: number of reads
  2. write_count: number of writes
  3. read_bytes: number of bytes read
  4. write_bytes: number of bytes written

以下屬性會因為使用平台而異.

  1. read_time: (all except NetBSD and OpenBSD) time spent reading from disk (in milliseconds)
  2. write_time: (all except NetBSD and OpenBSD) time spent writing to disk (in milliseconds)
  3. busy_time: (Linux, FreeBSD) time spent doing actual I/Os (in milliseconds)
  4. read_merged_count (Linux): number of merged reads
  5. write_merged_count (Linux): number of merged writes

更多關於 Disk 資訊請參考說明文件 https://psutil.readthedocs.io/en/latest/#disks

Network

檢視網路狀態.

>>> psutil.net_if_stats()
{'lo': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=65536), 'virbr0-nic': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=10, mtu=1500), 'virbr0': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1500), 'ens33': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=1000, mtu=1500)}

檢視網路 I/O 狀態,預設 pernic 參數為 False,如需看個別網路狀況可以使用 True .

>>> psutil.net_io_counters()
snetio(bytes_sent=28746670, bytes_recv=989227584, packets_sent=83524, packets_recv=737631, errin=0, errout=0, dropin=0, dropout=0)
>>> psutil.net_io_counters(pernic=True)
{'lo': snetio(bytes_sent=2768, bytes_recv=2768, packets_sent=32, packets_recv=32, errin=0, errout=0, dropin=0, dropout=0), 'virbr0-nic': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'virbr0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'ens33': snetio(bytes_sent=28745470, bytes_recv=989226888, packets_sent=83507, packets_recv=737626, errin=0, errout=0, dropin=0, dropout=0)}
  1. bytes_sent: number of bytes sent
  2. bytes_recv: number of bytes received
  3. packets_sent: number of packets sent
  4. packets_recv: number of packets received
  5. errin: total number of errors while receiving
  6. errout: total number of errors while sending
  7. dropin: total number of incoming packets which were dropped
  8. dropout: total number of outgoing packets which were dropped (always 0 on macOS and BSD)

更多關於 Network 資訊請參考說明文件 https://psutil.readthedocs.io/en/latest/#network

沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料