#!/usr/bin/ksh #set -xv #@(#) up version 2.0 #@(#) #@(#) #@(#) COPYRIGHT #@(#) Copyright (C) 2004 Mark A. Lane. #@(#) All rights reserved. #@(#) #@(#) #@(#) LICENSING GENERAL #@(#) The Copywrite Owner hereby grants you unlimited permision to use/copy/modify #@(#) this software. #@(#) #@(#) #@(#) WARRANTY #@(#) This software is provided as is without any express or implied warranty. #@(#) #@(#) #@(#) AUTHOR #@(#) Mark A. Lane #@(#) #@(#) #@(#) DATE WRITTEN #@(#) 2004. #@(#) #@(#) REQUIREMENTS #@(#) ${Hosts} is required for the script to function along with the directory /usr/local/etc #@(#) #@(#) If ${Hosts} dosen't exist, a base ${Hosts} is created using localhost. #@(#) #@(#) If `dirname ${Hosts}` dosen't exist, `dirname ${Hosts}` is attemped to be created. #@(#) #@(#) DESCRIPTION #@(#) up uses ping to monitor whether a host is up or down and displays the results in colors. #@(#) #@(#) up also uses dd to capture keystrokes for menu input. #@(#) #@(#) #@(#) umask 077 stty sane Tmp=/tmp/$$ Hosts=$HOME/.up.hosts Type=default PATH=/bin:/usr/sbin [ ! -d $Tmp ] && mkdir $Tmp >/dev/null 2>&1 cd $Tmp >/dev/null 2>&1 rm * >/dev/null 2>&1 InputKey=$Tmp/InputKey Logname=`logname` Id=`id | awk '{FS=")"}{print $1}' | awk '{FS="("}{print $2}'` if [ ! "$Id" = "$Logname" ]; then echo " Error: `basename $0` must be run as ${Logname} not ${Id} " exit fi Update(){ PID=`cat Display.PID` case $Key in c|C|h|H) clear kill -9 $PID > /dev/null 2>&1 unset Udisplay echo "\\033[0;39m" tput cup 0 0 echo " Configuration and Help Menu [L] List Hosts ( List currently defined hosts ) [A] Add Host ( Add a host to monitor ) [D] Delete Host ( Stop monitoring a host ) [I] Ignore Host ( Ignore a host ) [U] Un-Ignore Host ( Start monitoring a host ) [O] Output Format ( Change output format ) [C] Configure ( Configure menu - This ) [H] Help ( Help menu - This ) [R] Return ( Return to display ) [R] Refresh Display ( Refresh display ) [X] Xterm ( Spawn an xterm ) [T] Top ( Spawn top ) [Q] Quit ( Quit ) " printf "%s" "-> " stty raw dd if=$Tty count=1 bs=1 of=$InputKey >/dev/null 2>&1 stty -raw printf "%s\n" export Key=`head -1 $InputKey` Update $Key ;; l|L) kill -9 $PID > /dev/null 2>&1 unset Udisplay clear echo "\\033[0;39m" tput cup 0 0 ls mon,* | cut -d"," -f3 | sort | pg -r ;; a|A) kill -9 $PID > /dev/null 2>&1 unset Udisplay echo "\\033[0;39m" tput cup 0 0 printf "%s\015%s" " " "Enter Host -> " read Host if [ ! "$Host" = "" ]; then touch mon,up,${Host} echo ${Host}: >> $Hosts else printf "\015%s" "Invalid entry" sleep 2 fi ;; r|R) kill -9 $PID > /dev/null 2>&1 unset Udisplay echo "\\033[0;39m" tput cup 0 0 ;; u|U) kill -9 $PID > /dev/null 2>&1 unset Udisplay echo "\\033[0;39m" tput cup 0 0 printf "%s\015%s" " " "Enter Host -> " read Host if [ ! "$Host" = "" ]; then rm mon,zig,${Host} >/dev/null 2>&1 touch mon,up,${Host} else printf "\015%s" "Invalid entry" sleep 2 fi ;; i|I) kill -9 $PID > /dev/null 2>&1 unset Udisplay echo "\\033[0;39m" tput cup 0 0 printf "%s\015%s" " " "Enter Host -> " read Host if [ ! "$Host" = "" ]; then touch mon,zig,${Host} rm mon,up,${Host} > /dev/null 2>&1 rm mon,down,${Host} > /dev/null 2>&1 else printf "\015%s" "Invalid entry" sleep 2 fi ;; d|D) kill -9 $PID > /dev/null 2>&1 unset Udisplay echo "\\033[0;39m" tput cup 0 0 printf "%s\015%s" " " "Enter Host -> " read Host if [ ! "$Host" = "" ]; then rm *,${Host} > /dev/null 2>&1 grep -v "^${Host}:" $Hosts > ${Hosts}.new 2>/dev/null mv ${Hosts}.new $Hosts 2>/dev/null else printf "\015%s" "Invalid entry" sleep 2 fi ;; q|Q) cd /tmp kill -9 $PID > /dev/null 2>&1 rm -fr $Tmp echo "\\033[0;39m" clear exit ;; o|O) kill -9 $PID > /dev/null 2>&1 unset Udisplay echo "\\033[0;39m" tput cup 0 0 printf "%s\015%s\n" " " "Enter Type -> " printf "%s\015%s" " " "Default Host" tput cup 0 15 stty raw dd if=$Tty count=1 bs=1 of=$InputKey >/dev/null 2>&1 stty -raw printf "%s\n" reply=`cat $InputKey` case $reply in d|D) Type=default ;; h|H) Type=host ;; esac ;; x|X) kill -9 $PID > /dev/null 2>&1 unset Udisplay echo "\\033[0;39m" tput cup 0 0 cd $HOME >/dev/null 2>&1 /usr/openwin/bin/xterm -title `hostname`.its.monash.edu.au -fg green -bg black -sb -ls -sl 10000 -geometry 171x55+322+148 & cd - >/dev/null 2>&1 ;; f|F) kill -9 $PID > /dev/null 2>&1 unset Udisplay echo "\\033[0;39m" tput cup 0 0 cd $HOME >/dev/null 2>&1 /usr/bin/firefox & cd - >/dev/null 2>&1 ;; esac } Display_default(){ ( Tmp=/tmp/$1 cd $Tmp clear Plines=0 Pcols=0 while : do Lines=`tput lines` Cols=`tput cols` Ocols=0 Olines=4 [ ! $Lines = $Plines ] && clear [ ! $Cols = $Pcols ] && clear echo "\\033[0;35m" tput cup 0 0 printf "\015%+41s\n\015%+53s\n\015" "`hostname`" "`date`" printf "\015%-35s%-25s%-15s\n\015" " Host" " IP" " Time/Date" ls $Tmp/mon,* | while read A do Host=`echo $A | cut -d"," -f3` case $A in *mon,up*) ping $Host 1 > $Tmp/ping,${Host} 2>&1 & ;; *mon,down,*) ping $Host 1 > $Tmp/ping,${Host} 2>&1 & ;; esac done wait `jobs -l %% 2>/dev/null` 2>/dev/null ls $Tmp/mon,* | while read A do if [ $Olines -gt $(($Lines - 4)) ]; then echo "\\033[0;35m" tput cup 0 0 printf "\015%+41s\n\015%+53s\n\015" "`hostname`" "`date`" printf "\015%-35s%-25s%-15s\n\015" " Host" " IP" " Time/Date" Ocols=0 Olines=4 fi tput cup $Olines $Ocols Host=`echo $A | cut -d"," -f3` [ `nslookup $Host 2>/dev/null | grep Address | wc -l | awk '{print $1}'` = 2 ] && IP=`nslookup $Host 2>/dev/null | grep Address | tail -1 | awk '{print $2}'` || IP=unknown case $A in *mon,zig,*) Ret=2 ;; *mon,up,*) grep "${Host} is alive" $Tmp/ping,${Host} > /dev/null 2>&1 Ret=$? ;; *mon,down,*) grep "${Host} is alive" $Tmp/ping,${Host} > /dev/null 2>&1 Ret=$? ;; esac case $Ret in 0) [ ! -f mon,up,${Host} ] && touch mon,up,${Host} Time=`ls -la mon,up,${Host} | awk '{print $8" "$7"/"$6}'` echo "\\033[1;32m" "`printf "\015%-35s%-25s%-15s" $Host $IP "$Time"` \015" [ -f mon,down,${Host} ] && rm mon,down,${Host} ;; 1) [ ! -f mon,down,${Host} ] && touch mon,down,${Host} Time=`ls -la mon,down,${Host} | awk '{print $8" "$7"/"$6}'` echo "\\033[1;31m" "`printf "\015%-35s%-25s%-15s" $Host $IP "$Time"` \015" [ -f mon,up,${Host} ] && rm mon,up,${Host} ;; 2) echo "\\033[0;34m" "`printf "\015%-35s%-25s%-15s" $Host $IP "$Time"` \015" ;; esac Olines=$(($Olines + 1)) done sleep 5 Pcols=$Cols Plines=$Lines done ) & jobs -l %% 2>/dev/null | awk '{print $3}' > Display.PID 2>/dev/null } Display_host(){ ( Tmp=/tmp/$1 cd $Tmp Plines=0 Pcols=0 while : do Lines=`tput lines` Cols=`tput cols` [ ! $Lines = $Plines ] && clear [ ! $Cols = $Pcols ] && clear echo "\\033[0;35m" tput cup 0 0 printf "\015%+11s\n\015%+11s\n\015" "`hostname`" "`date` " Ocols=0 Olines=2 ls $Tmp/mon,* | while read A do Host=`echo $A | cut -d"," -f3` case $A in *mon,up*) ping $Host 1 > $Tmp/ping,${Host} 2>&1 & ;; *mon,down,*) ping $Host 1 > $Tmp/ping,${Host} 2>&1 & ;; esac done wait `jobs -l %% 2>/dev/null` ls $Tmp/mon,* | while read A do Host=`echo $A | cut -d"," -f3` if [ $Ocols -gt $(($Cols - 15)) ]; then echo Ocols=0 Olines=$(($Olines + 1)) fi if [ $Olines -gt $(($Lines - 2)) ]; then echo "\\033[0;35m" tput cup 0 0 printf "\015%+11s\n\015%+11s\n\015" "`hostname`" "`date` " Ocols=0 Olines=2 fi tput cup $Olines $Ocols case $A in *mon,zig,*) Ret=2 ;; *mon,up,*) grep "${Host} is alive" $Tmp/ping,${Host} > /dev/null 2>&1 Ret=$? ;; *mon,down,*) grep "${Host} is alive" $Tmp/ping,${Host} > /dev/null 2>&1 Ret=$? ;; esac case $Ret in 0) [ ! -f mon,up,${Host} ] && touch mon,up,${Host} echo "\\033[1;32m" `printf "$Host "` [ -f mon,down,${Host} ] && rm mon,down,${Host} ;; 1) [ ! -f mon,down,${Host} ] && touch mon,down,${Host} echo "\\033[1;31m" `printf "$Host "` [ -f mon,up,${Host} ] && rm mon,up,${Host} ;; 2) echo "\\033[1;34m" `printf "$Host "` ;; esac Ocols=$(($Ocols + 15)) done sleep 5 Pcols=$Cols Plines=$Lines done ) & jobs -l %% 2>/dev/null | awk '{print $3}' > Display.PID 2>/dev/null } # # Main # if [ ! -f $Hosts -o -z $Hosts ]; then echo $Hosts does not exist, creating base $Hosts file if [ ! -d `dirname $Hosts` ]; then mkdir -p `dirname $Hosts` if [ ! "$?" = "0" ]; then echo "Unable to create `dirname ${Hosts}`" exit 1 fi fi echo localhost: > $Hosts if [ ! "$?" = "0" ]; then echo "Unable to create ${Hosts}" exit 1 fi fi cut -d":" -f1 $Hosts | while read Host do touch mon,up,${Host} done unset Udisplay Tty=`tty` echo $Tty > TTY while : do if [ -z $Udisplay ]; then Display_${Type} $$ Udisplay=yes fi stty raw dd if=$Tty count=1 bs=1 of=$InputKey >/dev/null 2>&1 stty -raw export Key=`head -1 $InputKey` Update $Key done ############################################################################## ### This script is submitted to BigAdmin by a user of the BigAdmin community. ### Sun Microsystems, Inc. is not responsible for the ### contents or the code enclosed. ### ### ### Copyright 2008 Sun Microsystems, Inc. ALL RIGHTS RESERVED ### Use of this software is authorized pursuant to the ### terms of the license found at ### http://www.sun.com/bigadmin/common/berkeley_license.html ##############################################################################