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 b30b9083..d5013a60 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
27check_range() {
28 local v range yes no err decimal start end cmp match
29 v="$1"
30 range="$2"
31
32 # whether to raise an alert or not
33 yes=0
34 no=1
35 err=2
36
37 # regex to match a decimal number
38 decimal="-?([0-9]+\.?[0-9]*|[0-9]*\.[0-9]+)"
39
40 # compare numbers (including decimals), returning true/false
41 cmp() { awk "BEGIN{ if ($1) exit(0); exit(1)}"; }
42
43 # returns successfully if the string in the first argument matches the
44 # regex in the second
45 match() { echo "$1" | grep -E -q -- "$2"; }
46
47 # make sure value is valid
48 if ! match "$v" "^$decimal$"; then
49 echo "${0##*/}: check_range: invalid value" >&2
50 unset -f cmp match
51 return "$err"
52 fi
53
54 # make sure range is valid
55 if ! match "$range" "^@?(~|$decimal)(:($decimal)?)?$"; then
56 echo "${0##*/}: check_range: invalid range" >&2
57 unset -f cmp match
58 return "$err"
59 fi
60
61 # check for leading @ char, which negates the range
62 if match $range '^@'; then
63 range=${range#@}
64 yes=1
65 no=0
66 fi
67
68 # parse the range string
69 if ! match "$range" ':'; then
70 start=0
71 end="$range"
72 else
73 start="${range%%:*}"
74 end="${range#*:}"
75 fi
76
77 # do the comparison, taking positive ("") and negative infinity ("~")
78 # into account
79 if [ "$start" != "~" ] && [ "$end" != "" ]; then
80 if cmp "$start <= $v" && cmp "$v <= $end"; then
81 unset -f cmp match
82 return "$no"
83 else
84 unset -f cmp match
85 return "$yes"
86 fi
87 elif [ "$start" != "~" ] && [ "$end" = "" ]; then
88 if cmp "$start <= $v"; then
89 unset -f cmp match
90 return "$no"
91 else
92 unset -f cmp match
93 return "$yes"
94 fi
95 elif [ "$start" = "~" ] && [ "$end" != "" ]; then
96 if cmp "$v <= $end"; then
97 unset -f cmp match
98 return "$no"
99 else
100 unset -f cmp match
101 return "$yes"
102 fi
103 else
104 unset -f cmp match
105 return "$no"
106 fi
107}
108