2 ################################################################################
5 # Description: Pings machines listed in machines database and produces a report
6 # about which machines are down
7 # Author: Andrew@DeFaria.com
8 # Created: Thu Oct 5 09:32:21 PDT 2000
11 # (c) Copyright 2001, Andrew@DeFaria.com, all rights reserved
13 ################################################################################
14 # Set me to command name
18 adm_base=${adm_base:-//sonscentral/Corporate/Software/adm}
21 adm_fpath=${adm_fpath:-$adm_base/functions}
27 machines=${machines:-$adm_base/data/machines}
30 if [ "_$1" != "_" ]; then
34 display "Usage: $me [-[c|ount] n | -[u|p] [-nocolor]"
37 display "\t-c|ount:\tNumber of pings to attempt"
38 display "\t-u|p:\t\tReport machines that are up too"
39 display "\t-nocolor\tDo not produce color output"
43 function print_totals {
46 display_stderr ------------------------
48 if [ "$up" = "true" ]; then
49 display_stderr "Total machines:\t$total_machines"
50 display_stderr "Total up:\t$total_up"
52 if [ $total_down -ne 0 ]; then
53 display_stderr "Total down:\t$total_down"
55 if [ $total_weak -ne 0 ]; then
56 display_stderr "Total weak:\t$total_week"
63 declare -i total_machines=0
65 declare -i total_down=0
66 declare -i total_weak=0
73 while [ $# -ge 1 ]; do
88 if [[ $# -lt 2 ]]; then
89 error "Count not specified" 1
105 usage "Unrecognized parameter $1"
111 if [ "$color_output" = "true" ]; then
115 if [ "$TERM" = "vt100" -o "$TERM" = "vt220" ]; then
118 limping_color="$esc[4m"
120 elif [ "$TERM" = "dtterm" -o -z DTTERM ]; then
123 limping_color="$esc[35m"
124 down_color="$esc[31m"
125 elif [ "$TERM" = "hp" -o "$TERM" = "hpterm" ]; then
126 normal="$esc&d@$esc&v0S"
127 down_color="$esc&v1S"
128 limping_color="$esc&v5S"
130 elif [ "$TERM" = "cygwin" ]; then
133 limping_color="$esc[33m"
134 down_color="$esc[31m"
139 display "Machine\t\tState"
140 display --------\\t--------
142 trap print_totals INT
144 # Check each known machine
145 for machine in $(grep -v ^# $machines | cut -f1 -d:); do
146 let total_machines=total_machines+1
148 if [ "$up" = "true" ]; then
149 # If we are displaying up machine then print the machine name first
150 display "$machine\t\c"
151 if [ ${#machine} -lt 8 ]; then
157 ping_result=$(ping $machine -n $count | tail -3 | head -1 | cut -f2 -d'(' | cut -f1 -d')')
160 # Translate the return string to states
161 case "$ping_result" in
162 "100% loss") # Total packet loss - machine is downes
166 "0% loss") # No packet loss - machine is fine
170 *) # Some other percentage of packet loss - machine is limping
175 debug "Pinged $machine; result = $ping_result; state = $state"
177 # Output based on state
178 if [ $state -eq 0 ]; then
179 if [ "$up" = "true" ]; then
180 let total_up=total_up+1
181 display "${up_color}Up${normal}"
184 if [ "$up" = "false" ]; then
185 display "$machine\t\c"
186 if [ ${#machine} -lt 8 ]; then
190 if [ $state -eq 1 ]; then
191 let total_down=total_down+1
192 display "${down_color}Down${normal}"
194 let total_weak=total_weak+1
195 display "${limping_color}Weak response${normal}"