check_swap.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. int warn_size = -1;
  43. int crit_size = -1;
  44. int verbose;
  45. int allswaps;
  46. int
  47. main (int argc, char **argv)
  48. {
  49. int total_swap = 0, used_swap = 0, free_swap = 0, percent_used;
  50. int total, used, free, percent;
  51. int result = STATE_OK;
  52. char input_buffer[MAX_INPUT_BUFFER];
  53. #ifdef HAVE_SWAP
  54. char *temp_buffer;
  55. #endif
  56. #ifdef HAVE_PROC_MEMINFO
  57. FILE *fp;
  58. #endif
  59. char str[32];
  60. char *status = "";
  61. if (process_arguments (argc, argv) != OK)
  62. usage ("Invalid command arguments supplied\n");
  63. #ifdef HAVE_PROC_MEMINFO
  64. fp = fopen (PROC_MEMINFO, "r");
  65. asprintf (&status, "%s", "Swap used:");
  66. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  67. if (sscanf (input_buffer, " %s %d %d %d", str, &total, &used, &free) == 4 &&
  68. strstr (str, "Swap")) {
  69. total_swap += total;
  70. used_swap += used;
  71. free_swap += free;
  72. if (allswaps) {
  73. percent = 100 * (((float) used) / ((float) total));
  74. if (percent >= crit_percent || free <= crit_size)
  75. result = max_state (STATE_CRITICAL, result);
  76. else if (percent >= warn_percent || free <= warn_size)
  77. result = max_state (STATE_WARNING, result);
  78. if (verbose)
  79. asprintf (&status, "%s [%d/%d]", status, used, total);
  80. }
  81. }
  82. }
  83. percent_used = 100 * (((float) used_swap) / ((float) total_swap));
  84. if (percent_used >= crit_percent || free_swap <= crit_size)
  85. result = max_state (STATE_CRITICAL, result);
  86. else if (percent_used >= warn_percent || free_swap <= warn_size)
  87. result = max_state (STATE_WARNING, result);
  88. asprintf (&status, "%s %2d%% (%d out of %d)", status, percent_used,
  89. used_swap, total_swap);
  90. fclose (fp);
  91. #else
  92. #ifdef HAVE_SWAP
  93. child_process = spopen (SWAP_COMMAND);
  94. if (child_process == NULL) {
  95. printf ("Could not open pipe: %s\n", SWAP_COMMAND);
  96. return STATE_UNKNOWN;
  97. }
  98. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  99. if (child_stderr == NULL)
  100. printf ("Could not open stderr for %s\n", SWAP_COMMAND);
  101. sprintf (str, "%s", "");
  102. /* read 1st line */
  103. fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
  104. if (strcmp (SWAP_FORMAT, "") == 0) {
  105. temp_buffer = strtok (input_buffer, " \n");
  106. while (temp_buffer) {
  107. if (strstr (temp_buffer, "blocks"))
  108. sprintf (str, "%s %s", str, "%f");
  109. else if (strstr (temp_buffer, "free"))
  110. sprintf (str, "%s %s", str, "%f");
  111. else
  112. sprintf (str, "%s %s", str, "%*s");
  113. temp_buffer = strtok (NULL, " \n");
  114. }
  115. }
  116. asprintf (&status, "%s", "Swap used:");
  117. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  118. sscanf (input_buffer, SWAP_FORMAT, &total, &free);
  119. used = total - free;
  120. total_swap += total;
  121. used_swap += used;
  122. free_swap += free;
  123. if (allswaps) {
  124. percent = 100 * (((float) used) / ((float) total));
  125. if (percent >= crit_percent || free <= crit_size)
  126. result = max_state (STATE_CRITICAL, result);
  127. else if (percent >= warn_percent || free <= warn_size)
  128. result = max_state (STATE_WARNING, result);
  129. if (verbose)
  130. asprintf (&status, "%s [%d/%d]", status, used, total);
  131. }
  132. }
  133. percent_used = 100 * ((float) used_swap) / ((float) total_swap);
  134. asprintf (&status, "%s %2d%% (%d out of %d)",
  135. status, percent_used, used_swap, total_swap);
  136. if (percent_used >= crit_percent || free_swap <= crit_size)
  137. result = max_state (STATE_CRITICAL, result);
  138. else if (percent_used >= warn_percent || free_swap <= warn_size)
  139. result = max_state (STATE_WARNING, result);
  140. /* If we get anything on STDERR, at least set warning */
  141. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  142. result = max_state (result, STATE_WARNING);
  143. /* close stderr */
  144. (void) fclose (child_stderr);
  145. /* close the pipe */
  146. if (spclose (child_process))
  147. result = max_state (result, STATE_WARNING);
  148. #endif
  149. #endif
  150. #ifndef SWAP_COMMAND
  151. #ifndef SWAP_FILE
  152. #ifndef HAVE_PROC_MEMINFO
  153. return STATE_UNKNOWN;
  154. #endif
  155. #endif
  156. #endif
  157. if (result == STATE_OK)
  158. printf ("Swap ok - %s\n", status);
  159. else if (result == STATE_CRITICAL)
  160. printf ("CRITICAL - %s\n", status);
  161. else if (result == STATE_WARNING)
  162. printf ("WARNING - %s\n", status);
  163. else if (result == STATE_UNKNOWN)
  164. printf ("Unable to read output\n");
  165. else {
  166. result = STATE_UNKNOWN;
  167. printf ("UNKNOWN - %s\n", status);
  168. }
  169. return result;
  170. }
  171. /* process command-line arguments */
  172. int
  173. process_arguments (int argc, char **argv)
  174. {
  175. int c = 0; /* option character */
  176. int wc = 0; /* warning counter */
  177. int cc = 0; /* critical counter */
  178. #ifdef HAVE_GETOPT_H
  179. int option_index = 0;
  180. static struct option long_options[] = {
  181. {"warning", required_argument, 0, 'w'},
  182. {"critical", required_argument, 0, 'c'},
  183. {"all", 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. #endif
  190. if (argc < 2)
  191. return ERROR;
  192. while (1) {
  193. #ifdef HAVE_GETOPT_H
  194. c = getopt_long (argc, argv, "+?Vvhac:w:", long_options, &option_index);
  195. #else
  196. c = getopt (argc, argv, "+?Vvhac:w:");
  197. #endif
  198. if (c == -1 || c == EOF)
  199. break;
  200. switch (c) {
  201. case 'w': /* warning time threshold */
  202. if (is_intnonneg (optarg)) {
  203. warn_size = atoi (optarg);
  204. break;
  205. }
  206. else if (strstr (optarg, ",") &&
  207. strstr (optarg, "%") &&
  208. sscanf (optarg, "%d,%d%%", &warn_size, &warn_percent) == 2) {
  209. break;
  210. }
  211. else if (strstr (optarg, "%") &&
  212. sscanf (optarg, "%d%%", &warn_percent) == 1) {
  213. break;
  214. }
  215. else {
  216. usage ("Warning threshold must be integer or percentage!\n");
  217. }
  218. wc++;
  219. case 'c': /* critical time threshold */
  220. if (is_intnonneg (optarg)) {
  221. crit_size = atoi (optarg);
  222. break;
  223. }
  224. else if (strstr (optarg, ",") &&
  225. strstr (optarg, "%") &&
  226. sscanf (optarg, "%d,%d%%", &crit_size, &crit_percent) == 2) {
  227. break;
  228. }
  229. else if (strstr (optarg, "%") &&
  230. sscanf (optarg, "%d%%", &crit_percent) == 1) {
  231. break;
  232. }
  233. else {
  234. usage ("Critical threshold must be integer or percentage!\n");
  235. }
  236. cc++;
  237. case 'a': /* verbose */
  238. allswaps = TRUE;
  239. break;
  240. case 'v': /* verbose */
  241. verbose = TRUE;
  242. break;
  243. case 'V': /* version */
  244. print_revision (progname, "$Revision$");
  245. exit (STATE_OK);
  246. case 'h': /* help */
  247. print_help ();
  248. exit (STATE_OK);
  249. case '?': /* help */
  250. usage ("Invalid argument\n");
  251. }
  252. }
  253. c = optind;
  254. if (c == argc)
  255. return validate_arguments ();
  256. if (warn_percent > 100 && is_intnonneg (argv[c]))
  257. warn_percent = atoi (argv[c++]);
  258. if (c == argc)
  259. return validate_arguments ();
  260. if (crit_percent > 100 && is_intnonneg (argv[c]))
  261. crit_percent = atoi (argv[c++]);
  262. if (c == argc)
  263. return validate_arguments ();
  264. if (warn_size < 0 && is_intnonneg (argv[c]))
  265. warn_size = atoi (argv[c++]);
  266. if (c == argc)
  267. return validate_arguments ();
  268. if (crit_size < 0 && is_intnonneg (argv[c]))
  269. crit_size = atoi (argv[c++]);
  270. return validate_arguments ();
  271. }
  272. int
  273. validate_arguments (void)
  274. {
  275. if (warn_percent > 100 && crit_percent > 100 && warn_size < 0
  276. && crit_size < 0) {
  277. return ERROR;
  278. }
  279. else if (warn_percent > crit_percent) {
  280. usage
  281. ("Warning percentage should not be less than critical percentage\n");
  282. }
  283. else if (warn_size < crit_size) {
  284. usage
  285. ("Warning free space should not be more than critical free space\n");
  286. }
  287. return OK;
  288. }
  289. void
  290. print_usage (void)
  291. {
  292. printf
  293. ("Usage:\n"
  294. " %s [-a] -w <used_percentage>%% -c <used_percentage>%%\n"
  295. " %s [-a] -w <bytes_free> -c <bytes_free>\n"
  296. " %s (-h | --help) for detailed help\n"
  297. " %s (-V | --version) for version information\n",
  298. progname, progname, progname, progname);
  299. }
  300. void
  301. print_help (void)
  302. {
  303. print_revision (progname, REVISION);
  304. printf
  305. ("Copyright (c) %s %s <%s>\n\n%s\n", COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
  306. print_usage ();
  307. printf
  308. ("\nOptions:\n"
  309. " -w, --warning=INTEGER\n"
  310. " Exit with WARNING status if less than INTEGER bytes of swap space are free\n"
  311. " -w, --warning=PERCENT%%\n"
  312. " Exit with WARNING status if more than PERCENT of swap space has been used\n"
  313. " -c, --critical=INTEGER\n"
  314. " Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n"
  315. " -c, --critical=PERCENT%%\n"
  316. " Exit with CRITCAL status if more than PERCENT of swap space has been used\n"
  317. " -a, --allswaps\n"
  318. " Conduct comparisons for all swap partitions, one by one\n"
  319. " -h, --help\n"
  320. " Print detailed help screen\n"
  321. " -V, --version\n" " Print version information\n\n");
  322. support ();
  323. }