This article mainly introduces how to use python to monitor system load scripts in linux. For more information, see the following code:
#! /Usr/bin/env Python
Import OS
Def load_stat ():
Loadavg = {}
F = open ("/proc/loadavg ")
Con = f. read (). split ()
F. close ()
Loadavg ['lavg _ 1'] = con [0]
Loadavg ['lavg _ 5'] = con [1]
Loadavg ['lavg _ 15'] = con [2]
Loadavg ['NR '] = con [3]
Loadavg ['last _ pid '] = con [4]
Return loadavg
Print "loadavg", load_stat () ['lavg _ 15']
A brief description of listing 2: Listing 2 reads the information in/proc/loadavg. in import OS: Python, import is used to import different modules, including the system-provided and custom modules. The basic form is: import module name [as Alias]. if you only need to import part or all of the content in the module, you can use the form: from module name import * to import the corresponding module. The OS module provides a unified operating system interface function. the OS module can automatically switch between specific functions in different operating system platforms, such as nt and posix, to implement cross-platform operations.
You can use the Python command to run the script CPU1.py. The result is shown in figure 2.