check_swap.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. setlocale (LC_ALL, "");
  68. bindtextdomain (PACKAGE, LOCALEDIR);
  69. textdomain (PACKAGE);
  70. status = strdup("");
  71. if (process_arguments (argc, argv) != OK)
  72. usage (_("Invalid command arguments supplied\n"));
  73. #ifdef HAVE_PROC_MEMINFO
  74. fp = fopen (PROC_MEMINFO, "r");
  75. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  76. if (sscanf (input_buffer, " %s %lu %lu %lu", str, &dsktotal, &dskused, &dskfree) == 4 &&
  77. strstr (str, "Swap")) {
  78. dsktotal = dsktotal / 1048576;
  79. dskused = dskused / 1048576;
  80. dskfree = dskfree / 1048576;
  81. #endif
  82. #ifdef HAVE_SWAP
  83. if (!allswaps && sun) {
  84. asprintf(&swap_command, "%s", "/usr/sbin/swap -s");
  85. asprintf(&swap_format, "%s", "%*s %*dk %*s %*s + %*dk %*s = %dk %*s %dk %*s");
  86. conv_factor = 2048;
  87. } else {
  88. asprintf(&swap_command, "%s", SWAP_COMMAND);
  89. asprintf(&swap_format, "%s", SWAP_FORMAT);
  90. conv_factor = SWAP_CONVERSION;
  91. }
  92. if (verbose >= 2)
  93. printf (_("Command: %s\n"), swap_command);
  94. if (verbose >= 3)
  95. printf ("_(Format: %s\n"), swap_format);
  96. child_process = spopen (swap_command);
  97. if (child_process == NULL) {
  98. printf (_("Could not open pipe: %s\n"), swap_command);
  99. return STATE_UNKNOWN;
  100. }
  101. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  102. if (child_stderr == NULL)
  103. printf (_("Could not open stderr for %s\n"), swap_command);
  104. sprintf (str, "%s", "");
  105. /* read 1st line */
  106. fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
  107. if (strcmp (swap_format, "") == 0) {
  108. temp_buffer = strtok (input_buffer, " \n");
  109. while (temp_buffer) {
  110. if (strstr (temp_buffer, "blocks"))
  111. sprintf (str, "%s %s", str, "%f");
  112. else if (strstr (temp_buffer, "dskfree"))
  113. sprintf (str, "%s %s", str, "%f");
  114. else
  115. sprintf (str, "%s %s", str, "%*s");
  116. temp_buffer = strtok (NULL, " \n");
  117. }
  118. }
  119. if (!allswaps && sun) {
  120. sscanf (input_buffer, swap_format, &used_swap, &free_swap);
  121. used_swap = used_swap / 1024;
  122. free_swap = free_swap / 1024;
  123. total_swap = used_swap + free_swap;
  124. } else {
  125. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  126. sscanf (input_buffer, swap_format, &dsktotal, &dskfree);
  127. dsktotal = dsktotal / conv_factor;
  128. dskfree = dskfree / conv_factor;
  129. if (verbose >= 3)
  130. printf (_("total=%d, free=%d\n"), dsktotal, dskfree);
  131. dskused = dsktotal - dskfree;
  132. #endif
  133. total_swap += dsktotal;
  134. used_swap += dskused;
  135. free_swap += dskfree;
  136. if (allswaps) {
  137. percent = 100 * (((double) dskused) / ((double) dsktotal));
  138. result = max_state (result, check_swap (percent, dskfree));
  139. if (verbose)
  140. asprintf (&status, "%s [%lu (%d%%)]", status, dskfree, 100 - percent);
  141. }
  142. }
  143. }
  144. percent_used = 100 * ((double) used_swap) / ((double) total_swap);
  145. result = max_state (result, check_swap (percent_used, free_swap));
  146. asprintf (&status, _(" %d%% free (%lu MB out of %lu MB)%s"),
  147. (100 - percent_used), free_swap, total_swap, status);
  148. #ifdef HAVE_PROC_MEMINFO
  149. fclose(fp);
  150. #endif
  151. #ifdef HAVE_SWAP
  152. /* If we get anything on STDERR, at least set warning */
  153. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  154. result = max_state (result, STATE_WARNING);
  155. /* close stderr */
  156. (void) fclose (child_stderr);
  157. /* close the pipe */
  158. if (spclose (child_process))
  159. result = max_state (result, STATE_WARNING);
  160. #endif
  161. die (result, "SWAP %s:%s\n", state_text (result), status);
  162. return STATE_UNKNOWN;
  163. }
  164. int
  165. check_swap (int usp, long unsigned int free_swap)
  166. {
  167. int result = STATE_UNKNOWN;
  168. if (usp >= 0 && usp >= (100.0 - crit_percent))
  169. result = STATE_CRITICAL;
  170. else if (crit_size > 0 && free_swap <= crit_size)
  171. result = STATE_CRITICAL;
  172. else if (usp >= 0 && usp >= (100.0 - warn_percent))
  173. result = STATE_WARNING;
  174. else if (warn_size > 0 && free_swap <= warn_size)
  175. result = STATE_WARNING;
  176. else if (usp >= 0.0)
  177. result = STATE_OK;
  178. return result;
  179. }
  180. /* process command-line arguments */
  181. int
  182. process_arguments (int argc, char **argv)
  183. {
  184. int c = 0; /* option character */
  185. int option = 0;
  186. static struct option longopts[] = {
  187. {"warning", required_argument, 0, 'w'},
  188. {"critical", required_argument, 0, 'c'},
  189. {"allswaps", no_argument, 0, 'a'},
  190. {"verbose", no_argument, 0, 'v'},
  191. {"version", no_argument, 0, 'V'},
  192. {"help", no_argument, 0, 'h'},
  193. {0, 0, 0, 0}
  194. };
  195. if (argc < 2)
  196. return ERROR;
  197. while (1) {
  198. c = getopt_long (argc, argv, "+?Vvhac:w:", longopts, &option);
  199. if (c == -1 || c == EOF)
  200. break;
  201. switch (c) {
  202. case 'w': /* warning size threshold */
  203. if (is_intnonneg (optarg)) {
  204. warn_size = atoi (optarg);
  205. break;
  206. }
  207. else if (strstr (optarg, ",") &&
  208. strstr (optarg, "%") &&
  209. sscanf (optarg, "%lu,%d%%", &warn_size, &warn_percent) == 2) {
  210. break;
  211. }
  212. else if (strstr (optarg, "%") &&
  213. sscanf (optarg, "%d%%", &warn_percent) == 1) {
  214. break;
  215. }
  216. else {
  217. usage (_("Warning threshold must be integer or percentage!\n"));
  218. }
  219. case 'c': /* critical size 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, "%lu,%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. 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_help (void)
  290. {
  291. print_revision (progname, revision);
  292. printf (_(COPYRIGHT), copyright, email);
  293. printf (_("Check swap space on local server.\n\n"));
  294. print_usage ();
  295. printf (_(UT_HELP_VRSN));
  296. printf (_("\n\
  297. -w, --warning=INTEGER\n\
  298. Exit with WARNING status if less than INTEGER bytes of swap space are free\n\
  299. -w, --warning=PERCENT%%\n\
  300. Exit with WARNING status if less than PERCENT of swap space has been used\n\
  301. -c, --critical=INTEGER\n\
  302. Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n\
  303. -c, --critical=PERCENT%%\n\
  304. Exit with CRITCAL status if less than PERCENT of swap space has been used\n\
  305. -a, --allswaps\n\
  306. Conduct comparisons for all swap partitions, one by one\n"));
  307. #ifdef sun
  308. printf (_("\n\
  309. On Solaris, if -a specified, uses swap -l, otherwise uses swap -s.\n\
  310. Will be discrepencies because swap -s counts allocated swap and includes\n\
  311. real memory\n"));
  312. #endif
  313. printf (_(UT_SUPPORT));
  314. }
  315. void
  316. print_usage (void)
  317. {
  318. printf (_("Usage:\n\
  319. %s [-a] -w <used_percentage>%% -c <used_percentage>%%\n\
  320. %s [-a] -w <bytes_free> -c <bytes_free>\n\
  321. %s (-h | --help) for detailed help\n\
  322. %s (-V | --version) for version information\n"),
  323. progname, progname, progname, progname);
  324. }