check_swap.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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, long unsigned 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. int
  49. main (int argc, char **argv)
  50. {
  51. int percent_used, percent;
  52. long unsigned int total_swap = 0, used_swap = 0, free_swap = 0;
  53. long unsigned int dsktotal, dskused, dskfree;
  54. int result = STATE_OK;
  55. char input_buffer[MAX_INPUT_BUFFER];
  56. #ifdef HAVE_SWAP
  57. int conv_factor; /* Convert to MBs */
  58. char *temp_buffer;
  59. char *swap_command;
  60. char *swap_format;
  61. #endif
  62. #ifdef HAVE_PROC_MEMINFO
  63. FILE *fp;
  64. #endif
  65. char str[32];
  66. char *status;
  67. status = strdup("");
  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, &dsktotal, &dskused, &dskfree) == 4 &&
  74. strstr (str, "Swap")) {
  75. dsktotal = dsktotal / 1048576;
  76. dskused = dskused / 1048576;
  77. dskfree = dskfree / 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, "dskfree"))
  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, &dsktotal, &dskfree);
  124. dsktotal = dsktotal / conv_factor;
  125. dskfree = dskfree / conv_factor;
  126. if (verbose >= 3)
  127. printf (_("total=%d, free=%d\n"), dsktotal, dskfree);
  128. dskused = dsktotal - dskfree;
  129. #endif
  130. total_swap += dsktotal;
  131. used_swap += dskused;
  132. free_swap += dskfree;
  133. if (allswaps) {
  134. percent = 100 * (((double) dskused) / ((double) dsktotal));
  135. result = max_state (result, check_swap (percent, dskfree));
  136. if (verbose)
  137. asprintf (&status, "%s [%lu (%d%%)]", status, dskfree, 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. die (result, "SWAP %s:%s\n", state_text (result), status);
  159. return STATE_UNKNOWN;
  160. }
  161. int
  162. check_swap (int usp, long unsigned int free_swap)
  163. {
  164. int result = STATE_UNKNOWN;
  165. if (usp >= 0 && usp >= (100.0 - crit_percent))
  166. result = STATE_CRITICAL;
  167. else if (crit_size > 0 && free_swap <= crit_size)
  168. result = STATE_CRITICAL;
  169. else if (usp >= 0 && usp >= (100.0 - warn_percent))
  170. result = STATE_WARNING;
  171. else if (warn_size > 0 && free_swap <= warn_size)
  172. result = STATE_WARNING;
  173. else if (usp >= 0.0)
  174. result = STATE_OK;
  175. return result;
  176. }
  177. /* process command-line arguments */
  178. int
  179. process_arguments (int argc, char **argv)
  180. {
  181. int c = 0; /* option character */
  182. int wc = 0; /* warning counter */
  183. int cc = 0; /* critical counter */
  184. int option = 0;
  185. static struct option longopts[] = {
  186. {"warning", required_argument, 0, 'w'},
  187. {"critical", required_argument, 0, 'c'},
  188. {"allswaps", no_argument, 0, 'a'},
  189. {"verbose", no_argument, 0, 'v'},
  190. {"version", no_argument, 0, 'V'},
  191. {"help", no_argument, 0, 'h'},
  192. {0, 0, 0, 0}
  193. };
  194. if (argc < 2)
  195. return ERROR;
  196. while (1) {
  197. c = getopt_long (argc, argv, "+?Vvhac:w:", longopts, &option);
  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, "%lu,%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. 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. case 'a': /* all swap */
  236. allswaps = TRUE;
  237. break;
  238. case 'v': /* verbose */
  239. verbose++;
  240. break;
  241. case 'V': /* version */
  242. print_revision (progname, revision);
  243. exit (STATE_OK);
  244. case 'h': /* help */
  245. print_help ();
  246. exit (STATE_OK);
  247. case '?': /* help */
  248. usage (_("Invalid argument\n"));
  249. }
  250. }
  251. c = optind;
  252. if (c == argc)
  253. return validate_arguments ();
  254. if (warn_percent > 100 && is_intnonneg (argv[c]))
  255. warn_percent = atoi (argv[c++]);
  256. if (c == argc)
  257. return validate_arguments ();
  258. if (crit_percent > 100 && is_intnonneg (argv[c]))
  259. crit_percent = atoi (argv[c++]);
  260. if (c == argc)
  261. return validate_arguments ();
  262. if (warn_size == 0 && is_intnonneg (argv[c]))
  263. warn_size = atoi (argv[c++]);
  264. if (c == argc)
  265. return validate_arguments ();
  266. if (crit_size == 0 && is_intnonneg (argv[c]))
  267. crit_size = atoi (argv[c++]);
  268. return validate_arguments ();
  269. }
  270. int
  271. validate_arguments (void)
  272. {
  273. if (warn_percent > 100 && crit_percent > 100 && warn_size == 0
  274. && crit_size == 0) {
  275. return ERROR;
  276. }
  277. else if (warn_percent < crit_percent) {
  278. usage
  279. (_("Warning percentage should be more than critical percentage\n"));
  280. }
  281. else if (warn_size < crit_size) {
  282. usage
  283. (_("Warning free space should be more than critical free space\n"));
  284. }
  285. return OK;
  286. }
  287. void
  288. print_help (void)
  289. {
  290. print_revision (progname, revision);
  291. printf (_(COPYRIGHT), copyright, email);
  292. printf (_("Check swap space on local server.\n\n"));
  293. print_usage ();
  294. printf (_(UT_HELP_VRSN));
  295. printf (_("\n\
  296. -w, --warning=INTEGER\n\
  297. Exit with WARNING status if less than INTEGER bytes of swap space are free\n\
  298. -w, --warning=PERCENT%%\n\
  299. Exit with WARNING status if less than PERCENT of swap space has been used\n\
  300. -c, --critical=INTEGER\n\
  301. Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n\
  302. -c, --critical=PERCENT%%\n\
  303. Exit with CRITCAL status if less than PERCENT of swap space has been used\n\
  304. -a, --allswaps\n\
  305. Conduct comparisons for all swap partitions, one by one\n"));
  306. #ifdef sun
  307. printf (_("\n\
  308. On Solaris, if -a specified, uses swap -l, otherwise uses swap -s.\n\
  309. Will be discrepencies because swap -s counts allocated swap and includes\n\
  310. real memory\n"));
  311. #endif
  312. printf (_(UT_SUPPORT));
  313. }
  314. void
  315. print_usage (void)
  316. {
  317. printf (_("Usage:\n\
  318. %s [-a] -w <used_percentage>%% -c <used_percentage>%%\n\
  319. %s [-a] -w <bytes_free> -c <bytes_free>\n\
  320. %s (-h | --help) for detailed help\n\
  321. %s (-V | --version) for version information\n"),
  322. progname, progname, progname, progname);
  323. }