Linux systems use python to monitor system load script sharing
- 2020-04-02 13:19:21
- OfStack
#!/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']
Listing 2: listing 2 reads the information in /proc/loadavg, import OS: in Python, import is used to import different modules, including system-provided and custom modules. Its basic form is: import module name [as alias], if only need to import part or all of the contents of the module can use the form: from module name import * to import the corresponding module. OS module OS module provides a unified operating system interface function, the OS module can automatically switch between different operating system platforms such as nt, posix in the specific functions, so as to achieve cross-platform operation.
You can use the Python command to run the script cpu1.py as shown in figure 2
< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201401/20140115113159.jpg? 2014015113221 ">