SVN: nagios-notify/trunk/nagios-notify

glen glen at pld-linux.org
Tue Jan 22 15:59:49 CET 2008


Author: glen
Date: Tue Jan 22 15:59:49 2008
New Revision: 9192

Modified:
   nagios-notify/trunk/nagios-notify
Log:
- convert to awk only

Modified: nagios-notify/trunk/nagios-notify
==============================================================================
--- nagios-notify/trunk/nagios-notify	(original)
+++ nagios-notify/trunk/nagios-notify	Tue Jan 22 15:59:49 2008
@@ -2,10 +2,9 @@
 # $Id: nagios-notify.sh,v 1.3 2006/08/03 12:39:22 glen Exp $
 #
 # Template based Nagios notify script.
-# Requires: sed, awk
+# Requires: awk
 #
 # Author: Elan Ruusamäe <glen at pld-linux.org>
-# Date: 2006-08-03
 # License: Same as Nagios (GPL v2)
 #
 
@@ -16,37 +15,38 @@
 # Substutute Nagios $VAR$-s (which are exported to environment by Nagios) from template.
 template_subst() {
 	local tmpl="$1"
-	awk 'BEGIN {
+	awk '
+	function base64(file) {
+		cmd = "base64 " file;
+		out = ""
+		while ((cmd | getline) > 0) {
+			out = out $0;
+		}
+		close(cmd);
+		return out;
+	}
+	{
+		# replace environ variables
 		for (var in ENVIRON) {
 			if (substr(var, 1, length("NAGIOS_")) == "NAGIOS_") {
 				val = ENVIRON[var];
 				var = substr(var, 1 + length("NAGIOS_"));
-				gsub(/#/,"\\#",val);
-				printf("s#\$%s\$#%s#g\n", var, val);
+				gsub("\$" var "\$", val);
 			}
 		}
-		exit;
-	}' | sed -f - $tmpl | perl -e '
-		use strict;
-		use MIME::Base64;
-
-		sub cat {
-			open(FH, shift) or die;
-			local $/ = undef;
-			local $_ = <FH>;
-			close FH;
-			return $_;
-		}
 
-		sub base64_subst {
-			encode_base64(cat(shift));
+		# replace special: base64 handler
+		if (match($0, /\$\(base64:(.*)\)/)) {
+			file = substr($0, RSTART + 9, RLENGTH - 10);
+			left = substr($0, 0, RSTART);
+			right = substr($0, RSTART + RLENGTH);
+			$0 = left base64(file) right;
 		}
 
-		while (<>) {
-			s/\$\(base64:(\S+)\)/base64_subst($1)/eg;
-			print;
-		}
-	'
+		# print out
+		print
+	}
+	' $tmpl
 }
 
 if [ -z "$NAGIOS_STATUSDATAFILE" ]; then


More information about the pld-cvs-commit mailing list