added -n and more display_html stuff
[monitoring-plugins.git] / plugins-scripts / utils.sh.in
blob031c035780e9dca91ecba3522fda8cd8895e9be6
1 #! /bin/sh
3 STATE_OK=0
4 STATE_WARNING=1
5 STATE_CRITICAL=2
6 STATE_UNKNOWN=3
7 STATE_DEPENDENT=4
9 print_revision() {
10 echo "$1 v$2 (@PACKAGE@ @VERSION@)"
11 printf '%b' "@WARRANTY@"
14 support() {
15 printf '%b' "@SUPPORT@"
19 # check_range takes a value and a range string, returning successfully if an
20 # alert should be raised based on the range. Range values are inclusive.
21 # Values may be integers or floats.
23 # Example usage:
25 # Generating an exit code of 1:
26 # check_range 5 2:8
28 # Generating an exit code of 0:
29 # check_range 1 2:8
31 check_range() {
32 local v range yes no err decimal start end cmp match
33 v="$1"
34 range="$2"
36 # whether to raise an alert or not
37 yes=0
38 no=1
39 err=2
41 # regex to match a decimal number
42 decimal="-?([0-9]+\.?[0-9]*|[0-9]*\.[0-9]+)"
44 # compare numbers (including decimals), returning true/false
45 cmp() { awk "BEGIN{ if ($1) exit(0); exit(1)}"; }
47 # returns successfully if the string in the first argument matches the
48 # regex in the second
49 match() { echo "$1" | grep -E -q -- "$2"; }
51 # make sure value is valid
52 if ! match "$v" "^$decimal$"; then
53 echo "${0##*/}: check_range: invalid value" >&2
54 unset -f cmp match
55 return "$err"
58 # make sure range is valid
59 if ! match "$range" "^@?(~|$decimal)(:($decimal)?)?$"; then
60 echo "${0##*/}: check_range: invalid range" >&2
61 unset -f cmp match
62 return "$err"
65 # check for leading @ char, which negates the range
66 if match $range '^@'; then
67 range=${range#@}
68 yes=1
69 no=0
72 # parse the range string
73 if ! match "$range" ':'; then
74 start=0
75 end="$range"
76 else
77 start="${range%%:*}"
78 end="${range#*:}"
81 # do the comparison, taking positive ("") and negative infinity ("~")
82 # into account
83 if [ "$start" != "~" ] && [ "$end" != "" ]; then
84 if cmp "$start <= $v" && cmp "$v <= $end"; then
85 unset -f cmp match
86 return "$no"
87 else
88 unset -f cmp match
89 return "$yes"
91 elif [ "$start" != "~" ] && [ "$end" = "" ]; then
92 if cmp "$start <= $v"; then
93 unset -f cmp match
94 return "$no"
95 else
96 unset -f cmp match
97 return "$yes"
99 elif [ "$start" = "~" ] && [ "$end" != "" ]; then
100 if cmp "$v <= $end"; then
101 unset -f cmp match
102 return "$no"
103 else
104 unset -f cmp match
105 return "$yes"
107 else
108 unset -f cmp match
109 return "$no"