check_swap.c 16 KB

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