check_swap.c 15 KB

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