Linux system uses Python monitoring system load script to share _python

Source: Internet
Author: User

Copy Code code as follows:

#!/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 ']

Simply explain Listing 2: Listing 2 reads the information in the/proc/loadavg, import Os:python imports the different modules, including system-supplied and custom modules. The basic form is: Import module name [as Alias], if you want to import only some or all of the modules in the module can be in the form of: from Module name import * to the appropriate module. OS Module OS module provides a unified operating system interface function, the OS module can switch automatically between the specific functions of different operating system platform, such as Nt,posix, so as to achieve cross-platform operation.
You can run the script using the Python command cpu1.py results are shown in Figure 2

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.