Update THANKS file
[monitoring-plugins.git] / plugins-scripts / utils.sh.in
blob4a07df894dcaad4d81f07ebaa0044d2842014970
1 #! /bin/sh
3 STATE_OK=0
4 STATE_WARNING=1
5 STATE_CRITICAL=2
6 STATE_UNKNOWN=3
7 STATE_DEPENDENT=4
9 if test -x /usr/bin/printf; then
10 ECHO=/usr/bin/printf
11 else
12 ECHO=echo
15 print_revision() {
16 echo "$1 v$2 (@PACKAGE@ @VERSION@)"
17 $ECHO "@WARRANTY@" | sed -e 's/\n/ /g'
20 support() {
21 $ECHO "@SUPPORT@" | sed -e 's/\n/ /g'
25 # check_range takes a value and a range string, returning successfully if an
26 # alert should be raised based on the range. Range values are inclusive.
27 # Values may be integers or floats.
29 # Example usage:
31 # Generating an exit code of 1:
32 # check_range 5 2:8
34 # Generating an exit code of 0:
35 # check_range 1 2:8
37 check_range() {
38 local v range yes no err decimal start end cmp match
39 v="$1"
40 range="$2"
42 # whether to raise an alert or not
43 yes=0
44 no=1
45 err=2
47 # regex to match a decimal number
48 decimal="-?([0-9]+\.?[0-9]*|[0-9]*\.[0-9]+)"
50 # compare numbers (including decimals), returning true/false
51 cmp() { awk "BEGIN{ if ($1) exit(0); exit(1)}"; }
53 # returns successfully if the string in the first argument matches the
54 # regex in the second
55 match() { echo "$1" | grep -E -q -- "$2"; }
57 # make sure value is valid
58 if ! match "$v" "^$decimal$"; then
59 echo "${0##*/}: check_range: invalid value" >&2
60 unset -f cmp match
61 return "$err"
64 # make sure range is valid
65 if ! match "$range" "^@?(~|$decimal)(:($decimal)?)?$"; then
66 echo "${0##*/}: check_range: invalid range" >&2
67 unset -f cmp match
68 return "$err"
71 # check for leading @ char, which negates the range
72 if match $range '^@'; then
73 range=${range#@}
74 yes=1
75 no=0
78 # parse the range string
79 if ! match "$range" ':'; then
80 start=0
81 end="$range"
82 else
83 start="${range%%:*}"
84 end="${range#*:}"
87 # do the comparison, taking positive ("") and negative infinity ("~")
88 # into account
89 if [ "$start" != "~" ] && [ "$end" != "" ]; then
90 if cmp "$start <= $v" && cmp "$v <= $end"; then
91 unset -f cmp match
92 return "$no"
93 else
94 unset -f cmp match
95 return "$yes"
97 elif [ "$start" != "~" ] && [ "$end" = "" ]; then
98 if cmp "$start <= $v"; then
99 unset -f cmp match
100 return "$no"
101 else
102 unset -f cmp match
103 return "$yes"
105 elif [ "$start" = "~" ] && [ "$end" != "" ]; then
106 if cmp "$v <= $end"; then
107 unset -f cmp match
108 return "$no"
109 else
110 unset -f cmp match
111 return "$yes"
113 else
114 unset -f cmp match
115 return "$no"