#!/bin/sh # #!F:tdsl-ip-up-delaytime-collector # #!P:/usr/local/sbin # #!S:root:root 555 # #!D:Program to collect the T-DSL ip-up delaytime values # #!C:(P) & (C) 2001 by Peter Bieringer # # Changes to # 20010106: Initial (template was triggersit-ip6) # 20010107: [Feature]: Improve statistics # 20010108: [Feature]: Add HTTPserver to logline, add ClassC to statistic # 20010108: [Feature]: Add exit code for wget, add hops from traceroute to log # 20010109: [Feature]: Change statistic output to percent # 20010109: [Cosmetic]: Order beep code new (one before traceroute, one after) # 20010109: [MinorBug]: 2. Unixtime was also named "Start" on log, now "End" # 20010110: [Feature]: Create output on ClassC addresses for statistic graphic ##--------------- need to review below this line # Name of syslog facility DEFAULT_NameLogfile="/var/log/tdsl-ip-up-delaytime" # Default debug option DEFAULT_DEBUG="0" # Default beep mode DEFAULT_BeepMode="yes" # Specify the IPv4 address of a HTTP server for connection test # www.heise.de DEFAULT_IPv4TestHTTP="193.99.144.71" # Specify the network interface (device) of your host connected to the Internet # needed to grab the dynamic assigned IPv4 address of your dialup connection DEFAULT_DEVNAME="ppp0" # Option for statistic OPTION_STATISTICDELAYTRIGGERS="sum 5 15 30 45 60 75 90 120 240" ##---- Defines of binaries, normally not to be reviewed (on RedHat 6.2 systems) # Specify binary of 'ping' BIN_PING="/bin/ping" # Specify binary of 'date' BIN_DATE="/bin/date" # Specify binary of 'cat' BIN_CAT="/bin/cat" # Specify binary of 'awk' BIN_AWK="/bin/awk" # Specify binary of 'wc' BIN_WC="/usr/bin/wc" # Specify binary of 'wget' BIN_WGET="/usr/bin/wget" # Specify binary of 'sort' BIN_SORT="/bin/sort" # Specify binary of 'uniq' BIN_UNIQ="/usr/bin/uniq" # Specify binary of 'grep' BIN_GREP="/bin/grep" # Specify binary of 'printf' BIN_PRINTF="/usr/bin/printf" # Specify binary of 'cut' BIN_CUT="/usr/bin/cut" # Specify binary of 'traceroute' BIN_TRACEROUTE="/usr/sbin/traceroute" # Specify binary of 'beep' (optional, see http://www.johnath.com/beep/ for package) BIN_BEEP="/usr/bin/beep" ##---- Related defines, normally not to be reviewed below this line # Name for logging LOGNAME="t-dsl" ##--------------- normally nothing to change below this line # Set umask to prevent race conditions umask 077 # Displays information function info() { grep "^#!D" $0 | sed 's/^#!D://g' >&2 grep "^#!C" $0 | sed 's/^#!C://g' >&2 grep "^#!V" $0 | sed 's/^#!V://g' >&2 echo } # Help information function help() { cat <>$OPTION_NameLogfile else if [ "$priority" = "warn" -o "$priorty" = "err" -o "$priorty" = "crit" -o "$priorty" = "emerg" -o "$priorty" = "alert" ]; then echo -en "\a" >&2 fi if ! [ "$OPTION_QUIET" = "0" ]; then echo "$loginfo: $logmessage" >&2 else echo "$logmessage" >&2 fi fi } # Function "Get options" function getoptions() { # Returnvalue 1: display help OPTION_QUIET="0" OPTION_UseLogfile="no" OPTION_RunStatistic="no" OPTION_RunStatistic4SpreadSheet="no" OPTION_DEBUG="$DEFAULT_DEBUG" OPTION_BeepMode="$DEFAULT_BeepMode" while [ ! -z "$1" ]; do # strip "-" OPTION=`echo $1 | sed 's/^-//g'` #echo $OPTION if [ "$OPTION" = '?' -o "$OPTION" = 'h' ]; then return 1 elif [ "$OPTION" = 'ipv4' ]; then shift if [ $# -ge 1 ]; then OPTION_IPv4TestHTTP="$1" shift else log err "Missing IPv4 address of HTTP server!" return 1 fi elif [ "$OPTION" = "i" ]; then shift if [ $# -ge 1 ]; then OPTION_DEVNAME="$1" shift else log err "Missing interface name!" return 1 fi elif [ "$OPTION" = "d" ]; then shift if [ $# -ge 1 ]; then OPTION_DEBUG="$1" shift else log err "Missing debug level!" return 1 fi elif [ "$OPTION" = "lfn" ]; then shift if [ $# -ge 1 ]; then OPTION_NameLogfile="$1" shift else log err "Missing logfile name!" return 1 fi elif [ "$OPTION" = "q" ]; then # Option quiet OPTION_QUIET="1" shift elif [ "$OPTION" = "nb" ]; then # Option beep mode OPTION_BeepMode="no" shift elif [ "$OPTION" = "ulf" ]; then # Option use logfile OPTION_UseLogfile="yes" shift elif [ "$OPTION" = "rs" ]; then # Option run statistics OPTION_RunStatistic="yes" shift elif [ "$OPTION" = "rsss" ]; then # Option run statistics 4 spreadsheet OPTION_RunStatistic4SpreadSheet="yes" shift else log err "Option '$1' isn't understood!" return 1 fi done } # Beep functions for background execution, i.e. for use in ip-up.local function do_beep() { code=$1 if [ "$OPTION_BeepMode" = "yes" ]; then # beep relating to exit codes if [ ! -z "$BIN_BEEP" ] ; then # use of a controllable beeper case $code in 0) # success $BIN_BEEP -f 4200 & ;; 1) # option error $BIN_BEEP -f 500 -n -f 250 -n -f 125 & ;; 2) # mandatory error $BIN_BEEP -f 500 -r 5 & ;; 3) # temp error $BIN_BEEP -f 500 -n -f 250 & ;; 4) # start test $BIN_BEEP -f 3000 & ;; 5) # success $BIN_BEEP -f 3600 & ;; esac else case $code in 0) # success (echo -en "\a" >/dev/console) & ;; 1) # option error (echo -en "\a" >/dev/console; sleep 1; echo -en "\a" >/dev/console) & ;; 2) # mandatory error (echo -en "\a" >/dev/console; sleep 1; echo -en "\a" >/dev/console; sleep 1; echo -en "\a" >/dev/console; sleep 1; echo -en "\a" >/dev/console) & ;; 3) # temp error (echo -en "\a" >/dev/console; sleep 1; echo -en "\a" >/dev/console; sleep 1; echo -en "\a" >/dev/console) & ;; 4) # start test (echo -en "\a" >/dev/console; sleep 1; echo -en "\a" >/dev/console; sleep 1; echo -en "\a" >/dev/console ; sleep 1; echo -en "\a" >/dev/console) & ;; esac fi fi } ####--------------------------------- Start # Get options getoptions $* if [ $? -gt 0 ]; then info help do_beep 1 exit 1 fi #Test whether logfile name is specified, default is $DEFAULT_SyslogFacility if [ -z "$OPTION_NameLogfile" ] ; then OPTION_NameLogfile=$DEFAULT_NameLogfile fi #Test whether IP is specified, default is $DEFAULT_IPv4TestHTTP if [ -z "$OPTION_IPv4TestHTTP" ] ; then OPTION_IPv4TestHTTP=$DEFAULT_IPv4TestHTTP fi #Test whether Interface is specified, default is $DEFAULT_DEVNAME if [ -z "$OPTION_DEVNAME" ] ; then OPTION_DEVNAME=$DEFAULT_DEVNAME fi #Test whether needed binares exist for testbin in $BIN_ECHO $BIN_DATE $BIN_CAT $BIN_AWK $BIN_WGETi $BIN_GREP $BIN_SORT $BIN_UNIQ $BIN_PRINTF; do if [ ! -x $testbin ]; then log err "Needed binary '$testbin' missing, check whether installed or change location in this script!" do_beep 2 exit 2 fi done ## Interrupt here, if run statistics if [ "$OPTION_RunStatistic" = "yes" ]; then # Run statistics now # Disable logfile for interactive utility OPTION_UseLogfile="no" if [ ! -f $OPTION_NameLogfile ]; then log err "Specified logfile '$OPTION_NameLogfile' doesn't exist - stop!" fi log notice "Run statistic now at `date`" # Statistic header $BIN_PRINTF "%-20s:" "Delay over in [s]" for trigger in $OPTION_STATISTICDELAYTRIGGERS; do if [ "$trigger" = "sum" ]; then $BIN_PRINTF "%5s" "sum" else $BIN_PRINTF "%5d" $trigger fi done echo ## Complete statistic # Count lines over trigger value $BIN_PRINTF "%-20s:" "All data" for trigger in $OPTION_STATISTICDELAYTRIGGERS; do count=`$BIN_CAT $OPTION_NameLogfile | $BIN_GREP '!DateStart' | $BIN_AWK -F! '{ print $2 }' | while read tag datestart tag dateend tag ipv4 tag timeout rest; do if [ "$trigger" = sum ]; then echo "timeout"; elif [ $timeout -gt $trigger ]; then echo $timeout; fi; done | $BIN_WC -l` $BIN_PRINTF "%5d" $count done echo # Count lines over trigger value in percent $BIN_PRINTF "%-20s:" "All data (%)" for trigger in $OPTION_STATISTICDELAYTRIGGERS; do count=`$BIN_CAT $OPTION_NameLogfile | $BIN_GREP '!DateStart' | $BIN_AWK -F! '{ print $2 }' | while read tag datestart tag dateend tag ipv4 tag timeout rest; do if [ "$trigger" = sum ]; then echo "timeout"; elif [ $timeout -gt $trigger ]; then echo $timeout; fi; done | $BIN_WC -l` if [ "$trigger" = sum ]; then sum=$[ $count ]; $BIN_PRINTF "%5d" $count else percent=$[ $[ $count * 100 ] / $sum ] $BIN_PRINTF "%4d%s" $percent "%" fi done echo ## Per day statistic # First, got all days echo "Per day:" $BIN_CAT $OPTION_NameLogfile | $BIN_GREP '!DateStart' | $BIN_AWK -F! '{ print $2 }' | $BIN_AWK '{ print $2 }' | $BIN_AWK -F- '{ print $1 }'| $BIN_SORT | $BIN_UNIQ | while read day; do $BIN_PRINTF "%-20s:" "$day" for trigger in $OPTION_STATISTICDELAYTRIGGERS; do count=`$BIN_CAT $OPTION_NameLogfile | $BIN_GREP '!DateStart' | $BIN_AWK -F! '{ print $2 }' | $BIN_GREP $day | while read tag datestart tag dateend tag ipv4 tag timeout rest; do if [ "$trigger" = sum ]; then echo "timeout"; elif [ $timeout -gt $trigger ]; then echo $timeout; fi; done | $BIN_WC -l` if [ "$trigger" = sum ]; then sum=$[ $count ]; $BIN_PRINTF "%5d" $count else percent=$[ $[ $count * 100 ] / $sum ] $BIN_PRINTF "%4d%s" $percent "%" fi done echo done ## Per hour statistic # First, got all hour echo "Per hour:" $BIN_CAT $OPTION_NameLogfile | $BIN_GREP '!DateStart' | $BIN_AWK -F! '{ print $2 }' | $BIN_AWK '{ print $2 }' | $BIN_AWK -F- '{ print $2 }'| $BIN_CUT -c 1-2 | $BIN_SORT | $BIN_UNIQ | while read hour; do $BIN_PRINTF "%-20s:" "Hour $hour" for trigger in $OPTION_STATISTICDELAYTRIGGERS; do count=`$BIN_CAT $OPTION_NameLogfile | $BIN_GREP '!DateStart' | $BIN_AWK -F! '{ print $2 }' | $BIN_GREP "\-$hour.....DateEnd" | while read tag datestart tag dateend tag ipv4 tag timeout rest; do if [ "$trigger" = sum ]; then echo "timeout"; elif [ $timeout -gt $trigger ]; then echo $timeout; fi; done | $BIN_WC -l` if [ "$trigger" = sum ]; then sum=$[ $count ]; $BIN_PRINTF "%5d" $count else percent=$[ $[ $count * 100 ] / $sum ] $BIN_PRINTF "%4d%s" $percent "%" fi done echo done ## Per Class B IPv4 address statistic # First, got all addresses echo "Per IPv4-ClassB:" $BIN_CAT $OPTION_NameLogfile | $BIN_GREP '!DateStart' | $BIN_AWK -F! '{ print $2 }' | $BIN_AWK '{ print $6 }' | $BIN_AWK -F. '{ print $1 "." $2 }'| $BIN_SORT | $BIN_UNIQ | while read ipv4classb; do $BIN_PRINTF "%-20s:" "$ipv4classb.0.0/16" for trigger in $OPTION_STATISTICDELAYTRIGGERS; do count=`$BIN_CAT $OPTION_NameLogfile | $BIN_GREP '!DateStart' | $BIN_AWK -F! '{ print $2 }' | $BIN_GREP "LocalIPofPPP $ipv4classb\." | while read tag datestart tag dateend tag ipv4 tag timeout rest; do if [ "$trigger" = sum ]; then echo "timeout"; elif [ $timeout -gt $trigger ]; then echo $timeout; fi; done | $BIN_WC -l` if [ "$trigger" = sum ]; then sum=$[ $count ]; $BIN_PRINTF "%5d" $count else percent=$[ $[ $count * 100 ] / $sum ] $BIN_PRINTF "%4d%s" $percent "%" fi done echo done ## Per Class C IPv4 address statistic # First, got all addresses echo "Per IPv4-ClassC:" $BIN_CAT $OPTION_NameLogfile | $BIN_GREP '!DateStart' | $BIN_AWK -F! '{ print $2 }' | $BIN_AWK '{ print $6 }' | $BIN_AWK -F. '{ print $1 "." $2 "." $3 }'| $BIN_SORT | $BIN_UNIQ | while read ipv4classc; do $BIN_PRINTF "%-20s:" "$ipv4classc.0/24" for trigger in $OPTION_STATISTICDELAYTRIGGERS; do count=`$BIN_CAT $OPTION_NameLogfile | $BIN_GREP '!DateStart' | $BIN_AWK -F! '{ print $2 }' | $BIN_GREP "LocalIPofPPP $ipv4classc\." | while read tag datestart tag dateend tag ipv4 tag timeout rest; do if [ "$trigger" = sum ]; then echo "timeout"; elif [ $timeout -gt $trigger ]; then echo $timeout; fi; done | $BIN_WC -l` if [ "$trigger" = sum ]; then sum=$[ $count ]; $BIN_PRINTF "%5d" $count else percent=$[ $[ $count * 100 ] / $sum ] $BIN_PRINTF "%4d%s" $percent "%" fi done echo done exit fi ## Interrupt here, if run statistics 4 spreadsheet # Now the spaghetti code increases... if [ "$OPTION_RunStatistic4SpreadSheet" = "yes" ]; then # Run statistics now # Disable logfile for interactive utility OPTION_UseLogfile="no" if [ ! -f $OPTION_NameLogfile ]; then log err "Specified logfile '$OPTION_NameLogfile' doesn't exist - stop!" fi log notice "Run statistics for SpreadSheet now at `date`" # First, fill time variable i=0 delaytriggers=`while [ $i -lt 120 ]; do i=$[ $i + 1 ]; echo -n " $i"; done` # echo "$delaytriggers" ## Per Class C IPv4 address statistic # First, got all addresses echo "Per IPv4-ClassC: in %" >&2 classcnets=`$BIN_CAT $OPTION_NameLogfile | $BIN_GREP '!DateStart' | $BIN_AWK -F! '{ print $2 }' | $BIN_AWK '{ print $6 }' | $BIN_AWK -F. '{ print $1 "." $2 "." $3 }'| $BIN_SORT | $BIN_UNIQ | while read ipv4classc; do echo -n " $ipv4classc"; done` #echo "$classcnets" # Print header row for r in "delay[s]" $classcnets; do $BIN_PRINTF " %15s" $r done echo # Print rows for trigger in $delaytriggers; do # first column is delay $BIN_PRINTF " %15s" $trigger # Next columns are the networks for classc in $classcnets; do count=`$BIN_CAT $OPTION_NameLogfile | $BIN_GREP '!DateStart' | $BIN_AWK -F! '{ print $2 }' | $BIN_GREP "LocalIPofPPP $classc\." | while read tag datestart tag dateend tag ipv4 tag timeout rest; do if [ $timeout -lt $trigger ]; then echo $timeout; fi; done | $BIN_WC -l` sum=`$BIN_CAT $OPTION_NameLogfile | $BIN_GREP '!DateStart' | $BIN_AWK -F! '{ print $2 }' | $BIN_GREP "LocalIPofPPP $classc\." | $BIN_WC -l` percent=$[ $[ $count * 100 ] / $sum ] $BIN_PRINTF " %15d" $percent done echo done exit fi ## No statistic, go on here: # Show information if ! [ "$OPTION_QUIET" = "1" ]; then info else log notice "Test starts now" fi # Test for optional beeper if [ ! -x $BIN_BEEP ]; then BIN_BEEP="" elif [ "$OPTION_BeepMode" = "yes" ]; then # Test, if beep can be executed by user if ! $BIN_BEEP -l 1 -f 10 >/dev/null 2>&1; then log warn "The beeper binary '$BIN_BEEP' has not set the SUID bit, users cannot" log notice " trigger the sound output!" log notice "You can fix this by executing 'chmod u+s $BIN_BEEP' but keep in mind that" log notice " this can be a local securtiy hole - for now turn back to simple console beep!" BIN_BEEP="" fi fi # Beep mode tester if [ ! $[ $OPTION_DEBUG & 16 ] = 0 ]; then log notice "Beep mode tester" OPTION_BeepMode="yes" if [ -z "$BIN_BEEP" ]; then log err "Sorry, beep binary doesn't exist, see http://www.johnath.com/beep/ for information" exit 2 fi CODE_BEEP=$[ $OPTION_DEBUG & 7 ] log notice "Code for beeper is '$CODE_BEEP'" do_beep $CODE_BEEP exit 1 fi # Show info log info "HTTP server : '$OPTION_IPv4TestHTTP'" log info "Device : '$OPTION_DEVNAME'" if [ "$OPTION_DEBUG" -gt 0 ]; then log notice "Debuglevel : '$OPTION_DEBUG'" else log info "Debuglevel : '$OPTION_DEBUG'" fi log info "BeepMode : '$OPTION_BeepMode'" log info "UseLogfile : '$OPTION_UseLogfile'" log info "NameLogfile : '$OPTION_NameLogfile'" ###Get local IPv4 address of PPP interface # Test, whether given interface exists if ! grep -q "$OPTION_DEVNAME:" /proc/net/dev; then log err "Given interface '$OPTION_DEVNAME' doesn't exist - stop!" do_beep 1 exit 1 fi # Get own IPv4 address for the interface connected to the Internet # (This is surely not the best method, but an easy one...) OPTION_DEVIP4=`/sbin/ifconfig $OPTION_DEVNAME | grep "inet addr:" | awk -F ":" '{ print $2 }' | awk '{ print $1 }'` # Address valid? if [ -z $OPTION_DEVIP4 ] ; then log err "No IPv4 address is given on device '$OPTION_DEVNAME'!" log err " Possible you haven't dial up until now!\n" do_beep 3 exit 3 fi log info "IPv4 : '$OPTION_DEVIP4'" ### Now build up a simple HTTP connection connection if [ $[ $OPTION_DEBUG & 1 ] = 0 ]; then log debug "Run a simple request to the specified IPv4 address of a HTTP server" do_beep 4 TIME_START="`$BIN_DATE '+%s'`" DATE_START="`$BIN_DATE '+%Y%m%d-%H%M%S'`" log info "Start Unixtime: $TIME_START" if ! [ "$OPTION_QUIET" = "1" ]; then if [ "$OPTION_UseLogfile" = "yes" ]; then $BIN_WGET --proxy=off --output-document=/dev/null http://$OPTION_IPv4TestHTTP/ >>$DEFAULT_NameLogfile 2>&1 else $BIN_WGET --proxy=off --output-document=/dev/null http://$OPTION_IPv4TestHTTP/ fi else $BIN_WGET --proxy=off --output-document=/dev/null http://$OPTION_IPv4TestHTTP/ >/dev/null 2>&1 fi CODE_EXIT=$? TIME_END="`$BIN_DATE '+%s'`" DATE_END="`$BIN_DATE '+%Y%m%d-%H%M%S'`" log info "End Unixtime: $TIME_END" TIME_DIFF=$[ $TIME_END - $TIME_START ] log info "Diff (s) : $TIME_DIFF" if [ "$OPTION_BeepMode" = "yes" ]; then do_beep 5 fi # Run traceroute to server RESULT_TRACEROUTE="`$BIN_TRACEROUTE -I -n $OPTION_IPv4TestHTTP | awk '{ printf " " $2 }' 2>/dev/null`" log info "Traceroute : $RESULT_TRACEROUTE" log notice "!DateStart $DATE_START DateEnd $DATE_END LocalIPofPPP $OPTION_DEVIP4 Timeout $TIME_DIFF HTTPserver $OPTION_IPv4TestHTTP ExitCode $CODE_EXIT !HOPs $RESULT_TRACEROUTE" else log debug "Test ran in dry-run mode!" fi if [ "$OPTION_BeepMode" = "yes" ]; then if [ $[ $OPTION_DEBUG & 1 ] = 0 ]; then # not in dry-run mode do_beep 0 fi fi