Linux basics-use shell scripts to implement automatic monitoring of system services, linuxshell

Source: Internet
Author: User

Linux basics-use shell scripts to implement automatic monitoring of system services, linuxshell

Purpose: To monitor whether the nginx and nfs services in the cluster are running normally. If any service is abnormal, an email is sent to the user.

Condition: 1. the IP address of the host and sub-host, hostname, has been determined;

2. Password-free communication between the host and the sub-host, that is, key-based communication (related commands: ssh-keygen; ssh-copy-id-I web1 );

Required files:

1. python mail sending tool;

2. nfc. sh monitors scripts, monitors nginx and nfs service statuses, and calls the mail sending tool to notify users;

3. nfc-install.sh monitoring deployment script, run on the host, configure the file for the Child machine, execute the command;

     

Code details:

1. Email sending Tool

Create the following code in the "/usr/bin/mail" file and grant the execution permission (chmod + x/usr/bin/mail)

#! /Usr/bin/python #-*-coding: UTF-8-*-import sysimport smtplibimport email. mime. multipartimport email. mime. textserver = 'smtp .163.com 'port = '25' def sendmail (server, port, user, pwd, msg): smtp = smtplib. SMTP () smtp. connect (server, port) smtp. login (user, pwd) smtp. sendmail (msg ['from'], msg ['to'], msg. as_string () smtp. quit () print ('email sent successfully email has send out! ') If _ name _ =' _ main _ ': msg = email. mime. multipart. MIMEMultipart () msg ['subobject'] = 'Check your service of nginx and nfs 'msg ['from'] = 'python4 _ mail@163.com' msg ['to'] = 'python4 _ recvmail@163.com' user = 'python4 _ mail' pwd = '000000' content = '% s \ n % s' % (' \ n '. join (sys. argv []), ''. join (sys. argv [4:]) # format processing, specifically for our mail format txt = email. mime. text. MIMEText (content, _ charset = 'utf-8') msg. attach (txt) sendmail (server, port, user, pwd, msg)
View Code

Failed to send mail via SMTP in python:
Error 1: smtplib. SMTPAuthenticationError: (550, B 'user has no permission ')
When we use python to send an email, it is equivalent to customizing the client to log on based on the user name and password, and then sending an email using the SMTP service, the client Authorization is disabled by default for the newly registered 163 mailbox (enabled for the specified master mailbox client by default). Therefore, logon is always rejected. solution (take the 163 mailbox as an example ): go to 163 mailbox-settings-client Authorization password-enable (authorization code is the dedicated password used to log on to the third-party mail client)
Error 2: smtplib. SMTPAuthenticationError: (535, B 'Error: authentication failed ')
Take mailbox 163 as an example. When the POP3/SMTP service is enabled and the client Authorization password is enabled, an authorization code is set to replace smtplib with this authorization code. SMTP (). password in the login (user, password) method.

2. nfc. sh monitoring script

#! /Bin/bash # nginx and nfs service monitoring script. If an exception occurs, an email will be sent to function monitor_nfc () {systemctl status nginxnginx =$? Systemctl status nfsnfs =$? Clearif [$ nginx-eq 0] & [$ nfs-eq 0] then msg = "TIME: $ (date + % F _ % T) HOSTNAME: $ (hostname) IPADDR: $ (ifconfig | awk 'nr = 2 {print $2} ') MSG: nginx. service and nfs. service is both running "echo msg #/usr/bin/mail $ msg # The service is running normally, do not send an email notification to elif [$ nginx-ne 0] & [$ nfs-eq 0] then msg = "TIME: $ (date + % F _ % T) HOSTNAME: $ (hostname) IPADDR: $ (ifconfig | awk 'nr = 2 {print $2} ') MSG: nginx. service is dead, nfs. service is running "echo $ msg/usr/bin/mail $ msgelif [$ nginx-ne 0] & [$ nfs-ne 0] then msg =" TIME: $ (date + % F _ % T) HOSTNAME: $ (hostname) IPADDR: $ (ifconfig | awk 'nr = 2 {print $2} ') MSG: nginx. service and nfs. service is both dead "echo $ msg/usr/bin/mail $ msgelif [$ nginx-eq 0] & [$ nfs-ne 0] then msg =" TIME: $ (date + % F _ % T) HOSTNAME: $ (hostname) IPADDR: $ (ifconfig | awk 'nr = 2 {print $2} ') MSG: nginx. service is running, nfs. service is dead "echo $ msg/usr/bin/mail $ msgfi} monitor_nfc &>/tmp/monitor. log
View Code

3. nfc-install monitoring deployment script

#! /Bin/bash # first execute the nfc of the host. sh service monitoring script/root/nfc. sh # then send the host's service monitoring script nfc. sh and send the email file to the web machine for I in {134,135,136} doscp/root/nfc. sh 192.168.47. $ I:/share/# Send the host's service monitoring script nfc. sh upload to scp/usr/bin/mail 192.168.47 on the web server. $ I:/usr/bin/# upload the mail file to the web server ssh root@192.168.47. $ I chmod + x/share/nfc. sh # Add nfc Script File Execution permission ssh root@192.168.47. $ I chmod + x/usr/bin/mail # added the Execute Permission for sending mail files to ssh root@192.168.47. $ I/share/nfc. sh # execute the nfc script monitoring function donessh 192.168.47.htm # finally return to the Host terminal
View Code

For details, see the image.

Result:

Host

Sub-machine 1

 

 

 

References

1. http://www.cnblogs.com/linhaifeng/p/6602149.html

Related Article

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.