pustil obtain system information

I. Introduction

psutil name is Process and System Utilities . psutil is used in a cross-platform system monitoring, analysis, and system process management of certain third-party Python libraries. It is not only easy access to the process and system utilization (such as CPU, memory, disk, network, etc.) information, but also to achieve similar functionality with UNIX system command line tool system in normal operation. Operation and maintenance work can be said of "essential goods." Operation and maintenance engineers often written in Python scripts do condition monitoring system operation. If you own manually using Python's standard library to perform system commands to obtain information, it will become very troublesome. Both compatible with different operating systems, but also to deal with their own IP addresses. To solve the problem of pain points, psutil it turned out. Its appearance is undoubtedly the Gospel operation and maintenance engineers. Operation and maintenance of small partners to implement a system supervisor through its implementation of one or two lines of code.

It is powerful, easy to operate. It also contributed to many open source projects are integrated into its own project, may wish to have Google's GRR project, osquery projects face book.

github address : https: //github.com/giampaolo/psutil

Second, the installation

There are several ways to install psutil is: by pip install, installed by source, tar archive to install the download. Pip which by the way is the easiest.

pip install psutil

Third, the use

3.1 acquire CPU information

  • I would like to get their computer CPU cores, and my computer's CPU model is I5 4590. I learned through a search engine that is four nuclear power plant model CPU line.
psutil Import 

psutil.cpu_count () # get the logic cores of the CPU, default = True the Logical 
psutil.cpu_count (the Logical = False) # get the number of physical CPU core 

>> 4 
>> 4 
# This shows that the model is a quad-core CPU .
  • CPU time statistics
import psutil
psutil.cpu_times() 

>> scputimes(user=9276.365234375, system=5034.5390625, idle=96077.0703125, interrupt=181.78796863555908, dpc=298.227108001709)

  cpu_times () returns a tuple with the system of all logical CPU time, in seconds. Returns tuple fields have several common fields:

    • user: a user process execution time, Linux system also includes time visitors
    • system: the kernel execution process time
    • idle: idle time
    • iowait (Linux specific): Wait I / O operation time
    • irp (Linux-specific): Time hardware interrupt service
    • interrupt (Windows-specific): similar field with irp
    • dpc (Windows-specific): Service delayed procedure call (DPCs) time

If you increase percpu = True , cpu_times () outputs for each logical CPU time in the form of a list.

  • Gets the current percentage of CPU utilization:
import psutil

psutil.cpu_percent()
>> 16.5

cpu_percent () The default parameters interval The = None, percpu = False . When the interval when 0 or None, sys represents the usage time within the interval. When percpu represents all logical CPU usage to False.

If you want statistics for 15 seconds, 5 second intervals for each logical CPU utilization, you can do this:

import psutil

for i in range(3):
    psutil.cpu_percent(interval=5, percpu=True)

>>  [9.1, 7.2, 7.2, 6.2]
>>  [5.9, 3.8, 9.0, 8.4]
>>  [12.3, 8.4, 3.4, 4.1]
  • CPU frequency to obtain information:
import psutil

psutil.cpu_freq()
>> [scpufreq(current=931.42925, min=0.0, max=3301.0)]

  It found cpu_freq () tuple with all the logical CPU frequencies returned, including current, minimum and maximum frequencies.

3.2 to get memory information

  • Get physical memory information:
import psutil

psutil.virtual_memory()
>>  svmem(total=8509177856, 
available=1692307456, percent=80.1, used=6816870400, free=1692307456)

virtual_memory () returns a tuple describes information of a current physical memory available in the computer equipment, in bytes. Return results from that, the total size of current memory 8509177856 Byte = 8 GB, available memory (idle memory) 1692307456 Byte = 1.6 GB`, current memory usage rate of 80.1%. It is worth noting that the total memory size is not equal to the sum of both available and Used

available fields in the Linux system, the calculation is different. available = free + buffers + cached. buffers refers Buffers under Linux memory, cache page represents a block device (block device) occupied; and cached refers Cache memory under Linux, by definition as a cache.

  • Swap memory to obtain information:
import psutil

psutil.swap_memory()
>>  sswap(total=17016451072, used=7407996928, free=9608454144, percent=43.5, sin=0, sout=0)

  swap_memory () to obtain the information exchange system memory, that is, we often say that the virtual memory. The first four fields with the same physical memory meaning. While sin represents a disk is transferred from the size of the swap, Sout represents transferred from the swap out to the disk size. These two fields in the Windows system does not make sense. Therefore, to obtain the result is zero.

3.3 Obtaining Disk Information

  • Get the disk partition information:
import psutil

psutil.disk_partitions()

>>  
[sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed'), 
sdiskpart(device='D:\\', mountpoint='D:\\', fstype='NTFS', opts='rw,fixed'), 
sdiskpart(device='E:\\', mountpoint='E:\\', fstype='NTFS', opts='rw,fixed'), 
sdiskpart(device='F:\\', mountpoint='F:\\', fstype='NTFS', opts='rw,fixed')]

  

disk_partitions (all = False) returns a list of all mount the disk partition information. Linux is somewhat similar to the df command. Containing various fields are:

    • device: partition
    • mountpoint: mountpoint
    • fstype: file system format
    • opts: Mount parameter

Most people are on the Windows partition information system to understand more, little is known of the Linux system. So, I give Ubuntu virtual machine disk information systems, to facilitate learning.

[sdiskpart(device='/dev/sda1', mountpoint='/', fstype='ext4', opts='rw,errors=remount-ro'),
 sdiskpart(device='/dev/sda2', mountpoint='', fstype='swap', opts='rw')]
  • Get disk usage

 disk_usage () disk statistical parameters in the directory path usage. It needs to pass a path argument; my argument passed is "/" , which means get the entire current hard drive usage.

import psutil

psutil.disk_usage('/')

>>  sdiskusage(total=128033574912, used=81997942784, free=46035632128, percent=64.0)
  • Disk IO obtain information:
import psutil

psutil.disk_io_counters()

>>  sdiskio(read_count=510749, write_count=505110, read_bytes=13353246720, write_bytes=8962015232, read_time=275, write_time=238)

3.4 Obtaining Network Information  

  • Obtain network information of the whole system
import psutil

psutil.disk_io_counters()

>>  snetio(bytes_sent=77587966, bytes_recv=1113555204, packets_sent=500638, packets_recv=1048467, errin=0, errout=0, dropin=0, dropout=0)

disk_io_counters () returns all cards of the entire system (including wired LAN, wireless interface) for network read and write data, the number of contract information and the like. Personally I think that this method can be used to catch bag. Each field have the following meanings:

    • bytes_sent: Bytes Sent
    • bytes_recv: the number of bytes received
    • packets_sent: the number of packets sent to
    • The number of received data packets: packets_recv
    • errout: Total transmission packet errors
    • The total number of discarded packets received: dropin
    • dropout: discarded when the total number of packets sent (OSX and BSD systems always 0)

If you increase pernic = True , disk_io_counters () will output the information data of each network card.

Guess you like

Origin www.cnblogs.com/hester/p/12176070.html