X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=plugins%2Fcheck_swap.c;h=ad5eb25ff972912a00d827de3267183b8b9c7dd5;hb=11b35b92e3195d230bef359f6a0679ae4414716b;hp=b213c964fb805d7ad52ce5eeaeaa8ab3de88b459;hpb=0c3386274ef5002dffc20337ef02407f24d7400c;p=nagiosplug.git diff --git a/plugins/check_swap.c b/plugins/check_swap.c index b213c96..ad5eb25 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -43,16 +43,17 @@ void print_help (void); int warn_percent = 200; int crit_percent = 200; -int warn_size = -1; -int crit_size = -1; +long unsigned int warn_size = 0; +long unsigned int crit_size = 0; int verbose; int allswaps; int main (int argc, char **argv) { - int total_swap = 0, used_swap = 0, free_swap = 0, percent_used; - int total, used, free, percent; + int percent_used, percent; + long unsigned int total_swap = 0, used_swap = 0, free_swap = 0; + long unsigned int total, used, free; int result = STATE_OK; char input_buffer[MAX_INPUT_BUFFER]; #ifdef HAVE_SWAP @@ -71,28 +72,28 @@ main (int argc, char **argv) fp = fopen (PROC_MEMINFO, "r"); asprintf (&status, "%s", "Swap used:"); while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) { - if (sscanf (input_buffer, " %s %d %d %d", str, &total, &used, &free) == 4 && + 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 * (((float) used) / ((float) total)); + 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 [%d/%d]", status, used, total); + asprintf (&status, "%s [%lu/%lu]", status, used, total); } } } - percent_used = 100 * (((float) used_swap) / ((float) total_swap)); + 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%% (%d out of %d)", status, percent_used, + asprintf (&status, "%s %2d%% (%lu out of %lu)", status, percent_used, used_swap, total_swap); fclose (fp); #else @@ -131,22 +132,22 @@ main (int argc, char **argv) used_swap += used; free_swap += free; if (allswaps) { - percent = 100 * (((float) used) / ((float) total)); + 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 [%d/%d]", status, used, total); + asprintf (&status, "%s [%lu/%lu]", status, used, total); } } - percent_used = 100 * ((float) used_swap) / ((float) total_swap); - asprintf (&status, "%s %2d%% (%d out of %d)", - status, percent_used, used_swap, total_swap); + 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)) @@ -197,28 +198,22 @@ process_arguments (int argc, char **argv) int wc = 0; /* warning counter */ int cc = 0; /* critical counter */ -#ifdef HAVE_GETOPT_H int option_index = 0; static struct option long_options[] = { {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, - {"all", no_argument, 0, 'a'}, + {"allswaps", no_argument, 0, 'a'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; -#endif if (argc < 2) return ERROR; while (1) { -#ifdef HAVE_GETOPT_H c = getopt_long (argc, argv, "+?Vvhac:w:", long_options, &option_index); -#else - c = getopt (argc, argv, "+?Vvhac:w:"); -#endif if (c == -1 || c == EOF) break; @@ -231,7 +226,7 @@ process_arguments (int argc, char **argv) } else if (strstr (optarg, ",") && strstr (optarg, "%") && - sscanf (optarg, "%d,%d%%", &warn_size, &warn_percent) == 2) { + sscanf (optarg, "%lu,%d%%", &warn_size, &warn_percent) == 2) { break; } else if (strstr (optarg, "%") && @@ -249,7 +244,7 @@ process_arguments (int argc, char **argv) } else if (strstr (optarg, ",") && strstr (optarg, "%") && - sscanf (optarg, "%d,%d%%", &crit_size, &crit_percent) == 2) { + sscanf (optarg, "%lu,%d%%", &crit_size, &crit_percent) == 2) { break; } else if (strstr (optarg, "%") &&