Index: check_swap.c =================================================================== RCS file: /cvsroot/nagiosplug/nagiosplug/plugins/check_swap.c,v retrieving revision 1.9 diff -u -r1.9 check_swap.c --- check_swap.c 18 Feb 2003 03:46:15 -0000 1.9 +++ check_swap.c 27 Feb 2005 23:58:43 -0000 @@ -36,6 +36,18 @@ #define EMAIL "kdebisschop@users.sourceforge.net" #define SUMMARY "Check swap space on local server.\n" +#ifdef HAVE_LONGLONG +typedef long long unsigned int swapsize_t; +#define SWAPSIZEFORMAT "%llu" +#define SWAPSIZEFORMAT_OUT SWAPSIZEFORMAT +#define atos atoll +#else +typedef float swapsize_t; +#define SWAPSIZEFORMAT "%f" +#define SWAPSIZEFORMAT_OUT "%.0f" +#define atos (float)atof +#endif + int process_arguments (int argc, char **argv); int validate_arguments (void); void print_usage (void); @@ -43,8 +55,8 @@ int warn_percent = 200; int crit_percent = 200; -long unsigned int warn_size = 0; -long unsigned int crit_size = 0; +swapsize_t warn_size = 0; +swapsize_t crit_size = 0; int verbose; int allswaps; @@ -52,8 +64,8 @@ main (int argc, char **argv) { int percent_used, percent; - long unsigned int total_swap = 0, used_swap = 0, free_swap = 0; - long unsigned int total, used, free; + swapsize_t total_swap = 0, used_swap = 0, free_swap = 0; + swapsize_t total, used, free; int result = STATE_OK; char input_buffer[MAX_INPUT_BUFFER]; #ifdef HAVE_SWAP @@ -61,6 +73,7 @@ #endif #ifdef HAVE_PROC_MEMINFO FILE *fp; + char str_format[32]; #endif char str[32]; char *status = ""; @@ -70,9 +83,10 @@ #ifdef HAVE_PROC_MEMINFO fp = fopen (PROC_MEMINFO, "r"); + sprintf (str_format, " %%s %s %s %s", SWAPSIZEFORMAT, SWAPSIZEFORMAT, SWAPSIZEFORMAT); 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 && + if (sscanf (input_buffer, str_format, str, &total, &used, &free) == 4 && strstr (str, "Swap")) { total_swap += total; used_swap += used; @@ -83,8 +97,10 @@ 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); + if (verbose) { + sprintf (str_format, "%%s [%s/%s]", SWAPSIZEFORMAT_OUT, SWAPSIZEFORMAT_OUT); + asprintf (&status, str_format, status, used, total); + } } } } @@ -93,7 +109,8 @@ 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, + sprintf (str_format, "%%s %%2d%%%% (%s out of %s)", SWAPSIZEFORMAT_OUT, SWAPSIZEFORMAT_OUT); + asprintf (&status, str_format, status, percent_used, used_swap, total_swap); fclose (fp); #else @@ -137,8 +154,9 @@ 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); + if (verbose) { + sprintf (str_format, "%%s [%s/%s]", SWAPSIZEFORMAT_OUT, SWAPSIZEFORMAT_OUT); + asprintf (&status, str_format, status, used, total); } } percent_used = 100 * ((double) used_swap) / ((double) total_swap); @@ -146,8 +164,9 @@ 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); + sprintf (str_format, "%%s %%2d%%%% (%s out of %s)", SWAPSIZEFORMAT_OUT, SWAPSIZEFORMAT_OUT) + asprintf (&status, str_format, + 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)) @@ -198,6 +217,8 @@ int wc = 0; /* warning counter */ int cc = 0; /* critical counter */ + char str_format[32]; + #ifdef HAVE_GETOPT_H int option_index = 0; static struct option long_options[] = { @@ -224,15 +245,16 @@ if (c == -1 || c == EOF) break; + sprintf (str_format, "%s,%%d%%%%", SWAPSIZEFORMAT); switch (c) { case 'w': /* warning time threshold */ if (is_intnonneg (optarg)) { - warn_size = atoi (optarg); + warn_size = atos (optarg); break; } else if (strstr (optarg, ",") && strstr (optarg, "%") && - sscanf (optarg, "%lu,%d%%", &warn_size, &warn_percent) == 2) { + sscanf (optarg, str_format, &warn_size, &warn_percent) == 2) { break; } else if (strstr (optarg, "%") && @@ -245,12 +267,12 @@ wc++; case 'c': /* critical time threshold */ if (is_intnonneg (optarg)) { - crit_size = atoi (optarg); + crit_size = atos (optarg); break; } else if (strstr (optarg, ",") && strstr (optarg, "%") && - sscanf (optarg, "%lu,%d%%", &crit_size, &crit_percent) == 2) { + sscanf (optarg, str_format, &crit_size, &crit_percent) == 2) { break; } else if (strstr (optarg, "%") && @@ -292,12 +314,12 @@ if (c == argc) return validate_arguments (); if (warn_size < 0 && is_intnonneg (argv[c])) - warn_size = atoi (argv[c++]); + warn_size = atos (argv[c++]); if (c == argc) return validate_arguments (); if (crit_size < 0 && is_intnonneg (argv[c])) - crit_size = atoi (argv[c++]); + crit_size = atos (argv[c++]); return validate_arguments (); }