Linux service monitoring and operation and maintenance

Linux service monitoring and operation and maintenance

1. Install the psutil package

1.1. Install the compressed package: Log in to CentOS as root and execute the following commands in sequence:

wget https://pypi.python.org/packages/source/p/psutil/psutil-2.1.3.tar.gz

If wget cannot download it, copy and paste the link https://pypi.python.org/packages/source/p/psutil/psutil-2.1.3.tar.gz directly in the browser to download it, use the ftp transfer tool to transfer it to Linux, and then perform the following operations

1.2 Unzip: tar zxvf psutil-2.1.3.tar.gz

1.3 Enter the unzipped directory: cd psutil-2.1.3/

1.4 Start the installation: python3 setup.py install install

1.5 Verification: Enter python3 in the command window, then enter:

If import psutil does not report an error, the installation is successful.

Then enter:

res = psutil.process_iter()

for var in res:

print(var)

A bunch of information will be displayed

Second, find the name and pid of all services in the operating system

import psutil
proc_dict = {}
#ID: process name proc_name = set()
#About the process set, set deduplication for p in psutil.process_iter():
    proc_dict[p.pid] = p.name()
    proc_name.add(p.name())
print(proc_dict)
print('\n\n--------------------------\n')
print(proc_name)


3. Find the service that needs to be monitored but is not started

proc_stop = monitor_name - proc_name

That is: the name of the service to be monitored minus the names of all services in the operating system

4. Monitoring service, if it is not enabled, it will be enabled automatically, and an email will be sent to notify the staff.

1. Enable the service using os.system(command).
The complete code is as follows:

import psutil
import os
#If you need to send an email to notify the administrator, you may need to use request and json
#import request
#import json
import time

time_now = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
#Service to be monitored monitor_name = {'nginx'}
#Start command monitor_map = {'nginx':'service nginx start'}

while True:
    #Dictionary of all processes in the operating system proc_dict = {}

    #Set of all process names in the operating system proc_name = set()

    #psutil.process_iter() will return all process names and process IDs in the operating system
    for p in psutil.process_iter():
        proc_dict[p.pid] = p.name()
        proc_name.add(p.name())
    print(proc_dict)
    print('\n\n--------------------------\n')
    print(proc_name)

    # monitor_name - proc_name == died process name
    #Extract the monitoring process that has not started proc_stop = monitor_name - proc_name
    print(proc_stop)
    if proc_stop:
        for p in proc_stop:
             p_status = 'Stop'
             p_name = p
             data ={p_status,p_name,time_now}
             headers = {'Content-Type':'application/json;charset=utf-8'}
             #send_data = json.dumps(data).encode('utf-8')
             #Send an email to the administrator#request.post(url=url,data=send_data,headers=headers)

             os.system(monitor_map[p])
             proc_name = set()

             for p2 in psutil.process_iter():
                 proc_name.add(p2.name())

             if p in proc_name:
                  print("Restart successful")
              else:
                  print("Restart failed")
 time.sleep(2000)

The effect after running:

This is the end of this article about Linux service monitoring and operation and maintenance. For more relevant Linux service content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the implementation method of Linux monitoring important processes
  • Shell script to implement Linux system and process resource monitoring
  • Simple implementation method of Linux process monitoring and automatic restart
  • Linux operation and maintenance basic process management real-time monitoring and control

<<:  CSS realizes the speech bubble effect with sharp corners in the small sharp corner chat dialog box

>>:  The specific implementation of div not automatically wrapping and forcing not wrapping in html

Recommend

25 Ways and Tips to Increase Web Page Loading Speed

Introduction <br />Not everyone has access t...

Binary installation of mysql 5.7.23 under CentOS7

The installation information on the Internet is u...

Solve the problem of ifconfig being unavailable in docker

Recently, when I was learning docker, I found tha...

Several ways to update batches in MySQL

Typically, we use the following SQL statement to ...

Realize super cool water light effect based on canvas

This article example shares with you the specific...

How to query whether the mysql table is locked

Specific method: (Recommended tutorial: MySQL dat...

Centos8 builds nfs based on kdc encryption

Table of contents Configuration nfs server (nfs.s...

CSS realizes the layout method of fixed left and adaptive right

1. Floating layout 1. Let the fixed width div flo...

Common styles of CSS animation effects animation

animation Define an animation: /*Set a keyframe t...

Two methods of MySql comma concatenation string query

The following two functions are used in the same ...