check_swap.c 11 KB

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