summaryrefslogtreecommitdiffstats
path: root/plugins-scripts/utils.sh.in
diff options
context:
space:
mode:
Diffstat (limited to 'plugins-scripts/utils.sh.in')
-rw-r--r--plugins-scripts/utils.sh.in85
1 files changed, 85 insertions, 0 deletions
diff --git a/plugins-scripts/utils.sh.in b/plugins-scripts/utils.sh.in
index b30b908..5fae6ad 100644
--- a/plugins-scripts/utils.sh.in
+++ b/plugins-scripts/utils.sh.in
@@ -21,3 +21,88 @@ support() {
21 $ECHO "@SUPPORT@" | sed -e 's/\n/ /g' 21 $ECHO "@SUPPORT@" | sed -e 's/\n/ /g'
22} 22}
23 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#
28check_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}