Fixes for macros introduced in latest CVS update

Ethan Galstad nagios at nagios.org
Tue Oct 26 05:27:54 CEST 2004


I implemented this a bit differently than your original patch.  The  
affected macros are now run through the process_macro() command to 
have any macros they contain expanded.  The macros are also URL-
encoded, so the links should be good.  

I left the CGIs they way they were, as I think it offers a bit more 
flexibility.  Even though it does take a few extra CPU cycles, it 
makes it easier for people to add more macro processing if they wish 
(especially for dynamic variables like HOSTSTATUS, etc.).

Patch should be in CVS shortly.


On 23 Oct 2004 at 12:05, Andreas Ericsson wrote:

> As it turns out there were a couple of errors in the patch. Most
> notably in the add_hostextinfo and add_serviceextinfo functions, where
> notes_url and action_url weren't beeing assigned by reference instead
> of getting strdup()'ed.
> 
> I had also left some debugging output and missed to re-insert the
> original number of empty lines in the original patch, causing a few
> really silly hunks to be included.
> 
> Use this new one instead. It's been tested rather thoroughly and works
> splendidly.
> 
> Andreas Ericsson wrote:
> > Ahoy.
> > 
> > I've been playing around with the nagios code a bit and found a two
> > bugs inserted while making the latest additions to the macro family;
> > HOSTACTIONURL, HOSTNOTESURL, SERVICEACTIONURL and SERVICENOTESURL.
> > 
> > 1. Originally, Nagios would free and NULL the extended data after
> > parsing it and dumping it to the objects.cache file. As such, the
> > extended info macros couldn't be expanded at notification time and
> > thus failed. This has been fixed in the attached patch.
> > 
> > 2. It is allowed to use a limited subset of macros in these
> > variables, namely HOSTADDRESS, HOSTNAME and (for services only)
> > SERVICEDESC. Previously, these macros were expanded at run-time by
> > the CGI programs which turned out to be troublesome come
> > notification time. I relocated the code so that URL macro expansion
> > takes place at loading time and made the necessary changes to the
> > CGI's. As a bonus, a few of the cgi's will run a little faster as
> > well.
> > 
> > 
> > I have done a few test so far and everything appears to be in order.
> > Let me know if you find any bugs.
> > 
> > 
> > --------------------------------------------------------------------
> > ----
> > 
> > diff -urN ../Nagios/base/nagios.c ./base/nagios.c
> > --- ../Nagios/base/nagios.c	Fri Oct 22 18:51:27 2004
> > +++ ./base/nagios.c	Sat Oct 23 02:49:51 2004
> > @@ -556,9 +556,6 @@
> >  				exit(ERROR);
> >  			        }
> >  
> > -			/* free extended info data - we don't need this for monitoring
> > */ -			free_extended_data(); -
> >  			/* initialize embedded Perl interpreter */
> >  			init_embedded_perl();
> >  
> > diff -urN ../Nagios/base/utils.c ./base/utils.c
> > --- ../Nagios/base/utils.c	Fri Oct 22 18:51:29 2004
> > +++ ./base/utils.c	Sat Oct 23 04:20:13 2004
> > @@ -303,9 +303,74 @@
> >  /************************ MACRO FUNCTIONS
> >  *************************/
> >  /******************************************************************
> >  /
> >  
> > +/* substitute macros in url type vars for hostextinfo and
> > +serviceextinfo */ char *process_extinfo_url(char *host_name, char
> > +*svc_description, char *url){ 	char
> > +input_buffer[MAX_INPUT_BUFFER]=""; 	char
> > +output_buffer[MAX_INPUT_BUFFER]=""; 	char *temp_buffer; 	char
> > +*processed_url; 	int in_macro=FALSE; 	service *temp_service; 	host
> > +*temp_host;
> > +
> > +	if(host_name==NULL || url==NULL)
> > +		return;
> > +
> > +	if(svc_description!=NULL) {
> > +		temp_service=find_service(host_name,svc_description);
> > +		if(temp_service==NULL)
> > +			return(url);
> > +			}
> > +
> > +	temp_host=find_host(host_name);
> > +	if(temp_host==NULL)
> > +		return(url);
> > +
> > +	strncpy(input_buffer,url,sizeof(input_buffer)-1);
> > +	input_buffer[sizeof(input_buffer)-1]='\x0';
> > +
> > +	for(temp_buffer=my_strtok(input_buffer,"$");temp_buffer!=NULL;temp
> > +_buffer=my_strtok(NULL,"$")){
> > +
> > +		if(in_macro==FALSE){
> > +			if(strlen(output_buffer)+strlen(temp_buffer)<sizeof(output_buffe
> > +r)-1){
> > +				strncat(output_buffer,temp_buffer,sizeof(output_buffer)-strlen(
> > +output_buffer)-1);
> > +				output_buffer[sizeof(output_buffer)-1]='\x0'; 			        }
> > +			in_macro=TRUE; 			} 		else{
> > +
> > +			if(strlen(output_buffer)+strlen(temp_buffer) <
> > +sizeof(output_buffer)-1){
> > +
> > +				if(!strcmp(temp_buffer,"HOSTNAME"))
> > +					strncat(output_buffer,temp_host->name,sizeof(output_buffer)-st
> > +rlen(output_buffer)-1);
> > +
> > +				else if(!strcmp(temp_buffer,"HOSTADDRESS") && temp_host!=NULL)
> > +					strncat(output_buffer,(temp_host->address==NULL)?"":temp_host-
> > +>address,sizeof(output_buffer)-strlen(output_buffer)-1);
> > +
> > +				else if(temp_service!=NULL &&
> > +!strcmp(temp_buffer,"SERVICEDESC"))
> > +					strncat(output_buffer,temp_service->description,sizeof(output_
> > +buffer)-strlen(output_buffer)-1); 			        }
> > +
> > +			in_macro=FALSE;
> > +		        }
> > +	        }
> > +
> > +
> > +	temp_buffer=strdup(output_buffer);
> > +	if(temp_buffer==NULL)
> > +		return(url);
> > +
> > +	return(temp_buffer);
> > +        }
> > +
> > +
> > +
> >  /* replace macros in notification commands with their values */ int
> >  process_macros(char *input_buffer, char *output_buffer, int
> >  buffer_length, int options){ 	char *temp_buffer;
> > +	char extinfo_url_buffer[MAX_INPUT_BUFFER];
> > +	int extinfo_url_macro_options=0;
> >  	int in_macro;
> >  	int arg_index=0;
> >  	int user_index=0;
> > @@ -430,8 +495,13 @@
> >  				else if(!strcmp(temp_buffer,"HOSTGROUPALIAS"))
> >  					selected_macro=macro_x[MACRO_HOSTGROUPALIAS];
> >  
> > -				else if(!strcmp(temp_buffer,"HOSTACTIONURL"))
> > +				else if(!strcmp(temp_buffer,"HOSTACTIONURL")){
> >  					selected_macro=macro_x[MACRO_HOSTACTIONURL];
> > +					/*
> > +process_macros(selected_macro,extinfo_url_buffer,sizeof(extinfo_url
> > +_buffer),extinfo_url_macro_options); */ 					/* printf("*****\npost
> > +processing, hostactionurl turned out as %s\n********\n",
> > +extinfo_url_buffer); */ 					/* selected_macro=extinfo_url_buffer;
> > +*/ 					/* printf("*****\npost processing, selected_macro turned
> > +out as %s\n********\n", selected_macro); */ 				}
> >  
> >  				else if(!strcmp(temp_buffer,"HOSTNOTESURL"))
> >  					selected_macro=macro_x[MACRO_HOSTNOTESURL];
> > diff -urN ../Nagios/cgi/extinfo.c ./cgi/extinfo.c
> > --- ../Nagios/cgi/extinfo.c	Fri Oct 22 18:51:32 2004
> > +++ ./cgi/extinfo.c	Sat Oct 23 04:26:07 2004
> > @@ -373,17 +373,15 @@
> >  				printf("<TABLE BORDER='0'>\n");
> >  				if(temp_hostextinfo->action_url!=NULL &&
> >  strcmp(temp_hostextinfo->action_url,"")){ 					printf("<TR><TD
> >  ALIGN='right'>\n");
> > -					printf("<A HREF='");
> > -					print_extra_host_url(temp_hostextinfo->host_name,temp_hostexti
> > nfo->action_url); -					printf("' TARGET='_blank'><img src='%s%s'
> > border=0 alt='Perform Additional Actions On This Host'
> > title='Perform Additional Actions On This
> > Host'></A>\n",url_images_path,ACTION_ICON);
> > +					printf("<A
> > +HREF='%s'",url_encode(temp_hostextinfo->action_url)); 					printf("
> > +TARGET='_blank'><img src='%s%s' border=0 alt='Perform Additional
> > +Actions On This Host' title='Perform Additional Actions On This
> > +Host'></A>\n",url_images_path,ACTION_ICON);
> >  					printf("<BR CLEAR=ALL><FONT SIZE=-1><I>Extra Host
> >  Actions</I></FONT><BR CLEAR=ALL><BR CLEAR=ALL>\n");
> >  					printf("</TD></TR>\n"); 				        }
> >  				if(temp_hostextinfo->notes_url!=NULL &&
> >  strcmp(temp_hostextinfo->notes_url,"")){ 					printf("<TR><TD
> >  ALIGN='right'>\n");
> > -					printf("<A HREF='");
> > -					print_extra_host_url(temp_hostextinfo->host_name,temp_hostexti
> > nfo->notes_url); -					printf("' TARGET='_blank'><img src='%s%s'
> > border=0 alt='View Additional Notes For This Host' title='View
> > Additional Notes For This Host'></A>\n",url_images_path,NOTES_ICON);
> > +					printf("<A HREF='%s'",
> > +url_encode(temp_hostextinfo->notes_url)); 					printf("
> > +TARGET='_blank'><img src='%s%s' border=0 alt='View Additional Notes
> > +For This Host' title='View Additional Notes For This
> > +Host'></A>\n",url_images_path,NOTES_ICON);
> >  					printf("<BR CLEAR=ALL><FONT SIZE=-1><I>Extra Host
> >  Notes</I></FONT><BR CLEAR=ALL><BR CLEAR=ALL>\n");
> >  					printf("</TD></TR>\n"); 				        }
> > @@ -463,7 +461,7 @@
> >  
> >  	/* free all allocated memory */
> >  	free_memory();
> > -	free_extended_data();
> > +	free_object_data();
> >  	free_comment_data();
> >  	free_downtime_data();
> >  
> > diff -urN ../Nagios/cgi/status.c ./cgi/status.c
> > --- ../Nagios/cgi/status.c	Fri Oct 22 18:51:34 2004
> > +++ ./cgi/status.c	Sat Oct 23 04:26:44 2004
> > @@ -466,7 +466,7 @@
> >  
> >  	/* free all allocated memory */
> >  	free_memory();
> > -	free_extended_data();
> > +	free_object_data();
> >  	free_comment_data();
> >  
> >  	/* free memory allocated to the sort lists */
> > diff -urN ../Nagios/cgi/statusmap.c ./cgi/statusmap.c
> > --- ../Nagios/cgi/statusmap.c	Fri Oct 22 18:51:35 2004
> > +++ ./cgi/statusmap.c	Sat Oct 23 04:27:23 2004
> > @@ -293,7 +293,7 @@
> >  
> >  	/* free all allocated memory */
> >  	free_memory();
> > -	free_extended_data();
> > +	free_object_data();
> >  	free_layer_list();
> >  
> >  	return OK;
> > diff -urN ../Nagios/cgi/statuswrl.c ./cgi/statuswrl.c
> > --- ../Nagios/cgi/statuswrl.c	Fri Oct 22 18:51:35 2004
> > +++ ./cgi/statuswrl.c	Sat Oct 23 04:27:04 2004
> > @@ -181,7 +181,7 @@
> >  
> >  	/* free all allocated memory */
> >  	free_memory();
> > -	free_extended_data();
> > +	free_object_data();
> >  
> >  	return OK;
> >          }
> > diff -urN ../Nagios/common/objects.c ./common/objects.c
> > --- ../Nagios/common/objects.c	Fri Oct 22 18:51:41 2004
> > +++ ./common/objects.c	Sat Oct 23 04:24:12 2004
> > @@ -79,7 +79,6 @@
> >  
> >  
> >  
> > -
> >  /******************************************************************
> >  / /******* TOP-LEVEL HOST CONFIGURATION DATA INPUT FUNCTION
> >  *********/
> >  /******************************************************************
> >  /
> > @@ -4351,7 +4350,11 @@
> >  	if(notes_url==NULL || !strcmp(notes_url,""))
> >  		new_hostextinfo->notes_url=NULL;
> >  	else{
> > -		new_hostextinfo->notes_url=strdup(notes_url);
> > +#ifdef NSCORE
> > +		new_hostextinfo->notes_url=process_extinfo_url(new_hostextinfo->h
> > +ost_name,NULL,notes_url);
> > +#else
> > +		new_hostextinfo->notes_url=notes_url;
> > +#endif
> >  		if(new_hostextinfo->notes_url==NULL){
> >  			free(new_hostextinfo->notes);
> >  			free(new_hostextinfo->host_name);
> > @@ -4363,12 +4366,19 @@
> >  #endif
> >  			return NULL;
> >  		        }
> > +		else{
> > +			printf("new_hostextinfo->notes_url = %s\n",
> > +new_hostextinfo->notes_url); 		}
> >  	        }
> >  
> >  	if(action_url==NULL || !strcmp(action_url,""))
> >  		new_hostextinfo->action_url=NULL;
> >  	else{
> > -		new_hostextinfo->action_url=strdup(action_url);
> > +#ifdef NSCORE
> > +		new_hostextinfo->action_url=process_extinfo_url(new_hostextinfo->
> > +host_name,NULL,action_url);
> > +#else
> > +		new_hostextinfo->action_url=action_url;
> > +#endif
> >  		if(new_hostextinfo->action_url==NULL){
> >  			free(new_hostextinfo->notes_url);
> >  			free(new_hostextinfo->notes);
> > @@ -4601,7 +4611,11 @@
> >  	if(notes_url==NULL || !strcmp(notes_url,""))
> >  		new_serviceextinfo->notes_url=NULL;
> >  	else{
> > -		new_serviceextinfo->notes_url=strdup(notes_url);
> > +#ifdef NSCORE
> > +		new_serviceextinfo->notes_url=process_extinfo_url(new_serviceexti
> > +nfo->host_name,new_serviceextinfo->description,notes_url);
> > +#else
> > +		new_serviceextinfo->notes_url=notes_url;
> > +#endif
> >  		if(new_serviceextinfo->notes_url==NULL){
> >  			free(new_serviceextinfo->notes);
> >  			free(new_serviceextinfo->description);
> > @@ -4619,7 +4633,11 @@
> >  	if(action_url==NULL || !strcmp(action_url,""))
> >  		new_serviceextinfo->action_url=NULL;
> >  	else{
> > -		new_serviceextinfo->action_url=strdup(action_url);
> > +#ifdef NSCORE
> > +		new_serviceextinfo->action_url=process_extinfo_url(new_serviceext
> > +info->host_name,new_serviceextinfo->description,action_url);
> > +#else
> > +		new_serviceextinfo->action_url=action_url;
> > +#endif
> >  		if(new_serviceextinfo->action_url==NULL){
> >  			free(new_serviceextinfo->notes_url);
> >  			free(new_serviceextinfo->notes);
> > @@ -5687,6 +5705,11 @@
> >  	hostdependency *next_hostdependency=NULL;
> >  	hostescalation *this_hostescalation=NULL;
> >  	hostescalation *next_hostescalation=NULL;
> > +	hostextinfo *this_hostextinfo;
> > +	hostextinfo *next_hostextinfo;
> > +	serviceextinfo *this_serviceextinfo;
> > +	serviceextinfo *next_serviceextinfo;
> > +
> >  	int day;
> >  	int i;
> >  
> > @@ -6035,32 +6058,10 @@
> >  	hostescalation_hashlist=NULL;
> >  	hostescalation_list=NULL;
> >  
> > -	/* free extended info data */
> > -	free_extended_data();
> > -
> >  #ifdef DEBUG1
> >  	printf("\thostescalation_list freed\n");
> >  #endif
> >  
> > -#ifdef DEBUG0
> > -	printf("free_object_data() end\n");
> > -#endif
> > -
> > -	return OK;
> > -        }
> > -
> > -
> > -/* free all allocated memory for extended info objects */
> > -int free_extended_data(void){
> > -	hostextinfo *this_hostextinfo;
> > -	hostextinfo *next_hostextinfo;
> > -	serviceextinfo *this_serviceextinfo;
> > -	serviceextinfo *next_serviceextinfo;
> > -
> > -#ifdef DEBUG0
> > -	printf("free_extended_data() start\n");
> > -#endif
> > -
> >  	/* free memory for the extended host info list */
> >  	for(this_hostextinfo=hostextinfo_list;this_hostextinfo!=NULL;this_
> >  hostextinfo=next_hostextinfo){
> >  		next_hostextinfo=this_hostextinfo->next;
> > @@ -6105,11 +6106,9 @@
> >  	serviceextinfo_hashlist=NULL;
> >  	serviceextinfo_list=NULL;
> >  
> > -
> >  #ifdef DEBUG0
> > -	printf("free_extended_data() start\n");
> > +	printf("free_object_data() end\n");
> >  #endif
> >  
> >  	return OK;
> >          }
> > -
> > diff -urN ../Nagios/include/nagios.h.in ./include/nagios.h.in
> > --- ../Nagios/include/nagios.h.in	Fri Oct 22 18:52:17 2004
> > +++ ./include/nagios.h.in	Sat Oct 23 03:59:46 2004
> > @@ -594,6 +594,7 @@
> >  
> >  
> >  /**** Macro Functions ****/
> > +char *process_extinfo_url(char *,char *,char *);        /* replace
> > +macros with their actual values in extinfo object urls */
> >  int process_macros(char *,char *,int,int);             	/* replace
> >  macros with their actual values */ char *clean_macro_chars(char
> >  *,int);                    /* cleans macros characters before
> >  insertion into output string */ int grab_service_macros(service *);
> >                   	/* updates the service macro data */
> 
> -- 
> Andreas Ericsson                   andreas.ericsson at op5.se
> OP5 AB                             www.op5.se
> Lead Developer
> 



Ethan Galstad,
Nagios Developer
---
Email: nagios at nagios.org
Website: http://www.nagios.org



-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal
Use IT products in your business? Tell us what you think of them. Give us
Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl




More information about the Developers mailing list