check_swap.c 12 KB

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