# This is a learning note for the Liaoche teacher Python tutorial
Under Linux, there are many system commands that allow us to monitor the state of the system at all times, such as ps Top free et cetera. To obtain these system information, Python can pass subprocess module call and get results. But the best way is to use psutil This third-party module
Psutil = Process and System utilities, it can not only realize system monitoring through one or two lines of code, but also can be used across platforms, support linux/unix/osx/windows, etc. Essential module for system administrators and operations partners
1 , Get CPU Information
1) Get CPU of Information
>>> Import Psutil
>>> psutil. Cpu_count () # Number of CPU logic
4
>>> psutil. Cpu_count (Logical=false) # CPU Physical Core
2 # 2 Description is dual-core hyper-threading, 4 is 4-core non-hyper-threading
2) Statistics CPU User/System/Idle time
>>> psutil. Cpu_times ()
Scputimes (user=10963.31, nice=0.0, system=5138.67, idle=356102.45)
achieve similar Top of the command CPU Usage Rate
>>> for x in range: # show ten times
... psutil . cpu_percent (interval=1, percpu=true) # display interval is 1 seconds
2 , get the memory letter Interest
1) Use Psutil get virtual memory and swap memory information
>>> Psutil. virtual_memory ()
Svmem (total=8589934592, available=2866520064, percent=66.6, used=7201386496, free=216178688, active=3342192640, inactive=2650341376, wired=1208852480)
>>> Psutil. swap_memory ()
Sswap (total=1073741824, used=150732800, free=923009024, percent=14.0, sin=10705981440, sout=40353792)
3 , get disk information
1) through Psutil get disk partitions, disk usage, and disk IO Information
>>> Psutil. disk_partitions () # disk partition information
[Sdiskpart (device= '/dev/disk1 ', mountpoint= '/', fstype= ' HFS ', opts= ' Rw,local,rootfs,dovolfs,journaled,multilabel ') )]
>>> psutil. disk_usage ('/') # disk usage, here's / It's a mount point.
Sdiskusage (total=998982549504, used=390880133120, free=607840272384, percent=39.1)
>>> Psutil. disk_io_counters () # disk IO
Sdiskio (read_count=988513, write_count=274457, read_bytes=14856830464, write_bytes=17509420032, read_time=2228966, write_time=1618405)
4 , access to network information
1) Psutil can get network interface and network connection information
>>> Psutil. net_io_counters () # Get the number of bytes/packets read from the network
Snetio (bytes_sent=3885744870, bytes_recv=10357676702, packets_sent=10613069, packets_recv=10423357, errin=0, Errout =0, dropin=0, dropout=0)
>>> Psutil. Net_if_addrs () # Get network interface Information
>>> Psutil. net_if_stats () # Get network interface status
>>> psutil. net_connections () # get current network connection information
5 , get process information
1) Psutil can get detailed information to all processes
>>> Psutil. PIDs () # All Process IDs
[3865, 3864, 3863, 3856, 3855, 3853, 3776, ..., 45, 44, 1, 0]
>>> p = psutil. Process (3776) # Gets the specified process id=3776, which is actually the current Python interactive environment
>>> p. Name () # process name
' python3.6 '
>>> P.exe () # process EXE path
'/users/michael/anaconda3/bin/python3.6 '
>>> P.cwd () # Process working directory
'/users/michael '
>>> P.cmdline () # Process-initiated command line
[' Python3 ']
>>> P.ppid () # parent Process ID
3765
>>> p. Parent() # Parental process
<psutil. Process (pid=3765, name= ' Bash ') at 4503144040>
>>> P.children () # sub-process list
[]
>>> p Status() # process status
' Running '
>>> P.username () # process user name
' Michael '
>>> P.create_time () # Process creation Time
1511052731.120333
>>> p.terminal () # process Terminal
'/dev/ttys002 '
>>> P. Cpu_times () # CPU time used by the process
Pcputimes (user=0.081150144, system=0.053269812, children_user=0.0, children_system=0.0)
>>> p.memory_info () # Memory used by the process
Pmem (rss=8310784, vms=2481725440, pfaults=3207, pageins=18)
>>> P.open_files () # process Open File
[]
>>> p.connections () # Process-related network connections
[]
>>> p.num_threads () # Number of threads in a process
1
>>> p.threads () # All thread information
[Pthread (Id=1, user_time=0.090318, system_time=0.062736)]
>>> p.environ () # Process Environment Variables
{' SHELL ': '/bin/bash ', ' PATH ': '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin: ... ', ' PWD ': '/users/michael ', ' LANG ': ' Zh_cn. UTF-8 ', ...}
>>> P.terminate () # End Process
Terminated:15 <--himself to the end of himself.
>>> Psutil. Test () # simulate the effect of the PS command
6 , expand Documents
Psutil's official website (https://github.com/giampaolo/psutil)
Python Learning note __13.4 psutil