X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=plugins%2Fcheck_swap.c;h=aeeb9ba0a8872044e55dc1064a91f00c38cbac0e;hb=9d878eab82bede19e80b4aa9ce1409da3ba6d658;hp=6a448e106b07a93a48b65b61d5b78a4dde87fb2d;hpb=2ad398d2e04717ce3a6a8fb869e855c42581a30a;p=nagiosplug.git diff --git a/plugins/check_swap.c b/plugins/check_swap.c index 6a448e1..aeeb9ba 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -34,7 +34,7 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net"; #include "popen.h" #include "utils.h" -int check_swap (int usp, long unsigned int free_swap); +int check_swap (int usp, float free_swap); int process_arguments (int argc, char **argv); int validate_arguments (void); void print_usage (void); @@ -42,8 +42,8 @@ void print_help (void); int warn_percent = 0; int crit_percent = 0; -unsigned long long warn_size = 0; -unsigned long long crit_size = 0; +double warn_size = 0; +double crit_size = 0; int verbose; int allswaps; @@ -51,15 +51,14 @@ int main (int argc, char **argv) { int percent_used, percent; - unsigned long long total_swap = 0, used_swap = 0, free_swap = 0; - unsigned long long dsktotal = 0, dskused = 0, dskfree = 0, tmp = 0; + float total_swap = 0, used_swap = 0, free_swap = 0; + float dsktotal = 0, dskused = 0, dskfree = 0, tmp = 0; int result = STATE_UNKNOWN; char input_buffer[MAX_INPUT_BUFFER]; - char *perf; - int conv_factor = SWAP_CONVERSION; #ifdef HAVE_PROC_MEMINFO FILE *fp; #else + int conv_factor = SWAP_CONVERSION; # ifdef HAVE_SWAP char *temp_buffer; char *swap_command; @@ -79,23 +78,21 @@ main (int argc, char **argv) # endif #endif char str[32]; - char *status, *tmp_status; + char *status; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); status = strdup (""); - tmp_status = strdup (""); - perf = strdup (""); - if (process_arguments (argc, argv) != TRUE) + if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments")); #ifdef HAVE_PROC_MEMINFO fp = fopen (PROC_MEMINFO, "r"); while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) { - if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %llu %llu %llu", &dsktotal, &dskused, &dskfree) == 3) { + if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %f %f %f", &dsktotal, &dskused, &dskfree) == 3) { dsktotal = dsktotal / 1048576; dskused = dskused / 1048576; dskfree = dskfree / 1048576; @@ -109,10 +106,10 @@ main (int argc, char **argv) percent = 100 * (((double) dskused) / ((double) dsktotal)); result = max_state (result, check_swap (percent, dskfree)); if (verbose) - asprintf (&status, "%s [%llu (%d%%)]", status, dskfree, 100 - percent); + asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree, 100 - percent); } } - else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFre]%*[:] %llu %*[k]%*[B]", str, &tmp)) { + else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFre]%*[:] %f %*[k]%*[B]", str, &tmp)) { if (strcmp ("Total", str) == 0) { dsktotal = tmp / 1024; } @@ -136,7 +133,7 @@ main (int argc, char **argv) # ifdef _AIX if (!allswaps) { asprintf(&swap_command, "%s", "/usr/sbin/lsps -s"); - asprintf(&swap_format, "%s", "%d%*s %d"); + asprintf(&swap_format, "%s", "%f%*s %f"); conv_factor = 1; } # endif @@ -180,7 +177,7 @@ main (int argc, char **argv) free_swap = total_swap * (100 - used_swap) /100; used_swap = total_swap - free_swap; if (verbose >= 3) - printf (_("total=%d, used=%d, free=%d\n"), total_swap, used_swap, free_swap); + printf (_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap, used_swap, free_swap); } else { # endif while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { @@ -194,7 +191,7 @@ main (int argc, char **argv) dskfree = dskfree / conv_factor; # endif if (verbose >= 3) - printf (_("total=%d, free=%d\n"), dsktotal, dskfree); + printf (_("total=%.0f, free=%.0f\n"), dsktotal, dskfree); dskused = dsktotal - dskfree; total_swap += dsktotal; @@ -204,7 +201,7 @@ main (int argc, char **argv) percent = 100 * (((double) dskused) / ((double) dsktotal)); result = max_state (result, check_swap (percent, dskfree)); if (verbose) - asprintf (&status, "%s [%llu (%d%%)]", status, dskfree, 100 - percent); + asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree, 100 - percent); } } # ifdef _AIX @@ -239,20 +236,23 @@ main (int argc, char **argv) /* and now, tally 'em up */ swapctl_res=swapctl(SC_LIST, tbl); if(swapctl_res < 0){ - perror("swapctl failed: "); + perror(_("swapctl failed: ")); result = STATE_WARNING; } for(i=0;iswt_ent[i].ste_pages / SWAP_CONVERSION; - dskfree = tbl->swt_ent[i].ste_free / SWAP_CONVERSION; + dsktotal = (float) tbl->swt_ent[i].ste_pages / SWAP_CONVERSION; + dskfree = (float) tbl->swt_ent[i].ste_free / SWAP_CONVERSION; dskused = ( dsktotal - dskfree ); + if (verbose >= 3) + printf ("dsktotal=%.0f dskfree=%.0f dskused=%.0f\n", dsktotal, dskfree, dskused); + if(allswaps && dsktotal > 0){ percent = 100 * (((double) dskused) / ((double) dsktotal)); result = max_state (result, check_swap (percent, dskfree)); if (verbose) { - asprintf (&status, "%s [%d (%d%%)]", status, (int)dskfree, 100 - percent); + asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree, 100 - percent); } } @@ -278,20 +278,20 @@ main (int argc, char **argv) /* and now, tally 'em up */ swapctl_res=swapctl(SWAP_STATS, ent, nswaps); if(swapctl_res < 0){ - perror("swapctl failed: "); + perror(_("swapctl failed: ")); result = STATE_WARNING; } for(i=0;ise_nblks / conv_factor; - dskused = ent->se_inuse / conv_factor; + dsktotal = (float) ent->se_nblks / conv_factor; + dskused = (float) ent->se_inuse / conv_factor; dskfree = ( dsktotal - dskused ); if(allswaps && dsktotal > 0){ percent = 100 * (((double) dskused) / ((double) dsktotal)); result = max_state (result, check_swap (percent, dskfree)); if (verbose) { - asprintf (&status, "%s [%d (%d%%)]", status, (int)dskfree, 100 - percent); + asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree, 100 - percent); } } @@ -308,26 +308,31 @@ main (int argc, char **argv) # endif /* HAVE_SWAP */ #endif /* HAVE_PROC_MEMINFO */ - percent_used = 100 * ((double) used_swap) / ((double) total_swap); + /* if total_swap == 0, let's not divide by 0 */ + if(total_swap) { + percent_used = 100 * ((double) used_swap) / ((double) total_swap); + } else { + percent_used = 0; + } + result = max_state (result, check_swap (percent_used, free_swap)); - /* broken into two steps because of funkiness with builtin asprintf */ - asprintf (&tmp_status, _(" %d%% free (%llu MB out of %llu MB)"), - (100 - percent_used), free_swap, total_swap); - asprintf (&status, "%s%s", tmp_status, status); - - asprintf (&perf, "%s", perfdata ("swap", (long) free_swap, "MB", - TRUE, (long) max (warn_size/1024, warn_percent/100.0*total_swap), - TRUE, (long) max (crit_size/1024, crit_percent/100.0*total_swap), - TRUE, 0, - TRUE, (long) total_swap)); - printf ("SWAP %s:%s |%s\n", state_text (result), status, perf); + printf (_("SWAP %s - %d%% free (%.0f MB out of %.0f MB) %s|"), + state_text (result), + (100 - percent_used), free_swap, total_swap, status); + + puts (perfdata ("swap", (long) free_swap, "MB", + TRUE, (long) max (warn_size/1024, warn_percent/100.0*total_swap), + TRUE, (long) max (crit_size/1024, crit_percent/100.0*total_swap), + TRUE, 0, + TRUE, (long) total_swap)); + return result; } int -check_swap (int usp, long unsigned int free_swap) +check_swap (int usp, float free_swap) { int result = STATE_UNKNOWN; free_swap = free_swap * 1024; /* Convert back to bytes as warn and crit specified in bytes */ @@ -375,12 +380,13 @@ process_arguments (int argc, char **argv) switch (c) { case 'w': /* warning size threshold */ if (is_intnonneg (optarg)) { - warn_size = atoi (optarg); + warn_size = (double) atoi (optarg); break; } else if (strstr (optarg, ",") && strstr (optarg, "%") && - sscanf (optarg, "%llu,%d%%", &warn_size, &warn_percent) == 2) { + sscanf (optarg, "%g,%d%%", &warn_size, &warn_percent) == 2) { + warn_size = floor(warn_size); break; } else if (strstr (optarg, "%") && @@ -388,16 +394,17 @@ process_arguments (int argc, char **argv) break; } else { - usage (_("Warning threshold must be integer or percentage!\n")); + usage4 (_("Warning threshold must be integer or percentage!")); } case 'c': /* critical size threshold */ if (is_intnonneg (optarg)) { - crit_size = atoi (optarg); + crit_size = (double) atoi (optarg); break; } else if (strstr (optarg, ",") && strstr (optarg, "%") && - sscanf (optarg, "%llu,%d%%", &crit_size, &crit_percent) == 2) { + sscanf (optarg, "%g,%d%%", &crit_size, &crit_percent) == 2) { + crit_size = floor(crit_size); break; } else if (strstr (optarg, "%") && @@ -405,7 +412,7 @@ process_arguments (int argc, char **argv) break; } else { - usage (_("Critical threshold must be integer or percentage!\n")); + usage4 (_("Critical threshold must be integer or percentage!")); } case 'a': /* all swap */ allswaps = TRUE; @@ -420,9 +427,7 @@ process_arguments (int argc, char **argv) print_help (); exit (STATE_OK); case '?': /* error */ - printf (_("%s: Unknown argument: %s\n\n"), progname, optarg); - print_usage (); - exit (STATE_UNKNOWN); + usage2 (_("Unknown argument"), optarg); } } @@ -440,12 +445,12 @@ process_arguments (int argc, char **argv) if (c == argc) return validate_arguments (); if (warn_size == 0 && is_intnonneg (argv[c])) - warn_size = atoi (argv[c++]); + warn_size = (double) atoi (argv[c++]); if (c == argc) return validate_arguments (); if (crit_size == 0 && is_intnonneg (argv[c])) - crit_size = atoi (argv[c++]); + crit_size = (double) atoi (argv[c++]); return validate_arguments (); } @@ -460,12 +465,12 @@ validate_arguments (void) return ERROR; } else if (warn_percent < crit_percent) { - usage - (_("Warning percentage should be more than critical percentage\n")); + usage4 + (_("Warning percentage should be more than critical percentage")); } else if (warn_size < crit_size) { - usage - (_("Warning free space should be more than critical free space\n")); + usage4 + (_("Warning free space should be more than critical free space")); } return OK; } @@ -479,7 +484,7 @@ print_help (void) printf (_(COPYRIGHT), copyright, email); - printf (_("Check swap space on local server.\n\n")); + printf (_("Check swap space on local machine.\n\n")); print_usage (); @@ -499,10 +504,6 @@ print_help (void) -v, --verbose\n\ Verbose output. Up to 3 levels\n")); - printf (_("\n\ -On Solaris, if -a specified, uses swap -l, otherwise uses swap -s.\n\ -Will be discrepencies because swap -s counts allocated swap and includes\n\ -real memory\n")); printf (_("\n\ On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.\n")); @@ -514,9 +515,7 @@ On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.\n")); void print_usage (void) { - printf ("Usage:\n\ - %s [-av] -w %% -c %%\n\ - %s [-av] -w -c \n", progname, progname); - - printf (UT_HLP_VRS, progname, progname); + printf ("\ +Usage: %s [-av] -w %% -c %%\n\ + %s [-av] -w -c \n", progname, progname); }