check_swap.c 16 KB

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