Linux system monitoring tools, such as Inotify-sync (file system security monitoring software), Glances (Resource monitoring tool), are currently in use in the Python language, Linux The system administrator can write a simple and practical script to monitor the Linux server according to the specific situation of the server he uses. This article describes the use of Python scripting to implement a monitoring script for the Linux server CPU memory network.
Python Release Notes
Python was developed by Guido van Rossum for free and very advanced interpretive language. Its syntax is simple and understandable, and its object-oriented semantics are powerful (but flexible). Python can be widely used and highly portable. This document Linux server is Ubuntu 12.10, the Python version is 2.7. If there is a syntax for Python version 3.0 There is a certain discrepancy. In addition, the python that I'm talking about here is Cpython,cpython is a Python interpreter that is implemented in C, and is also the official and most widely used Python interpreter. In addition to CPython, it is also useful for Java-implemented Jython and IronPython implemented with. NET to make Python easy to integrate with Java programs,. NET programs. In addition, there are some experimental Python interpreters such as PyPy. CPython is an interpreter that uses bytecode, and any program source code is compiled into bytecode before execution. It also has an external function interface that interacts with several other languages, including the C language.
How it works: Based on the/proc file system
The Linux system provides an excellent way for administrators to change the kernel while the system is running, without rebooting the kernel system, which is achieved through the/proc virtual file system. The/proc file virtual system is a mechanism that the kernel and kernel modules use to send information to the process (so called "/proc"), a pseudo-file system that allows interaction with the kernel's internal data structures to obtain useful information about the process, in operation (on the Fly) to change the settings (by changing the kernel parameters). Unlike other file systems,/proc exists in memory and not on the hard disk. The information provided by the proc file system is as follows:
- Process information: Any process in the system that has a process ID with the same name in the proc subdirectory can find CmdLine, mem, root, Stat, STATM, and status. Some information is only visible to the superuser, such as the process root directory. Each process that contains existing process information alone has some specialized links available, and any process in the system has a separate self-link pointing to the process information, which is useful for getting command-line information from the process.
- System Information: If you need to know the entire system information can also be obtained from/proc/stat, including CPU usage, disk space, memory swap, interrupt and so on.
- CPU information: Use the/proc/cpuinfo file to obtain the current accurate information of the CPU.
- Payload information: The/proc/loadavg file contains system payload information.
- System memory Information: The/proc/meminfo file contains details of the system memory, which shows the amount of physical memory, the amount of free swap space, and the amount of free memory.
Table 1 is a description of the main files in the/proc directory:
Table 1/proc Description of the main file in the directory
Here are a few examples of using Python scripts to read the main files in the/proc directory to implement monitoring of Linux servers.
Using Python scripts to monitor Linux servers
For CPU (central processing unit) monitoring
Script 1 name cpu1.py, which acts to obtain information about the CPU.
Listing 1. Getting information about the CPU
#!/usr/bin/env pythonfrom __future__ Import print_functionfrom Collections import Ordereddictimport pprint def CPUinfo () : "Return the information in/proc/cpuinfo as a dictionary in the following format: cpu_info[' proc0 ']={... } cpu_info[' Proc1 ']={...} ' Cpuinfo=ordereddict () procinfo=ordereddict () nprocs = 0 with open ('/proc/cpuinfo ') as F: For line in F : if not Line.strip (): # End of one processor cpuinfo[' proc%s '% nprocs] = ProcInfo nprocs=nprocs+1< c14/># Reset procinfo=ordereddict () else: If Len (Line.split (': ')) = = 2: procinfo[line.split (': ') [0].strip ()] = Line.split (': ') [1].strip () Else: procinfo[line.split (': ') [0].strip ()] = ' return ' CPUinfo if __name__== ' __main__ ': CPUinfo = CPUinfo () for processor in Cpuinfo.keys (): print (cpuinfo[ processor][' model name '])
Simply explain listing 1, read the information in the/proc/cpuinfo, and return to the list, one dict per core. Where list is a set of ordered elements enclosed in square brackets. The List can be used as an array starting with the 0 subscript. Dict is one of the built-in data types for Python, which defines the relationship between the key and the value. Ordereddict is a dictionary subclass that remembers the order in which its contents are incremented. Regular dict do not track the insertion order, and the iteration process generates values based on the order in which the keys are stored in the hash table. In Ordereddict, instead, it remembers the order in which elements are inserted and uses that order when creating iterators.
You can run the script using the Python command cpu1.py the results shown in Figure 1
# Python cpu1.py
Intel (R) Celeron (r) CPU E3200 @ 2.40GHz
Figure 1: Running Listing 1
You can also use the chmod command to add permissions to run directly cpu1.py
#chmod +x cpu1.py
#./cpu1.py
For system load Monitoring
Script 2 name cpu2.py, function to get load information of the system
Listing 2 Obtaining Load information for the system
#!/usr/bin/env Python Import Osdef 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 loadavgprint "Loadavg", Load_stat () [' Lavg_15 ']
Simply explain Listing 2: Listing 2 reads the information from the/proc/loadavg, and import Os:python imports the different modules, including the system-provided and custom modules. The basic form is: Import module name [as Alias], if you only need to import some or all of the modules can be in the form: from the module name import * to the corresponding module. The OS Module OS module provides a unified operating system interface function that allows the OS module to automatically switch between specific functions in different operating system platforms, such as Nt,posix, for cross-platform operation.
You can run the script using the python command cpu1.py the results shown in Figure 2 # Python cpu2.py
Figure 2: Running Listing 2
Access to memory information
Script 3 name mem.py, which is used to get memory usage information
Listing 3 Getting memory usage
#!/usr/bin/env Python from __future__ import print_functionfrom collections import Ordereddict def meminfo (): "' Ret Urn the information in/proc/meminfo as a dictionary "' meminfo=ordereddict () with open ('/proc/meminfo ') As F: For line in F: meminfo[line.split (': ') [0]] = line.split (': ') [1].strip () return meminfo if __name__== ' __main__ ': #print (Meminfo ()) meminfo = Meminfo () print (' Total memory: {0} '. Format (meminfo[' Memtotal ']) print (' free memory: {0} '. Format (meminfo[' Memfree '))
A brief explanation of Listing 3: Listing 3 reads the information in Proc/meminfo, and the split method of the Python string is used more frequently or more. For example, we need to store a very long data, and according to the structure of the method of storage, convenient to take the data later processing. Of course, it can be in JSON form. But you can also store the data in a field and then have some kind of identifier to split it. The strip in Python is used to remove the first character of the string, and the last listing 3 prints out the total memory and the number of idle numbers.
You can run the script using the Python command mem.py the results are shown in Figure 3. # Python mem.py
Figure 3: Running Listing 3
Monitoring of network interfaces
The script 4 name is net.py, which functions to obtain the usage of the network interface.
Listing 4 net.py getting the input and output of the network interface
#!/usr/bin/env pythonimport timeimport sys if Len (SYS.ARGV) > 1: INTERFACE = sys.argv[1]else: INTERFACE = ' eth ' 0 ' STATS = []print ' Interface: ', Interface def Rx (): ifstat = open ('/proc/net/dev '). ReadLines () for Interface in Ifstat: If INTERFACE in INTERFACE: stat = float (Interface.split () [1]) stats[0:] = [STAT] def TX (): Ifstat = open ('/proc/net/dev '). ReadLines () for interface in Ifstat: if interface in interface: stat = float (Interface.split () [9]) Stats[1:] = [stat] Print ' in off ' Rx () TX () while True: time.sleep (1) rxstat_o = List (STATS) Rx () TX () rx = float (stats[0]) rx_o = rxstat_o[0] tx = float (stats[1]) tx_o = rxstat_o[ 1] rx_rate = round ((rx-rx_o)/1024/1024,3) tx_rate = Round ((tx-tx_o)/1024/1024,3) print rx_rate, ' mb< c26/> ', tx_rate, ' MB '
A brief description of Listing 4: Listing 4 reads the information in/proc/net/dev, and the file operation in Python can be done through the open function, which is indeed much like the fopen in C. Use the Open function to get a file object, and then call Read (), write (), and so on to read and write the files. It is also very easy for Python to read the contents of a text file into a string variable that can be manipulated. The file object provides three "read" Methods: Read (), ReadLine (), and ReadLines (). Each method can accept a variable to limit the amount of data that is read each time, but they typically do not use variables. Read () reads the entire file each time, and it is typically used to place the contents of the file into a string variable. however. Read () produces the most direct string representation of the file content, but it is unnecessary for continuous row-oriented processing, and it is not possible to implement this processing if the file is larger than the available memory. The difference between ReadLine () and. ReadLines () is that the latter reads the entire file at once , like. Read (): ReadLines () automatically parses the contents of a file into a list of rows that can be used by Python for ... in ... Structure for processing. On the other hand,. ReadLine () reads only one line at a time, usually much slower than. ReadLines (). You should use. ReadLine () only if there is not enough memory to read the entire file at once. The last listing 4 prints out the input and output of the network interface.
You can run the script using the Python command net.py the results shown in Figure 4 #Python net.py
Figure 4: Running Listing 4
Python script to monitor Apache server processes
The Apache server process may exit unexpectedly due to various reasons for the system, causing the Web service to pause. So I write a Python script file:
Listing 5 crtrl.py Python script for monitoring Apache server processes
#!/usr/bin/env pythonimport OS, sys, time while True:time.sleep (4) Try:ret = Os.popen (' ps-c apache-o pid,cmd '). ReadLines ( If Len (ret) < 2:print "Apache process exits unexpectedly, restarts after 4 seconds" time.sleep (3) Os.system ("Service apache2 restart") except:print "Error" , Sys.exc_info () [1]
Set file permissions to execute properties (using command chmod +x crtrl.py), and then join to/etc/rc.local, which automatically checks and restarts once the Apache server process exits abnormally. A brief description of listing 5 This script is not based on the/proc pseudo file system, but is based on some of the modules provided by Python itself. This is the embedded time template for Python, which provides functions for various operating times.
Summarize
In the actual work, the Linux system administrator can write a simple and practical script to monitor the Linux server according to the specific situation of the server he uses. This article describes how to use a Python script to implement a monitoring script for Linux server CPU, System load, memory, and network usage.