Python----psutil Module

Source: Internet
Author: User

1. 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, etc.) 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 such as Linux, Windows, OS X, FreeBSD, and Sun Solaris.

2. Installation

    https://pypi.python.org/packages/source/p/psutil/psutil-2.0.0.tar.gz tar -xzvf psutil-2.0.0.tar.gz cd psutil-2.0.0 python setup.py install

3. Use

Get system Performance Information (CPU, memory, disk, network)

3.1CPU related
View CPU Information

import Psutil查看cpu所有信息>>> psutil.cpu_times()scputimes(user=11677.09, nice=57.93, system=148675.58, idle=2167147.79, iowait=260828.48, irq=7876.28, softirq=0.0, steal=3694.59, guest=0.0, guest_nice=0.0)

Display all CPU logic information

>>> psutil.cpu_times(percpu=True)[scputimes(user=11684.17, nice=57.93, system=148683.01, idle=2168982.08, iowait=260833.18, irq=7882.35, softirq=0.0, steal=3697.3, guest=0.0, guest_nice=0.0)]

View the user's CPU time ratio

psutil.cpu_times().user11684.4

To view the number of CPU logic

>>> psutil.cpu_count()1

View CPU Physical Number

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

3.2 Viewing system memory

>>> import psutil>>> mem = psutil.virtual_memory()>>> mem#系统内存的所有信息svmem(total=1040662528, available=175054848, percent=83.2, used=965718016, free=74944512, active=566755328, inactive=59457536, buffers=9342976, cached=90767360)

System Total Memory

>>> mem.total1040662528

The system already uses memory

>>> mem.used965718016

System free Memory

>>> mem.free112779264

Get Swap Memory information

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

Read disk parameters

Disk utilization is obtained using the Psutil.disk_usage method,

Disk IO information includes read_count (read IO number), Write_count (write IO number)
Read_bytes (io Write bytes), read_time (disk read time), Write_time (disk write time), these IO information

psutil.disk_io_counters()

Get full information about a disk

psutil.disk_partitions()

Get parameters for a partitioned table

psutil.disk_usage(‘/‘)   #获取/分区的状态

Get the total number of HDD IO

psutil.disk_io_counters()

Get the number of individual partition IO

psutil.disk_io_counters(perdisk=True)    #perdisk=True参数获取单个分区IO个数

Read Network information

Network information is similar to disk IO information, involving several key points, including byes_sent (number of bytes sent), Byte_recv=xxx (bytes accepted),
Pack-ets_sent=xxx (Bytes sent), pack-ets_recv=xxx (number of packets received), these network information

Get Network Total IO information

psutil.net_io_counters()  

Output network per interface information

psutil.net_io_counters(pernic=True)     #pernic=True

Get current system User login information

psutil.users()

Get boot time

Psutil.boot_time () #以linux时间格式返回

Datetime.datetime.fromtimestamp (Psutil.boot_time ()). Strftime ("%y-%m-%d%H:%m:%s") #转换成自然时间格式

System Process Management
Gets the current system's process information, gets the current program's running state, including the start time of the process, view the settings CPU affinity, memory utilization, IO information
Socket connection, number of threads, etc.
Get process Information

View all system processes

psutil.pids()

viewing individual processes

p = psutil. Process (2423) P.name ()#进程名p. exe ()#进程的bin路径p. CWD ()#进程的工作目录绝对路径p. Status () #进程状态p. Create_time ()  #进程创建时间p. UIDs ()  #进程uid信息p. Gids ()  #进程的gid信息p. Cpu_times ()  #进程的cpu时间信息, including User,system two CPU information p.cpu_affinity () # Get process CPU affinity, if you want to set CPU affinity, the CPU number as a reference is good p.memory_percent ()  #进程内存利用率p. Memory_info ()  #进程内存rss, VMS information p.io_counters ()  #进程的IO信息, including read/write IO numbers and Parameters P.connectios ( )  #返回进程列表p. Num_threads ()  #进程开启的线程数听过psutil的Popen方法启动应用程序, You can track information about your program from subprocess import Pipep = Psutil. Popen ([ "/usr/bin/python",  "-C",  " Print (' Hello ') "],stdout=pipe) P.name () p.username ()         

Python----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.