bib2html.py: Add incollection bibtype
[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 "$PROGNAME" -- "$@"`
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 shift
87 break ;;
88 * )
89 break ;;
90 esac
91 done
92
93 if [ -z "$ADDRESS" ]; then
94 echo "Error: No address given."
95 usage
96 exit $STATE_CRITICAL
97 fi
98
99 DIGOPTS="+time=$TIMEOUT +noquestion +noauthority -t PTR"
100 [ -z "$SERVER" ] || DIGOPTS="@$SERVER $DIGOPTS"
101
102 RESULT=$(dig $DIGOPTS -x "$ADDRESS"):
103 DIGSTATUS=$?
104
105 if [ $DIGSTATUS != "0" ]; then
106 echo "DNS failed: dig exit code $DIGSTATUS |"
107 exit $STATE_CRITICAL
108 fi
109
110 # Get the actual result
111 HOST=$(echo "$RESULT" | grep -m 1 -o "[[:space:]]IN[[:space:]]*PTR[[:space:]].*\.$" | awk '{ print $3 }' )
112 if [ -z "$HOST" ]; then
113 echo "DNS failed: reverse DNS gave no answer. |"
114 exit $STATE_CRITICAL
115 fi
116
117
118 # Get the query time in msec
119 QUERYTIME=$(echo "$RESULT" | grep -m 1 "Query time:" | cut -d ":" -f 2 | awk '{ print $1 }')
120
121
122 MATCHED=
123 if [ -n "$EXPECT" ]; then
124 if [ "$EXPECT" != "$HOST" ]; then
125 echo "DNS critical - query result \"$HOST\" != \"$EXPECT\", query time: $QUERYTIME msec |"
126 exit $STATE_CRITICAL
127 else
128 MATCHED=" (match ok)"
129 fi
130 fi
131
132 if [ -n "$CRITICAL" ] && [ "$QUERYTIME" -gt "$CRITICAL" ]; then
133 echo "DNS critical - query time $QUERYTIME msec too large ($CRITICAL msec), query result: \"$HOST\"$MATCHED |"
134 exit $STATE_CRITICAL
135 fi
136
137 if [ -n "$WARNING" ] && [ "$QUERYTIME" -gt "$WARNING" ]; then
138 echo "DNS warning - query time $QUERYTIME msec too large ($WARNING msec), query result: \"$HOST\"$MATCHED |"
139 exit $STATE_WARNING
140 fi
141
142 echo "DNS OK - query time $QUERYTIME msec, query result: \"$HOST\"$MATCHED |"
143 exit $STATE_OK
144