check_swap.c 10 KB

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