ping monitoring script case study

 

Case number one

[root@test opt]# cat /opt/hosts_ip_list
192.168.10.10
192.168.10.11
192.168.10.12
192.168.10.13
192.168.10.14
192.168.10.15
192.168.10.16
192.168.10.17

[root@test opt]# cat /opt/hosts_ip_monit.sh
#!/bin/bash
for ip in $(cat /opt/hosts_ip_list)   
  do
     ping -c 1 $ip &>/dev/null #ping 3 times, when all 3 pings fail, it is determined that the ip network communication fails.
     a=$?
     sleep 2
     ping -c 1 $ip &>/dev/null
     b=$?
     sleep 2
     ping -c 1 $ip &>/dev/null
     c=$?
     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."
         /bin/sed -i 's/^'$ip'/'#$ip'/g' /etc/hosts
     else
         echo "$ip ping is successful."
        /bin/sed -i 's/^'#$ip'/'$ip'/g' /etc/hosts
     be
done

[root@test opt]# chmod 755 /opt/hosts_ip_monit.sh

[root@test opt]# sh /opt/hosts_ip_monit.sh
Date : 2018-04-24 15:49
Host : 192.168.10.10
Problem : Ping is failed.
Date : 2018-04-24 15:50
Host : 192.168.10.11
Problem : Ping is failed.
192.168.10.12 ping is successful.
192.168.10.13 ping is successful.
192.168.10.14 ping is successful.
192.168.10.15 ping is successful.
192.168.10.16 ping is successful.
Date : 2018-04-24 15:51
Host : 192.168.10.17
Problem : Ping is failed.

Case 2

Now requires:
When the ip bound in the /etc/hosts file fails and the ping fails, the binding of the ip will be automatically commented and an email alarm will be sent; if the ip resumes normal communication, the binding setting of the ip will be automatically opened .

[root@cx-app01 ~]# cat /etc/hosts
#192.168.10.10 www.test.com
#192.168.10.11 www.test.com
192.168.10.12 www.test.com
192.168.10.13 www.test.com
192.168.10.14 www.test.com
192.168.10.15 www.test.com
192.168.10.16 www.test.com
#192.168.10.17 www.test.com

[root@cx-app01 ~]# ping www.test.com
PING www.test.com (192.168.10.12) 56(84) bytes of data.
64 bytes from www.test.com (192.168.10.12): icmp_seq=1 ttl=50 time=31.1 ms
64 bytes from www.test.com (192.168.10.12): icmp_seq=2 ttl=50 time=30.7 ms
64 bytes from www.test.com (192.168.10.12): icmp_seq=3 ttl=50 time=30.8 ms
.......

[root@cx-app01 ~]# cat /opt/hosts_ip_list
192.168.10.10
192.168.10.11
192.168.10.12
192.168.10.13
192.168.10.14
192.168.10.15
192.168.10.16
192.168.10.17

[root@cx-app01 ~]# cat /opt/hosts_ip_monit.sh
#!/bin/bash
for ip in $(cat /opt/hosts_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=$?
     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."
         cat /etc/hosts|grep "^#$ip"
         d=$?
           if [ $d -ne 0 ];then
              /bin/bash /opt/sendemail.sh [email protected] "Test the communication between the system and www.test.com" "The connection between $HOSTNAME and $ip failed. Now comment out the ip in the /etc/hosts file. Mapping relations"
              /bin/bash /opt/sendemail.sh [email protected] "Test the communication between the system and www.test.com" "The connection between $HOSTNAME and $ip failed. Now comment out the ip in the /etc/hosts file. Mapping relations"
              /bin/bash /opt/sendemail.sh [email protected] "Test the communication between the system and www.test.com" "The connection between $HOSTNAME and $ip failed. Now comment out the ip in the /etc/hosts file. Mapping relations"
              /bin/sed -i 's/^'$ip'/'#$ip'/g' /etc/hosts
           else
              echo "$ip is not conneted,and it has been done"
           be
     else
         echo "$ip ping is successful."
         cat /etc/hosts|grep "^#$ip"
         f=$?
           if [ $f -eq 0 ];then
              /bin/bash /opt/sendemail.sh [email protected] "Test the communication between the system and www.test.com" "The connection between $HOSTNAME and $ip is successful, and the mapping of this ip has been restored in the /etc/hosts file. relation"
              /bin/bash /opt/sendemail.sh [email protected] "Test the communication between the system and www.test.com" "The connection between $HOSTNAME and $ip is successful, and the mapping of this ip has been restored in the /etc/hosts file. relation"
              /bin/bash /opt/sendemail.sh [email protected] "Test the communication between the system and www.test.com" "The connection between $HOSTNAME and $ip is successful, and the mapping of this ip has been restored in the /etc/hosts file relation"
              /bin/sed -i 's/^'#$ip'/'$ip'/g' /etc/hosts
           else
              echo "$ip connection has been restored"
           be
     be
done


Use sendemail to send email alerts, sendemail deployment reference: http://www.cnblogs.com/kevingrace/p/5961861.html
[root@cx-app01 ~]# cat /opt/sendemail.sh
#!/bin/bash
# Filename: SendEmail.sh
# Notes: use sendEmail
#
# script log file
LOGFILE="/tmp/Email.log"
:>"$LOGFILE"
exec 1>"$LOGFILE"
exec 2>&1
SMTP_server='smtp.test.com'
username='[email protected]'
password='monit@123'
from_email_address='[email protected]'
to_email_address="$1"
message_subject_utf8="$2"
message_body_utf8="$3"
# Convert the email title to GB2312 to solve the problem that the email title contains Chinese, and the received email displays garbled characters.
message_subject_gb2312=`iconv -t GB2312 -f UTF-8 << EOF
$message_subject_utf8
EOF`
[ $? -eq 0 ] && message_subject="$message_subject_gb2312" || message_subject="$message_subject_utf8"
# Convert the content of the email to GB2312 to solve the garbled content of the received email
message_body_gb2312=`iconv -t GB2312 -f UTF-8 << EOF
$message_body_utf8
EOF`
[ $? -eq 0 ] && message_body="$message_body_gb2312" || message_body="$message_body_utf8"
# send email
sendEmail='/usr/local/bin/sendEmail'
set -x
$sendEmail -s "$SMTP_server" -xu "$username" -xp "$password" -f "$from_email_address" -t "$to_email_address" -u "$message_subject" -m "$message_body" -o message-content-type=text -o message-charset=gb2312


Execute the monitoring script regularly every 10 minutes
[root@cx-app01 ~]# crontab -l
*/10 * * * *  /bin/bash -x /opt/hosts_ip_monit.sh > /dev/null 2>&1

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324779733&siteId=291194637