The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

IpAliases Script (checks and turns on ipaliases on failure)

Discussion in 'cPanel Developers' started by hostmedic, Aug 20, 2008.

  1. hostmedic

    hostmedic Well-Known Member

    Joined:
    Apr 30, 2003
    Messages:
    559
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Washington Court House, Ohio, United States
    cPanel Access Level:
    DataCenter Provider
    I am having some strange issues with Ipaliases failing on a few servers...
    I wrote this quick dirty little script to monitor the issue and then I can use the logs to post a ticket / bug report

    Figured if anyone else has the same thing - it might help there as well.
    Good news is - this could be used for most any service...

    ---- quick --- dirty --- but works ---

    Dirty way around this:

    I setup a cron to run every minute...
    apparently ipaliases kept failing ... so ...

    first -

    touch /var/log/ipaliases_checker.log

    then

    Add this ip to your server 10.187.1.1
    (in whm - then make it so it is a reserved ip)

    Then

    mkdir /scripts/servicechecks

    Then

    touch /scripts/servicechecks/ipaliases.sh

    then

    nano /scripts/servicechecks/ipaliases.sh

    and paste this into the file...

    PHP:

    #!/bin/sh

    # written by Glenn Kelley, 2008-08-20

    notification_email=YOUREMAILGOESHERE

    echo "Checking ipaliases at $(date)" >> /var/log/ipaliases_checker.log

    if ! /bin/ping -t 1 -c 1 10.187.1.1 > /dev/null 2>&1then
      service ipaliases restart  
    > /dev/null 2>&1
      
    echo "Ipaliases down at $(date)" >> /var/log/ipaliases_checker.log

      
    # make sure that remedied things... give it 10s and then see if it's up (if not, sound the alarm)
      
    sleep 10
      
    if ! /bin/ping -t 60 -c 1 10.187.1.1 > /dev/null 2>&1then
        
    echo "Failed to restart" >> /var/logs/ipaliases_checker.log
        
    echo "Failed to restart Ipaliases ; no icmp echo responses" mail -" IpAliases bounce failure $(date)"
    ${notification_email}
      
    fi
    fi
    Now Control S and save.

    Then add a cron to run the script every few minutes

    */5 * * * * /scripts/servicechecks/ipaliases_checker.sh
     
  2. DWHS.net

    DWHS.net Well-Known Member
    PartnerNOC

    Joined:
    Jul 28, 2002
    Messages:
    1,569
    Likes Received:
    6
    Trophy Points:
    38
    Location:
    LA, Costa RIca
    cPanel Access Level:
    Root Administrator
    I tried this but got this error:

    /scripts/servicechecks/ipaliases.sh: line 15: syntax error near unexpected token `if'
    /scripts/servicechecks/ipaliases.sh: line 15: `up (if not, sound the alarm)'
     
  3. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    DWHS.net,

    I didn't get that error. I did, however, have to make minor modifications
    1) the second log message on line 16 needs to go to "/var/log/" not "logs"
    2) On line 17, there was a return character between the email subject and the recipient email address, which cause mail to complain that I didn't have a send-to argument. The return/newline character may have been an artifact of the forum code block.

    The only thing that I can think of, other than a similar cut/paste artifact, would be if /bin/sh is aliased to a different shell that is not properly interpreting the IF statement or the test block.

    It looks pretty vanilla to me; I would think that any Bourne/Korn (sh/bash/ksh) based shells would be able to handle it fine. If you have a C based shell (csh/tcsh), however, you might consider rewriting the script. That is, I believe there's:
    A) 'then' is not preceded with a semi-colon in the IF statement structure
    B) redirects are handled differently.

    If this is the case, one solution is just change the shebang line to '#!/bin/bash'. If you're running FreeBSD, you may have to install it yourself (and the exact location will possibly be in /usr/local/bin or /usr/bin). Last time I checked, tcsh was the default shell for FreeBSD.

    Regards,
    -DavidN
     
  4. cPBrianW

    cPBrianW Sysadmin
    Staff Member

    Joined:
    Jan 26, 2011
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    The following is an adaptation of the above script that does basically the same thing, but cleaned up a bit:
    PHP:
    #!/bin/sh

    #
    # Functions
    #
    add_date() {
      while 
    IFSread -r line; do
        echo 
    "`date` $line"
      
    done
    }

    is_it_up() {
      /
    bin/ping -W1 -c1 $>/dev/null 2>&1
    }


    #
    # Variables
    #
    email=YOUR_EMAIL_ADDRESS
    log_file
    =/var/log/ip_checker.log
    ip
    =192.168.1.1
    ip_is_down_msg
    ="$ip is still down after restarting ipaliases"
    wait_period=10


    #
    # Main program
    #
    echo "Checking to see if $ip is up" |add_date >>$log_file

    if `is_it_up $ip`
    then
      
    echo "$ip is detected as up" |add_date >>$log_file
    else
      
    service ipaliases restart >/dev/null 2>&1
      
    echo "$ip detected as down" |add_date >>$log_file

      
    # Check again after $wait_period seconds and send alert if ipaliases is still down
      
    sleep $wait_period
      
    if `is_it_up $ip`
      
    then
        
    echo "$ip back up after restarting ipaliases" |add_date >>$log_file
      
    else
        echo 
    "$ip_is_down_msg|add_date >>$log_file
        
    echo "$ip_is_down_msg (no icmp echo responses)" |mail -"IP checker detected down ip" $email
      fi
    fi
     
    #4 cPBrianW, Sep 25, 2014
    Last edited: Sep 25, 2014
Loading...

Share This Page