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:-$HOME/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 integer total_machines=0
71 if [ "$interactive" = "true" ]; then
77 while [ $# -ge 1 ]; do
92 if [[ $# -lt 2 ]]; then
93 error "Count not specified" 1
109 usage "Unrecognized parameter $1"
115 if [ "$color_output" = "true" ]; then
119 if [ "$TERM" = "vt100" -o "$TERM" = "vt220" ]; then
122 limping_color="$esc[4m"
124 elif [ "$TERM" = "dtterm" -o -z DTTERM ]; then
127 limping_color="$esc[35m"
128 down_color="$esc[31m"
129 elif [ "$TERM" = "hp" -o "$TERM" = "hpterm" ]; then
130 normal="$esc&d@$esc&v0S"
131 down_color="$esc&v1S"
132 limping_color="$esc&v5S"
138 display_stderr "Machine\t\tState"
139 display_stderr - --------\\t--------
141 trap print_totals INT
143 # Check each known machine
144 for machine in $(grep -v ^# $machines | cut -f1 -d:); do
145 let total_machines=total_machines+1
147 if [ "$up" = "true" ]; then
148 # If we are displaying up machine then print the machine name first
149 display "$machine\t\c"
150 if [ ${#machine} -lt 8 ]; then
156 ping_result=$(ping $machine -n $count | tail -1 | cut -c44-)
159 # Translate the return string to states
160 case "$ping_result" in
161 "100% packet loss") # Total packet loss - machine is downes
165 "") # No packet loss - machine is fine
169 *) # Some other percentage of packet loss - machine is limping
174 debug "Pinged $machine; result = $ping_result; state = $state"
176 # Output based on state
177 if [ $state -eq 0 ]; then
178 if [ "$up" = "true" ]; then
179 let total_up=total_up+1
180 display "${up_color}Up${normal}"
183 if [ "$up" = "false" ]; then
184 display "$machine\t\c"
185 if [ ${#machine} -lt 8 ]; then
189 if [ $state -eq 1 ]; then
190 let total_down=total_down+1
191 display "${down_color}Down${normal}"
193 let total_weak=total_weak+1
194 display "${limping_color}Weak response${normal}"