Python's operating module for Linux systems

Source: Internet
Author: User

1. PIP installation (PIP is "A tool for installing and managing Python packages.", which means Pip is a Python software installation Tool)
Yum-y Install Epel-release
Yum-y Install Python-pip
Yum-y Install Python-devel
Yum-y Install gcc*


2. Download psutil (or install using PIP install Psutil)
wget https://pypi.python.org/packages/source/p/psutil/psutil-3.2.2.tar.gz
Tar zxvf psutil-3.2.2.tar.gz
CD PSUTIL-3.2.2/
Python setup.py Install


3. Clear the Cache
Yum Clean All


4. Python enters python command line
[[email protected] ~]# python
Python 2.7.5 (default, Sep 15 2016, 22:37:39)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more Information.
Import Psutil
>>> Import Psutil


5, CPU Complete information (command top-bn 1-i-c, vmstat, etc.)
Psutil.cpu_times ()
Scputimes (user=471.21, nice=104.58, system=276.85, idle=28935.3, iowait=391.02, irq=0.0, softirq=16.35, steal=0.0, guest=0.0, guest_nice=0.0
>>> Psutil.cpu_times (percpu=true)
[scputimes (user=249.03, nice=61.25, system=156.09, idle=20729.97, iowait=195.16, irq=0.0, softirq=7.25, steal=0.0, guest=0.0, guest_nice=0.0),
Scputimes (user=231.55, nice=43.33, system=140.27, idle=20633.5, iowait=196.1, irq=0.0, softirq=9.24, steal=0.0, guest= 0.0, guest_nice=0.0)]
Gets the individual data, such as the User's CPU time
>>> psutil.cpu_times (). User
471.25
Gets the CPU physical and logical number, the default logical value is True
>>> Psutil.cpu_count ()
2
>>> Psutil.cpu_count (logical=false)
2


6. Memory Information
>>> mem = psutil.virtual_memory ()
Get full Memory information
>>> Mem
Svmem (total=2082361344l, available=1130319872l, percent=45.7, used=1805217792l, free=277143552l, active=664281088, inactive=797175808, buffers=0l, Cached=853176320)
Get Total Memory
>>> Mem.total
2082361344L
Get free Save Count
>>> Mem.free
277143552L
Get swap partition information
>>> psutil.swap_memory ()
Sswap (total=2147479552l, used=8335360l, free=2139144192l, percent=0.4, sin=3416064, sout=11354112)


Can be converted to units
>>> Print (mem.total/8/1024)
127874
>>> Print (mem.total/1024/1024)


Where percent represents the actual amount of memory used, i.e. (2082361344-1130319872)/2082361344*100%. Available indicates memory that can also be used.


7. Disk Information
There are two main disk information, one is the utilization of the disk, one is io, they can be obtained through the Disk_usage and Disk_io_counters methods Respectively.
Get the partition information first, and then look at the usage of the root Partition.
Get disk complete information
>>> psutil.disk_partitions ()
[sdiskpart (device= '/dev/mapper/centos-root ', mountpoint= '/', fstype= ' xfs ', opts= ' rw,seclabel,relatime,attr2, Inode64,noquota '),
Sdiskpart (device= '/dev/sda1 ', mountpoint= '/boot ', fstype= ' xfs ', opts= ' Rw,seclabel,relatime,attr2,inode64,noquota ' )]
Get partition usage
>>> Psutil.disk_usage ('/')
Sdiskusage (total=18746441728, used=5844000768, free=12902440960, percent=31.2)
Get the total number of io, read and write information
>>> psutil.disk_io_counters ()
Sdiskio (read_count=146151, write_count=203807, read_bytes=4112552448, write_bytes=11684216320, read_time=2269317, Write_time=39627462)
Perdisk=ture get the number of individual partitions io, read and write information
>>> psutil.disk_io_counters (perdisk=true)
{' fd0 ': Sdiskio (read_count=0, write_count=0, read_bytes=0, write_bytes=0, read_time=0, write_time=0),
' Sr0 ': Sdiskio (read_count=0, write_count=0, read_bytes=0, write_bytes=0, read_time=0, write_time=0),
' Sda2 ': Sdiskio (read_count=71699, write_count=82805, read_bytes=2054048768, write_bytes=5806939648,
read_time=1124424, write_time=11573392), ' sda1 ': sdiskio (read_count=1952, write_count=2255, read_bytes=5081600,
write_bytes=70337024, read_time=10686, write_time=27267), ' dm-0 ': Sdiskio (read_count=71452, write_count=115975,
read_bytes=2048183296, write_bytes=5795585536, read_time=1129014, write_time=25763350),
' Dm-1 ': Sdiskio (read_count=1048, write_count=2772, read_bytes=5238784, write_bytes=11354112, read_time=5193, write_ time=2263453)}

8. Network Information
Get the total IO information of the network, default Pernic=false
>>> psutil.net_io_counters ()
Snetio (bytes_sent=17640593, bytes_recv=837836495, packets_sent=227041, packets_recv=594110, errin=0, errout=0, dropin=0, dropout=0)
Pernic=true output IO information for each network interface
>>> psutil.net_io_counters (pernic=true)
{' eno16777736 ': snetio (bytes_sent=17641429, bytes_recv=837839462, packets_sent=227046, packets_recv=594142, errin=0 , errout=0, dropin=0, dropout=0),
' Virbr0 ': snetio (bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0),
' Virbr0-nic ': snetio (bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout= 0),
' Lo ': snetio (bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0)}

9. Other System Information
View currently logged in user
>>> psutil.users ()
[suser (name= ' Wolf ', terminal= ': 0 ', host= ' localhost ', started=1477147392.0),
Suser (name= ' wolf ', terminal= ' pts/0 ', host= ' localhost ', started=1477147520.0),
Suser (name= ' wolf ', terminal= ' pts/4 ', host= ' 192.168.3.26 ', started=1477190016.0),
Suser (name= ' wolf ', terminal= ' pts/6 ', host= ' 192.168.3.26 ', started=1477190144.0)]
>>> Import psutil, datetime
View Time (linux format Timestamp)
>>> Psutil.boot_time ()
1477177417.0
Linux timestamp converted into natural time format
>>> datetime.datetime.fromtimestamp (psutil.boot_time ()). strftime ("%y-%m-%d%h:%m:%s")
' 2016-10-23 07:03:37 '


10. System Process Management
List all process PID
>>> Psutil.pids ()
[[1, 2, 3, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, ....]
Instantiate a Process object with parameter as a progress PID
>>> P=psutil. Process (1)
Process Name
>>> P.name ()
' Systemd '
>>> P=psutil. Process (76242)
>>> P.name ()
' Pickup '
Process bin Path
>>> P.exe ()
'/usr/libexec/postfix/pickup '
Process Jobs Directory Absolute path
>>> P.CWD ()
'/var/spool/postfix '
Process status
>>> P.status ()
' Sleeping '
Process creation Time
>>> P.create_time ()
1477208086.18
Process UID
>>> P.uids ()
PUIDs (real=89, effective=89, Saved=89)
Process GID
>>> P.gids ()
Pgids (real=89, effective=89, Saved=89)
Process CPU time information, including user, system two CPU time
>>> P.cpu_times ()
Pcputimes (user=0.02, system=0.02)
Get process CPU affinity, If you set process CPU affinity, the CPU number as a time parameter can be
>>> p.cpu_affinity ()
[0, 1]
Process Memory Utilization
>>> p.memory_percent ()
0.19276577581340273
Process Memory Css,vms Information
>>> P.memory_info ()
Pmem (rss=4014080, Vms=95567872)
Process io information, including read/write io number and number of bytes
>>> p.io_counters ()
Pio (read_count=149, write_count=160, read_bytes=0, write_bytes=0)
Returns the Namedutples list of open process socker, containing information such as fs, family, laddr, and more
>>> p.connections ()
[]
Number of threads opened by the process
>>> p.num_threads ()
1


Popen class Action: gets user-initiated application process information to track the running state of a program Process.
>>> from subprocess Import PIPE
Applications launched through the Psutil Popen method can keep track of all relevant information about the program
>>> p = Psutil. Popen (["/usr/bin/python", "-c", "print (' Hello ')"],stdout=pipe)
>>> P.name ()
' Python '
>>> P.username ()
' Root '
>>> p.communicate ()
(' hello\n ', None)
>>> Psutil.cpu_times ()
Scputimes (user=499.47, nice=104.58, system=335.63, idle=69964.99, iowait=391.57, irq=0.0, softirq=16.75, steal=0.0, guest=0.0, guest_nice=0.0)




Python's operating module for Linux systems

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.