Results 1 to 10 of 12
Im using a script that sends a email if my server goes down. I would like to update it to:
If ping fails it would send a ping to another ...
- 01-18-2012 #1Just Joined!
- Join Date
- Jan 2012
- Posts
- 5
Help to update existing script "Ping Monitor with email alert"
Im using a script that sends a email if my server goes down. I would like to update it to:
- If ping fails it would send a ping to another host. The other host is a backupserver that uses wol and starts up.
Is there someway to get this to work?
This is the script im using in crontab at the moment:
#!/bin/bash
HOSTS="IP ADRESS"
COUNT=4
for myHost in $HOSTS
do
count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | a$
if [ $count -eq 0 ]; then
# 100% failed
echo "Server failed at $(date)" | mail -s "Server Down" myadress_gmail.com
echo "Host : $myHost is down (ping failed) at $(date)"
fi
done
- 01-19-2012 #2Just Joined!
- Join Date
- Dec 2011
- Posts
- 11
You can provide lot of different hosts in the that HOSTS variable which in turn in loop will work one by one.So it can automtally pic up other host and try to ping on it.
- 01-20-2012 #3Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
Working off what vikram said, I'd change up your code to something like this:
Code:#!/bin/bash hosts='192.168.1.5 192.168.1.6' for host in $hosts; do echo -n "Pinging host $host ... " out=$(ping -c4 $host|grep -q ' 0% packet loss') if [ $? -eq 0 ]; then echo alive rc=0 break else echo down rc=1 fi done exit $rc
- 01-20-2012 #4Just Joined!
- Join Date
- Dec 2011
- Posts
- 11
Atreyu your code are always perfect
- 01-23-2012 #5Just Joined!
- Join Date
- Jan 2012
- Posts
- 5
Thanx for the answer! I tried it and it works fine, but...
I seems that i cant get it to work to start up server2 with wol with just pinging it. Is there a way to connect this to get it to work with wol?
In ubuntu the command "wakeonlan + mac-adress" works fine.
That i want to do is if server1 fails > "pingcomputer" sends a mail AND starts server2 with wake on lan. Is there a way to get this done ?
- 01-24-2012 #6Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
I have no idea what wakeonlan is, I will assume it is a program that takes a MAC as an argument to it, and I will assume it returns 0 on successful completion and non-zero if errors are encountered.
Also, it doesn't really make sense to loop thru a list of servers, as your paradigm is more of a master/backup setup.
You'll need to hard-code your server-to-MAC list somewhere (in a file or database). For the sake of simplicity, we'll put it right in the script.
Btw, if you don't know your MAC, you can a remote machine's MAC, by pinging it first, then checking the arp table, e.g.:Code:#!/bin/bash # ip address or hostname of primary and backup servers primary_ip='192.168.1.5' backup_ip='192.168.1.6' # mac of primary and backup servers primary_mac='00:11:22:33:44:55' backup_mac='00:00:00:11:11:11' ping_host() { local host out rc host=$1 echo -n "Pinging host $host ... " out=$(ping -c4 $host|grep -q ' 0% packet loss') if [ $? -eq 0 ]; then echo alive rc=0 else echo down rc=1 fi return $rc } # ping the primary server, and if it is up, exit ping_host $primary_ip && exit 0 # primary server is not up, so send an email echo "Sending email..." #(put your email command here) # ping the backup server ping_host $backup_ip # if backup server is up... if [ $? -eq 0 ]; then # run you wol command /path/to/wakeonlan $backup_mac # evaluate exit value of the command (not sure about this) if [ $? -eq 0 ]; then echo "The wakeup command succeeded" exit 0 else echo "The wakeup command FAILED!" exit 1 fi # if backup server is down... else echo "Cannot continue" exit 1 fi
also, get it locally with one of these:Code:ping <REMOTE_IPADDR> arp -a <REMOTE_IPADDR>
Code:ifconfig |grep -i hwaddr ip a
- 01-24-2012 #7Just Joined!
- Join Date
- Jan 2012
- Posts
- 5
@atreyu. I appreciate your attempt to understand. I will try this that you have suggested and see if i get the wol to work.
I understand that its no need to loop trough a list of servers when i only ned to know if server1 is up, and if its not server2 will start up and i get a email.
The "wakeonlan" command that i used in ubuntu server is a package that i installed with apt-get install. The to run it i have to know the mac-adress of the server i like to start up with this command "wakeonlan backup_mac". I dont know if there is another way or a better way to try to get this to work with starting server2 with wol
I did test your script. I seems i cant get it to work with wol...the error "cannot continue" comes after trying to ping backuphost=down. Must be something with this that doesnt work...
# if backup server is up...
if [ $? -eq 0 ]; then
# run you wol command
/usr/bin/wakeonlan $backup_mac
# evaluate exit value of the command (not sure about this)
if [ $? -eq 0 ]; then
echo "The wakeup command succeeded"
exit 0
else
echo "The wakeup command FAILED!"
exit 1
fi
# if backup server is down...
else
echo "Cannot continue"
exit 1
fiLast edited by seven01; 01-25-2012 at 12:35 AM.
- 01-25-2012 #8Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
My mistake. Now I see what you are trying to do: if server1 is not up (pingable), then don't PING server2, just wake it up. Is that right? If so, then just comment out the server2-ping-logic. Also, i'll throw in a ping of server2 AFTER wol is run. here it again with new edits:
Code:#!/bin/bash # ip address or hostname of primary and backup servers primary_ip='192.168.1.5' backup_ip='192.168.1.6' # mac of primary and backup servers primary_mac='00:11:22:33:44:55' backup_mac='00:00:00:11:11:11' ping_host() { local host out rc host=$1 echo -n "Pinging host $host ... " out=$(ping -c4 $host|grep -q ' 0% packet loss') if [ $? -eq 0 ]; then echo alive rc=0 else echo down rc=1 fi return $rc } # ping the primary server, and if it is up, exit ping_host $primary_ip && exit 0 # send an email echo "Sending email..." #(put your email command here) # ping the backup server #ping_host $backup_ip # if backup server is up... #if [ $? -eq 0 ]; then /path/to/wakeonlan $backup_mac if [ $? -eq 0 ]; then echo "The wakeup command succeeded" # see if the server truly woke up and is ping-able sleep 1 ping_host $backup_ip exit $? else echo "The wakeup command FAILED!" exit 1 fi # if backup server is down... #else # echo "Cannot continue" # exit 1 #fiLast edited by atreyu; 01-25-2012 at 01:51 AM. Reason: typo
- 01-25-2012 #9Just Joined!
- Join Date
- Jan 2012
- Posts
- 5
@atreyu. It works like a charm
. Thank you very much!!!!
Just one more thing i thinking about. When "primary ip" is down it sends a mail. I like crontab to run this every 5 min but is there a way to limit the amount of mail that beeing sent out if primary server is down to for example twelve? The inbox gonna be full if im not fast enough to fix the "primary server"
. Thats 288 mail in one day, but still not the biggest problem in the world (!)
- 01-26-2012 #10Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
My first thought here is, I would want those email messages. They are doing their job by annoying me! And they're only a few KB apiece, right?
Anyway, here's a possible solution. You can use a separate file as a counter, which gets one-upped every time an email message is sent. Furthermore, whenever server1 is found to be up again, the counter file is zeroed.
At top of the script are two variables which you can define yourself: the first is the path to the file containing a counter (just a number on line 1, that's it), and the second is the value representing the maximum number of emails you wish to receive.
Code:#!/bin/bash # the counter file counterFile='/tmp/counter.txt' # set this to the maximum number of messages to be emailed maxCnt=10 # ip address or hostname of primary and backup servers primary_ip='192.168.1.5' backup_ip='192.168.1.6' # mac of primary and backup servers primary_mac='00:11:22:33:44:55' backup_mac='00:00:00:11:11:11' ping_host() { local host out rc host=$1 echo -n "Pinging host $host ... " out=$(ping -c4 $host|grep -q ' 0% packet loss') if [ $? -eq 0 ]; then echo alive rc=0 else echo down rc=1 fi return $rc } # ping the primary server, and if it is up, exit ping_host $primary_ip && echo 0 > $counterFile && exit 0 # get the current number of messages sent from the counter file declare -i cnt if [ -f $counterFile ]; then cnt=$(<$counterFile) else cnt=0 fi # see if the count max has been reached if [ $cnt -ge $maxCnt ]; then echo "Message max has been reached" else # one-up the file counter cnt+=1 echo $cnt > $counterFile # send an email echo "Sending email..." #(put your email command here) fi # ping the backup server #ping_host $backup_ip # if backup server is up... #if [ $? -eq 0 ]; then /path/to/wakeonlan $backup_mac if [ $? -eq 0 ]; then echo "The wakeup command succeeded" # see if the server truly woke up and is ping-able sleep 1 ping_host $backup_ip exit $? exit 0 else echo "The wakeup command FAILED!" exit 1 fi # if backup server is down... #else # echo "Cannot continue" # exit 1 #fi


Reply With Quote
