check_swap.c 12 KB

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