The Psutil module is capable of capturing the process and system utilization of the system. Includes: CPU, memory, disk, network and other information. It is generally used for system monitoring, analyzing and restricting the management of system resources and processes.
First download the installation Psutil:
wget Https://pypi.python.org/packages/source/p/psutil/psutil-2.0.0.tar.gztar ZXVF PSUTIL-2.0.0.TAR.GZCD Psutil-2.0.0python setup.py Install
Installation error message:
Error:command ' gcc ' failed with exit status 1
Workaround:
Yum Install gcc python-devel-y
First, view the total amount of memory and usage
>>>import Psutil>>>mem = Psutil.virtual_memory () >>>mem.total,mem.used (1968566272L, 371720192L)
Second, obtain system performance information
1.cpu Information
2.User: Percentage of time to execute user processes
3.System time: Percent execution of kernel processes and interrupts
The percentage of time that the CPU is idle idle due to IO waits for the 4.Wait io
5.idle,cpu percentage of time in idle state
We use the Python Psutil.cpu_times () method to get this information simply, and to get the hardware-related information of the CPU, such as the number of CPUs and the number of logical, examples are as follows:
>>>psutil.cpu_times () scputimes (user=7.9100000000000001,nice=0.0, system=13.41, idle=645.64999999999998, iowait=5.1500000000000004,irq=0.33000000000000002, softirq=0.32000000000000001, steal=0.0, guest=0.0) >>> Psutil.cpu_times (). User #获取user的cpu时间比8.0099999999999998>>>psutil.cpu_count () #获取cpu的逻辑个数1 >>> Psutil.cpu_count (Logical=false) #获取CPU的物理个数1
# Memory Information
>>>mem = Psutil.virtual_memory () >>>memsvmem (total=1968566272l,available=1779888128l, percent= 9.5999999999999996, used=372531200l,free=1596035072l, active=225411072, inactive=77631488, Buffers=10407936L, CACHED=173445120) >>>mem.total1968566272l>>>mem.free1596035072l>>>psutil.swap_memory () Sswap (total=2147479552l,used=0l, free=2147479552l, percent=0.0, sin=0, sout=0) >>>
# Disk Information
>>>mem = Psutil.disk_partitions () >>>psutil.disk_partitions () [Sdiskpart (device= '/dev/sda3 ', Mountpoint= '/', fstype= ' ext4 ', opts= ' RW '), Sdiskpart (device= '/dev/sda1 ', mountpoint= ' boot ', fstype= ' ext4 ', opts= ' RW ')]>>>psutil.disk_usage ('/') sdiskusage (total=18682343424,used=2136817664, free=15589699584, percent= 11.4) >>>psutil.disk_io_counters () Sdiskio (read_count=4895,write_count=2763, read_bytes=173164544, Write_ bytes=44500992, read_time=8461,write_time=12124) >>>
# Network Information
>>>psutil.net_io_counters () snetio (bytes_sent=708988,bytes_recv=4904912, packets_sent=4577, PACKETS_RECV =5314, Errin=0, errout=0,dropin=0, dropout=0) >>>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), ' eth1 ': Snetio (Bytes_ sent=711716,bytes_recv=4908234, packets_sent=4601, packets_recv=5352, errin=0, errout=0,dropin=0, Dropout=0)}
# Other System Information
>>>psutil.users () [Suser (name= ' root ', terminal= ' pts/0 ', host= ' 192.168.1.5 ', started=1434034432.0)]> >>psutil.boot_time (1434034443.0)
This article from "Liang Enyu-9527" blog, reproduced please contact the author!
Python System Information Module Psutil