check_swap.c 16 KB

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