Ping Tool to check if the server is online

Is this tool that I created from various SOF threads valid? Will this work? I want to check the ping test on the server every minute. If he does not work 5 times in a row, he sends an email. Then it resets and resets the script again to check again.

#!/bin/bash # ping checker tool numOfFails=0 incrememnt=1 EMAILMESSAGE="/tmp/emailmessage.txt" while true; do if ! ping -c 1 google.com ; then #if ping exits nonzero... numOfFails=$(($num + $increment)) else numOfFails=0 fi if ((numOfFails > 4)); then numOfFails=0 echo "SAN is offline!" > $EMAILMESSAGE mail -s "SAN offline" " test@test.com " < $EMAILMESSAGE fi sleep 60 #check again in one minute done 
+4
source share
3 answers

Your code will not work at all, this is a modified version:

 #!/bin/bash # ping checker tool FAILS=0 EMAIL_ADDRESS=" example@example.com " SERVER="192.168.1.1" SLEEP=60 while true; do ping -c 1 $SERVER >/dev/null 2>&1 if [ $? -ne 0 ] ; then #if ping exits nonzero... FAILS=$[FAILS + 1] else FAILS=0 fi if [ $FAILS -gt 4 ]; then FAILS=0 echo "Server $SERVER is offline!" \ | mail -s "Server offline" "$EMAIL_ADDRESS" fi sleep $SLEEP #check again in SLEEP seconds done 

Change example@example.com and 192.168.1.1 for your email address and the IP address of the server you are testing. I recommend using an IP address instead of a host name to prevent confusion of name resolution errors with connection errors.

Please keep in mind that although this will work, I would recommend running a slightly different script from cron instead of working continuously, as you think, when you start with cron you won’t need to keep track of what the script is running, because, if it stops for some reason, server monitoring also stops.

Something like this starts from crontab every minute.

 #!/bin/bash # ping checker tool TMP_FILE="/tmp/ping_checker_tool.tmp" if [ -r $TMP_FILE ]; then FAILS=`cat $TMP_FILE` else FAILS=0 fi EMAIL_ADDRESS=" example@example.com " SERVER="192.168.1.1" ping -c 1 $SERVER >/dev/null 2>&1 if [ $? -ne 0 ] ; then #if ping exits nonzero... FAILS=$[FAILS + 1] else FAILS=0 fi if [ $FAILS -gt 4 ]; then FAILS=0 echo "Server $SERVER is offline!" \ | mail -s "Server offline" "$EMAIL_ADDRESS" fi echo $FAILS > $TMP_FILE 
+5
source

Consider using Pingdom. He provides you this service.

One thing you have not considered is when your site goes down, you will continue to receive emails every minute until the site appears again or until you stop this script.

A good approach is to switch states from reports when the site is not reporting when the site is up, once you find that it is down. And then again, as soon as he returns.

Essentially, you get a "site down" error message, and then another, I hope, reports that the site is up.

Pingdom does this for you, very nice.

0
source

I’m learning how to do this so that I can activate / deactivate the services depending on whether my phone is at home. I came up with the following:

 #!/bin/bash HOST_TO_CHECK=<hostname> if ping -qc 20 $HOST_TO_CHECK >/dev/null; then echo "Host $HOST_TO_CHECK is up" else echo "Host $HOST_TO_CHECK is down" fi 

Replace <hostname> with the host you want to verify.

The script will ping the host 20 times. The reason this happens is because my mobile phone does not always answer calls right away.

Obviously, you can replace the echo commands with something to actually do something useful :-)

You can then schedule the script to be checked every 5 minutes by adding it to your crontab:

 */5 * * * * /opt/pingcheck.sh 
0
source

Source: https://habr.com/ru/post/1497294/


All Articles