Re: [Nagios-devel] Undefined custom macros remain unparsed

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
Guest

Re: [Nagios-devel] Undefined custom macros remain unparsed

Post by Guest »


--=-IqXLD+EEq8xEq1oX+8n1
Content-Transfer-Encoding: 7bit
Content-Type: text/plain

> > 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, customvariablesmem

...[email truncated]...


This post was automatically imported from historical nagios-devel mailing list archives
Original poster: [email protected]
Locked