Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just 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:

    1. 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

  2. #2
    Just 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.

  3. #3
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    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

  4. #4
    Just Joined!
    Join Date
    Dec 2011
    Posts
    11
    Atreyu your code are always perfect

  5. #5
    Just 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 ?

  6. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    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.

    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
    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:
    ping <REMOTE_IPADDR>
    arp -a <REMOTE_IPADDR>
    also, get it locally with one of these:
    Code:
    ifconfig |grep -i hwaddr
    ip a

  7. #7
    Just 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
    fi
    Last edited by seven01; 01-25-2012 at 12:35 AM.

  8. #8
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by seven01 View Post
    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.
    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
    #fi
    Last edited by atreyu; 01-25-2012 at 01:51 AM. Reason: typo

  9. #9
    Just 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 (!)

  10. #10
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by seven01 View Post
    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 (!)
    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

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •