Objective: To monitor the operation of Nginx and NFS services in the cluster, such as any service exception, send an email to notify the user
Condition: 1. Host and the IP address of the sub-machine, hostname has been determined;
2. The host and the sub-machine can be free of dense communication, that is, based on key communication (related commands: Ssh-keygen;ssh-copy-id-i web1);
Required Files:
1. Python Mail sending tool;
2. nfc.sh monitor script, monitor Nginx and NFS service status, and call Mail Sending tool to notify users;
3. nfc-install.sh Monitor deployment scripts, run on host, configure files for sub-machine, execute commands;
Detailed code:
1. Mail Sending tool
Create the following code in the "/usr/bin/mail" file and give Execute permission (chmod +x/usr/bin/mail)
#!/usr/bin/python#-*-coding:utf-8-*-ImportSYSImportSmtplibImportEmail.mime.multipartImportEmail.mime.textserver='smtp.163.com'Port=' -'defSendMail (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('Mail sent successfully email has send out!')if __name__=='__main__': Msg=Email.mime.multipart.MIMEMultipart () msg['Subject'] ='Check your service of Nginx and NFS'msg[' from'] ='[email protected]'msg[' to'] ='[email protected]'User='Python4_mail'pwd='123456789'content='%s\n%s'%('\ n'. Join (Sys.argv[1:4]),' '. Join (Sys.argv[4:]))#format processing, specifically for our message formattxt= Email.mime.text.MIMEText (Content, _charset='Utf-8') Msg.attach (TXT) sendmail (server,port,user,pwd,msg)
View Code
Python failed to send mail via SMTP:
Error 1:SMTPLIB. Smtpauthenticationerror: (550, B ' User has no permission ')
When we use Python to send a message, the custom client logs in based on the user name and password, and then uses the SMTP service to send mail, and the newly registered 163 mailbox is not turned on by default (enabled by default for the specified mailbox master client), so the login is always denied. Workaround (Take 163 mailboxes for example): Enter 163 mailbox-Setup-Client authorization password-on (authorization code is a private password used to log in to a third-party mail client)
Error 2:SMTPLIB. Smtpauthenticationerror: (535, B ' error:authentication failed ')
Take 163 mailbox As an example, the authorization code will be set when the POP3/SMTP service is turned on and the client authorization password is turned on, instead of Smtplib. Password in the SMTP (). Login (User,password) method.
2. nfc.sh Monitoring Scripts
#! /bin/Bash#nginx and NFS service monitoring scripts, if exceptions, will send mail notificationfunctionMONITOR_NFC () {systemctl status Nginxnginx=$?systemctl Status Nfsnfs=$?Clearif[$nginx-eq0] && [$nfs-eq0 ] Thenmsg="time:$ (date +%f_%t)hostname:$ (hostname) ipaddr:$ (ifconfig|awk 'Nr==2{print $}') MSG:nginx.service and Nfs.service is both running" Echomsg#/usr/bin/Mail $msg #服务运行正常, do not send email notificationselif[$nginx-ne0] && [$nfs-eq0 ] Thenmsg="time:$ (date +%f_%t)hostname:$ (hostname) ipaddr:$ (ifconfig|awk 'Nr==2{print $}') MSG:nginx.service is Dead,nfs.service is running" Echo$msg/usr/bin/Mail $msgelif[$nginx-ne0] && [$nfs-ne0 ] Thenmsg="time:$ (date +%f_%t)hostname:$ (hostname) ipaddr:$ (ifconfig|awk 'Nr==2{print $}') MSG:nginx.service and Nfs.service is both dead" Echo$msg/usr/bin/Mail $msgelif[$nginx-eq0] && [$nfs-ne0 ] Thenmsg="time:$ (date +%f_%t)hostname:$ (hostname) ipaddr:$ (ifconfig|awk 'Nr==2{print $}') 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 scripts
#! /bin/bash# First executes the host's nfc.sh service monitoring script/ROOT/NFC.SH#然后将主机的服务监控脚本nfc. SH and send mail file to Web machine forIinch{134,135,136} DoSCP/ROOT/NFC.SH 192.168. -. $i:/share/#将主机的服务监控脚本nfc. SH upload to Web machineSCP/usr/bin/mail192.168. -. $i:/usr/bin/#将发送邮件文件上传至web机器SSH[Email protected]192.168. -. $ichmod+X/SHARE/NFC.SH#增加nfc脚本文件的执行权限SSH[Email protected]192.168. -. $ichmod+x/usr/bin/Mail #增加发送邮件文件的执行权限SSH[Email protected]192.168. -. $i/SHARE/NFC.SH#执行nfc脚本监控功能 DoneSSH 192.168.47.133#最终回到主机终端
View Code
See image
Results:
Host
Sub-machine 1
The above is the Linux Foundation-the use of shell scripts to implement automatic monitoring system services of all content, more attention: CPP Learning Network _cpp University
This article fixed link: CPP Learning Network _cpp University-linux Foundation-using shell script to implement automatic monitoring system services
Linux Basics-Automatic monitoring of system services with shell scripts