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

How to view the docker run startup parameter command (recommended)

Use runlike to view the docker run startup parame...

MySQL query tree structure method

Table of contents MySQL query tree structure 1. A...

Docker image import, export, backup and migration operations

Export: docker save -o centos.tar centos:latest #...

Vue encapsulation component upload picture component

This article example shares the specific code of ...

Sequence implementation method based on MySQL

The team replaced the new frame. All new business...

Detailed steps to install MySql 5.7.21 in Linux

Preface The most widely used database in Linux is...

js realizes the dynamic loading of data by waterfall flow bottoming out

This article shares with you the specific code of...

Some summary of MySQL's fuzzy query like

1. Common usage: (1) Use with % % represents a wi...

JavaScript microtasks and macrotasks explained

Preface: js is a single-threaded language, so it ...

A brief analysis of the difference between ref and toRef in Vue3

1. ref is copied, the view will be updated If you...

4 ways to view processes in LINUX (summary)

A process is a program code that runs in the CPU ...

In-depth explanation of JavaScript this keyword

Table of contents 1. Introduction 2. Understand t...