-
Notifications
You must be signed in to change notification settings - Fork 3
/
check-ping
executable file
·151 lines (114 loc) · 2.94 KB
/
check-ping
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#! /bin/sh
#
PROG="$(basename $0)"
## defaults
count=1
interval=1
## usage and version
usage () {
cat <<EOF
Usage: $PROG [options] [HOST ...]
Check that the HOST is reachable using 'ping';
exit with nonzero status if this reports an error.
Any number of HOSTs can be given on the command-line: the exit status
will be the number of hosts that failed the 'qping' check. When
called with no arguments, '$PROG' reads the host names from STDIN, so
you can for instance do::
sge-list-nodes d | $PROG
No output is printed to STDOUT unless the '--verbose'
option is given.
Options:
--count, -c NUM
Probe each host NUM times (default: $count)
--interval, -i NUM
Wait NUM seconds between probes of the same host.
(Default: $interval.)
--help, -h Print this help text.
--verbose Output 'qping -info' report.
EOF
}
## helper functions
die () {
rc="$1"
shift
(echo -n "$PROG: ERROR: ";
if [ $# -gt 0 ]; then echo "$@"; else cat; fi) 1>&2
exit $rc
}
warn () {
(echo -n "$PROG: WARNING: ";
if [ $# -gt 0 ]; then echo "$@"; else cat; fi) 1>&2
}
have_command () {
type "$1" >/dev/null 2>/dev/null
}
require_command () {
if ! have_command "$1"; then
die 1 "Could not find required command '$1' in system PATH. Aborting."
fi
}
is_absolute_path () {
expr match "$1" '/' >/dev/null 2>/dev/null
}
## parse command-line
short_opts='c:i:hv'
long_opts='count:,interval:,help,verbose'
if [ "x$(getopt -T)" != 'x--' ]; then
# GNU getopt
args=$(getopt --name "$PROG" --shell sh -l "$long_opts" -o "$short_opts" -- "$@")
if [ $? -ne 0 ]; then
die 1 "Type '$PROG --help' to get usage information."
fi
# use 'eval' to remove getopt quoting
eval set -- $args
else
# old-style getopt, use compatibility syntax
args=$(getopt "$short_opts" "$@")
if [ $? -ne 0 ]; then
die 1 "Type '$PROG --help' to get usage information."
fi
set -- $args
fi
while [ $# -gt 0 ]; do
case "$1" in
--count|-c) count="$2"; shift ;;
--interval|-i) interval="$2"; shift ;;
--verbose|-v) verbose='-v' ;;
--help|-h) usage; exit 0 ;;
--) shift; break ;;
esac
shift
done
## main
require_command ping
require_command mktemp
require_command fgrep
# environment sanity checks
tmp=$(mktemp -q)
if [ -z "$tmp" ]; then
# EX_OSERR
die 71 "Cannot create temporary file."
fi
trap "rm '$tmp'" EXIT
# if no arguments, read them from STDIN
if [ $# -eq 0 ]; then
set -- $(cat)
fi
failures=0
for host in "$@"; do
# do the actual check
env LC_ALL=C ping -c "$count" -i "$interval" "$host" > "$tmp" 2>&1
if fgrep -q '100% packet loss' "$tmp"; then
rc=1
outcome='FAIL'
else
rc=0
outcome='ok'
fi
msg="$(fgrep 'packet loss' "$tmp")"
failures=$(expr $failures + $rc)
if [ -n "$verbose" ]; then
echo "${host}: ${outcome}: ${msg}"
fi
done
exit $failures