Python module psutil obtains the system status example

Source: Internet
Author: User
Tags memory usage sleep cpu usage disk usage

Obtaining the current operating status and load status of the operating system is a basic skill of the system administrator. This is closely related to our daily troubleshooting and problem locating, for example, to view the basic information of the current system, such as cpu, memory, network packet reception, and disk usage, is the content that our daily system administrators often pay attention to. Since this information is so important, can we automatically display this information when logging on to the system? In fact, it is very easy to solve this problem. We can write a script that prints the information we are interested in, then place the script in. in bashrc, the script will be automatically called to run every time you log on to the system, and the current system information will be output. Now that you have figured out, you can start to enter the topic of today, we use the psutil module of python, the main character of today, to obtain the system status. This module can obtain information about running processes and the CPU and memory usage of the system. This module is very powerful, in addition, it is also cross-platform and can run well on other systems. Let's look at a practical example to show how to use the psutil module:

#! /Usr/bin/env python
# Coding: UTF-8
Import psutil
Import time
Import sys
From optparse import OptionParser
Parser = OptionParser ()
Parser. add_option ("-t", "-- time", dest = "time ",
Help = "this parameter shows the bandwidth occupied by the current Download.-t indicates the test time", metavar = "10 ")
Parser. add_option ("-d", "-- deamon", action = "store_false", dest = "deamon", default = True,
Help = "run this script in the background ")
 
Def Sysinfo ():
Boot_Start = time. strftime ("% Y-% m-% d % H: % M: % S", time. localtime (psutil. boot_time ()))
Time. sleep (0.5)
Cpu_usage = psutil. cpu_percent ()
RAM = int (psutil. virtual_memory (). total/(1027*1024 ))
RAM_percent = psutil. virtual_memory (). percent
Swap = int (psutil. swap_memory (). total/(1027*1024 ))
Swap_percent = psutil. swap_memory (). percent
Net_sent = psutil.net _ io_counters (). bytes_sent
Net_recv = psutil.net _ io_counters (). bytes_recv
Net_spkg = psutil.net _ io_counters (). packets_sent
Net_rpkg = psutil.net _ io_counters (). packets_recv
BFH = R' %'
Print "\ 033 [1; 32 m Boot Time: % s \ 033 [1; m" % Boot_Start
Print "\ 033 [1; 32 m current CPU usage: % s \ 033 [1; m" % (Cpu_usage, BFH)
Print "\ 033 [1; 32 m physical memory: % dM \ t usage: % s \ 033 [1; m" % (RAM, RAM_percent, BFH)
Print "\ 033 [1; 32mSwap memory: % dM \ t usage: % s \ 033 [1; m" % (Swap, Swap_percent, BFH)
Print "\ 033 [1; 32 m send: % d Byte \ t number of sent packets: % d \ 033 [1; m" % (Net_sent, Net_spkg)
Print "\ 033 [1; 32 m received: % d Byte \ t received packets: % d \ 033 [1; m" % (Net_recv, Net_rpkg)
 
For I in psutil. disk_partitions ():
Print "\ 033 [1; 32 m drive letter: % s Mount point: % s usage: % s \ 033 [1; m" % (I [0], I [1], psutil. disk_usage (I [1]) [3], BFH)
 
 
Def Net_io (s ):
X = 0
Sum = 0
While True:
If x> = s:
Break
R1 = psutil.net _ io_counters (). bytes_recv
Time. sleep (1)
R2 = psutil.net _ io_counters (). bytes_recv
Y = r2-r1
Print "%. 2f Kb/s" % (y/1024.0)
Sum + = y
X + = 1
Result = sum/x
Print "\ 033 [1; average speed of 32 m % s: %. 2f Kb/s \ 033 [1; m" % (x, result/1024.0)
If _ name _ = "_ main __":
(Options, args) = parser. parse_args ()
If options. time:
Net_io (int (options. time ))
Else:
Sysinfo ()

There are two script running modes. If no parameter is added, the current operating system information can be directly output, for example:

[Root @ test demo_script] # python get_sysinfo.py
Start time: 15:21:19
Current CPU usage: 2.0%
Physical memory: 3991 M usage: 22.2%
Swap memory: 0 M usage: 0.0%
Sent: 47886457797 bytes sent packets: 74127845
Received: 102287702165 bytes received packets: 952507264
Drive letter:/dev/xvda1 mount point:/Usage: 44.2%

The other method is to add the-t parameter followed by a number to view the average network speed in a given second, as shown below:


[Root @ test demo_script] # python get_sysinfo.py-t 10
0.08 Kb/s
0.05 Kb/s
0.04 Kb/s
0.04 Kb/s
0.04 Kb/s
0.04 Kb/s
0.04 Kb/s
0.08 Kb/s
0.04 Kb/s
0.04 Kb/s
Average speed within 10 seconds: 0.05 Kb/s
For more details about the psutil module, refer to http://pythonhosted.org/psutil/, which is detailed in detail.

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.