nagios: add check_rdns
[shutils.git] / nagios / plugins / check_rdns
1 #!/bin/sh
2
3 # Copyright (c) 2013 Stefan Huber <shuber@sthu.org>
4 #
5 # Permission is hereby granted, free of charge, to any person
6 # obtaining a copy of this software and associated documentation
7 # files (the "Software"), to deal in the Software without
8 # restriction, including without limitation the rights to use,
9 # copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the
11 # Software is furnished to do so, subject to the following
12 # conditions:
13 #
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 # OTHER DEALINGS IN THE SOFTWARE.
25
26 . $(dirname $0)/utils.sh
27
28
29 PROGNAME=$(basename $0)
30 REVISION="0.1"
31
32 usage() {
33
34 cat <<EOF
35 check_rdns v$REVISION
36 Copyright (c) 2013 Stefan Huber <shuber@sthu.org>
37
38 Usage: $PROGNAME -H ip-address [OPTIONS]
39
40 Arguments:
41 -H, --address IP-address The ip-addres on which reverse-DNS is performed.
42
43 Options:
44 -h, --help Print this text.
45 -a, --expect=HOST The expected result.
46 -s, --server=HOST The DNS server to contact.
47 -t, --timeout=SEC Seconds before lookup times out. (Default: 10)
48 -w, --warning=MSEC Return warning if lookup time exceeds value.
49 -c, --critical=MSEC Return critical if lookup time exceeds value.
50 -V, --version Print version info.
51 EOF
52 }
53
54
55 TEMP=`getopt -o "H:a:c:hs:t:w:V" --long "help,address:,expect:,critical:,server:,timeout:,warning:,version" -n "$0" -- "$@"`
56 eval set - "$TEMP"
57
58 ADDRESS=
59 EXPECT=
60 SERVER=
61 TIMEOUT=10
62 WARNING=
63 CRITICAL=
64
65 while true; do
66 case "$1" in
67 -h | --help )
68 usage
69 exit $STATE_OK ;;
70 -V | --version )
71 echo "$PROGNAME v$REVISION"
72 exit $STATE_OK ;;
73 -H | --address )
74 ADDRESS="$2"; shift 2 ;;
75 -a | --expect )
76 EXPECT="$2"; shift 2 ;;
77 -s | --server )
78 SERVER="$2"; shift 2 ;;
79 -t | --timeout )
80 TIMEOUT="$2"; shift 2 ;;
81 -w | --warning )
82 WARNING="$2"; shift 2 ;;
83 -c | --critical )
84 CRITICAL="$2"; shift 2 ;;
85 -- | *)
86 break ;;
87 esac
88 done
89
90 if [ -z "$ADDRESS" ]; then
91 echo "Error: No address given."
92 usage
93 exit $STATE_CRITICAL
94 fi
95
96 DIGOPTS="+time=$TIMEOUT +noquestion +noauthority -t PTR"
97 [ -z "$SERVER" ] || DIGOPTS="@$SERVER $DIGOPTS"
98
99 RESULT=$(dig $DIGOPTS -x "$ADDRESS"):
100 DIGSTATUS=$?
101
102 if [ $DIGSTATUS != "0" ]; then
103 echo "DNS failed: dig exit code $DIGSTATUS |"
104 exit $STATUS_CRITICAL
105 fi
106
107 # Get the actual result
108 HOST=$(echo "$RESULT" | grep -m 1 -o "[[:space:]]IN[[:space:]]*PTR[[:space:]].*\.$" | awk '{ print $3 }' )
109 if [ -z "$HOST" ]; then
110 echo "DNS failed: reverse DNS gave no answer. |"
111 exit $STATUS_CRITICAL
112 fi
113
114
115 # Get the query time in msec
116 QUERYTIME=$(echo "$RESULT" | grep -m 1 "Query time:" | cut -d ":" -f 2 | awk '{ print $1 }')
117
118
119 MATCHED=
120 if [ -n "$EXPECT" ]; then
121 if [ "$EXPECT" != "$HOST" ]; then
122 echo "DNS critical - query result \"$HOST\" != \"$EXPECT\", query time: $QUERYTIME msec |"
123 exit $STATUS_CRITICAL
124 else
125 MATCHED=" (match ok)"
126 fi
127 fi
128
129 if [ -n "$CRITICAL" ] && [ "$QUERYTIME" -gt "$CRITICAL" ]; then
130 echo "DNS critical - query time $QUERYTIME msec too large ($CRITICAL msec), query result: \"$HOST\"$MATCHED |"
131 exit $STATUS_CRITICAL
132 fi
133
134 if [ -n "$WARNING" ] && [ "$QUERYTIME" -gt "$WARNING" ]; then
135 echo "DNS warning - query time $QUERYTIME msec too large ($WARNING msec), query result: \"$HOST\"$MATCHED |"
136 exit $STATUS_WARNING
137 fi
138
139 echo "DNS ok - query time $QUERYTIME msec, query result: \"$HOST\"$MATCHED |"
140 exit $STATUS_OK
141