基於Ping和Telnet/NC的監控指令碼案例分析

散盡浮華發表於2018-04-24

 

案例一:單純地對某些ip進行ping監控

[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次,當3次ping都失敗時,則判定此ip網路通訊失敗。
     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
     fi
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.

案例二:對/etc/hosts列表裡的ip對映關係進行ping監控報警

測試系統伺服器需要訪問域名www.test.com,該域名解析的DNS地址有很多個,需要在測試系統伺服器上的做host繫結。在/etc/hosts檔案了做了www.test.com域名的很多繫結,
在域名解析時,會從host繫結配置裡從上到下匹配,如果上面繫結的ip不通,則域名解析就會失敗,不會主動去解析到下一個繫結的地址,除非將這個不通的ip繫結註釋掉或刪除掉。

現在要求:
當/etc/hosts檔案裡繫結的ip出現故障,ping不通的時候,將該ip的繫結自動註釋,併發出郵件報警;如果該ip恢復了正常通訊,將自動開啟該ip的繫結設定。
 
[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 zhangsan@test.com "測試系統跟www.test.com通訊情況" "$HOSTNAME跟$ip連線失敗,現已在/etc/hosts檔案裡註釋掉該ip的對映關係"
              /bin/bash /opt/sendemail.sh lisi@test.com "測試系統跟www.test.com通訊情況" "$HOSTNAME跟$ip連線失敗,現已在/etc/hosts檔案裡註釋掉該ip的對映關係"
              /bin/bash /opt/sendemail.sh liuwu@test.com "測試系統跟www.test.com通訊情況" "$HOSTNAME跟$ip連線失敗,現已在/etc/hosts檔案裡註釋掉該ip的對映關係"
              /bin/sed -i 's/^'$ip'/'#$ip'/g' /etc/hosts
           else
              echo "$ip is not conneted,and it has been done"
           fi
     else
         echo "$ip ping is successful."
         cat /etc/hosts|grep "^#$ip"
         f=$?
           if [ $f -eq 0 ];then
              /bin/bash /opt/sendemail.sh zhangsan@test.com "測試系統跟www.test.com通訊情況" "$HOSTNAME跟$ip連線成功,現已在/etc/hosts檔案裡恢復該ip的對映關係"
              /bin/bash /opt/sendemail.sh lisi@test.com "測試系統跟www.test.com通訊情況" "$HOSTNAME跟$ip連線成功,現已在/etc/hosts檔案裡恢復該ip的對映關係"
              /bin/bash /opt/sendemail.sh liuwu@test.com "測試系統跟www.test.com通訊情況" "$HOSTNAME跟$ip連線成功,現已在/etc/hosts檔案裡恢復該ip的對映關係"
              /bin/sed -i 's/^'#$ip'/'$ip'/g' /etc/hosts
           else
              echo "$ip connection has been restored"
           fi
     fi
done
 
 
採用sendemail進行郵件告警傳送,sendemail部署參考:http://www.cnblogs.com/kevingrace/p/5961861.html
[root@cx-app01 ~]# cat /opt/sendemail.sh
#!/bin/bash
# Filename: SendEmail.sh
# Notes: 使用sendEmail
#
# 指令碼的日誌檔案
LOGFILE="/tmp/Email.log"
:>"$LOGFILE"
exec 1>"$LOGFILE"
exec 2>&1
SMTP_server='smtp.test.com'
username='monit@test.com'
password='monit@123'
from_email_address='monit@test.com'
to_email_address="$1"
message_subject_utf8="$2"
message_body_utf8="$3"
# 轉換郵件標題為GB2312,解決郵件標題含有中文,收到郵件顯示亂碼的問題。
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"
# 轉換郵件內容為GB2312,解決收到郵件內容亂碼
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"
# 傳送郵件
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
 
 
每10分鐘定時執行該監控指令碼
[root@cx-app01 ~]# crontab -l
*/10 * * * *  /bin/bash -x /opt/hosts_ip_monit.sh > /dev/null 2>&1

案例三:通過nc工具對/etc/hosts列表裡的ip的443埠跟本機通訊是否正常進行探測

案例二是針對ping編寫的監控指令碼,下面介紹下利用nc探測埠通訊是否正常的指令碼:

探測本機對下面/etc/hosts檔案裡的ip地址的443埠通訊是否正常,如果通訊失敗,則發出報警,並在/etc/hosts檔案裡註釋掉該ip地址的繫結關係。
如果註釋掉的ip的443埠跟本機恢復了通訊,則去掉/etc/hosts檔案裡該ip的註釋!

[root@cx-app01 ~]# cat /etc/hosts
192.168.10.201 www.test.com
192.168.10.205  www.test.com
192.168.10.17  www.test.com
192.168.10.85  www.test.com
192.168.10.176   www.test.com
192.168.10.245  www.test.com
192.168.10.25    www.test.com
192.168.10.47  www.test.com

[root@cx-app01 ~]# cat /opt/hosts_ip_list 
192.168.10.201
192.168.10.205
192.168.10.17
192.168.10.85
192.168.10.176
192.168.10.245
192.168.10.25
192.168.10.47

採用nc工具去探測埠是否正常通訊(yum install -y nc)
[root@cx-app01 ~]# /usr/bin/nc -z  -w 10 192.168.10.201 443
Connection to 192.168.10.201 443 port [tcp/https] succeeded!

針對上面ip列表裡的地址,進行批量ip的443埠通訊的探測。指令碼如下:
[root@cx-app01 ~]# cat /opt/host_ip_nc_monit.sh 
#!/bin/bash
for ip in $(cat /opt/hosts_ip_list)  
do
    echo -e "Date : $DATE\nHost : $ip\nProblem : Port 443 is connected."
    cat /etc/hosts|grep "^#$ip" 
    a=$?
    if [ $a -ne 0 ];then
       /usr/bin/nc -z  -w 10 $ip 443
       b=$?
       if [ $b -ne 0 ];then
          /bin/bash /opt/sendemail.sh wangshibo@test.com "測試系統跟www.test.com通訊情況" "$HOSTNAME跟$ip的443埠連線失敗,現已在/etc/hosts檔案裡註釋掉該ip的對映關係"
          /bin/bash /opt/sendemail.sh linan@test.com "測試系統跟www.test.com通訊情況" "$HOSTNAME跟$ip的443埠連線失敗,現已在/etc/hosts檔案裡註釋掉該ip的對映關係"
          /bin/sed -i 's/^'$ip'/'#$ip'/g' /etc/hosts
       else
       echo "$HOSTNAME跟$ip的443埠正常連線"
       fi
    else
       /usr/bin/nc -z  -w 10 $ip 443
       c=$?
       if [ $c -eq 0 ];then
         /bin/bash /opt/sendemail.sh wangshibo@test.com "測試系統跟www.test.com通訊情況" "$HOSTNAME跟$ip的443埠連線成功,現已在/etc/hosts檔案裡恢復該ip的對映關係"
         /bin/bash /opt/sendemail.sh linan@test.com "測試系統跟www.test.com通訊情況" "$HOSTNAME跟$ip的443埠連線成功,現已在/etc/hosts檔案裡恢復該ip的對映關係"
         /bin/sed -i 's/^'#$ip'/'$ip'/g' /etc/hosts
       else
         echo "$HOSTNAME跟$ip的443埠連線失敗"
       fi
    fi
done

給指令碼賦權
[root@cx-app01 ~]# chmod 755 /opt/host_ip_nc_monit.sh

執行指令碼:
[root@cx-app01 ~]# sh /opt/host_ip_nc_monit.sh
Date : 
Host : 192.168.10.201
Problem : Port 443 is connected.
Connection to 192.168.10.201 443 port [tcp/https] succeeded!
cx-app01.veredholdings.cn跟192.168.10.201的443埠正常連線
Date : 
Host : 192.168.10.205
Problem : Port 443 is connected.
Connection to 192.168.10.205 443 port [tcp/https] succeeded!
cx-app01.veredholdings.cn跟192.168.10.205的443埠正常連線
Date : 
Host : 192.168.10.17
Problem : Port 443 is connected.
Connection to 192.168.10.17 443 port [tcp/https] succeeded!
cx-app01.veredholdings.cn跟192.168.10.17的443埠正常連線
Date : 
Host : 192.168.10.85
Problem : Port 443 is connected.
Connection to 192.168.10.85 443 port [tcp/https] succeeded!
cx-app01.veredholdings.cn跟192.168.10.85的443埠正常連線
Date : 
Host : 192.168.10.176
Problem : Port 443 is connected.
Connection to 192.168.10.176 443 port [tcp/https] succeeded!
cx-app01.veredholdings.cn跟192.168.10.176的443埠正常連線
Date : 
Host : 192.168.10.245
Problem : Port 443 is connected.
Connection to 192.168.10.245 443 port [tcp/https] succeeded!
cx-app01.veredholdings.cn跟192.168.10.245的443埠正常連線
Date : 
Host : 192.168.10.25
Problem : Port 443 is connected.
Connection to 192.168.10.25 443 port [tcp/https] succeeded!
cx-app01.veredholdings.cn跟192.168.10.25的443埠正常連線
Date : 
Host : 192.168.10.47
Problem : Port 443 is connected.
Connection to 192.168.10.47 443 port [tcp/https] succeeded!
cx-app01.veredholdings.cn跟192.168.10.47的443埠正常連線

結合crontab進行計劃任務
[root@cx-app01 ~]# crontab -l
*/10 * * * *  /bin/bash -x /opt/host_ip_nc_monit.sh > /dev/null 2>&1

相關文章