Undefined custom macros remain unparsed

matthias eble matthias.eble at mailing.kaufland-informationssysteme.com
Thu Apr 9 13:48:12 CEST 2009


> > To me $_HOSTUNDEFINEDMACRO$ should evaluate to an empty string rather
> > than being left untouched. Especially when used in commands, the
> > $_HOSTUNDEFINEDMACRO part gets interpreted by the shell (usually empty)
> > and the trailing $ is displayed plainly.
> > 
> > Following patch is made against 3.0.5 but applies cleanly to latest git
> > snapshot from git.nagiosprojects.org. It changes the behavior for custom
> > macros.
> > 
> > Does anyone see any reasons why this should not be changed?
> > 
> 
> It makes it impossible to use shell variables in commands.
> Imagine something like this:
> 
>   echo "hoopla" > $HOME/$HOSTADDRESS$

Don't single $s need to be escaped using $$ ?
I made a short test. A similar line didn't work for me with current
version since everything between two $-signs seems to be considered as a
macro.

> A better way of achieving what you want is to extend the macro
> syntax so that
> 
>   $?_RANDOMCUSTOMVARIABLE$
> 
> expands to nothing if _RANDOMCUSTOMVARIABLE can't be located

Good idea. So how about following?

I also attached the patch as a file in case of wrapping problems.

Matthias
--- nagios-3.0.6/include/macros.h	2008-11-30 18:22:59.000000000 +0100
+++ nagios-3.0.6/include/macros.h.new	2009-04-09 13:18:53.850676500
+0200
@@ -248,7 +248,7 @@
 int grab_standard_contact_macro(int,contact *,char **);
 int grab_contact_address_macro(int,contact *,char **);
 int grab_standard_contactgroup_macro(int,contactgroup *,char **);
-int grab_custom_object_macro(char *,customvariablesmember *,char **);
+int grab_custom_object_macro(char *,customvariablesmember *,char **,
int);
 
 
 #ifdef NSCORE
--- nagios-3.0.6/common/macros.c	2008-11-30 18:22:58.000000000 +0100
+++ nagios-3.0.6/common/macros.c.new	2009-04-09 13:18:44.866115000 +0200
@@ -609,7 +609,7 @@
 		}
 
 	/***** CUSTOM VARIABLE MACROS *****/
-	else if(macro_name[0]=='_'){
+	else if(macro_name[0]=='_' || strstr(macro_name, "?_")==macro_name){
 
 		/* get the macro value */
 		result=grab_custom_macro_value(macro_name,arg[0],arg[1],output);
@@ -1282,12 +1282,18 @@
 	contactgroup *temp_contactgroup=NULL;
 	contactsmember *temp_contactsmember=NULL;
 	int delimiter_len=0;
+	int undef_is_empty=FALSE;
 	char *temp_buffer=NULL;
 	int result=OK;
 
 	if(macro_name==NULL || output==NULL)
 		return ERROR;
 
+	if(macro_name[0]=='?'){
+		undef_is_empty=TRUE;
+		macro_name++;
+		}
+
 	/***** CUSTOM HOST MACRO *****/
 	if(strstr(macro_name,"_HOST")==macro_name){
 
@@ -1305,7 +1311,7 @@
 				return ERROR;
 
 			/* get the host macro value */
-			result=grab_custom_object_macro(macro_name
+5,temp_host->custom_variables,output);
+			result=grab_custom_object_macro(macro_name
+5,temp_host->custom_variables,output,undef_is_empty);
 			}
 
 		/* a host macro with a hostgroup name and delimiter */
@@ -1356,7 +1362,7 @@
 				return ERROR;
 
 			/* get the service macro value */
-			result=grab_custom_object_macro(macro_name
+8,temp_service->custom_variables,output);
+			result=grab_custom_object_macro(macro_name
+8,temp_service->custom_variables,output,undef_is_empty);
 			}
 
 		/* else and ondemand macro... */
@@ -1368,7 +1374,7 @@

if((temp_service=find_service((macro_host_ptr)?macro_host_ptr->name:NULL,arg2))){
 
 				/* get the service macro value */
-				result=grab_custom_object_macro(macro_name
+8,temp_service->custom_variables,output);
+				result=grab_custom_object_macro(macro_name
+8,temp_service->custom_variables,output,undef_is_empty);
 				}
 
 			/* else we have a service macro with a servicegroup name and a
delimiter... */
@@ -1428,7 +1434,7 @@
 				return ERROR;
 
 			/* get the contact macro value */
-			result=grab_custom_object_macro(macro_name
+8,temp_contact->custom_variables,output);
+			result=grab_custom_object_macro(macro_name
+8,temp_contact->custom_variables,output,undef_is_empty);
 			}
 
 		/* a contact macro with a contactgroup name and delimiter */
@@ -2371,11 +2377,15 @@
 
 
 /* computes a custom object macro */
-int grab_custom_object_macro(char *macro_name, customvariablesmember
*vars, char **output){
+int grab_custom_object_macro(char *macro_name, customvariablesmember
*vars, char **output, int undef_is_empty){
 	customvariablesmember *temp_customvariablesmember=NULL;
 	int result=ERROR;
+	int found=FALSE;
 
-	if(macro_name==NULL || vars==NULL || output==NULL)
+	if(macro_name==NULL || output==NULL)
+		return ERROR;
+
+	if (!undef_is_empty && vars==NULL)
 		return ERROR;
 
 	/* get the custom variable */
@@ -2385,13 +2395,19 @@
 			continue;
 
 		if(!strcmp(macro_name,temp_customvariablesmember->variable_name)){
-			if(temp_customvariablesmember->variable_value)
+			if(temp_customvariablesmember->variable_value){
 				*output=(char *)strdup(temp_customvariablesmember->variable_value);
+				found=TRUE;
+				}
 			result=OK;
 			break;
 			}
 	        }
 
+	if(!found && undef_is_empty){
+		*output=(char *)strdup("");
+		result=OK;
+	}
 	return result;
 	}
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: macro2.patch
Type: text/x-patch
Size: 3798 bytes
Desc: not available
URL: <https://www.monitoring-lists.org/archive/developers/attachments/20090409/15bef0ed/attachment.bin>
-------------- next part --------------
------------------------------------------------------------------------------
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
-------------- next part --------------
_______________________________________________
Nagios-devel mailing list
Nagios-devel at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-devel


More information about the Developers mailing list