[nagiosplug] Added check_range shell function to utils.sh.in

"Nagios Plugin Development" <[email protected]> Tue, 29 May 2012 13:10:10 +0000
Newsgroups gmane.network.nagios.plugins.cvs
Message-ID <[email protected]>
    Module: nagiosplug
    Branch: master
    Commit: aab6b8a16c9ec1dfe9d27f70cfbe19281153e306
    Author: Alex Griffin <[email protected]>
 Committer: Holger Weiss <[email protected]>
      Date: Wed Apr 18 11:43:13 2012 -0500
       URL: http://nagiosplug.git.sf.net/git/gitweb.cgi?p=nagiosplug/nagiosplug;a=commit;h=aab6b8a

Added check_range shell function to utils.sh.in

---

 plugins-scripts/utils.sh.in |   85 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 85 insertions(+), 0 deletions(-)

diff --git a/plugins-scripts/utils.sh.in b/plugins-scripts/utils.sh.in
index b30b908..d5013a6 100644
--- a/plugins-scripts/utils.sh.in
+++ b/plugins-scripts/utils.sh.in
@@ -21,3 +21,88 @@ support() {
 	$ECHO "@SUPPORT@" | sed -e 's/\n/ /g'
 }
 
+
+# check_range takes a value and a range string, returning successfully if an
+# alert should be raised based on the range
+check_range() {
+	local v range yes no err decimal start end cmp match
+	v="$1"
+	range="$2"
+
+	# whether to raise an alert or not
+	yes=0
+	no=1
+	err=2
+
+	# regex to match a decimal number
+	decimal="-?([0-9]+\.?[0-9]*|[0-9]*\.[0-9]+)"
+
+	# compare numbers (including decimals), returning true/false
+	cmp() { awk "BEGIN{ if ($1) exit(0); exit(1)}"; }
+
+	# returns successfully if the string in the first argument matches the
+	# regex in the second
+	match() { echo "$1" | grep -E -q -- "$2"; }
+
+	# make sure value is valid
+	if ! match "$v" "^$decimal$"; then
+		echo "${0##*/}: check_range: invalid value" >&2
+		unset -f cmp match
+		return "$err"
+	fi
+
+	# make sure range is valid
+	if ! match "$range" "^@?(~|$decimal)(:($decimal)?)?$"; then
+		echo "${0##*/}: check_range: invalid range" >&2
+		unset -f cmp match
+		return "$err"
+	fi
+
+	# check for leading @ char, which negates the range
+	if match $range '^@'; then
+		range=${range#@}
+		yes=1
+		no=0
+	fi
+
+	# parse the range string
+	if ! match "$range" ':'; then
+		start=0
+		end="$range"
+	else
+		start="${range%%:*}"
+		end="${range#*:}"
+	fi
+
+	# do the comparison, taking positive ("") and negative infinity ("~")
+	# into account
+	if [ "$start" != "~" ] && [ "$end" != "" ]; then
+		if cmp "$start <= $v" && cmp "$v <= $end"; then
+			unset -f cmp match
+			return "$no"
+		else
+			unset -f cmp match
+			return "$yes"
+		fi
+	elif [ "$start" != "~" ] && [ "$end" = "" ]; then
+		if cmp "$start <= $v"; then
+			unset -f cmp match
+			return "$no"
+		else
+			unset -f cmp match
+			return "$yes"
+		fi
+	elif [ "$start" = "~" ] && [ "$end" != "" ]; then
+		if cmp "$v <= $end"; then
+			unset -f cmp match
+			return "$no"
+		else
+			unset -f cmp match
+			return "$yes"
+		fi
+	else
+		unset -f cmp match
+		return "$no"
+	fi
+}
+


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/