Python script for Linux Server monitoring tutorial, pythonlinux

Source: Internet
Author: User

Python script for Linux Server monitoring tutorial, pythonlinux

Currently, some Linux system monitoring tools, such as inotify-sync (File System security monitoring software) and glances (resource monitoring tool), are written in Python, the Linux system administrator can compile a simple and practical script to monitor the Linux server based on the specific circumstances of the server in use. This article describes how to write a monitoring script for the CPU memory network of a Linux server using a Python script.
Python version description

Python is a free and advanced interpreted language developed by Guido van rosum. Its syntax is easy to understand, and its object-oriented semantics is powerful (but flexible ). Python is widely used and highly portable. In this article, the Linux server is Ubuntu 12.10 and the Python version is 2.7. For Python 3.0, there are some differences in syntax. In addition, the author mentioned Python as CPython. CPython is a Python interpreter implemented in C language and is also the official and most widely used Python interpreter. In addition to CPython, Jython implemented in Java and IronPython implemented in. NET make Python easy to integrate with Java programs and. NET programs. There are also some experimental Python interpreters such as PyPy. CPython is an interpreter that uses bytecode. Any program source code must be compiled into bytecode before execution. It also has external function interfaces that interact with several other languages (including the C language.
Working principle: Based on/proc file system

The Linux system provides an excellent way for administrators to change the kernel while the system is running without re-booting the kernel system. This is achieved through the/proc Virtual File System. The/proc file virtual system is a mechanism used by the kernel and kernel module to send information to the process (so it is called "/proc "), this pseudo file system allows interaction with the internal data structure of the kernel to obtain useful information about the process. on the fly, you can change the settings (by changing the kernel parameters ). Unlike other file systems,/proc exists in the memory instead of the hard disk. The proc file system provides the following information:

  • Process Information: any process in the system has a process ID with the same name in the proc subdirectory. You can find the cmdline, mem, root, stat, statm, and status. Some information is only visible to super users, such as the process root directory. Each process that contains existing process information has some available dedicated links. Any process in the system has a separate self-link pointing to process information, the command line information is obtained from the process.
  • System Information: If you need to know the system information, you can also obtain it from/proc/stat, including CPU usage, disk space, memory swap, and interruption.
  • CPU information: the current accurate information of the central processor can be obtained using the/proc/CPUinfo file.
  • Load information: The/proc/loadavg file contains the system load information.
  • System memory information: The/proc/meminfo file contains detailed information about the system memory, including the number of physical memory, the number of available swap space, and the number of idle memory.

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

The following examples use the Python script to read the main files in the/proc directory to monitor the Linux server.
Monitor Linux servers using Python scripts
CPU (central processor) Monitoring

Script 1 Name CPU1.py is used to obtain CPU information.
List 1. Obtain CPU Information

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

A brief description of Listing 1 reads the information in/proc/CPUinfo and returns a list with a dict for each core. List is a set of ordered elements enclosed by square brackets. List can be used as an array starting with 0 subscript. Dict is one of Python's built-in data types. It defines the one-to-one relationship between keys and values. OrderedDict is a dictionary subclass that remembers the order in which the content is added. Conventional dict does not track the insertion sequence. During iteration, values are generated based on the order in which keys are stored in the hash table. In OrderedDict, it remembers the order of element insertion and uses this order when creating the iterator.

You can run the script CPU1.py using the Python command. The result is shown in Figure 1.
 
# Python CPU1.py
Intel (R) celeon (R) CPU E3200 @ 2.40 GHz
Figure 1. Running List 1

You can also use the chmod command to add permissions to directly run CPU1.py
 
# Chmod + x CPU1.py
#./CPU1.py
System load monitoring

Script 2 Name CPU2.py, used to obtain the system load information
Listing 2 obtain the system load information
 

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

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 # Python CPU2.py
Figure 2. Running List 2

Obtain memory information

Script 3 name mem. py, used to obtain memory usage information
Listing 3 obtain memory usage
 

#!/usr/bin/env Python from __future__ import print_functionfrom collections import OrderedDict def meminfo():  ''' Return 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 description of listing 3: Listing 3 reads information from proc/meminfo. The split method of Python strings is often used. For example, we need to store a long data and store it in a structured way to facilitate data retrieval and processing in the future. Of course, it can be in json format. However, data can also be stored in a field and separated by some identifier. Strip in Python is used to remove the first character of a string. Listing 3 shows the total memory and idle space.

You can run the script mem. py using the Python command. The result is shown in figure 3. # Python mem. py
Figure 3. Running List 3

Monitoring of Network Interfaces

The name of script 4 is net. py, which is used to obtain the usage of network interfaces.
Listing 4. net. py: Get the input and output of Network Interfaces

#!/usr/bin/env Pythonimport timeimport 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     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'

A brief description of Listing 4: Listing 4 reads the information in/proc/net/dev. File Operations in Python can use the open function, which is indeed similar to fopen in C. Obtain a file object through the open function, and then call the read (), write () and other methods to read and write the object. In addition, it is very easy for Python to read the content of a text file into string variables that can be operated. The file object provides three "read" Methods: read (), readline (), and readlines (). Each method can accept a variable to limit the amount of data read each time, but they usually do not use variables. . Read () reads the entire file every time. It is usually used to put the file content into a string variable. However. read () generates the most direct string representation of the file content, but it is unnecessary for continuous row-oriented processing, and if the file is larger than the available memory, this processing is not possible .. The difference between readline () and. readlines () is that the latter reads the entire file at a time, like. read .. Readlines () automatically analyzes the file content into a row list, which can be composed of Python's... In... Structure. On the other hand,. readline () reads only one row at a time, which is usually much slower than. readlines. Use. readline () only when there is not enough memory to read the entire file at a time (). Listing 4 shows the input and output of the network interface.

You can run the net. py script using the Python command. The result is shown in Figure 4 # Python net. py
Figure 4. Running List 4

Monitor Python scripts of Apache server processes

The Apache server process may exit abnormally due to various system reasons, causing the Web service to be suspended. Therefore, the author writes a Python script file:
Listing 5 Python scripts for monitoring Apache server processes using crtrl. py
 

#! /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 "the apache process exits unexpectedly and restarts 4 seconds later." time. sleep (3) OS. system ("service apache2 restart") failed T: print "Error", sys. exc_info () [1]

Set the File Permission to the execution attribute (use the command chmod + x crtrl. py), and then add it to/etc/rc. local. Once the Apache server process exits unexpectedly, the script is automatically checked and restarted. The script in listing 5 is not based on the/proc pseudo file system and is implemented based on some modules provided by Python. The embedded time template of Python is used here. The time module provides various time functions.
Summary

In practice, the Linux system administrator can write a simple and practical script to monitor the Linux server based on the specific circumstances of the server in use. This article describes how to write a monitoring script for Linux Server CPU, system load, memory, and network usage using a Python script.

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.