Template For External Monitors

Problem this snippet solves:

This external monitor template shows how to use a shell command to perform a custom health check of a pool

How to use this snippet:

  1. Create a new file containing the code below on the LTM filesystem. Required location is /usr/bin/monitors/. Permissions on the file must be 700 or better, giving root rwx access to the file. For GTM monitors, set permissions to 755 to allow the big3d process execute access to the file1.

    • Example: chmod 755 /usr/bin/monitors/custom_monitor.bash
  2. Create a monitor profile of type "External" with the following values:

    • External Program: . . the name of the script file created in step 1
    • Arguments: . . . . .None needed
    • Variables: . . . . . .DEBUG=0 or DEBUG=1 to log debug messages to /var/log/ltm

If you add a DEBUG variable in the monitor definition and set it to 1, the script will write out debug to /var/log/ltm.

3.Customize the shell command run and the debug logging for the command or result under the following two comments:

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

Example monitor definition

# b monitor custom_external_monitor list
monitor custom_external_monitor {
   defaults from external
   DEBUG "0"
   run "custom_monitor.bash"
}

Adjust the interval and timeout as appropriate for your application

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 [ -n "$DEBUG" ]
then
   if [ $DEBUG -eq 1 ]; then echo "EAV `basename $0`: \$DEBUG: $DEBUG" | logger -p local0.debug; fi
else
   # If the monitor config didn't specify debug, enable/disable it here
   DEBUG=0
   #echo "EAV `basename $0`: \$DEBUG: $DEBUG" | logger -p local0.debug
fi

# 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 [ -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 [ $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
nc $IP $PORT | grep "my receive string" 2>&1 > /dev/null

# 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 [ $? -eq 0 ]
then
   rm -f $pidfile
   if [ $DEBUG -eq 1 ]; then echo "EAV `basename $0`: Succeeded for ${IP}:${PORT}" | logger -p local0.debug; fi
   echo "UP"
else
   rm -f $pidfile
   if [ $DEBUG -eq 1 ]; 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?

No CommentsBe the first to comment