SVN: nagios-notify/trunk/nagios-notify

glen glen at pld-linux.org
Sun Oct 12 13:16:18 CEST 2008


Author: glen
Date: Sun Oct 12 13:16:18 2008
New Revision: 9906

Modified:
   nagios-notify/trunk/nagios-notify
Log:
- add $(math:expression)

Modified: nagios-notify/trunk/nagios-notify
==============================================================================
--- nagios-notify/trunk/nagios-notify	(original)
+++ nagios-notify/trunk/nagios-notify	Sun Oct 12 13:16:18 2008
@@ -92,6 +92,38 @@
 		return s;
 	}
 
+	function eval_math(left, op, right) {
+		if (op == "+") {
+			return left + right;
+
+		} else if (op == "-") {
+			return left - right;
+
+		} else if (op == "*") {
+			return left * right;
+
+		} else if (op == "/") {
+			return left / right;
+
+		}
+	}
+
+	# evaluate mathematical expression
+	function math(expr,   op, left, right) {
+		# find left expression, operator, right expression
+		if (match(expr, "^.*("MATH_OPS")")) {
+			left = trim(substr(expr, 1, RLENGTH));
+			right = trim(substr(expr, RSTART + RLENGTH));
+			# find op from end of left
+			if (match(left, "("MATH_OPS")$")) {
+				op = substr(left, RSTART, RLENGTH);
+				left = substr(left, 1, RSTART - 1);
+			}
+		}
+
+		return eval_math(int(left), op, int(right));
+	}
+
 	function eval_expr(left, op, right) {
 		left = unquote(left);
 		right = unquote(right);
@@ -99,7 +131,7 @@
 		if (op == "==") {
 			return left == right;
 
-		} else if (op == "=~") {
+		} else if (op == "=~" || op == "~=") {
 			return left ~ right;
 
 		} else if (op == "!~") {
@@ -130,11 +162,11 @@
 			line = substr(line, 1 + RLENGTH);
 
 			# find left expression, operator, right expression
-			if (match(line, "^.*("OPS")")) {
+			if (match(line, "^.*("COND_OPS")")) {
 				left = trim(substr(line, 1, RLENGTH));
 				right = trim(substr(line, RSTART + RLENGTH));
 				# find op from end of left
-				if (match(left, "("OPS")$")) {
+				if (match(left, "("COND_OPS")$")) {
 					op = substr(left, RSTART, RLENGTH);
 					left = substr(left, 1, RSTART - 1);
 				}
@@ -183,15 +215,17 @@
 			}
 		}
 
-		# list of comparision operands
-		CMDS = "if|else|endif"
 		# valid operands
-		OPS = "==|=~|!~|!=|>=|<=|>|<";
+		COND_OPS = "==|=~|~=|!~|!=|>=|<=|>|<";
 		# valid variables in expression
 		# - numbers
 		# - strings in quotes
 		# - regexps between //
-		VARS = "[0-9]+|\"[^\"]+\"|/[^/]+/";
+		COND_VARS = "[0-9]+|\"[^\"]+\"|/[^/]+/";
+
+		# valid mathematical operands we support
+		MATH_OPS = "\+|-|\*|/"
+		MATH_EXPR = "[0-9]+"
 
 		# by default we have no condition set
 		_stack_depth = 0;
@@ -205,7 +239,7 @@
 			gsub("\$" var "\$", val);
 		}
 
-		if (match($0, "^#(if *("VARS") *("OPS") *("VARS") *$|else|endif)")) {
+		if (match($0, "^#(if *("COND_VARS") *("COND_OPS") *("COND_VARS") *$|else|endif)")) {
 			process_expr($0);
 			next;
 		}
@@ -242,6 +276,15 @@
 			$0 = left urlencode(data) right;
 		}
 
+		# $(math:expression)
+		if (match($0, "\$\(math:("MATH_EXPR") *("MATH_OPS") *("MATH_EXPR")\)")) {
+			pos = length("$(math:")
+			expr = substr($0, RSTART + pos, RLENGTH - pos - 1);
+			left = substr($0, 0, RSTART);
+			right = substr($0, RSTART + RLENGTH);
+			$0 = left math(expr) right;
+		}
+
 		# print out
 		print;
 	}


More information about the pld-cvs-commit mailing list