#!/bin/ksh
#set -xv
#####################################################################
#
# Name: Padraig Lennon
# Date: 03-Aug-2009
# Script Description: Check the SSH tunnels to DMZ (External) Hobbit/Xymon clients
# Version: 0.03
# Licence: Please feel free to modify, and use without cost
# Please leave reference to original author.
#
# #########################################################
#
# Date - Modifier - Version - Change
# 12-Dec-2007 - P.Lennon - 0.01 - Initial Release
# 19-Aug-2008 - P.Lennon - 0.02 - Updated the script to display the coloured icon on the client
# web page
# 03-Aug-2009 - P.Lennon - 0.03 - Allow user define the ssh port (if non-standard) in
# bb-host using ssh-tunnel:port syntax
# 25-Oct-2009 - G.Janssen - 0.04 - Use fully qualified domain names for tunnel
#
###########################################################################
# Constants/Global variables
###########################################################################
PROGNAME=$(basename $0) # Script Name
TEMP_FILE=/tmp/${PROGNAME}.$$.$RANDOM # Temp Output File
TEST=ssh-tunnel # Hobbit/Xymon test name
COLUMN=$TEST # Hobbit/Xymon test name
#AUTHOR=padraig.lennon@pioneerinvestments.com # Test Author
AUTHOR="Version 0.04 Ge Janssen & Adraig Lennon" # Test Author
VERSION="
`basename $0`, $AUTHOR
"
SSH_PORT="22"
###########################################################################
# Functions
###########################################################################
#####
# Function to remove temporary files and other housekeeping
# Arguments=0
#####
function clean_up
{
rm -f ${TEMP_FILE} # Remove the temp output file
}
#####
# Function called for a graceful exit
# Arguments=0
#####
function graceful_exit
{
clean_up
exit
}
#####
# Function for exit due to fatal program error
# Arguments=1
# Argument 0: string containing descriptive error message
#####
function error_exit
{
local ERR_MSG
ERR_MSG="##\n#Error: ${1}\n##\n"
echo -e ${ERR_MSG} >&2
clean_up
exit 255
}
#####
# Function for printing warning messages
# Arguments=1
# Argument 0: string containing descriptive warning message
#####
function warning
{
local WARN_MSG
WARN_MSG="##\n#Warning: ${1}\n##\n"
echo -e ${WARN_MSG} >&2
}
#####
# Function for printing script steps
# Arguments=1
# Argument 0: string containing descriptive step message
#####
function print_step
{
local STEP_MSG
STEP_MSG="#----> ${1}"
echo -e ${STEP_MSG}
}
#####
# Function to perform exit if interrupt signal is trapped
# Arguments=0
#####
function int_exit
{
echo -e "${PROGNAME}: Aborted by user"
clean_up
exit 255
}
#####
# Function to display help message for program
# No arguments
#####
function help
{
local tab=$(echo -en "\t\t")
cat <<- -EOF-
Check ssh-tunnels to dmz clients
Usage: ${PROGNAME} [-h]
Required parameters:
Optional parameters:
-h, --help Display this help message and exit.
Example(s):
${PROGNAME}
Exit Codes:
0 Success
255 Error
Author: Padraig Lennon
-EOF-
}
##### USER DEFINED FUNCTIONS ######################
###########################################################################
# Check command line parameters
###########################################################################
# Trap INT signals and properly exit
trap int_exit INT
# Process command line arguments
# Parameters with arguments divide with : i.e. for option o use o:
# Parameters with no arguments add the option after the h. no extra :
while getopts ":h" opt; do
case $opt in
h ) help
graceful_exit
;;
* ) help
error_exit "Wrong parameter passed"
;;
esac
done
###########################################################################
# Main Body of Script
###########################################################################
${GREP} -i "^[0-9].*#.*${TEST}" ${BBHOSTS} | while read L
do
set $L # To get one line of output from the grep output
HOSTIP=$1
MACHINEDOTS=$2
MACHINE=`echo $MACHINEDOTS | $SED -e 's/\./,/g'`
for OPTION in `echo $* | $AWK -F# {'print $2'}| $SED s/\s+/\s/g`
do
OPTION_VAL=`echo $OPTION | $GREP ${TEST} 2>/dev/null`
if [ "$OPTION_VAL" != "" ] ; then
# We have found the test definition. Check if an alternative port was supplied
SSH_PORT_VAL=`echo $OPTION_VAL | $AWK -F: {'print $2'}`
if [ "$SSH_PORT_VAL" != "" ] ; then
SSH_PORT=$SSH_PORT_VAL
else
SSH_PORT=22
fi
fi
done
COLOR=green
MSG="$TEST status for host $MACHINEDOTS"
##CLIENT=`echo $MACHINEDOTS | $AWK -F. {'print $1'}`
##GJ I Need FQDN
CLIENT=$MACHINEDOTS
COUNT=`$PS -ef|$GREP "ssh -fnNR [1]984"| $EGREP "$SSH_PORT $CLIENT"| wc -l | $SED -e "s/\ //g"`
if [ $COUNT -eq 0 ] ; then
COLOR=yellow
# Restarting the Tunnel
ssh -fnNR 1984:`hostname`:1984 -p $SSH_PORT $CLIENT
if [ $? -ne 0 ] ; then
MSG="&red Tunnel is down.. Restart attempt failed"
COLOR=red
else
MSG="&yellow Tunnel recently restarted"
COLOR=yellow
fi
elif [ $COUNT -gt 1 ] ; then
for PROCESS in `$PS -ef | $GREP "ssh -fnNR"| GREP "$CLIENT" | $AWK {'print $2'}`
do
kill $PROCESS
done
# Restarting the Tunnel
ssh -fnNR 1984:`hostname`:1984 -p $SSH_PORT $CLIENT
if [ $? -ne 0 ] ; then
MSG="&red Tunnel is down.. Restart attempt failed"
COLOR=red
else
MSG="&yellow Tunnel recently restarted"
COLOR=yellow
fi
else
MSG="&green SSH Tunnel to $CLIENT ok"
fi
$BB $BBDISP "status $MACHINEDOTS.$COLUMN $COLOR `date`
${MSG}
$VERSION
"
done
graceful_exit