LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash Script to Continuously Ping Server and Send Email (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-to-continuously-ping-server-and-send-email-806500/)

schaefer76 05-07-2010 08:44 AM

Bash Script to Continuously Ping Server and Send Email
 
I'm trying to write a script that will continuously ping a server and then send out an email when the server is down, and then when it is back up. Then, continuing with monitoring. I would like to not run this in cron, because I don't want to script to run with multiple instances.

For example,
Ping a server every minute.

-If successful, do nothing.
-If failed to ping, then send out email stating that server is down.
-Once ping is successful, then send out email stating that server is up.

I only want it to send an email once after a failure, so the end user isn't get an email every minute that it fails. Once it is successful, then send the email (one time), stating that the server is up. Then, continue to ping and if fails again, repeat the process.

Anyone have any ideas? I'm new to this site so not sure if I'm posting in the right location. Thanks.

TB0ne 05-07-2010 08:58 AM

Quote:

Originally Posted by schaefer76 (Post 3960403)
I'm trying to write a script that will continuously ping a server and then send out an email when the server is down, and then when it is back up. Then, continuing with monitoring. I would like to not run this in cron, because I don't want to script to run with multiple instances.

For example,
Ping a server every minute.

-If successful, do nothing.
-If failed to ping, then send out email stating that server is down.
-Once ping is successful, then send out email stating that server is up.

I only want it to send an email once after a failure, so the end user isn't get an email every minute that it fails. Once it is successful, then send the email (one time), stating that the server is up. Then, continue to ping and if fails again, repeat the process.

Anyone have any ideas? I'm new to this site so not sure if I'm posting in the right location. Thanks.

Yep, just loop through the ping check, and put a sleep statement in it, so it pauses, and if it fails, send the email and exit the script. That way, it won't start up until you fix the problem, and restart the script.

Another alternative is to send the email, then go into another subroutine, which keeps checking the failed address until it FINDS it, then sends an email saying it's back up, which will throw the program back into the first subroutine.....

Post what you've written, and we can help. But we're no going to write a script for you, though.

catkin 05-07-2010 09:07 AM

Generally LQ does not write custom scripts to order (but you might get lucky); we prefer to help you to help yourself which means you show us what you have done and tried and where you got stuck and what doubts you have ...

To do what you want you will need a command scripting language of which bash is the most common choice. Do you want to use bash or some other language? What is your level of expertise in the chosen language?

Where is the email being sent? Locally, or via the Internet? Does the computer you are doing this on have a way of sending such mails?

What do you want to use to send mail? mail and mailx are possibilities; there are others (pine? mutt? I'm not sure).

schaefer76 05-07-2010 09:30 AM

Thanks for you input. I know some of the basic scripting, but you gave me enough guidance to get me started. Catkin, I know how to send the emails out through the script (maybe not the right way), so I now how to do that part. I wouldn't be doing this script if I didn't have a customer insisting that he gets an email when his server goes down and when it comes back up. I will do some research and see if I can put together something and will post what I came up with. Chances are, it will be a complete failure, but I'm still learning.

TB0ne, thanks for the advice on what to do. I will see what I can come up with. I know that it will be a subroutine that I will want to use, so I will head in that direction.

Thanks for all of the help.

catkin 05-07-2010 09:54 AM

Quote:

Originally Posted by schaefer76 (Post 3960444)
I will do some research and see if I can put together something and will post what I came up with.

Glad you're happy with that. Hopefully it will be a good way to learn.

You may like to use a variable to control sending the mails, something along the lines of this pseudo-bash-code
Code:

last_ping_OK_flag=unknown
while true
do
    ping
    if ping OK then
      if last_ping_OK_flag is false then send email
      last_ping_OK_flag=true
    else
      if last_ping_OK_flag is true then send email
      last_ping_OK_flag=false
    fi
    sleep a while
done

If the network is iffy you might like to wait for more than one successive ping failure before emailing.


All times are GMT -5. The time now is 10:04 PM.