check_swap.c 9.9 KB

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