[Nagiosplug-checkins] CVS: nagiosplug/plugins check_swap.c,1.10,1.11

Ton Voon tonvoon at users.sourceforge.net
Fri Jun 27 08:03:08 CEST 2003


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

Modified Files:
	check_swap.c 
Log Message:
Support for swap -s for solaris. Also changes size of swap to MBs through
a conversion amount in configure. Possible breakage on other OSes


Index: check_swap.c
===================================================================
RCS file: /cvsroot/nagiosplug/nagiosplug/plugins/check_swap.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -r1.10 -r1.11
*** check_swap.c	11 Mar 2003 22:22:11 -0000	1.10
--- check_swap.c	27 Jun 2003 14:43:01 -0000	1.11
***************
*** 49,52 ****
--- 49,56 ----
  int allswaps;
  
+ #if !defined(sun)
+ int sun = 0;	/* defined by compiler if it is a sun solaris system */
+ #endif
+ 
  int
  main (int argc, char **argv)
***************
*** 55,62 ****
--- 59,69 ----
  	long unsigned int total_swap = 0, used_swap = 0, free_swap = 0;
  	long unsigned int total, used, free;
+ 	int conv_factor;		/* Convert to MBs */
  	int result = STATE_OK;
  	char input_buffer[MAX_INPUT_BUFFER];
  #ifdef HAVE_SWAP
  	char *temp_buffer;
+ 	char *swap_command;
+ 	char *swap_format;
  #endif
  #ifdef HAVE_PROC_MEMINFO
***************
*** 71,105 ****
  #ifdef HAVE_PROC_MEMINFO
  	fp = fopen (PROC_MEMINFO, "r");
- 	asprintf (&status, "%s", "Swap used:");
  	while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  		if (sscanf (input_buffer, " %s %lu %lu %lu", str, &total, &used, &free) == 4 &&
  		    strstr (str, "Swap")) {
! 			total_swap += total;
! 			used_swap += used;
! 			free_swap += free;
! 			if (allswaps) {
! 				percent = 100 * (((double) used) / ((double) total));
! 				if (percent >= crit_percent || free <= crit_size)
! 					result = max_state (STATE_CRITICAL, result);
! 				else if (percent >= warn_percent || free <= warn_size)
! 					result = max_state (STATE_WARNING, result);
! 				if (verbose)
! 					asprintf (&status, "%s [%lu/%lu]", status, used, total);
! 			}
! 		}
! 	}
! 	percent_used = 100 * (((double) used_swap) / ((double) total_swap));
! 	if (percent_used >= crit_percent || free_swap <= crit_size)
! 		result = max_state (STATE_CRITICAL, result);
! 	else if (percent_used >= warn_percent || free_swap <= warn_size)
! 		result = max_state (STATE_WARNING, result);
! 	asprintf (&status, "%s %2d%% (%lu out of %lu)", status, percent_used,
! 	          used_swap, total_swap);
! 	fclose (fp);
! #else
  #ifdef HAVE_SWAP
! 	child_process = spopen (SWAP_COMMAND);
  	if (child_process == NULL) {
! 		printf ("Could not open pipe: %s\n", SWAP_COMMAND);
  		return STATE_UNKNOWN;
  	}
