Forum Discussion

Gary_31854's avatar
Gary_31854
Icon for Nimbostratus rankNimbostratus
Mar 26, 2014

Variables in an external monitor BASH script

Greetings,

I have written a shell script to use as an external monitor. It works as expected when I execute it from the bash shell, but fails as a monitor. I believe the reason for the failure is the way I am using VARIABLEs.

Is the code below legal?

TMSH='/usr/bin/tmsh'
VCS1='10.12.1.11'
VCS2='10.12.1.12'
DEBUG=1
PRI=$TMSH list ltm snat snat_ids_primary | $GREP -E "$VCS1|$VCS2"
if [ "$DEBUG" -eq 1 ]; then
  echo "IDS_SNAT: Result of Primary SNAT check -> $PRI" | $LOGGER -p local0.debug;
fi

When run from the CLI interactively the log will have data to in the $PRI variable.

Mar 26 17:24:46 local/bip-bxb-lab-01 debug logger: IDS_SNAT: Result of Primary SNAT check -> 10.12.1.11/32 Mar 26 17:24:46 local/bip-bxb-lab-01 debug logger: 10.12.1.12/32

When run as an external monitor the $PRI variable is empty.

Mar 26 17:21:24 local/bip-bxb-lab-01 debug logger: IDS_SNAT: Result of Primary SNAT check ->

I am clearly doing something wrong I just don't know what.

Any help would be appreciated.

Thank you.

  • Hamish's avatar
    Hamish
    Icon for Cirrocumulus rankCirrocumulus

    Well... if you want $PRI to contain the output of the tmsh command, then you really need back ticks around it (Although they could already be there and just missing from the post (Eaten by the post gremlins when the text was formatted).

     

    I also don't see anywhere you define $GREP in the script (or $LOGGER). (Is that just missing the code above that defines it?). Also the script interpreter is missing? Possible issues there (e.g. the magic number line

     

    !/bin/bash

    The formatting is a bit wonky sorry... Hard to tell (Can you re-post the whole script using 4 spaces in front of every line so the website shows it as pre formatted code?

     

    H

     

  • I was unable to attach the full script to this thread. I put it up on pastebin --> http://pastebin.com/PMwFXs5z .

     

    The backticks and missing variables are defined. It does work when executed from the CLI. Here is the piece of code again with the preformatted code view.

     

     Full path variables to commands used in the script
    CURL='/usr/bin/curl'
    GREP='/bin/grep'
    TMSH='/usr/bin/tmsh'
    LOGGER='/bin/logger'
    
    IP addresses of the VCS members
    VCS1='10.12.1.11'
    VCS2='10.12.1.12'
    
    Debug flag
    DEBUG=1
    
     TMSH list the content of snat definition snat_ids_primary, and look for
     the VCS IP addresses to store them in the $PRI variable. If the VCS are
     not in the primary SNAT, $PRI should be empty.
    PRI=`$TMSH list ltm snat snat_ids_primary | $GREP -E "$VCS1|$VCS2"`
    if [ "$DEBUG" -eq 1 ]; then echo "IDS_SNAT: Result of Primary SNAT check -> $PRI" | $LOGGER -p local0.debug; fi
  • After reading LTM External Monitors: The Basics

    I think I will just change the logic to rely on $?. Example below:

     

    if [ $PAGE = "bxb" ]
    then
             Echo up to STDOUT for the pool member to be listed as up.
            echo "Up"
             Check that the VCS Members are listed as origins in the snat_ids_primary definition.
            $TMSH list ltm snat snat_ids_lfep_out | $GREP -E "$VCS1|$VCS2|$PRI_IP" 2>&1 > /dev/null
            if [ $? -eq 0 ]
            then
                    rm -f $PIDFILE
                    exit 0  
             Check that the VCS Members are listed as origins in the snat_ids_alt definition. If so the SNAT config needs to be changed.
            else
            ......rest of the script