check_swap.c 12 KB

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