--- 78,104 ----
  #ifdef HAVE_PROC_MEMINFO
  	fp = fopen (PROC_MEMINFO, "r");
  	while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  		if (sscanf (input_buffer, " %s %lu %lu %lu", str, &total, &used, &free) == 4 &&
  		    strstr (str, "Swap")) {
! #endif
  #ifdef HAVE_SWAP
! 	if (!allswaps && sun) {
! 		asprintf(&swap_command, "%s", "/usr/sbin/swap -s");
! 		asprintf(&swap_format, "%s", "%*s %*dk %*s %*s + %*dk %*s = %dk %*s %dk %*s");
! 		conv_factor = 2048;
! 	} else {
! 		asprintf(&swap_command, "%s", SWAP_COMMAND);
! 		asprintf(&swap_format, "%s", SWAP_FORMAT);
! 		conv_factor = SWAP_CONVERSION;
! 	}
! 
! 	if (verbose >= 2)
! 		printf ("Command: %s\n", swap_command);
! 	if (verbose >= 3)
! 		printf ("Format: %s\n", swap_format);
! 
! 	child_process = spopen (swap_command);
  	if (child_process == NULL) {
! 		printf ("Could not open pipe: %s\n", swap_command);
  		return STATE_UNKNOWN;
  	}
***************
*** 107,116 ****
  	child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  	if (child_stderr == NULL)
! 		printf ("Could not open stderr for %s\n", SWAP_COMMAND);
  
  	sprintf (str, "%s", "");
  	/* read 1st line */
  	fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
! 	if (strcmp (SWAP_FORMAT, "") == 0) {
  		temp_buffer = strtok (input_buffer, " \n");
  		while (temp_buffer) {
--- 106,115 ----
  	child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  	if (child_stderr == NULL)
! 		printf ("Could not open stderr for %s\n", swap_command);
  
  	sprintf (str, "%s", "");
  	/* read 1st line */
  	fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
! 	if (strcmp (swap_format, "") == 0) {
  		temp_buffer = strtok (input_buffer, " \n");
  		while (temp_buffer) {
***************
*** 125,153 ****
  	}
  
! 	asprintf (&status, "%s", "Swap used:");
! 	while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
! 		sscanf (input_buffer, SWAP_FORMAT, &total, &free);
! 		used = total - free;
! 		total_swap += total;
! 		used_swap += used;
! 		free_swap += free;
! 		if (allswaps) {
! 			percent = 100 * (((double) used) / ((double) total));
! 			if (percent >= crit_percent || free <= crit_size)
! 				result = max_state (STATE_CRITICAL, result);
! 			else if (percent >= warn_percent || free <= warn_size)
! 				result = max_state (STATE_WARNING, result);
! 			if (verbose)
! 				asprintf (&status, "%s [%lu/%lu]", status, used, total);
  		}
  	}
  	percent_used = 100 * ((double) used_swap) / ((double) total_swap);
! 	if (percent_used >= crit_percent || free_swap <= crit_size)
! 		result = max_state (STATE_CRITICAL, result);
! 	else if (percent_used >= warn_percent || free_swap <= warn_size)
! 		result = max_state (STATE_WARNING, result);
! 	asprintf (&status, "%s %2d%% (%lu out of %lu)",
! 						status, percent_used, used_swap, total_swap);
  
  	/* If we get anything on STDERR, at least set warning */
  	while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
--- 124,163 ----
  	}
  
! 	if (!allswaps && sun) {
! 		sscanf (input_buffer, swap_format, &used_swap, &free_swap);
! 		used_swap = used_swap / 1024;
! 		free_swap = free_swap / 1024;
! 		total_swap = used_swap + free_swap;
! 	} else {
! 		while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
! 			sscanf (input_buffer, swap_format, &total, &free);
! 
! 			total = total / conv_factor;
! 			free = free / conv_factor;
! 			if (verbose >= 3)
! 				printf ("total=%d, free=%d\n", total, free);
! 
! 			used = total - free;
! #endif
! 			total_swap += total;
! 			used_swap += used;
! 			free_swap += free;
! 			if (allswaps) {
! 				percent = 100 * (((double) used) / ((double) total));
! 				result = max_state (result, check_swap (percent, free));
! 				if (verbose)
! 					asprintf (&status, "%s [%lu (%d%%)]", status, free, 100 - percent);
! 			}
  		}
  	}
  	percent_used = 100 * ((double) used_swap) / ((double) total_swap);
! 	result = max_state (result, check_swap (percent_used, free_swap));
! 	asprintf (&status, " %d%% free (%lu MB out of %lu MB)%s",
! 						(100 - percent_used), free_swap, total_swap, status);
  
+ #ifdef HAVE_PROC_MEMINFO
+ 	fclose(fp);
+ #endif
+ #ifdef HAVE_SWAP
  	/* If we get anything on STDERR, at least set warning */
  	while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
***************
*** 161,192 ****
  		result = max_state (result, STATE_WARNING);
  #endif
- #endif
- 
- #ifndef SWAP_COMMAND
- #ifndef SWAP_FILE
- #ifndef HAVE_PROC_MEMINFO
- 	return STATE_UNKNOWN;
- #endif
- #endif
- #endif
  
! 	if (result == STATE_OK)
! 		printf ("Swap ok - %s\n", status);
! 	else if (result == STATE_CRITICAL)
! 		printf ("CRITICAL - %s\n", status);
! 	else if (result == STATE_WARNING)
! 		printf ("WARNING - %s\n", status);
! 	else if (result == STATE_UNKNOWN)
! 		printf ("Unable to read output\n");
! 	else {
! 		result = STATE_UNKNOWN;
! 		printf ("UNKNOWN - %s\n", status);
! 	}
! 
! 	return result;
  }
  
  
  
  
  
