Closed StephanKeller76 closed 8 years ago
There is a problem with getopt in AIX.
The output in the problem is:
TEMP=$(getopt -o d:hi:KTvV --long database:,help,instance:,mk,trace,verbose,version \
-n ${APPL_NAME} -- "${@}")
getopt -o d:hi:KTvV --long database:,help,instance:,mk,trace,verbose,version -n ${APPL_NAME} -- "${@}"
++ getopt -o d:hi:KTvV --long database:,help,instance:,mk,trace,verbose,version -n check_database_connection -- -i /db2/db2ez1 -d EZ1 -vvv
+ TEMP='-- d:hi:KTvV --long database:,help,instance:,mk,trace,verbose,version -n check_database_connection -- -i /db2/db2ez1 -d EZ1 -vvv '
The output in Linux
TEMP=$(getopt -o d:hi:KTvV --long database:,help,instance:,mk,trace,verbose,version \
-n ${APPL_NAME} -- "${@}")
getopt -o d:hi:KTvV --long database:,help,instance:,mk,trace,verbose,version -n ${APPL_NAME} -- "${@}")
getopt -o d:hi:KTvV --long database:,help,instance:,mk,trace,verbose,version -n ${APPL_NAME} -- "${@}"
++ getopt -o d:hi:KTvV --long database:,help,instance:,mk,trace,verbose,version -n nagios -- -i /home/db2inst1 -d sample
+ TEMP=' -i '\''/home/db2inst1'\'' -d '\''sample'\'' --'
The output in the TEMP variable was not correctly defined in AIX. However, in Linux it is set in the right way (parameter value parameter value...)
I am going to see what can be done. I am not a AIX expert.
It seems the AIX includes the getopt version that does not support long name parameters. You can install the GNU getopt that support them.
In any case, I put a condition in the code that checks the OS before filling the variable. Please tell me if that works. If it does, I will perform the change in the rest of the scripts.
For more information: https://stackoverflow.com/questions/27376607/getopt-is-not-working-on-aix-as-it-does-in-linux/27377607#27377607
Hi Andres,
i installed the GNU getopt and then it worked. Thanks
I tried the standard getopt in AIX but don't know if i edited your script correct.
TEMP=$(getopt -o d:hi:KTvV --long database:,help,instance:,mk,trace,verbose,version \ -n ${APPL_NAME} -- "${@}")
to
TEMP=$(getopt -o d:hi:KTvV )
Mit freundlichen Grüßen / Best regards Stephan Keller Senior System Engineer
Sto SE & Co. KGaA Ehrenbachstr. 1, D-79780 Stühlingen FOI - IT Infrastruct./Architect. & Operations Tel: +49-77 44-57 16 63 Fax: +49-77 44-57 26 63 Mobile: +49-170-45 71 498 www.sto.com & www.sto.de
Von: Andres Gomez Casanova notifications@github.com An: angoca/monitor-db2-with-nagios monitor-db2-with-nagios@noreply.github.com Kopie: StephanKeller76 s.keller@sto.com Datum: 09.12.2014 15:12 Betreff: Re: [monitor-db2-with-nagios] Problem running plugins on AIX (#43)
It seems the AIX includes the getopt version that does not support long name parameters. You can install the GNU getopt that support them.
In any case, I put a condition in the code that checks the OS before filling the variable. Please tell me if that works. If it does, I will perform the change in the rest of the scripts.
For more information: https://stackoverflow.com/questions/27376607/getopt-is-not-working-on-aix-as-it-does-in-linux/27377607#27377607
— Reply to this email directly or view it on GitHub.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Sto SE & Co. KGaA, Ehrenbachstr.1, D-79780 Stuehlingen Sitz der Gesellschaft/Head Office: D-79780 Stuehlingen Handelsregister/Registration: Amtsgericht Freiburg i.Br. HRB 711236 USt-ID/Tax ID: DE142834082 Aufsichtsratsvorsitzender/Chairman Supervisory Board: Dr. Max-Burkhard Zwosta
Persönlich haftende Gesellschafterin/General Partner: STO Management SE Sitz der Gesellschaft/Head Office: D-79780 Stuehlingen Handelsregister/Registration: Amtsgericht Freiburg i.Br. HRB 709900 Vorstand/Board: Jochen Stotmeister (Vorsitzender/Chairman), Gerd Stotmeister, Rolf Woehrle, Rainer Huettenberger Aufsichtsratsvorsitzender/Chairman Supervisory Board:
Follow us on Sto-Apps
I modified the scripts in order to work natively in AIX with no need to use GNU getopt
I tried the scripts on AIX but i can't get them running. AIX Version 7.1 TL2 SP4, Bash 3.2.1, getopt-1.1.4-3
Here is the output with -vvv
Here the output with debug on
db2ez1> ./check_database_connection -i /db2/db2ez1 -d EZ1 -vvv
Locale to print messages in English. Prevent language problems.
export LANG=en_US
Version of this script.
function print_revision { echo Andres Gomez Casanova - AngocA echo v1.1 2013-05-25 }
Function to show the help
function print_usage { /bin/cat <<EOT Usage: ${1} { -i instanceHomeDirectory -d databaseName [-K] | -h | -V } [-T][-v] EOT }
function print_help { print_revision print_usage ${1}
Max 80 chars width.
/bin/cat <<__EOT
This script checks the connectivity to a database. -d | --database STRING Database name. -h | --help Shows the current documentation. -i | --instance STRING Instance home directory. It is usually /home/db2inst1. -K | --mk Changes the output to be compatible with Check_MK. -T | --trace Trace mode: writes date and output in /tmp. -v | --verbose Executes the script in verbose mode (multiple times). -V | --version Shows the current version of this script. __EOT }
Variable to control the flow execution. Prevent Spaghetti code.
CONTINUE=true
Nagios return codes
OK=0
This is the returned code.
RETURN=${UNKNOWN}
Nagios Output
Text output 80 chars | Optional Perf Data Line 1
Long text Line 1
Long text Line 2 | Optional Perf Data Line 2
Optional Perf Data Line 3
OUTPUT=
APPL_NAME=$(basename ${0}) basename ${0} ++ basename ./check_database_connection
if [[ ${#} -eq 0 ]] ; then print_usage ${APPL_NAME} RETURN=${UNKNOWN} CONTINUE=false fi
The following requieres GNU getopt. See the following discusion.
http://stackoverflow.com/questions/402377
TEMP=$(getopt -o d:hi:KTvV --long database:,help,instance:,mk,trace,verbose,version \ -n ${APPL_NAME} -- "${@}") getopt -o d:hi:KTvV --long database:,help,instance:,mk,trace,verbose,version -n ${APPL_NAME} -- "${@}" ++ getopt -o d:hi:KTvV --long database:,help,instance:,mk,trace,verbose,version -n check_database_connection -- -i /db2/db2ez1 -d EZ1 -vvv
if [[ ${?} -ne 0 ]] ; then print_usage ${APPL_NAME} RETURN=${UNKNOWN} CONTINUE=false fi
if [[ ${CONTINUE} == true ]] ; then
Note the quotes around ${TEMP}: they are essential!
eval set -- "${TEMP}"
HELP=false VERSION=false CHECK_MK=false
Verbosity level
VERBOSE=0
Trace activated
TRACE=false LOG=/tmp/${APPL_NAME}.log INSTANCE_HOME= DATABASE_NAME= while true; do case "${1}" in -d | --database ) DATABASE_NAME=$(echo ${2} | cut -d' ' -f1) ; shift 2 ;; -h | --help ) HELP=true ; shift ;; -i | --instance ) INSTANCE_HOME=$(echo ${2} | cut -d' ' -f1) ; shift 2 ;; -K | --mk ) CHECK_MK=true ; shift ;; -T | --trace ) TRACE=true ; shift ;; -v | --verbose ) VERBOSE=$(( ${VERBOSE} + 1 )) ; shift ;; -V | --version ) VERSION=true ; shift ;; -- ) shift; break ;; * ) break ;; esac done fi
if [[ ${TRACE} == true ]] ; then echo ">>>>>" >> ${LOG} date >> ${LOG} echo "Instance at ${INSTANCE_HOME}" >> ${LOG} echo "PID ${$}" >> ${LOG} fi
ECHO="help:${HELP}, version:${VERSION}, verbose:${VERBOSE}"
if [[ ${VERBOSE} -ge 2 ]] ; then echo ${ECHO} fi
if [[ ${TRACE} == true ]] ; then echo "PARAMS:${ECHO}" >> ${LOG} fi
if [[ ${CONTINUE} == true && ${HELP} == true ]] ; then print_help ${APPL_NAME} RETURN=${UNKNOWN} CONTINUE=false fi
if [[ ${CONTINUE} == true && ${VERSION} == true ]] ; then print_revision ${APPL_NAME} RETURN=${UNKNOWN} CONTINUE=false fi
if [[ ${CONTINUE} == true && ${INSTANCE_HOME} == "" ]] ; then print_usage ${APPL_NAME} RETURN=${UNKNOWN} CONTINUE=false fi
if [[ ${CONTINUE} == true && ${DATABASE_NAME} == "" ]] ; then print_usage ${APPL_NAME} RETURN=${UNKNOWN} CONTINUE=false fi
if [[ ${CONTINUE} == true ]] ; then if [[ -d ${INSTANCE_HOME} && -e ${INSTANCE_HOME}/sqllib/db2profile ]] ; then
Load the DB2 profile.
else OUTPUT="Instance directory is invalid." RETURN=${UNKNOWN} CONTINUE=false fi fi
if [[ ${CONTINUE} == true ]] ; then COMMAND_DATABASE="db2 list db directory" if [[ ${VERBOSE} -ge 2 ]] ; then echo "COMMAND: ${COMMAND_DATABASE}" fi DATABASE=$(${COMMAND_DATABASE}) if [[ ${TRACE} == true ]] ; then echo "RESULT:'${DATABASE}'" >> ${LOG} fi DATABASE=$(printf '%s\n' "${DATABASE}" | awk '/Database alias/ {print $4}' | grep -iw ${DATABASE_NAME}) if [[ ${VERBOSE} -ge 3 ]] ; then echo "RESULT:'${DATABASE}'" fi
if [[ ${DATABASE} == "" ]] ; then OUTPUT="The database ${DATABASE_NAME} is not cataloged." RETURN=${UNKNOWN} CONTINUE=false fi fi
if [[ ${CONTINUE} == true ]] ; then COMMAND_ACTIVE="db2 list active databases" if [[ ${VERBOSE} -ge 2 ]] ; then echo "COMMAND: ${COMMAND_ACTIVE}" fi ACTIVE=$(${COMMAND_ACTIVE}) if [[ ${TRACE} == true ]] ; then echo "RESULT:'${ACTIVE}'" >> ${LOG} fi ACTIVE=$(printf '%s\n' "${ACTIVE}" | awk '/Database name/ {print $4}' | grep -iw ${DATABASE_NAME}) if [[ ${VERBOSE} -ge 3 ]] ; then echo "RESULT:'${ACTIVE}'" fi
if [[ ${ACTIVE} == "" ]] ; then OUTPUT_ACTIVE="The database is not active. " LONG_OUTPUT="${OUTPUT_ACTIVE}" LONG_PERFORMANCE_1="'Database_Active'=0.2;0.5" else OUTPUT_ACTIVE="The database is active. " LONG_OUTPUT="${OUTPUT_ACTIVE}" LONG_PERFORMANCE_1="'Database_Active'=0.8;0.5" fi
COMMAND_CONNECTABLE="db2 -a connect to ${DATABASE_NAME}" if [[ ${VERBOSE} -ge 2 ]] ; then echo "COMMAND: ${COMMAND_CONNECTABLE}" fi CONNECTABLE=$(${COMMAND_CONNECTABLE}) if [[ ${TRACE} == true ]] ; then echo "RESULT:'${CONNECTABLE}'" >> ${LOG} fi CONNECTABLE=$(printf '%s\n' "${CONNECTABLE}" | awk '/sqlcode/ {print $7}') if [[ ${VERBOSE} -ge 3 ]] ; then echo "RESULT:'${CONNECTABLE}'" fi
if [[ ${CONNECTABLE} -eq 0 ]] ; then OUTPUT="OK Connection to database ${DATABASE_NAME}. "${OUTPUT_ACTIVE} RETURN=${OK} PERFORMANCE="'Connectable_Database'=0.9;0.6;0.3" elif [[ ${CONNECTABLE} -eq -20157 ]] ; then OUTPUT="The database is in quiesce mode. "${OUTPUT_ACTIVE} RETURN=${WARNING} PERFORMANCE="'Connectable_Database'=0.4;0.6;0.3" else OUTPUT="A connection to database ${DATABASE_NAME} was not succesful. "${OUTPUT_ACTIVE} LONG_OUTPUT="${LONG_OUTPUT} ${CONNECTABLE}" RETURN=${CRITICAL} PERFORMANCE="'Connectable_Database'=0.1;0.6;0.3" fi
Check for HADR Window replay
COMMAND_ROLE="db2 get db cfg for ${DATABASE_NAME}" if [[ ${VERBOSE} -ge 2 ]] ; then echo "COMMAND: ${COMMAND_ROLE}" fi ROLE=$(${COMMAND_ROLE}) if [[ ${TRACE} == true ]] ; then echo "RESULT:'${ROLE}'" >> ${LOG} fi ROLE=$(printf '%s\n' "${ROLE}" | awk '/HADR database role/ {print $5}') if [[ ${VERBOSE} -ge 3 ]] ; then echo "RESULT:'${ROLE}'" fi if [[ ${ROLE} == "STANDBY" ]] ; then COMMAND_REPLAY="db2pd -db wfscpd -hadr" if [[ ${VERBOSE} -ge 2 ]] ; then echo "COMMAND: ${COMMAND_REPLAY}" fi REPLAY=$(${COMMAND_REPLAY}) if [[ ${TRACE} == true ]] ; then echo "RESULT:'${REPLAY}'" >> ${LOG} fi REPLAY=$(printf '%s\n' "${REPLAY}" | awk '/^Active/ {print "active"}') if [[ ${VERBOSE} -ge 3 ]] ; then echo "RESULT:'${REPLAY}'" fi if [[ ${REPLAY} == "active" ]] ; then LONG_PERFORMANCE_2="HADR-replay=0.3;0.5" else LONG_PERFORMANCE_2="HADR-replay=0.7;0.5" fi fi LONG_PERFORMANCE="${LONG_PERFORMANCE_1} ${LONG_PERFORMANCE_2}" if [[ ${LONG_PERFORMANCE_2} == "" ]] ; then PERF_MK="${PERFORMANCE}|${LONG_PERFORMANCE_1}" else PERF_MK="${PERFORMANCE}|${LONG_PERFORMANCE_1}|${LONG_PERFORMANCE_2}" fi fi
Prints the output.
if [[ ${OUTPUT} == "" ]] ; then OUTPUT="Note: The test was not executed." fi
Builds the output.
if [[ ${CHECK_MK} == true ]] ; then echo "${RETURN} databaseConnection-${INSTANCE_NAME}-${DATABASE_NAME} ${PERF_MK} ${OUTPUT}" else echo -e "${OUTPUT}|${PERFORMANCE}\n${LONG_OUTPUT}|${LONG_PERFORMANCE}" fi
Returns the error code.
if [[ ${VERBOSE} -ge 2 ]] ; then echo "Return code: ${RETURN}" fi
Best regards Stephan