check_swap.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /******************************************************************************
  2. *
  3. * Program: Swap space plugin for Nagios
  4. * License: GPL
  5. *
  6. * License Information:
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
  23. *
  24. * $Id$
  25. *
  26. *****************************************************************************/
  27. #include "common.h"
  28. #include "popen.h"
  29. #include "utils.h"
  30. const char *progname = "check_swap";
  31. #define REVISION "$Revision$"
  32. #define COPYRIGHT "2000-2002"
  33. #define AUTHOR "Karl DeBisschop"
  34. #define EMAIL "kdebisschop@users.sourceforge.net"
  35. #define SUMMARY "Check swap space on local server.\n"
  36. int process_arguments (int argc, char **argv);
  37. int validate_arguments (void);
  38. void print_usage (void);
  39. void print_help (void);
  40. int warn_percent = 200;
  41. int crit_percent = 200;
  42. long unsigned int warn_size = 0;
  43. long unsigned int crit_size = 0;
  44. int verbose;
  45. int allswaps;
  46. int
  47. main (int argc, char **argv)
  48. {
  49. int percent_used, percent;
  50. long unsigned int total_swap = 0, used_swap = 0, free_swap = 0;
  51. long unsigned int total, used, free;
  52. int result = STATE_OK;
  53. char input_buffer[MAX_INPUT_BUFFER];
  54. #ifdef HAVE_SWAP
  55. char *temp_buffer;
  56. #endif
  57. #ifdef HAVE_PROC_MEMINFO
  58. FILE *fp;
  59. #endif
  60. char str[32];
  61. char *status = "";
  62. if (process_arguments (argc, argv) != OK)
  63. usage ("Invalid command arguments supplied\n");
  64. #ifdef HAVE_PROC_MEMINFO
  65. fp = fopen (PROC_MEMINFO, "r");
  66. asprintf (&status, "%s", "Swap used:");
  67. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  68. if (sscanf (input_buffer, " %s %lu %lu %lu", str, &total, &used, &free) == 4 &&
  69. strstr (str, "Swap")) {
  70. total_swap += total;
  71. used_swap += used;
  72. free_swap += free;
  73. if (allswaps) {
  74. percent = 100 * (((double) used) / ((double) total));
  75. if (percent >= crit_percent || free <= crit_size)
  76. result = max_state (STATE_CRITICAL, result);
  77. else if (percent >= warn_percent || free <= warn_size)
  78. result = max_state (STATE_WARNING, result);
  79. if (verbose)
  80. asprintf (&status, "%s [%lu/%lu]", status, used, total);
  81. }
  82. }
  83. }
  84. percent_used = 100 * (((double) used_swap) / ((double) total_swap));
  85. if (percent_used >= crit_percent || free_swap <= crit_size)
  86. result = max_state (STATE_CRITICAL, result);
  87. else if (percent_used >= warn_percent || free_swap <= warn_size)
  88. result = max_state (STATE_WARNING, result);
  89. asprintf (&status, "%s %2d%% (%lu out of %lu)", status, percent_used,
  90. used_swap, total_swap);
  91. fclose (fp);
  92. #else
  93. #ifdef HAVE_SWAP
  94. child_process = spopen (SWAP_COMMAND);
  95. if (child_process == NULL) {
  96. printf ("Could not open pipe: %s\n", SWAP_COMMAND);
  97. return STATE_UNKNOWN;
  98. }
  99. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  100. if (child_stderr == NULL)
  101. printf ("Could not open stderr for %s\n", SWAP_COMMAND);
  102. sprintf (str, "%s", "");
  103. /* read 1st line */
  104. fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
  105. if (strcmp (SWAP_FORMAT, "") == 0) {
  106. temp_buffer = strtok (input_buffer, " \n");
  107. while (temp_buffer) {
  108. if (strstr (temp_buffer, "blocks"))
  109. sprintf (str, "%s %s", str, "%f");
  110. else if (strstr (temp_buffer, "free"))
  111. sprintf (str, "%s %s", str, "%f");
  112. else
  113. sprintf (str, "%s %s", str, "%*s");
  114. temp_buffer = strtok (NULL, " \n");
  115. }
  116. }
  117. asprintf (&status, "%s", "Swap used:");
  118. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  119. sscanf (input_buffer, SWAP_FORMAT, &total, &free);
  120. used = total - free;
  121. total_swap += total;
  122. used_swap += used;
  123. free_swap += free;
  124. if (allswaps) {
  125. percent = 100 * (((double) used) / ((double) total));
  126. if (percent >= crit_percent || free <= crit_size)
  127. result = max_state (STATE_CRITICAL, result);
  128. else if (percent >= warn_percent || free <= warn_size)
  129. result = max_state (STATE_WARNING, result);
  130. if (verbose)
  131. asprintf (&status, "%s [%lu/%lu]", status, used, total);
  132. }
  133. }
  134. percent_used = 100 * ((double) used_swap) / ((double) total_swap);
  135. if (percent_used >= crit_percent || free_swap <= crit_size)
  136. result = max_state (STATE_CRITICAL, result);
  137. else if (percent_used >= warn_percent || free_swap <= warn_size)
  138. result = max_state (STATE_WARNING, result);
  139. asprintf (&status, "%s %2d%% (%lu out of %lu)",
  140. status, percent_used, used_swap, total_swap);
  141. /* If we get anything on STDERR, at least set warning */
  142. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  143. result = max_state (result, STATE_WARNING);
  144. /* close stderr */
  145. (void) fclose (child_stderr);
  146. /* close the pipe */
  147. if (spclose (child_process))
  148. result = max_state (result, STATE_WARNING);
  149. #endif
  150. #endif
  151. #ifndef SWAP_COMMAND
  152. #ifndef SWAP_FILE
  153. #ifndef HAVE_PROC_MEMINFO
  154. return STATE_UNKNOWN;
  155. #endif
  156. #endif
  157. #endif
  158. if (result == STATE_OK)
  159. printf ("Swap ok - %s\n", status);
  160. else if (result == STATE_CRITICAL)
  161. printf ("CRITICAL - %s\n", status);
  162. else if (result == STATE_WARNING)
  163. printf ("WARNING - %s\n", status);
  164. else if (result == STATE_UNKNOWN)
  165. printf ("Unable to read output\n");
  166. else {
  167. result = STATE_UNKNOWN;
  168. printf ("UNKNOWN - %s\n", status);
  169. }
  170. return result;
  171. }
  172. /* process command-line arguments */
  173. int
  174. process_arguments (int argc, char **argv)
  175. {
  176. int c = 0; /* option character */
  177. int wc = 0; /* warning counter */
  178. int cc = 0; /* critical counter */
  179. int option_index = 0;
  180. static struct option long_options[] = {
  181. {"warning", required_argument, 0, 'w'},
  182. {"critical", required_argument, 0, 'c'},
  183. {"allswaps", no_argument, 0, 'a'},
  184. {"verbose", no_argument, 0, 'v'},
  185. {"version", no_argument, 0, 'V'},
  186. {"help", no_argument, 0, 'h'},
  187. {0, 0, 0, 0}
  188. };
  189. if (argc < 2)
  190. return ERROR;
  191. while (1) {
  192. c = getopt_long (argc, argv, "+?Vvhac:w:", long_options, &option_index);
  193. if (c == -1 || c == EOF)
  194. break;
  195. switch (c) {
  196. case 'w': /* warning time threshold */
  197. if (is_intnonneg (optarg)) {
  198. warn_size = atoi (optarg);
  199. break;
  200. }
  201. else if (strstr (optarg, ",") &&
  202. strstr (optarg, "%") &&
  203. sscanf (optarg, "%lu,%d%%", &warn_size, &warn_percent) == 2) {
  204. break;
  205. }
  206. else if (strstr (optarg, "%") &&
  207. sscanf (optarg, "%d%%", &warn_percent) == 1) {
  208. break;
  209. }
  210. else {
  211. usage ("Warning threshold must be integer or percentage!\n");
  212. }
  213. wc++;
  214. case 'c': /* critical time threshold */
  215. if (is_intnonneg (optarg)) {
  216. crit_size = atoi (optarg);
  217. break;
  218. }
  219. else if (strstr (optarg, ",") &&
  220. strstr (optarg, "%") &&
  221. sscanf (optarg, "%lu,%d%%", &crit_size, &crit_percent) == 2) {
  222. break;
  223. }
  224. else if (strstr (optarg, "%") &&
  225. sscanf (optarg, "%d%%", &crit_percent) == 1) {
  226. break;
  227. }
  228. else {
  229. usage ("Critical threshold must be integer or percentage!\n");
  230. }
  231. cc++;
  232. case 'a': /* verbose */
  233. allswaps = TRUE;
  234. break;
  235. case 'v': /* verbose */
  236. verbose = TRUE;
  237. break;
  238. case 'V': /* version */
  239. print_revision (progname, "$Revision$");
  240. exit (STATE_OK);
  241. case 'h': /* help */
  242. print_help ();
  243. exit (STATE_OK);
  244. case '?': /* help */
  245. usage ("Invalid argument\n");
  246. }
  247. }
  248. c = optind;
  249. if (c == argc)
  250. return validate_arguments ();
  251. if (warn_percent > 100 && is_intnonneg (argv[c]))
  252. warn_percent = atoi (argv[c++]);
  253. if (c == argc)
  254. return validate_arguments ();
  255. if (crit_percent > 100 && is_intnonneg (argv[c]))
  256. crit_percent = atoi (argv[c++]);
  257. if (c == argc)
  258. return validate_arguments ();
  259. if (warn_size < 0 && is_intnonneg (argv[c]))
  260. warn_size = atoi (argv[c++]);
  261. if (c == argc)
  262. return validate_arguments ();
  263. if (crit_size < 0 && is_intnonneg (argv[c]))
  264. crit_size = atoi (argv[c++]);
  265. return validate_arguments ();
  266. }
  267. int
  268. validate_arguments (void)
  269. {
  270. if (warn_percent > 100 && crit_percent > 100 && warn_size < 0
  271. && crit_size < 0) {
  272. return ERROR;
  273. }
  274. else if (warn_percent > crit_percent) {
  275. usage
  276. ("Warning percentage should not be less than critical percentage\n");
  277. }
  278. else if (warn_size < crit_size) {
  279. usage
  280. ("Warning free space should not be more than critical free space\n");
  281. }
  282. return OK;
  283. }
  284. void
  285. print_usage (void)
  286. {
  287. printf
  288. ("Usage:\n"
  289. " %s [-a] -w <used_percentage>%% -c <used_percentage>%%\n"
  290. " %s [-a] -w <bytes_free> -c <bytes_free>\n"
  291. " %s (-h | --help) for detailed help\n"
  292. " %s (-V | --version) for version information\n",
  293. progname, progname, progname, progname);
  294. }
  295. void
  296. print_help (void)
  297. {
  298. print_revision (progname, REVISION);
  299. printf
  300. ("Copyright (c) %s %s <%s>\n\n%s\n", COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
  301. print_usage ();
  302. printf
  303. ("\nOptions:\n"
  304. " -w, --warning=INTEGER\n"
  305. " Exit with WARNING status if less than INTEGER bytes of swap space are free\n"
  306. " -w, --warning=PERCENT%%\n"
  307. " Exit with WARNING status if more than PERCENT of swap space has been used\n"
  308. " -c, --critical=INTEGER\n"
  309. " Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n"
  310. " -c, --critical=PERCENT%%\n"
  311. " Exit with CRITCAL status if more than PERCENT of swap space has been used\n"
  312. " -a, --allswaps\n"
  313. " Conduct comparisons for all swap partitions, one by one\n"
  314. " -h, --help\n"
  315. " Print detailed help screen\n"
  316. " -V, --version\n" " Print version information\n\n");
  317. support ();
  318. }