4
0

check_swap.c 9.8 KB

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