Initial add of defaria.com
[clearscm.git] / defaria.com / Computers / code / adm / bin / whosdown
1 #!/bin/bash
2 ################################################################################
3 #
4 # File:         whosdown
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
9 # Language:     Korn Shell
10 #
11 # (c) Copyright 2001, Andrew@DeFaria.com, all rights reserved
12 #
13 ################################################################################
14 # Set me to command name
15 me=$(basename $0)
16
17 # Set adm_base
18 adm_base=${adm_base:-//sonscentral/Corporate/Software/adm}
19
20 # Set adm_fpath
21 adm_fpath=${adm_fpath:-$adm_base/functions}
22
23 # Source functions
24 . $adm_fpath/common
25
26 # Set machines
27 machines=${machines:-$adm_base/data/machines}
28
29 function usage {
30   if [ "_$1" != "_" ]; then
31     display "$1"
32     display
33   fi
34   display "Usage: $me [-[c|ount] n | -[u|p] [-nocolor]"
35   display
36   display "Where:"
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"
40   exit 1
41 } # usage
42
43 function print_totals {
44   # Print totals
45   display_stderr
46   display_stderr ------------------------
47
48   if [ "$up" = "true" ]; then
49     display_stderr "Total machines:\t$total_machines"
50     display_stderr "Total up:\t$total_up"
51   fi
52   if [ $total_down -ne 0 ]; then
53     display_stderr "Total down:\t$total_down"
54   fi
55   if [ $total_weak -ne 0 ]; then
56     display_stderr "Total weak:\t$total_week"
57   fi
58
59   exit
60 } # print_totals
61
62 # Totals
63 declare -i total_machines=0
64 declare -i total_up=0
65 declare -i total_down=0
66 declare -i total_weak=0
67
68 # Get parameters
69 declare -i count=2
70 up=false
71 color_output=true
72
73 while [ $# -ge 1 ]; do
74   case "$1" in
75     -usage)
76       usage
77     ;;
78
79     -v|-verbose)
80       verbose=yes
81     ;;
82
83     -d|-debug)
84       debug=yes
85     ;;
86
87     -c|-count)
88       if [[ $# -lt 2 ]]; then
89         error "Count not specified" 1
90       else
91         shift
92         count=$1
93       fi
94     ;;
95
96     -u|-up)
97       up=true
98     ;;
99
100     -nocolor)
101       color_output=false
102     ;;
103
104     *)
105       usage "Unrecognized parameter $1"
106     ;;
107   esac
108   shift
109 done
110
111 if [ "$color_output" = "true" ]; then
112   # Define some colors
113   esc=$(echo "\033")
114
115   if [ "$TERM" = "vt100" -o "$TERM" = "vt220" ]; then
116     normal="$esc[0m"
117     up_color="$esc[1m"
118     limping_color="$esc[4m"
119     down_color="$esc[5m"
120   elif [ "$TERM" = "dtterm" -o -z DTTERM ]; then
121     normal="$esc[39m"
122     up_color="$esc[32m"
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"
129     up_color="$esc&v2S"
130   elif [ "$TERM" = "cygwin" ]; then
131     normal="$esc[0m"
132     up_color="$esc[32m"
133     limping_color="$esc[33m"
134     down_color="$esc[31m"
135   fi
136 fi
137
138 # Print heading
139 display "Machine\t\tState"
140 display --------\\t--------
141
142 trap print_totals INT
143
144 # Check each known machine
145 for machine in $(grep -v ^# $machines | cut -f1 -d:); do
146   let total_machines=total_machines+1
147
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
152       display "\t\c"
153     fi
154   fi
155
156   # ping the machine
157   ping_result=$(ping $machine -n $count | tail -3 | head -1 | cut -f2 -d'(' | cut -f1 -d')')
158   declare -i state=0
159
160   # Translate the return string to states
161   case "$ping_result" in
162     "100% loss") # Total packet loss - machine is downes
163       state=1
164       ;;
165
166     "0% loss") # No packet loss - machine is fine
167       state=0
168       ;;
169
170     *) # Some other percentage of packet loss - machine is limping
171       state=2
172       ;;
173   esac
174
175   debug "Pinged $machine; result = $ping_result; state = $state"
176
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}"
182     fi
183   else
184     if [ "$up" = "false" ]; then
185       display "$machine\t\c"
186       if [ ${#machine} -lt 8 ]; then
187         display "\t\c"
188       fi
189     fi
190     if [ $state -eq 1 ]; then
191       let total_down=total_down+1
192       display "${down_color}Down${normal}"
193     else
194       let total_weak=total_weak+1
195       display "${limping_color}Weak response${normal}"
196     fi
197   fi
198 done
199
200 print_totals