check_swap.c 9.9 KB

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