Added client work scripts
[clearscm.git] / clients / HP / bin / whosdown
1 #!/bin/ksh
2 ################################################################################
3 #
4 # File:         whosedown
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:-$HOME/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 integer total_machines=0
64 integer total_up=0
65 integer total_down=0
66 integer total_weak=0
67
68 # Get parameters
69 integer count=2
70 up=false
71 if [ "$interactive" = "true" ]; then
72   color_output=true
73 else
74   color_output=false
75 fi
76
77 while [ $# -ge 1 ]; do
78   case "$1" in
79     -usage)
80       usage
81     ;;
82
83     -v|-verbose)
84       verbose=yes
85     ;;
86
87     -d|-debug)
88       debug=yes
89     ;;
90
91     -c|-count)
92       if [[ $# -lt 2 ]]; then
93         error "Count not specified" 1
94       else
95         shift
96         count=$1
97       fi
98     ;;
99
100     -u|-up)
101       up=true
102     ;;
103
104     -nocolor)
105       color_output=false
106     ;;
107
108     *)
109       usage "Unrecognized parameter $1"
110     ;;
111   esac
112   shift
113 done
114
115 if [ "$color_output" = "true" ]; then
116   # Define some colors
117   esc=$(print "\033")
118
119   if [ "$TERM" = "vt100" -o "$TERM" = "vt220" ]; then
120     normal="$esc[0m"
121     up_color="$esc[1m"
122     limping_color="$esc[4m"
123     down_color="$esc[5m"
124   elif [ "$TERM" = "dtterm" -o -z DTTERM ]; then
125     normal="$esc[39m"
126     up_color="$esc[32m"
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"
133     up_color="$esc&v2S"
134   fi
135 fi
136
137 # Print heading
138 display_stderr "Machine\t\tState"
139 display_stderr - --------\\t--------
140
141 trap print_totals INT
142
143 # Check each known machine
144 for machine in $(grep -v ^# $machines | cut -f1 -d:); do
145   let total_machines=total_machines+1
146
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
151       display "\t\c"
152     fi
153   fi
154
155   # ping the machine
156   ping_result=$(ping $machine -n $count | tail -1 | cut -c44-)
157   integer state=0
158
159   # Translate the return string to states
160   case "$ping_result" in
161     "100% packet loss") # Total packet loss - machine is downes
162       state=1
163       ;;
164
165     "") # No packet loss - machine is fine
166       state=0
167       ;;
168
169     *) # Some other percentage of packet loss - machine is limping
170       state=2
171       ;;
172   esac
173
174   debug "Pinged $machine; result = $ping_result; state = $state"
175
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}"
181     fi
182   else
183     if [ "$up" = "false" ]; then
184       display "$machine\t\c"
185       if [ ${#machine} -lt 8 ]; then
186         display "\t\c"
187       fi
188     fi
189     if [ $state -eq 1 ]; then
190       let total_down=total_down+1
191       display "${down_color}Down${normal}"
192     else
193       let total_weak=total_weak+1
194       display "${limping_color}Weak response${normal}"
195     fi
196   fi
197 done
198
199 print_totals