nagios: add check_rdns
[shutils.git] / nagios / plugins / utils.sh
1 #! /bin/sh
2
3 STATE_OK=0
4 STATE_WARNING=1
5 STATE_CRITICAL=2
6 STATE_UNKNOWN=3
7 STATE_DEPENDENT=4
8
9 if test -x /usr/bin/printf; then
10 ECHO=/usr/bin/printf
11 else
12 ECHO=echo
13 fi
14
15 print_revision() {
16 echo "$1 v$2 (nagios-plugins 1.4.16)"
17 $ECHO "The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\ncopies of the plugins under the terms of the GNU General Public License.\nFor more information about these matters, see the file named COPYING.\n" | sed -e 's/\n/ /g'
18 }
19
20 support() {
21 $ECHO "Send email to nagios-users@lists.sourceforge.net if you have questions\nregarding use of this software. To submit patches or suggest improvements,\nsend email to nagiosplug-devel@lists.sourceforge.net.\nPlease include version information with all correspondence (when possible,\nuse output from the --version option of the plugin itself).\n" | sed -e 's/\n/ /g'
22 }
23
24 #
25 # check_range takes a value and a range string, returning successfully if an
26 # alert should be raised based on the range.
27 #
28 check_range() {
29 local v range yes no err decimal start end cmp match
30 v="$1"
31 range="$2"
32
33 # whether to raise an alert or not
34 yes=0
35 no=1
36 err=2
37
38 # regex to match a decimal number
39 decimal="-?([0-9]+\.?[0-9]*|[0-9]*\.[0-9]+)"
40
41 # compare numbers (including decimals), returning true/false
42 cmp() { awk "BEGIN{ if ($1) exit(0); exit(1)}"; }
43
44 # returns successfully if the string in the first argument matches the
45 # regex in the second
46 match() { echo "$1" | grep -E -q -- "$2"; }
47
48 # make sure value is valid
49 if ! match "$v" "^$decimal$"; then
50 echo "${0##*/}: check_range: invalid value" >&2
51 unset -f cmp match
52 return "$err"
53 fi
54
55 # make sure range is valid
56 if ! match "$range" "^@?(~|$decimal)(:($decimal)?)?$"; then
57 echo "${0##*/}: check_range: invalid range" >&2
58 unset -f cmp match
59 return "$err"
60 fi
61
62 # check for leading @ char, which negates the range
63 if match $range '^@'; then
64 range=${range#@}
65 yes=1
66 no=0
67 fi
68
69 # parse the range string
70 if ! match "$range" ':'; then
71 start=0
72 end="$range"
73 else
74 start="${range%%:*}"
75 end="${range#*:}"
76 fi
77
78 # do the comparison, taking positive ("") and negative infinity ("~")
79 # into account
80 if [ "$start" != "~" ] && [ "$end" != "" ]; then
81 if cmp "$start <= $v" && cmp "$v <= $end"; then
82 unset -f cmp match
83 return "$no"
84 else
85 unset -f cmp match
86 return "$yes"
87 fi
88 elif [ "$start" != "~" ] && [ "$end" = "" ]; then
89 if cmp "$start <= $v"; then
90 unset -f cmp match
91 return "$no"
92 else
93 unset -f cmp match
94 return "$yes"
95 fi
96 elif [ "$start" = "~" ] && [ "$end" != "" ]; then
97 if cmp "$v <= $end"; then
98 unset -f cmp match
99 return "$no"
100 else
101 unset -f cmp match
102 return "$yes"
103 fi
104 else
105 unset -f cmp match
106 return "$no"
107 fi
108 }