A tutorial on using Python scripts to monitor Linux servers _python

Source: Internet
Author: User
Tags chmod readline file permissions python script in python

Currently Linux has some Linux system monitoring tools written in the Python language such as Inotify-sync (file system security monitoring software), glances (resource monitoring tools) in the actual work, Linux The system administrator can write a simple and practical script to monitor the Linux server according to the specific situation of the server that they use. This article describes the use of Python scripts to implement a Linux server CPU memory network of the script to write.
Python Version Description

Python is a very advanced interpretive language developed by Guido van Rossum and is available for free. Its syntax is simple and understandable, and its object-oriented semantics are powerful (but flexible). Python can be widely used and highly portable. This article Linux server is Ubuntu 12.10, Python version is 2.7. If there is a certain discrepancy in the syntax of the Python 3.0 version. In addition, Python, as described here, is Cpython,cpython, a Python interpreter implemented in C, and is the official and most widely used Python interpreter. In addition to CPython, it also uses the Jython of Java implementations and IronPython implemented with. NET, enabling Python to easily integrate with Java programs and. NET programs. There are also 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.
working principle: Based on/proc file system

The Linux system provides administrators with a very good way to change the kernel while the system is running without rebooting the kernel system, which is implemented 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 processes (so called "/proc"), which allows you to interact with the internal data structure of the kernel to get useful information about the process, which is running (on the Fly) Change the settings (by changing the kernel parameters). Unlike other file systems,/proc is present in memory rather than 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 subdirectory of proc, can find CmdLine, mem, root, Stat, STATM, and status. Some information is visible only to superuser, such as the process root directory. Each process that contains information about existing processes has a few specialized links available, and any process in the system has a separate link to process information that is useful for obtaining command-line information from the process.
    • System Information: If you need to know the entire system information can also be obtained from the/proc/stat, including CPU consumption, disk space, memory swap, interrupt, and so on.
    • CPU information: Use the/proc/cpuinfo file to obtain the current accurate information of the central processing Unit.
    • Load Information:/proc/loadavg file contains system load information.
    • System memory Information: The/proc/meminfo file contains detailed information about the system's memory, showing the number of physical memory, the amount of available swap space, and the amount of free memory.

Table 1 is a description of the main files in the/proc directory:
Description of the primary file in the table 1/proc directory

Here are a few examples of using Python scripts to read the main files in the/proc directory to implement the monitoring of Linux servers.
using Python scripts to monitor Linux servers
For CPU (central processing unit) monitoring

Script 1 Name cpu1.py, the role of the CPU to obtain information.
Listing 1. Get CPU Information

 #!/usr/bin/env Python from __future__ import print_function from collections import Ordere Ddict Import Pprint def CPUinfo (): "Return" information In/proc/cpuinfo as a dictionary in the following form
  at: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=NPR Ocs+1 # 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 () to processor in Cpuinfo.keys (): Print (cpuinfo[ processor][' model name ') 

Simply explain listing 1, read the information in/proc/cpuinfo, and return to list, a dict per core. Where list is a collection of ordered elements enclosed in square brackets. The List can be used as an array starting with a 0 subscript. Dict is one of the built-in data types of Python, which defines a one-to-one relationship between keys and values. Ordereddict is a dictionary subclass that remembers the order in which content is added. Regular dict do not track the insertion order, and iterations generate values based on the order in which the keys are stored in the hash table. In Ordereddict, in contrast, it remembers the order in which elements are inserted and uses this order when creating iterators.

You can run the script using the Python command cpu1.py results See figure 1

# Python cpu1.py
Intel (R) Celeron (R) CPU E3200 @ 2.40GHz
Figure 1. Run 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, action to get system load information
Listing 2 Gets the load information for the system

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

In a nutshell, Listing 2: Listing 2 reads the information in/proc/loadavg, and 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 see Figure 2 # Python cpu2.py
Figure 2. Run Listing 2

For access to memory information

Script 3 name mem.py to get memory usage information
Listing 3 Getting memory usage

#!/usr/bin/env Python from
 
__future__ import print_function from
collections import Ordereddict
 
def Meminfo (): ' Return to 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 '])

In a nutshell, Listing 3: Listing 3 reads the information in Proc/meminfo, and the split method of the Python string is more frequently used. For example, we need to store a very long data, and in accordance with the structure of the method of storage, to facilitate the subsequent processing of data. Of course you can use the form of JSON. But you can also store the data in a field, and then have some sort of identifier to split. The strip in Python is used to remove the first character of the string, and the last listing 3 prints out the total number of memory and the number of idle.

You can run the script using the Python command mem.py the results are shown in Figure 3. # Python mem.py
Figure 3. Run Listing 3

For the network interface monitoring

The script 4 name is net.py to get the usage of the network interface.
Listing 4 net.py Gets the input and output of the network interface

#!/usr/bin/env Python
Import time
import sys
 
if Len (SYS.ARGV) > 1:
  INTERFACE = sys.argv[1]
Else :
  INTERFACE = ' eth0 '
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< C23/>out '
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 '   , tx_rate, ' MB '

In a nutshell, 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 really like the fopen in C language. Get a file object by using the Open function, and then call Read (), write (), and so on to read and write files. In addition Python is easy 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 read at a time, but they usually do not use a variable. read () reads the entire file at a time, and is typically used to place the contents of the file in a string variable. however. Read () generates the most direct string representation of a file's content, but it is unnecessary for sequential row-oriented processing, and is not possible if the file is larger than available memory. The difference between ReadLine () and. ReadLines () is the latter reading the entire file at a time , like. Read (). ReadLines () automatically parse the contents of a file into a list of rows, which can be made up of Python's for ... in ... Structure for processing. On the other hand,. ReadLine () reads only one row at a time, usually much slower than. ReadLines (). You should use the. ReadLine () only if there is not enough memory to read the entire file at once. The final listing 4 prints out the input and output of the network interface.

You can run the script using the Python command net.py results are shown in Figure 4 #Python net.py
Figure 4. Run Listing 4

Python script to monitor the Apache server process

The Apache server process may exit unexpectedly because of various reasons for the system, causing the Web service to pause. So I write a Python script file:
Listing 5 crtrl.py Python script to monitor the Apache server process

#!/usr/bin/env Python
import OS, sys, time is
 
True:
time.sleep (4)
try:
ret = Os.popen (' ps-c Apache-o pid,cmd '). ReadLines ()
If Len (ret) < 2:
print "Apache process quits unexpectedly, reboot in 4 seconds"
Time.sleep (3)
Os.system ("Service apache2 restart")
except:
print "Error", Sys.exc_info () [1]

Set file permissions to execute properties (using the command chmod +x crtrl.py), and then add to/etc/rc.local, and once the Apache server process exits unexpectedly, the script automatically checks and restarts. The simple explanation for listing 5 is that the script is not based on the/proc pseudo file system, but is based on some of the modules provided by Python itself. Here is Python's inline time template, which provides functions for various operating times.
Summary

In practice, Linux system administrators can write a simple and practical script to monitor the Linux server based on the specifics of the server they use. This article describes how to use a Python script to implement a monitoring script for Linux server CPU, System load, memory, and network usage.

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.