--- 171,197 ----
  		result = max_state (result, STATE_WARNING);
  #endif
  
! 	terminate (result, "SWAP %s:%s\n", state_text (result), status);
  }
  
  
  
+ 
+ int
+ check_swap (int usp, int free_swap)
+ {
+ 	int result = STATE_UNKNOWN;
+ 	if (usp >= 0 && usp >= (100.0 - crit_percent))
+ 		result = STATE_CRITICAL;
+ 	else if (crit_size >= 0 && free_swap <= crit_size)
+ 		result = STATE_CRITICAL;
+ 	else if (usp >= 0 && usp >= (100.0 - warn_percent))
+ 		result = STATE_WARNING;
+ 	else if (warn_size >= 0 && free_swap <= warn_size)
+ 		result = STATE_WARNING;
+ 	else if (usp >= 0.0)
+ 		result = STATE_OK;
+ 	return result;
+ }
  
  
***************
*** 256,264 ****
  			}
  			cc++;
! 		case 'a':									/* verbose */
  			allswaps = TRUE;
  			break;
  		case 'v':									/* verbose */
! 			verbose = TRUE;
  			break;
  		case 'V':									/* version */
--- 261,269 ----
  			}
  			cc++;
! 		case 'a':									/* all swap */
  			allswaps = TRUE;
  			break;
  		case 'v':									/* verbose */
! 			verbose++;
  			break;
  		case 'V':									/* version */
***************
*** 308,318 ****
  		return ERROR;
  	}
! 	else if (warn_percent > crit_percent) {
  		usage
! 			("Warning percentage should not be less than critical percentage\n");
  	}
  	else if (warn_size < crit_size) {
  		usage
! 			("Warning free space should not be more than critical free space\n");
  	}
  	return OK;
--- 313,323 ----
  		return ERROR;
  	}
! 	else if (warn_percent < crit_percent) {
  		usage
! 			("Warning percentage should be more than critical percentage\n");
  	}
  	else if (warn_size < crit_size) {
  		usage
! 			("Warning free space should be more than critical free space\n");
  	}
  	return OK;
***************
*** 351,364 ****
  		 "   Exit with WARNING status if less than INTEGER bytes of swap space are free\n"
  		 " -w, --warning=PERCENT%%\n"
! 		 "   Exit with WARNING status if more than PERCENT of swap space has been used\n"
  		 " -c, --critical=INTEGER\n"
  		 "   Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n"
  		 " -c, --critical=PERCENT%%\n"
! 		 "   Exit with CRITCAL status if more than PERCENT of swap space has been used\n"
  		 " -a, --allswaps\n"
  		 "    Conduct comparisons for all swap partitions, one by one\n"
  		 " -h, --help\n"
  		 "    Print detailed help screen\n"
! 		 " -V, --version\n" "    Print version information\n\n");
  	support ();
  }
--- 356,375 ----
  		 "   Exit with WARNING status if less than INTEGER bytes of swap space are free\n"
  		 " -w, --warning=PERCENT%%\n"
! 		 "   Exit with WARNING status if less than PERCENT of swap space has been used\n"
  		 " -c, --critical=INTEGER\n"
  		 "   Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n"
  		 " -c, --critical=PERCENT%%\n"
! 		 "   Exit with CRITCAL status if less than PERCENT of swap space has been used\n"
  		 " -a, --allswaps\n"
  		 "    Conduct comparisons for all swap partitions, one by one\n"
  		 " -h, --help\n"
  		 "    Print detailed help screen\n"
! 		 " -V, --version\n" "    Print version information\n"
! #ifdef sun
! 		 "\nOn Solaris, if -a specified, uses swap -l, otherwise uses swap -s.\n"
! 		 "Will be discrepencies because swap -s counts allocated swap and includes real memory\n"
! #endif
! 		 "\n"
! 		 );
  	support ();
  }





More information about the Commits mailing list