check_swap.c 13 KB

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