Share the load script of the python monitoring system in linux

Source: Internet
Author: User
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.

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.