Python third-party module-psutil module

Source: Internet
Author: User

As an important part of the monitoring module, the system basic information acquisition module can help operators understand the health of the current system, as well as the basis of measuring the service quality of the business, such as the tight system resources, which will directly affect the service quality and user experience of the business, and also obtain the traffic information of the equipment. It also allows operations personnel to better assess bandwidth and whether device resources should be expanded. Using the Python third-party System Foundation module, we can easily get the data of service key operational metrics, including basic Linux performance, block device, NIC interface, System information, network address library and other information. After collecting these data, we can fully understand the status of the system services, combined with the alarm mechanism, can be in the first time response, the anomaly is now in the process of emerging.

Psutil Module

Psutil is a cross-platform library (http://code.google.com/p/psutil/) that makes it easy to get the process and system utilization (including CPU, memory, disk, network, and so on) information of the system running. It is mainly used for system monitoring, analyzing and restricting the management of system resources and processes. It implements the functions provided by equivalent command line tools such as PS, top, lsof, Netstat, ifconfig, who, DF, kill, free, nice, Ionice, Iostat, Iotop, uptime, pidof, TTY, Taskset, Pmap and so on. Currently supports 32-bit and 64-bit operating systems for Linux, Windows, OS X, FreeBSD, and Sun Solaris, and supports Python versions from 2.4 to 3.4.

First, obtain system performance information

Basic performance information of the acquisition system

The basic information of the system mainly includes CPU, memory, disk, network, etc., which can describe the usage of server resource very well, which plays an important role in the analysis of resource usage of our operation and maintenance personnel. The Psutil module has encapsulated these methods and can be used to complete calls and applications based on our needs.

(1) CPU Information

Linux operating system CPU utilization has the following parts

Usertime percentage of time the user process was executed:

Systemtime percentage of time to execute kernel processes and interrupts:

The percentage of time that wait IO has been idle (idle) for the CPU due to IO waits;

Idle CPU percent of time in idle state

Linux shell command to get the above information

# toptop-15:33:04 up to 11:44, 2 users, load average:0.00, 0.01, 0.00tasks:205 Total, 1 running, 204 sleepin G, 0 stopped, 0 zombiecpu (s): 0.8%us, 0.3%sy, 0.0%ni, 98.9%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%stmem:16330912k to Tal, 16150528k used, 180384k free, 238244k buffersswap:0k total, 0k used, 0k free, 2133688k CAC Hed

Python Psutil Module implementation

>>> import psutil>>> cputime=psutil.cpu_times () >>> cpuinfo=psutil.cpu_times () >> > Print (CPUTime) scputimes (user=27.52, nice=0.13, system=38.22, idle=27505.43, iowait=82.71, irq=0.0, softirq=1.17, steal=0.0, guest=0.0, guest_nice=0.0)

It is very simple to get the data we want, the output is a tuple we can use the slice to get the specific value.

>>> print (cputime[0]) #得到用户的值27 .52>>> print (cputime[2]) #系统值38 .22>>> or in the following ways >>> Print (Cpuinfo.user) 28.31>>> print (Cpuinfo.system) 40.19>>>

Get the number of CPUs

Psutil.cpu_count () #逻辑个数
Psutil.cpu_count (Logical=false) #物理个数

>>> psutil.cpu_count () 1>>> psutil.cpu_count (logical=false) 1

Memory information

The memory utilization information of the Linux system relates to total (amount of memory), used (number of memory used), free (number of idle memory), buffers (buffer usage), cache (number of cached uses), swap (swap partition usage), and so on, respectively, using
The Psutil.virtual_memory () and the Psutil.swap_memory () method obtain this information.

Get full information

Psutil.virtual_memory ()

Results

Svmem (total=461918208, available=107016192, percent=76.8, used=335712256, free=10584064, active=132857856, inactive= 206520320, Buffers=0, cached=115621888, shared=6508544) If you want to get one of them, you can use the following methods >>> Psutil.virtual_memory (). Free10395648>>> psutil.virtual_memory () .used335880192>>> psutil.virtual_memory (). total461918208

Swap parameter acquisition

Psutil.swap_memory () Sswap (total=2147479552, used=274354176, free=1873125376, percent=12.8, sin=21311488, sout= 298082304)
Get a single value >>> psutil.swap_memory () .total2147479552>>> psutil.swap_memory () .used274354176>> > psutil.swap_memory () .free1873125376>>>

Disk information

In all of the system's disk information, we pay more attention to disk utilization and IO information, where disk utilization makes the Psutil.disk_usage () method available. Disk IO information includes read_count (read IO), write_count (write io number), read_bytes (io read bytes), write_bytes (io write bytes), read_time (disk read time), Write_ Time (disk write times), and so on. These IO information can be obtained using psutil.disk_io_counters ()

For example

Psutil.disk_partitions () #获取磁盘所有信息 [Sdiskpart (device= '/dev/mapper/cl-root ', mountpoint= '/', fstype= ' xfs ', opts= ' RW, Relatime,attr2,inode64,noquota '), Sdiskpart (device= '/dev/sda1 ', mountpoint= '/boot ', fstype= ' xfs ', opts= ' RW, Relatime,attr2,inode64,noquota ')]psutil.disk_usage ("/dev/sda1") sdiskusage (total=219918336, Used=0, free= 219918336, percent=0.0) >>> psutil.disk_usage ("/dev/sda1") sdiskusage (total=219918336, Used=0, free= 219918336, percent=0.0) Note: Psutil.disk_usage requires parameters such as "/DEV/SDA1" partition information to get the usage of the partition psutil.disk_io_counters () Sdiskio ( read_count=48990, write_count=85420, read_bytes=2731183616, write_bytes=950151168, read_time=938874, write_time= 13787272, read_merged_count=4479, write_merged_count=73400, busy_time=259691)

Network Information

The network information of the system is similar to disk IO, involving several key points, including bytes_sent (number of bytes sent), bytes_recv=28220119 (number of bytes received), packets_sent=200978 (number of packets sent), PACKETS_RECV =212672 (number of packets received) and so on. These network information is obtained using the Psutil.net_io_counters () method


Use Psutil.net_io_counters to get the total IO information of the network default Pernic=false,

Snetio (bytes_sent=6328062, bytes_recv=19216785, packets_sent=12474, packets_recv=23414, errin=0, errout=0, Dropin=0, dropout=0)

Can get traffic information for each NIC when pernic=ture

Psutil.net_io_counters (pernic=true) {' Ens33 ': Snetio (bytes_sent=6262442, bytes_recv=19205069,packets_sent=12333, packets_recv=23271, Errin=0, errout=0, Dropin=0, dropout=0), ' Lo ': Snetio (bytes_sent=6588,bytes_recv=6588, Packets_ sent=76, packets_recv=76, errin=0, errout=0, dropin=0, Dropout=0)}

Information such as user information and boot time

Use the Psutil.users method to return user information for the currently logged on system

Psutil.users () [Suser (name= ' root ', terminal= ' tty1 ', host= ', started=1491535104.0), suser (name= ' root ', terminal= ' pts /0 ', host= ' 192.168.186.1 ', started=1491535104.0), suser (name= ' root ', terminal= ' pts/1 ', host= ' 192.168.186.1 ', started =1491543808.0), suser (name= ' root ', terminal= ' pts/2 ', host= ' 192.168.186.1 ', started=1492674560.0), suser (name= ' root ' , terminal= ' Pts/3 ', host= ' 192.168.186.1 ', started=1492676992.0)]

Use the Psutil.boot_time method to get boot time to Linux time stamp

>>> psutil.boot_time () 1492648101.0

Process Management

Obtain the process information of the current system, can let the operator know the running state of the application, including the start time of the process, view or set CPU affinity, memory utilization, IO information, socket connection, number of threads, etc., this information can show whether the specified process is alive, resource utilization, It provides a good data reference for code optimization and problem locating for developers.

Process information

The Psutil module also provides good support for getting process information, including using the Psutil.pids () method to get all the process PID, and using the Psutil.process () method to obtain information such as the name, path, state, system resource utilization, and so on for a single process.

psutil.pids ()    #获取目前所有运行进程的id [1, 2, 3, 4,  6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19,  25, 26, 27, 28, 36, 38, 39, 41, 60, 92, 270, 271,  273, 282, 283, 284, 287, 288, 290, 293, 365, 366, 377,  378, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400,  468, 486, 493, 732, 776, 777, 778, 782, 783, 786, 788,  790, 912, 978, 980, 982, 992, 993, 994, 997, 1009,  1012, 1018, 1330, 1332, 1372, 1469, 1965, 2706, 2722, 2724,  2798, 3516, 4218, 4259, 4261, 4343, 4370, 4479, 4507, 4508,  4531] 

We can use the process ID and  psutil. The process () method instantiates a session so that more information can be obtained such as we instantiate the ID 270

 mn=psutil. Process (>>> mn.name)    #进程名称 ' ATA_SFF ' >>> mn.exe ()        #进程bin路径 ' >>> mn.status ()   #进程状态 ' sleeping ' >>> mn.cwd ()  # Absolute path of process '/' mn.create_time ()   #进程创建时间   return timestamp 1492648102.1>>> mn.uids ()   #进程uid信息puids ( real=0, effective=0, saved=0) >>> mn.gids ()    #进程gid信息pgids (real=0,  effective=0, saved=0) >>> mn.cpu_times ()   #进程CPU时间信息, including user, system two CPU time Pcputimes (user= 0.0, system=0.0, children_user=0.0, children_system=0.0) >>> >>>  Mn.memory_percent ()   #进程内存使用率0.0>>> mn.memory_info ()   #进程内存rss, VMS information Pmem (RSS=0, VMS =0, shared=0, text=0, lib=0, data=0, dirty=0>>> mn.io_counters () #  Process IO information, including read/write IO number and byte Count   pio (Read_count=0, write_count=0, read_bytes=0, write_bytes=0,  read_chars=0, write_chars=0) >>> mn.connections ()   #回打开进程socket的namedutples列表, including FS, family, LADDR and other information []>>> mn.num_threads () #进程启动的线程数量1 >>>


This article from "adhere to the Dream" blog, reproduced please contact the author!

Python third-party module-psutil module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.