[Nagiosplug-checkins] CVS: nagiosplug/plugins check_nt.c,1.15,1.16

Karl DeBisschop kdebisschop at users.sourceforge.net
Fri Aug 8 09:49:07 CEST 2003


Update of /cvsroot/nagiosplug/nagiosplug/plugins
In directory sc8-pr-cvs1:/tmp/cvs-serv24772/plugins

Modified Files:
	check_nt.c 
Log Message:
change if/elseif block to case, general streamline

Index: check_nt.c
===================================================================
RCS file: /cvsroot/nagiosplug/nagiosplug/plugins/check_nt.c,v
retrieving revision 1.15
retrieving revision 1.16
diff -C2 -r1.15 -r1.16
*** check_nt.c	8 Aug 2003 04:33:21 -0000	1.15
--- check_nt.c	8 Aug 2003 16:48:26 -0000	1.16
***************
*** 34,50 ****
  #include "utils.h"
  
! #define CHECK_NONE	0
! #define CHECK_CLIENTVERSION  	1
! #define CHECK_CPULOAD  	2
! #define CHECK_UPTIME	3
! #define CHECK_USEDDISKSPACE	4
! #define CHECK_SERVICESTATE	5
! #define CHECK_PROCSTATE	6
! #define CHECK_MEMUSE	7
! #define CHECK_COUNTER	8
! #define CHECK_FILEAGE	9
! #define MAX_VALUE_LIST 30
! 
! #define PORT	1248	
  
  char *server_address=NULL;
--- 34,54 ----
  #include "utils.h"
  
! enum checkvars {
! 	CHECK_NONE,
! 	CHECK_CLIENTVERSION,
! 	CHECK_CPULOAD,
! 	CHECK_UPTIME,
! 	CHECK_USEDDISKSPACE,
! 	CHECK_SERVICESTATE,
! 	CHECK_PROCSTATE,
! 	CHECK_MEMUSE,
! 	CHECK_COUNTER,
! 	CHECK_FILEAGE
! };
! 
! enum {
! 	MAX_VALUE_LIST = 30,
! 	PORT = 1248
! };
  
  char *server_address=NULL;
***************
*** 59,67 ****
  int check_warning_value=FALSE;
  int check_critical_value=FALSE;
! int vars_to_check=CHECK_NONE;
  int show_all=FALSE;
  
  const char *progname = "check_nt";
  
  int process_arguments(int, char **);
  void preparelist(char *string);
--- 63,74 ----
  int check_warning_value=FALSE;
  int check_critical_value=FALSE;
! enum checkvars vars_to_check = CHECK_NONE;
  int show_all=FALSE;
  
  const char *progname = "check_nt";
  
+ char recv_buffer[MAX_INPUT_BUFFER];
+ 
+ void fetch_data (const char* address, int port, const char* sendb);
  int process_arguments(int, char **);
  void preparelist(char *string);
