Monitor the running status of the server cluster through shell scripts and trigger email alerts

Monitor the running status of server clusters and trigger email alerts

In the production environment, the sudden server cluster downtime often causes the service to fail to run normally. Use the shell script to monitor the running status of the server cluster in real time and send an email warning to the operation and maintenance personnel for timely processing after the server downtime is detected.

1. Install the mail client

yum -y install sendmail

yum -y install mailx

1.1 Configure mail mail.rc

set from=**@qq.com

set [email protected]

set smtp=smtp.qq.com

set smtp-auth-password=your-password

set stmp-auth=login

1.2 Parameter description

  • from: The sender displayed when the recipient receives the email
  • smtp: Specify the smtp server address of the third party to send mail
  • smtp-auth-user: The username of the third party to send emails
  • smtp-auth-password: username corresponding password (email authorization code)
  • smtp-auth: SMTP authentication method. The default is LOGIN, and it can also be changed to CRAM-MD5 or PLAIN

Two, write the monitoring shell script ping_server.sh

#!/bin/bash
Date=`date -d "today" +"%Y-%m-%dT%H-%M-%S"`
#echo "根据当前时间创建日志文件"
mkdir -p /log/Ping/ping_server
touch /log/Ping/${Date}_ping_server.log
ip_list="192.168.10.100 192.168.10.101 192.168.10.102 192.168.10.103 192.168.10.104 "
for ip in ${ip_list}
  do
     ping -c 1 $ip &>/dev/null
     a=$?
     sleep 2
     ping -c 1 $ip &>/dev/null
     b=$?
     sleep 2
     ping -c 1 $ip &>/dev/null
     c=$?                                     #三次循环中有一次ping通及通
     sleep 2
     DATE=$(date +%F" "%H:%M)
     if [ $a -ne 0 -a $b -ne 0 -a $c -ne 0 ];then
         echo -e "Date : $DATE\nHost : $ip\nProblem : Ping is failed."
         echo -e "Date : $DATE\nHost : $ip\nProblem : Ping is failed 该服务器运行状态异常,请及时排查" >> /log/Ping/${Date}_ping_server.log
     #else
     #    echo "$ip ping is successful."
     fi
done
if [ -s /log/Ping/${Date}_unnormal.log ];then
  echo "不为空,发送邮件"
  #/usr/bin/mail -s " Server Status " *******@qq.com  < /log/Ping/${Date}_ping_server.log
  /usr/bin/mail -s " Server Status " ******@qq.com  < /log/Ping/${Date}_ping_server.log
else
  echo "为空,不发送邮件"
fi

Third, the script execution results are shown in

Test an email alert as follows:

Four, crontab adds timing tasks

4.1 Install crontab service

Execute the following two commands to install
yum -y install vixie-cron
yum -y install crontabs

The installation is complete, use the following commands to enable and disable scheduled tasks, and view the status of crontab

service crond start #Start service
service crond stop #Close service
service crond restart #Restart service
service crond reload #Reload configuration
service crond status #View crontab service status

4.2 Add crontab timing task list

Enter the command: crontab -e

Add the scheduled task script to it

2 * * * * ./data/ping_server.sh # Execute the ping_server.sh script every two minutes to detect the server status

Guess you like

Origin blog.csdn.net/qq_52497256/article/details/128937574