Shell IP Address Monitor

#! / bin / bash 
# ip.txt stored IP address QQ450433231  
# ip2.txt Project Name stored IP address is used to format output, pre-defined text good IP location is conducive to rapid troubleshooting 
# This update adds recovery warning downtime  

. /root/weixin.sh # robot sends an alarm call to the micro-channel https://blog.51cto.com/junhai/2424374 
. /root/downiptime.sh failure time is calculated based on the log #  
# define time 
CUR_TIME () { 
	DATE " the Y% + /% m /% D% H:% m:% S " 
} 
alarm information defining output # ip2.txt lookup file output alarm information according to the IP 
SystemName () { 
name =` CAT ip2jk.txt | grep $ IP | WC -l` 
iF [$ name -eq 1]; the then 
        CAT ip2jk.txt | grep $ ip 
the else 
        echo "$ ip" 
fi 
} 
# determine whether a file exists 
[! -f /root/ipjk.txt] && echo " ipjk.txt file does not exist "&& Exit 1 
[! -f /root/ipadd.txt] && Touch ipadd.txtTXT 
# remove the file space
-i Sed '/ ^ $ / D' /root/ipjk.txt 

the while Read IP 
do 
	[the -Z $ IP] && echo "ipjk.txt space exists check the file format" && Exit. 1 
        for ((. 1 = I; I < . 6; I ++)) 
        do 
                of ping. 1 -W 2 -C IP & $> / dev / null determination rule # 
                IF [? $ = 0]; the then 
                        echo "$ (CUR_TIME) of ping $ IP Successful"> / dev / null # /root/connect.log >> 
                        BREAK 
                elif [$ i = 5]; the then 
						info = `echo -e"! $ (CUR_TIME) \ the n-$ ip did not respond to PING messages, check \ n $ (systemname) "` 
						echo "$ ip" >> ipadd. txt # unreachable IP removing monitoring list 
						sed -i "S | $ ip ||" ipjk.txt 
						sed -i "/ ^ $ / d" ipjk.txt 
						wx_ip # micro enterprises send a letter warning 
						echo -e "$ (cur_time) \ n $ ip did not respond to PING messages, check \ n $ (systemname)!" | mail -s " [IP] did not respond to an emergency alert message PING" [email protected] # send e-mail alerts to modify / etc / mail.rc can call third-party mailbox
						echo "$ (cur_time) of the $ i $ ip inspections did not respond to PING message" >> /root/connectfail.log # write to log 
                the else 
                        echo "$ (cur_time) of the $ i $ ip inspections did not respond to PING packets ">> /root/connectfail.log 
                        SLEEP 2 
                Fi 
        DONE 
DONE </root/ipjk.txt 

Sed -i" / ^ $ / D "ipadd.txt 
################ when transmitting IP ########### recovery alarm recovery is 
the while Read IP 
do 
        	[the -Z $ IP] && echo "ipadd.txt space exists check the file format" Exit. 1 && 
            of ping. 1 -W 2 -C ip & $> / dev / null 
                IF [? $ = 0]; the then 
					echo -e "$ (CUR_TIME) $ ip has been restored" >> / root / connectfail.log
                    u=$(grep -w "$ip" ipjk.txt|wc -l)
						if [ $u -eq 1 ];then 
							echo "$ (cur_time) $ ip already exists ">> /root/connectfail.log 
						the else
							echo "$ IP" >> ipjk.txt 
						Fi 
					Sed -i "S | $ IP ||" ipadd.txt 
                    Sed -i "/ ^ $ / D" ipadd.txt 
					info = `echo -e" $ (CUR_TIME) \ n $ ip has been restored \ n $ (systemname) \ n \ n $ (fail_time) "` 
                        wx_ip 
                        echo -e "$ (CUR_TIME) \ n $ ip has been restored \ n $ (systemname) \ n \ n $ (fail_time) "| mail -s" [IP] has been restored "[email protected] 
		the else 
			echo" $ (CUR_TIME) of the $ i $ ip inspections did not respond to PING message ">> / dev / null 
                fi 
DONE </ root / IPADD .TXT
! # / bin / the bash 
# Log Analysis calculated time 

fail_time () { 

startTime = `tail 500 connectfail.log -n | grep" $ IP "| grep" 1st "| tail -n 1 | awk ' {print $ 1, 2} $ ' ` 
the endtime =` tail 500 connectfail.log -n | grep "$ IP" | grep "restored" | tail -n. 1 | awk' {$ Print. 1, $ 2} ' ` 


startm = $ (DATE --date = "$ startTime"% M +); 
ENDM = $ (DATE --date = "the endtime $"% M +); 

starth = $ (DATE --date = "$ startTime"% + H); 
endh = $ ( --date = DATE "the endtime $"% H +); 

the startd = $ (DATE --date = "$ startTime" D +%); 
ENDD = $ (DATE --date = "$ the endtime" D +%); 


echo "The failure startTime start time $" 
echo "The fault end time $ endtime"

day = $ (($ ((10 # $ endd)) - $ ((10 # $ startd)))) 
hour = $ (($ ((10 # $ endh)) - $ ((10 # $ Homeh)) )) 
minute = $ (($ ((10 # $ endm)) - $ ((10 # $ Startm))))

Total = $ (($ (($ Day * 24 * 60)) + $ (($ hour * 60)) + $ minute)) 

echo "The fault duration: $ ((total / 60) ) h $ ( (% Total $ 60)) m " 

}

Enterprise micro-channel alerting effect 

image.png

E-mail alerts effect

image.png

image.png

Guess you like

Origin blog.51cto.com/junhai/2443928