#!/usr/bin/ksh93.att ################################################################# #### !!! Download the latest version of ksh93 to run this script. ################################################################# function usagemsg_ckdns_k93 { print -u 2 -- " Program: ckdns_k93 Check a host name for DNS resolution, log an error if it does not resolve. Usage: ${1##*/} [-?] [-vV] [-n nodeName] [-t type] [-c class] [-l logFile] Where: -n nodeName = Node name of LISTEN'er to test -t type = Log Message type: ERROR, WARN (Default:ERROR) -c class = Log Message class: O, S, U (Default:O) -l logFile = Log file name (Default:/var/log/ckdns.log) -v = Verbose Mode -V = Very Verbose Mode Author: Dana French (dfrench@mtxia.com) Copyright 2009 by Dana French: All Rights Reserved \"AutoContent\" enabled " } ################################################################ #### #### Description: #### #### This function checks a hostname provided by the user to #### determine if it will resolve through the DNS. If not, #### an error is logged. The log file can be monitored by #### various tools and utilities and notifications can be #### sent as necessary. This function is usually called from #### another script which is scheduled to run periodically #### through cron. #### #### Assumptions: #### #### This function assumes it has access to the standard Unix #### commands through the directories specified in the PATH #### environment variable. Unix commands required are: #### #### date #### host #### hostname #### #### Dependencies: #### #### This function requires a recent version of ksh93. The version #### shipped with AIX is INSUFFICIENT to run this script. Download #### the latest ksh93 AIX binary from http://www.kornshell.com #### #### If you download the latest ksh93 AIX binary, place it on your #### system at the location /usr/bin/ksh93.att. Do not overwrite #### the existing /usr/bin/ksh93. Then modify the shebang line #### to use #!/usr/bin/ksh93.att #### #### Products: #### #### This function records error messages to a default or #### user specified log file. The return code from this #### function can be used to indicate success or failure to #### resolve the user specified hostname. #### #### Configured Usage: #### #### This function may be executed from the command line or #### included in a script function library. #### #### Details: #### ################################################################ function ckdns_k93 { typeset VERSION="1.0" typeset TRUE="0" typeset FALSE="1" typeset VERBOSE="${FALSE}" typeset VERYVERB="${FALSE}" typeset DATETIME=$( date +"%m/%d/%Y %H:%M:%S" ) typeset SYSTEM="$( hostname )" typeset TYPE="${TYPE:-ERROR}" typeset CLASS="${CLASS:-O}" typeset LOGFILE="/var/log/ckdns.log" typeset NODENAME="" typeset TORF="" typeset SOURCE typeset DESCRIPTION typeset MSG while getopts ":vVp#n:t:c:l:a:" OPTION do case "${OPTION}" in 'n') NODENAME="${OPTARG}";; 't') TYPE="${OPTARG}";; 'c') CLASS="${OPTARG}";; 'l') LOGFILE="${OPTARG}";; 'v') VERBOSE="${TRUE}";; 'V') VERYVERB="${TRUE}";; [?:#]) usagemsg_ckdns_k93 "${0}" && return 1 ;; esac done shift $(( ${OPTIND} - 1 )) (( VERYVERB == TRUE )) && set -x (( VERBOSE == TRUE )) && print -u 2 -- "#\n# Function Name..........................: ${0}" (( VERBOSE == TRUE )) && print -u 2 -- "# Version................................: ${VERSION}" ################################################################ trap "usagemsg_ckdns_k93 ${0}" EXIT if [[ "_${NODENAME}" == "_" ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# ERROR: node name must be specified" return 1 fi if ! [[ "_${TYPE}" == _@(ERROR|WARN) ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# ERROR: Invalid log message type" return 1 fi if ! [[ "_${CLASS}" == _@(O|S|U) ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# ERROR: Invalid log message class" return 1 fi if ! [[ -d "${LOGFILE%/*}" ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# ERROR: Invalid log file directory: ${LOGFILE%/*}" return 1 fi trap "-" EXIT ################################################################ #### #### If verbose mode was specified by the user on the command #### line, output the configuration information to the #### standard error stream STDERR. [[ "_${NODENAME}" == "_" ]] && TORF="Not Specified" || TORF="${NODENAME}" (( VERBOSE == TRUE )) && print -u 2 -- "# Network Service Name...................: ${TORF}" (( VERBOSE == TRUE )) && print -u 2 -- "# Log message type.......................: ${TYPE}" (( VERBOSE == TRUE )) && print -u 2 -- "# Log message class......................: ${CLASS}" ################################################################ SOURCE="${NODENAME}" DESCRIPTION="DNS name resolution failed for ${NODENAME}" MSG="${DATETIME} ${SYSTEM} ${SOURCE} ${TYPE} ${CLASS} ${DESCRIPTION}" if host ${NODENAME} > /dev/null 2>&1 then (( VERBOSE == TRUE )) && print -- "# Successful name resolution: ${NODENAME}" return 0 else (( VERBOSE == TRUE )) && print -u 2 -- "# ERROR: Unsuccessful name resolution: ${NODENAME}" print -- "${MSG}" >> "${LOGFILE}" return 4 fi exec 3>&- return 5 } ################################################################ ckdns_k93 "${@}" # ckdns_k93 -v -n mailhost.mtxia.com -t ERROR # ckdns_k93 -v -n www.mtxia.com -t WARN