***************
*** 71,78 ****
  
  int main(int argc, char **argv){
- 	int result;
  	int return_code = STATE_UNKNOWN;
  	char *send_buffer=NULL;
- 	char recv_buffer[MAX_INPUT_BUFFER];
  	char *output_message=NULL;
  	char *temp_string=NULL;
--- 78,83 ----
***************
*** 93,98 ****
  	int upminutes=0;
  
- 	asprintf(&req_password, _("None"));
- 
  	if(process_arguments(argc,argv)==ERROR)
  		usage(_("Could not parse arguments\n"));
--- 98,101 ----
***************
*** 104,182 ****
  	alarm(socket_timeout);
  
! 	if (vars_to_check==CHECK_CLIENTVERSION) {
  
! 		asprintf(&send_buffer,strcat(req_password,"&1"));
! 		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
! 		if(result!=STATE_OK)
! 			return result;
! 		asprintf(&output_message,recv_buffer);
! 		return_code=STATE_OK;
  
! 	}
! 
! 	else if(vars_to_check==CHECK_CPULOAD){
  
! 		if (check_value_list==TRUE) {																			
! 			if (strtoularray(lvalue_list,value_list,",")==TRUE) {
! 				/* -l parameters is present with only integers */
! 				return_code=STATE_OK;
! 				asprintf(&temp_string,_("CPU Load"));
! 				while (lvalue_list[0+offset]> (unsigned long)0 &&
! 				       lvalue_list[0+offset]<=(unsigned long)17280 && 
! 				       lvalue_list[1+offset]> (unsigned long)0 &&
! 				       lvalue_list[1+offset]<=(unsigned long)100 && 
! 				       lvalue_list[2+offset]> (unsigned long)0 &&
! 				       lvalue_list[2+offset]<=(unsigned long)100) {
! 					/* loop until one of the parameters is wrong or not present */
! 
! 					/* Send request and retrieve data */
! 					asprintf(&send_buffer,"%s&2&%lu",req_password,lvalue_list[0+offset]);
! 					result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
! 					if(result!=STATE_OK)
! 						return result;
! 
! 					if (!strncmp(recv_buffer,"ERROR",5)) {
! 						printf("NSClient - %s\n",recv_buffer);
! 						exit(STATE_UNKNOWN);
! 					}
! 
! 					utilization=strtoul(recv_buffer,NULL,10);
! 
! 					/* Check if any of the request is in a warning or critical state */
! 					if(utilization >= lvalue_list[2+offset])
! 						return_code=STATE_CRITICAL;
! 					else if(utilization >= lvalue_list[1+offset] && return_code<STATE_WARNING)
! 						return_code=STATE_WARNING;
! 
! 					asprintf(&output_message,_(" %lu%% (%lu min average)"), utilization, lvalue_list[0+offset]);
! 					asprintf(&temp_string,"%s%s",temp_string,output_message);
! 					offset+=3;	/* move across the array */
! 				}		
! 				if (strlen(temp_string)>10) {
! 					/* we had at least on loop */
! 					asprintf(&output_message,"%s",temp_string);
! 				}	
! 				else
! 					asprintf(&output_message,"%s",_("not enough values for -l parameters"));
! 					
! 			} else 
! 				asprintf(&output_message,_("wrong -l parameter."));
  
! 		} else
! 			asprintf(&output_message,_("missing -l parameters"));
! 	}
  
! 	else if(vars_to_check==CHECK_UPTIME){
  
! 		asprintf(&send_buffer,strcat(req_password,"&3"));
! 		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
! 		if(result!=STATE_OK)
! 			return result;
! 
! 		if (!strncmp(recv_buffer,"ERROR",5)) {
! 			printf("NSClient - %s\n",recv_buffer);
! 			exit(STATE_UNKNOWN);
! 		}
  
  		uptime=strtoul(recv_buffer,NULL,10);
  		updays = uptime / 86400; 			
--- 107,165 ----
  	alarm(socket_timeout);
  
! 	switch (vars_to_check) {
  
! 	case CHECK_CLIENTVERSION:
  
! 		asprintf(&send_buffer, "%s&1", req_password);
! 		fetch_data (server_address, server_port, send_buffer);
! 		output_message = strdup (recv_buffer);
! 		return_code=STATE_OK;
! 		break;
  
! 	case CHECK_CPULOAD:
  
! 		if (value_list==NULL)
! 			output_message = strdup (_("missing -l parameters"));
! 		else if (strtoularray(lvalue_list,value_list,",")==FALSE)
! 			output_message = strdup (_("wrong -l parameter."));
! 		else {
! 			/* -l parameters is present with only integers */
! 			return_code=STATE_OK;
! 			temp_string = strdup (_("CPU Load"));
! 			/* loop until one of the parameters is wrong or not present */
! 			while (lvalue_list[0+offset]> (unsigned long)0 &&
! 						 lvalue_list[0+offset]<=(unsigned long)17280 && 
! 						 lvalue_list[1+offset]> (unsigned long)0 &&
! 						 lvalue_list[1+offset]<=(unsigned long)100 && 
! 						 lvalue_list[2+offset]> (unsigned long)0 &&
! 						 lvalue_list[2+offset]<=(unsigned long)100) {
! 
! 				/* Send request and retrieve data */
! 				asprintf(&send_buffer,"%s&2&%lu",req_password,lvalue_list[0+offset]);
! 				fetch_data (server_address, server_port, send_buffer);
! 
! 				utilization=strtoul(recv_buffer,NULL,10);
! 				
! 				/* Check if any of the request is in a warning or critical state */
! 				if(utilization >= lvalue_list[2+offset])
! 					return_code=STATE_CRITICAL;
! 				else if(utilization >= lvalue_list[1+offset] && return_code<STATE_WARNING)
! 					return_code=STATE_WARNING;
  
! 				asprintf(&output_message,_(" %lu%% (%lu min average)"), utilization, lvalue_list[0+offset]);
! 				asprintf(&temp_string,"%s%s",temp_string,output_message);
! 				offset+=3;	/* move across the array */
! 			}
! 			if (strlen(temp_string)>10)  /* we had at least one loop */
! 				output_message = strdup (temp_string);
! 			else
! 				output_message = strdup (_("not enough values for -l parameters"));
! 		}	
! 		break;
  
! 	case CHECK_UPTIME:
  
+ 		asprintf(&send_buffer, "%s&3", req_password);
+ 		fetch_data (server_address, server_port, send_buffer);
  		uptime=strtoul(recv_buffer,NULL,10);
  		updays = uptime / 86400; 			
***************
*** 185,271 ****
  		asprintf(&output_message,_("System Uptime : %u day(s) %u hour(s) %u minute(s)"),updays,uphours, upminutes);
  		return_code=STATE_OK;
! 	}
  
! 	else if(vars_to_check==CHECK_USEDDISKSPACE){
  
! 		return_code=STATE_UNKNOWN;	
! 		if (check_value_list==TRUE) {
! 			if (strlen(value_list)==1) {
! 				asprintf(&send_buffer,"%s&4&%s", req_password, value_list);
! 				result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
! 				if(result!=STATE_OK)
! 					return result;
! 		
! 				if (!strncmp(recv_buffer,"ERROR",5)) {
! 					printf("NSClient - %s\n",recv_buffer);
! 					exit(STATE_UNKNOWN);
! 				}
! 
! 				free_disk_space=atof(strtok(recv_buffer,"&"));
! 				total_disk_space=atof(strtok(NULL,"&"));
! 				percent_used_space = ((total_disk_space - free_disk_space) / total_disk_space) * 100;
! 
! 				if (free_disk_space>=0) {
! 					asprintf(&temp_string,_("%s:\\ - total: %.2f Gb - used: %.2f Gb (%.0f%%) - free %.2f Gb (%.0f%%)"),
! 							value_list, total_disk_space / 1073741824, (total_disk_space - free_disk_space) / 1073741824, percent_used_space,
! 							 free_disk_space / 1073741824, (free_disk_space / total_disk_space)*100); 
! 
! 
! 					if(check_critical_value==TRUE && percent_used_space >= critical_value)
! 						return_code=STATE_CRITICAL;
! 					else if (check_warning_value==TRUE && percent_used_space >= warning_value)
! 						return_code=STATE_WARNING;	
! 					else
! 						return_code=STATE_OK;	
! 
! 					asprintf(&output_message,"%s",temp_string);
! 
! 				}
! 				else {
! 					asprintf(&output_message,_("Free disk space : Invalid drive "));
! 					return_code=STATE_UNKNOWN;
! 				}		
  			}
! 			else 
! 				asprintf(&output_message,_("wrong -l argument"));
! 		} else 
! 			asprintf(&output_message,_("missing -l parameters"));
! 			
! 	}
  
! 	else if(vars_to_check==CHECK_SERVICESTATE || vars_to_check==CHECK_PROCSTATE){
  
! 		if (check_value_list==TRUE) {
  			preparelist(value_list);		/* replace , between services with & to send the request */
  			asprintf(&send_buffer,"%s&%u&%s&%s", req_password,(vars_to_check==CHECK_SERVICESTATE)?5:6,
! 				(show_all==TRUE)?_("ShowAll"):_("ShowFail"),value_list);
! 			result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
! 			if(result!=STATE_OK)
! 				return result;
! 	
! 			if (!strncmp(recv_buffer,"ERROR",5)) {
! 				printf("NSClient - %s\n",recv_buffer);
! 				exit(STATE_UNKNOWN);
! 			}
  			return_code=atoi(strtok(recv_buffer,"&"));
  			temp_string=strtok(NULL,"&");
! 			asprintf(&output_message, "%s",temp_string);
  		}
! 		else 
! 			asprintf(&output_message,_("No service/process specified"));
! 	}
  
! 	else if(vars_to_check==CHECK_MEMUSE) {
  		
  		asprintf(&send_buffer,"%s&7", req_password);
! 		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
! 		if (result!=STATE_OK)
! 			return result;
! 
! 		if (!strncmp(recv_buffer,"ERROR",5)) {
! 			printf("NSClient - %s\n",recv_buffer);
! 			exit(STATE_UNKNOWN);
! 		}
! 
  		mem_commitLimit=atof(strtok(recv_buffer,"&"));
  		mem_commitByte=atof(strtok(NULL,"&"));
--- 168,228 ----
  		asprintf(&output_message,_("System Uptime : %u day(s) %u hour(s) %u minute(s)"),updays,uphours, upminutes);
  		return_code=STATE_OK;
! 		break;
  
! 	case CHECK_USEDDISKSPACE:
  
! 		if (value_list==NULL)
! 			output_message = strdup (_("missing -l parameters"));
! 		else if (strlen(value_list)==1)
! 			output_message = strdup (_("wrong -l argument"));
! 		else {
! 			asprintf(&send_buffer,"%s&4&%s", req_password, value_list);
! 			fetch_data (server_address, server_port, send_buffer);
! 			free_disk_space=atof(strtok(recv_buffer,"&"));
! 			total_disk_space=atof(strtok(NULL,"&"));
! 			percent_used_space = ((total_disk_space - free_disk_space) / total_disk_space) * 100;
! 
! 			if (free_disk_space>=0) {
! 				asprintf(&temp_string,_("%s:\\ - total: %.2f Gb - used: %.2f Gb (%.0f%%) - free %.2f Gb (%.0f%%)"),
! 								 value_list, total_disk_space / 1073741824, (total_disk_space - free_disk_space) / 1073741824, percent_used_space,
! 								 free_disk_space / 1073741824, (free_disk_space / total_disk_space)*100); 
! 
! 
! 				if(check_critical_value==TRUE && percent_used_space >= critical_value)
! 					return_code=STATE_CRITICAL;
! 				else if (check_warning_value==TRUE && percent_used_space >= warning_value)
! 					return_code=STATE_WARNING;	
! 				else
! 					return_code=STATE_OK;	
! 
! 				output_message = strdup (temp_string);
  			}
! 			else {
! 				output_message = strdup (_("Free disk space : Invalid drive "));
! 				return_code=STATE_UNKNOWN;
! 			}
! 		}
! 		break;
  
! 	case CHECK_SERVICESTATE:
! 	case CHECK_PROCSTATE:
  
! 		if (value_list==NULL)
! 			output_message = strdup (_("No service/process specified"));
! 		else {
  			preparelist(value_list);		/* replace , between services with & to send the request */
  			asprintf(&send_buffer,"%s&%u&%s&%s", req_password,(vars_to_check==CHECK_SERVICESTATE)?5:6,
! 							 (show_all==TRUE)?_("ShowAll"):_("ShowFail"),value_list);
! 			fetch_data (server_address, server_port, send_buffer);
  			return_code=atoi(strtok(recv_buffer,"&"));
  			temp_string=strtok(NULL,"&");
! 			output_message = strdup (temp_string);
  		}
! 		break;
  
! 	case CHECK_MEMUSE:
  		
  		asprintf(&send_buffer,"%s&7", req_password);
! 		fetch_data (server_address, server_port, send_buffer);
  		mem_commitLimit=atof(strtok(recv_buffer,"&"));
  		mem_commitByte=atof(strtok(NULL,"&"));
***************
*** 281,311 ****
  		else
  			return_code=STATE_OK;	
- 		
- 	}
  
! 	else if(vars_to_check==CHECK_COUNTER) {
  
! 		if (check_value_list==TRUE) {																			
  			preparelist(value_list);		/* replace , between services with & to send the request */
  			asprintf(&send_buffer,"%s&8&%s", req_password,value_list);
! 			result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
! 			if (result!=STATE_OK)
! 				return result;
! 	
! 			if (!strncmp(recv_buffer,"ERROR",5)) {
! 				printf("NSClient - %s\n",recv_buffer);
! 				exit(STATE_UNKNOWN);
! 			}
! 
  			strtok(value_list,"&");			/* burn the first parameters */
  			description = strtok(NULL,"&");
  			counter_value = atof(recv_buffer);
  			if (description == NULL) 
  				asprintf(&output_message, "%.f", counter_value);
  			else
! 				asprintf(&output_message, description, counter_value);
  	
! 			if (critical_value > warning_value) {
! 				/* Normal thresholds */
  				if(check_critical_value==TRUE && counter_value >= critical_value)
  					return_code=STATE_CRITICAL;
--- 238,262 ----
  		else
  			return_code=STATE_OK;	
  
! 		break;
! 
! 	case CHECK_COUNTER:
  
! 		if (value_list==NULL)
! 			output_message = strdup (_("No counter specified"));
! 		else {
  			preparelist(value_list);		/* replace , between services with & to send the request */
  			asprintf(&send_buffer,"%s&8&%s", req_password,value_list);
! 			fetch_data (server_address, server_port, send_buffer);
  			strtok(value_list,"&");			/* burn the first parameters */
  			description = strtok(NULL,"&");
  			counter_value = atof(recv_buffer);
+ 
  			if (description == NULL) 
  				asprintf(&output_message, "%.f", counter_value);
  			else
! 				asprintf(&output_message,"%s = %.f",  description, counter_value);
  	
! 			if (critical_value > warning_value) {        /* Normal thresholds */
  				if(check_critical_value==TRUE && counter_value >= critical_value)
  					return_code=STATE_CRITICAL;
***************
*** 315,320 ****
  					return_code=STATE_OK;	
  			} 
! 			else {
! 				/* inverse thresholds */
  				if(check_critical_value==TRUE && counter_value <= critical_value)
  					return_code=STATE_CRITICAL;
--- 266,270 ----
  					return_code=STATE_OK;	
  			} 
! 			else {                                       /* inverse thresholds */
  				if(check_critical_value==TRUE && counter_value <= critical_value)
  					return_code=STATE_CRITICAL;
***************
*** 324,354 ****
  					return_code=STATE_OK;	
  			}	
- 		
  		}
! 		else {
! 			asprintf(&output_message,_("No counter specified"));
! 			result=STATE_UNKNOWN;
! 		}
! 	}
! 	else if(vars_to_check==CHECK_FILEAGE) {
  
! 		if (check_value_list==TRUE) {																			
  			preparelist(value_list);		/* replace , between services with & to send the request */
  			asprintf(&send_buffer,"%s&9&%s", req_password,value_list);
! 			result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
! 			if (result!=STATE_OK)
! 				return result;
! 	
! 			if (!strncmp(recv_buffer,"ERROR",5)) {
! 				printf("NSClient - %s\n",recv_buffer);
! 				exit(STATE_UNKNOWN);
! 			}
! 
  			age_in_minutes = atoi(strtok(recv_buffer,"&"));
  			description = strtok(NULL,"&");
! 			asprintf(&output_message, description);
  	
! 			if (critical_value > warning_value) {
! 				/* Normal thresholds */
  				if(check_critical_value==TRUE && age_in_minutes >= critical_value)
  					return_code=STATE_CRITICAL;
--- 274,293 ----
  					return_code=STATE_OK;	
  			}	
  		}
! 		break;
  
! 	case CHECK_FILEAGE:
! 
! 		if (value_list==NULL)
! 			output_message = strdup (_("No counter specified"));
! 		else {
  			preparelist(value_list);		/* replace , between services with & to send the request */
  			asprintf(&send_buffer,"%s&9&%s", req_password,value_list);
! 			fetch_data (server_address, server_port, send_buffer);
  			age_in_minutes = atoi(strtok(recv_buffer,"&"));
  			description = strtok(NULL,"&");
! 			output_message = strdup (description);
  	
! 			if (critical_value > warning_value) {        /* Normal thresholds */
  				if(check_critical_value==TRUE && age_in_minutes >= critical_value)
  					return_code=STATE_CRITICAL;
***************
*** 358,363 ****
  					return_code=STATE_OK;	
  			} 
! 			else {
! 				/* inverse thresholds */
  				if(check_critical_value==TRUE && age_in_minutes <= critical_value)
  					return_code=STATE_CRITICAL;
--- 297,301 ----
  					return_code=STATE_OK;	
  			} 
! 			else {                                       /* inverse thresholds */
  				if(check_critical_value==TRUE && age_in_minutes <= critical_value)
  					return_code=STATE_CRITICAL;
***************
*** 366,376 ****
  				else
  					return_code=STATE_OK;	
! 			}	
! 		
! 		}
! 		else {
! 			asprintf(&output_message,_("No file specified"));
! 			result=STATE_UNKNOWN;
  		}
  	}
  
--- 304,316 ----
  				else
  					return_code=STATE_OK;	
! 			}
  		}
+ 		break;
+ 
+ 	case CHECK_NONE:
+ 	default:
+ 		usage (_(""));
+ 		break;
+ 
  	}
  
***************
*** 411,415 ****
  	/* backwards compatibility */
  	if (! is_option(argv[1])) {
! 		server_address=argv[1];
  		argv[1]=argv[0];
  		argv=&argv[1];
--- 351,355 ----
  	/* backwards compatibility */
  	if (! is_option(argv[1])) {
! 		server_address = strdup(argv[1]);
  		argv[1]=argv[0];
  		argv=&argv[1];
***************
*** 445,452 ****
  				exit(STATE_OK);
  			case 'H': /* hostname */
! 				server_address=optarg;
  				break;
  			case 's': /* password */
! 				asprintf(&req_password,optarg);
  				break;
  			case 'p': /* port */
--- 385,393 ----
  				exit(STATE_OK);
  			case 'H': /* hostname */
! 				if (server_address)	free(server_address);
! 				server_address = strdup(optarg);
  				break;
  			case 's': /* password */
! 				req_password = strdup (optarg);
  				break;
  			case 'p': /* port */
***************
*** 481,486 ****
  				break;
  			case 'l': /* value list */
! 				asprintf(&value_list,"%s",optarg);
! 				check_value_list=TRUE;
  				break;
  			case 'w': /* warning threshold */
--- 422,426 ----
  				break;
  			case 'l': /* value list */
! 				value_list = strdup (optarg);
  				break;
  			case 'w': /* warning threshold */
***************
*** 507,510 ****
--- 447,453 ----
  		return ERROR;
  
+ 	if (req_password == NULL)
+ 		req_password = strdup (_("None"));
+ 
  	return OK;
  }
***************
*** 515,518 ****
--- 458,473 ----
  
  
+ void fetch_data (const char *address, int port, const char *sendb) {
+ 	int result;
+ 
+ 	result=process_tcp_request(address, port, sendb, recv_buffer,sizeof(recv_buffer));
+ 
+ 	if(result!=STATE_OK)
+ 		die (result, "could not fetch information from server\n");
+ 		
+ 	if (!strncmp(recv_buffer,"ERROR",5))
+ 		die (STATE_UNKNOWN, "NSClient - %s\n",recv_buffer);
+ }
+ 
  int strtoularray(unsigned long *array, char *string, const char *delim) {
  	/* split a <delim> delimited string into a long array */





More information about the Commits mailing list