FTP Monitor File Existence Verification

Problem this snippet solves:

This external monitor performs a health check of a FTP sever using curl. There is an inbuilt FTP monitor for LTM. However, If one needs to script it you can use the information here as a guide. What is special about this script is it will pass and mark the pool member(s) up only if a file is not found.

Please see following links for more information on external monitors, implementation and troubleshooting:

External Monitor

Template For External Monitors

Troubleshooting Ltm Monitors

See the comments in the script for details on how to implement it.

Code :

# !/bin/bash

# Save as /usr/bin/monitors/custom_monitor.bash
# Make executable using chmod 700 custom_monitor.bash

# Use a custom shell command to perform a health check of the pool member IP address and port

# Log debug to local0.debug (/var/log/ltm)?
# Check if a variable named DEBUG exists from the monitor definition
# This can be set using a monitor variable DEBUG=0 or 1
if [AdvDesignConfig.-n "$DEBUG"|-n "$DEBUG"]
then
if [AdvDesignConfig.$DEBUG -eq 1|$DEBUG -eq 1]; then echo "EAV `basename $0`: \$DEBUG: $DEBUG" | logger -p local0.debug; fi
else
# Remove IPv6/IPv4 compatibility prefix (LTM passes addresses in IPv6 format)
IP=`echo $1 | sed 's/::ffff://'`

# Save the port for use in the shell command
PORT=$2

# Check if there is a prior instance of the monitor running
pidfile="/var/run/`basename $0`.$IP.$PORT.pid"
if [AdvDesignConfig.-f $pidfile|-f $pidfile]
then
kill -9 `cat $pidfile` > /dev/null 2>&1
echo "EAV `basename $0`: exceeded monitor interval, needed to kill ${IP}:${PORT} with PID `cat $pidfile`" | logger -p local0.error
fi

# Add the current PID to the pidfile
echo "$$" > $pidfile

# Debug
if [AdvDesignConfig.$DEBUG -eq 1|$DEBUG -eq 1]
then

####  Customize the log statement here if you want to log the command run or the output ####

echo "EAV `basename $0`: Running for ${IP}:${PORT} using custom command" | logger -p local0.debug
fi

####  Customize the shell command to run here. ####

# Use $IP and $PORT to specify which host/port to perform the check against
# Modify this portion of the line:
# nc $IP $PORT | grep "my receive string"
# And leave this portion as is:
# '2>&1 > /dev/null'
# The above code redirects stderr and stdout to nothing to ensure we don't errantly mark the pool member up

# Send the request request and check the response
# Credential handling should be considered
#"<>" denotes user specific information and must be replaced.

curl -u : ftp://${IP}:${PORT}/ /dev/null 2>&1

# Check if the command ran successfully
# Note that any standard output will result in the script execution being stopped
# So do any cleanup before echoing to STDOUT
# If the  is not found curl will respond with libcurl error 19 and the monitor will mark the appropriate members up; else, it if not found, the monitor will timeout and mark the pool members down.

if [AdvDesignConfig.$? -eq 19|$? -eq 19]
then
   rm -f $pidfile
   if [AdvDesignConfig.$DEBUG -eq 1|$DEBUG -eq 1]; then echo "EAV `basename $0`: Succeeded for ${IP}:${PORT}" | logger -p local0.debug; fi
   echo "UP"
else
   rm -f $pidfile
   if [AdvDesignConfig.$DEBUG -eq 0|$DEBUG -eq 0]; then echo "EAV `basename $0`: Failed for ${IP}:${PORT}" | logger -p local0.debug; fi
fi
Published Mar 12, 2015
Version 1.0

Was this article helpful?

1 Comment

  • I had to change lines 16 and 19 to get this monitor to work - the brackets were missing. Tested on 14.1

     

    16. IP=`echo ${1} | sed 's/::ffff://'`

    19. PORT=${2}

     

    You can also do this on line 54:

    curl -u ${FTPUSER}:${FTPPASS} ftp://${IP}:${PORT}/${FILENAME} /dev/null 2>&1