#!/bin/ksh # # $Id: dhcpc.ksh,v 1.3 2007/03/16 16:13:25 mike Exp $ # # Hobbit Monitor Client DHCP Server Test # - Allows the Hobbit client to test DHCP servers. # - Useful when the Hobbit server is not on the correct network and # DHCP_RELAYs cannot be installed. # - Requires the dhcping utility: http://www.mavetju.org/unix/general.php # (Make sure dhcping is setuid root.) # - Test via "DEBUG=y bbcmd dhcpc.ksh hostname" # # On the Hobbit SERVER: # Add to the hobbitserver.cfg to the TEST2RRD and GRAPHS variables: dhcp=tcp # This will show a graph of response time. Restart the Hobbit server for # the change to take effect. # # Add to columndoc.csv: # dhcp;The dhcp column shows the status of the DHCP service on the host. DHCP is used to automatically assign IP addresses, to deliver TCP/IP stack configuration parameters such as the subnet mask and default router, and to provide other configuration information such as the addresses for printer, time and news servers.; # # # On the Hobbit CLIENT: # Add to clientlaunch.cfg: # [dhcpc] # ENVFILE $HOBBITCLIENTHOME/etc/hobbitclient.cfg # CMD $HOBBITCLIENTHOME/ext/dhcpc.ksh [ ... ] # LOGFILE $HOBBITCLIENTHOME/logs/dhcpc.log # INTERVAL 1m # # EXIT CODE: # 0 = success # 1 = print_help function (or incorrect commandline) # 2 = ERROR: Must be root. # 9 = ERROR: Missing Hobbit environment. Please use bbcmd. # 10 = ERROR: Unsupported platform $OS. # 11 = ERROR: Could not determine Ethernet MAC Address. # AUTHOR="Mike Arnold " VERSION=20070316 LOCATION="http://www.razorsedge.org/~mike/software/dhcpc.ksh" # if [ $DEBUG ]; then set -x; fi # ##### START CONFIG ################################################### TIMEOUT=10 ##### STOP CONFIG #################################################### PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/bin:/usr/local/sbin:${PATH} COLUMN=dhcp # Function to print the help screen. print_help () { print "Usage: $1 [-i|--interface eth0] [ ... ]" print " $1 [-h|--help]" print " $1 [-v|--version]" print " ex. $1 dhcpserver1" exit 1 } # Function to check for root priviledges. check_root () { if [[ `$ID | $AWK -F= '{print $2}' | $AWK -F"(" '{print $1}' 2>/dev/null` -ne 0 ]]; then print "You must have root priviledges to run this program." exit 2 fi } # If the variable DEBUG is set, then turn on tracing. # http://www.research.att.com/lists/ast-users/2003/05/msg00009.html if [ $DEBUG ]; then # This will turn on the ksh xtrace option for mainline code set -x # This will turn on the ksh xtrace option for all functions typeset +f | while read F junk do typeset -ft $F done unset F junk fi # Process arguments. while [[ $1 = -* ]]; do case $1 in -i|--interface) shift IF=$1 ;; -h|--help) print_help "$(basename $0)" ;; -v|--version) print "\tHobbit Monitor Client DHCP Server Test" print "\t$LOCATION" print "\tVersion: $VERSION" print "\tWritten by: $AUTHOR" exit 0 ;; *) print_help "$(basename $0)" ;; esac shift done # Check to see if we have no parameters. if [[ ! $# -ge 1 ]]; then print_help "$(basename $0)"; fi # Lets not bother continuing unless we have the privs to do something. #check_root # main # If we are running outside of the Hobbit environment, then complain. if [ -z "$HOBBITCLIENTHOME" ]; then echo "ERROR: Missing Hobbit environment. Please use bbcmd." echo ' Test via "DEBUG=y bbcmd dhcpc.ksh hostname"' exit 9 fi # Set the rest of the command-line arguements to a variable. SERVER=$@ # Gather platform dependent information. OS=$(uname -s) case $OS in HP-UX) IFACE=${IF:-lan0} MACADDR=$(/usr/sbin/lanscan) RETVAL=$? MACADDR=$(echo "$MACADDR" | $AWK /$IFACE/'{print (substr($2,3,2))":"(substr($2,5,2))":"(substr($2,7,2))":"(substr($2,9,2))":"(substr($2,11,2))":"(substr($2,13,2))}') TIME=/usr/bin/time ;; AIX) IFACE=${IF:-en0} MACADDR=$(/usr/bin/entstat $IFACE) RETVAL=$? MACADDR=$(echo "$MACADDR" | $AWK '/Hardware Address:/{print $3}') TIME=/usr/bin/time ;; Linux) IFACE=${IF:-eth0} MACADDR=$(/sbin/ip link ls $IFACE) RETVAL=$? MACADDR=$(echo "$MACADDR" | $AWK '/link\/ether/{print $2}') TIME="/usr/bin/time -p" ;; OSF1) IFACE=${IF:-tu0} MACADDR=$(netstat -i) RETVAL=$? MACADDR=$(echo "$MACADDR" | $AWK "/$IFACE.*Link/{print \$4}") TIME=/usr/bin/time ;; SunOS) IFACE=${IF:-hme0} MACADDR= RETVAL=$? TIME=/bin/ptime ;; *) echo "ERROR: Unsupported platform $OS." exit 10 ;; esac if [ $RETVAL -ne 0 ]; then echo "ERROR: Could not determine client's Ethernet MAC Address." exit 11 fi # Iterate through hosts given on the command-line. for DMACHINEDOTS in $SERVER; do DMACHINE=$(echo $DMACHINEDOTS | $SED 's|\.|,|g') # dhcping reponses: # Got answer from: $DMACHINEDOTS # $? = 0 # no answer # $? = 1 CMDOUT=$($TIME dhcping -v -t $TIMEOUT -h $MACADDR -s $DMACHINEDOTS 2>&1) RETVAL=$? SEC=$(echo "$CMDOUT" | $AWK '/^real/{print $2}') # Form the correct message based on dhcping's success. if [ $RETVAL -eq 0 ]; then COLOR=green MSG="DHCP query succeeded $(echo "$CMDOUT" | $HEAD -1) Tested from $MACHINEDOTS Seconds: $SEC " elif [ $RETVAL -eq 1 ]; then COLOR=red MSG="DHCP query failed $(echo "$CMDOUT" | $HEAD -1) Tested from $MACHINEDOTS Seconds: $SEC " else COLOR=yellow MSG="DHCP query error $CMDOUT Tested from $MACHINEDOTS Seconds: $SEC " fi # Send the message to the Hobbit server. if [ $DEBUG ]; then echo $BB $BBDISP "status $DMACHINE.$COLUMN $COLOR $($DATE) $MSG" else $BB $BBDISP "status $DMACHINE.$COLUMN $COLOR $($DATE) $MSG" fi done exit 0