check_swap.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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-2007 Nagios Plugins Development Team
  8. *
  9. * Last Modified: $Date$
  10. *
  11. * Description:
  12. *
  13. * This file contains the check_disk plugin
  14. *
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. *
  29. * $Id$
  30. *
  31. *****************************************************************************/
  32. const char *progname = "check_swap";
  33. const char *revision = "$Revision$";
  34. const char *copyright = "2000-2007";
  35. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  36. #include "common.h"
  37. #include "popen.h"
  38. #include "utils.h"
  39. #ifdef HAVE_DECL_SWAPCTL
  40. # ifdef HAVE_SYS_PARAM_H
  41. # include <sys/param.h>
  42. # endif
  43. # ifdef HAVE_SYS_SWAP_H
  44. # include <sys/swap.h>
  45. # endif
  46. # ifdef HAVE_SYS_STAT_H
  47. # include <sys/stat.h>
  48. # endif
  49. #endif
  50. #ifndef SWAP_CONVERSION
  51. # define SWAP_CONVERSION 1
  52. #endif
  53. int check_swap (int usp, float free_swap_mb);
  54. int process_arguments (int argc, char **argv);
  55. int validate_arguments (void);
  56. void print_usage (void);
  57. void print_help (void);
  58. int warn_percent = 0;
  59. int crit_percent = 0;
  60. float warn_size_bytes = 0;
  61. float crit_size_bytes= 0;
  62. int verbose;
  63. int allswaps;
  64. int
  65. main (int argc, char **argv)
  66. {
  67. int percent_used, percent;
  68. float total_swap_mb = 0, used_swap_mb = 0, free_swap_mb = 0;
  69. float dsktotal_mb = 0, dskused_mb = 0, dskfree_mb = 0, tmp_mb = 0;
  70. int result = STATE_UNKNOWN;
  71. char input_buffer[MAX_INPUT_BUFFER];
  72. #ifdef HAVE_PROC_MEMINFO
  73. FILE *fp;
  74. #else
  75. int conv_factor = SWAP_CONVERSION;
  76. # ifdef HAVE_SWAP
  77. char *temp_buffer;
  78. char *swap_command;
  79. char *swap_format;
  80. # else
  81. # ifdef HAVE_DECL_SWAPCTL
  82. int i=0, nswaps=0, swapctl_res=0;
  83. # ifdef CHECK_SWAP_SWAPCTL_SVR4
  84. swaptbl_t *tbl=NULL;
  85. swapent_t *ent=NULL;
  86. # else
  87. # ifdef CHECK_SWAP_SWAPCTL_BSD
  88. struct swapent *ent;
  89. # endif /* CHECK_SWAP_SWAPCTL_BSD */
  90. # endif /* CHECK_SWAP_SWAPCTL_SVR4 */
  91. # endif /* HAVE_DECL_SWAPCTL */
  92. # endif
  93. #endif
  94. char str[32];
  95. char *status;
  96. setlocale (LC_ALL, "");
  97. bindtextdomain (PACKAGE, LOCALEDIR);
  98. textdomain (PACKAGE);
  99. status = strdup ("");
  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]%*[:] %f %f %f", &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=100.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. asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent);
  123. }
  124. }
  125. else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFre]%*[:] %f %*[k]%*[B]", str, &tmp_mb)) {
  126. if (verbose >= 3) {
  127. printf("Got %s with %f\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. asprintf(&swap_command, "%s", SWAP_COMMAND);
  146. asprintf(&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. asprintf(&swap_command, "%s", "/usr/sbin/lsps -s");
  152. asprintf(&swap_format, "%s", "%f%*s %f");
  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, "%f");
  176. else if (strstr (temp_buffer, "dskfree"))
  177. sprintf (str, "%s %s", str, "%f");
  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. asprintf (&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 = (float) tbl->swt_ent[i].ste_pages / SWAP_CONVERSION;
  254. dskfree_mb = (float) 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. asprintf (&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 = (float) ent[i].se_nblks / conv_factor;
  288. dskused_mb = (float) 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. asprintf (&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, let's not divide by 0 */
  308. if(total_swap_mb) {
  309. percent_used = 100 * ((double) used_swap_mb) / ((double) total_swap_mb);
  310. } else {
  311. percent_used = 0;
  312. }
  313. result = max_state (result, check_swap (percent_used, free_swap_mb));
  314. printf (_("SWAP %s - %d%% free (%d MB out of %d MB) %s|"),
  315. state_text (result),
  316. (100 - percent_used), (int) free_swap_mb, (int) total_swap_mb, status);
  317. puts (perfdata ("swap", (long) free_swap_mb, "MB",
  318. TRUE, (long) max (warn_size_bytes/(1024 * 1024), warn_percent/100.0*total_swap_mb),
  319. TRUE, (long) max (crit_size_bytes/(1024 * 1024), crit_percent/100.0*total_swap_mb),
  320. TRUE, 0,
  321. TRUE, (long) total_swap_mb));
  322. return result;
  323. }
  324. int
  325. check_swap (int usp, float free_swap_mb)
  326. {
  327. int result = STATE_UNKNOWN;
  328. float free_swap = free_swap_mb * (1024 * 1024); /* Convert back to bytes as warn and crit specified in bytes */
  329. if (usp >= 0 && crit_percent != 0 && usp >= (100.0 - crit_percent))
  330. result = STATE_CRITICAL;
  331. else if (crit_size_bytes > 0 && free_swap <= crit_size_bytes)
  332. result = STATE_CRITICAL;
  333. else if (usp >= 0 && warn_percent != 0 && usp >= (100.0 - warn_percent))
  334. result = STATE_WARNING;
  335. else if (warn_size_bytes > 0 && free_swap <= warn_size_bytes)
  336. result = STATE_WARNING;
  337. else if (usp >= 0.0)
  338. result = STATE_OK;
  339. return result;
  340. }
  341. /* process command-line arguments */
  342. int
  343. process_arguments (int argc, char **argv)
  344. {
  345. int c = 0; /* option character */
  346. int option = 0;
  347. static struct option longopts[] = {
  348. {"warning", required_argument, 0, 'w'},
  349. {"critical", required_argument, 0, 'c'},
  350. {"allswaps", no_argument, 0, 'a'},
  351. {"verbose", no_argument, 0, 'v'},
  352. {"version", no_argument, 0, 'V'},
  353. {"help", no_argument, 0, 'h'},
  354. {0, 0, 0, 0}
  355. };
  356. if (argc < 2)
  357. return ERROR;
  358. while (1) {
  359. c = getopt_long (argc, argv, "+?Vvhac:w:", longopts, &option);
  360. if (c == -1 || c == EOF)
  361. break;
  362. switch (c) {
  363. case 'w': /* warning size threshold */
  364. if (is_intnonneg (optarg)) {
  365. warn_size_bytes = (float) atoi (optarg);
  366. break;
  367. }
  368. else if (strstr (optarg, ",") &&
  369. strstr (optarg, "%") &&
  370. sscanf (optarg, "%f,%d%%", &warn_size_bytes, &warn_percent) == 2) {
  371. warn_size_bytes = floorf(warn_size_bytes);
  372. break;
  373. }
  374. else if (strstr (optarg, "%") &&
  375. sscanf (optarg, "%d%%", &warn_percent) == 1) {
  376. break;
  377. }
  378. else {
  379. usage4 (_("Warning threshold must be integer or percentage!"));
  380. }
  381. case 'c': /* critical size threshold */
  382. if (is_intnonneg (optarg)) {
  383. crit_size_bytes = (float) atoi (optarg);
  384. break;
  385. }
  386. else if (strstr (optarg, ",") &&
  387. strstr (optarg, "%") &&
  388. sscanf (optarg, "%f,%d%%", &crit_size_bytes, &crit_percent) == 2) {
  389. crit_size_bytes = floorf(crit_size_bytes);
  390. break;
  391. }
  392. else if (strstr (optarg, "%") &&
  393. sscanf (optarg, "%d%%", &crit_percent) == 1) {
  394. break;
  395. }
  396. else {
  397. usage4 (_("Critical threshold must be integer or percentage!"));
  398. }
  399. case 'a': /* all swap */
  400. allswaps = TRUE;
  401. break;
  402. case 'v': /* verbose */
  403. verbose++;
  404. break;
  405. case 'V': /* version */
  406. print_revision (progname, revision);
  407. exit (STATE_OK);
  408. case 'h': /* help */
  409. print_help ();
  410. exit (STATE_OK);
  411. case '?': /* error */
  412. usage5 ();
  413. }
  414. }
  415. c = optind;
  416. if (c == argc)
  417. return validate_arguments ();
  418. if (warn_percent == 0 && is_intnonneg (argv[c]))
  419. warn_percent = atoi (argv[c++]);
  420. if (c == argc)
  421. return validate_arguments ();
  422. if (crit_percent == 0 && is_intnonneg (argv[c]))
  423. crit_percent = atoi (argv[c++]);
  424. if (c == argc)
  425. return validate_arguments ();
  426. if (warn_size_bytes == 0 && is_intnonneg (argv[c]))
  427. warn_size_bytes = (float) atoi (argv[c++]);
  428. if (c == argc)
  429. return validate_arguments ();
  430. if (crit_size_bytes == 0 && is_intnonneg (argv[c]))
  431. crit_size_bytes = (float) atoi (argv[c++]);
  432. return validate_arguments ();
  433. }
  434. int
  435. validate_arguments (void)
  436. {
  437. if (warn_percent == 0 && crit_percent == 0 && warn_size_bytes == 0
  438. && crit_size_bytes == 0) {
  439. return ERROR;
  440. }
  441. else if (warn_percent < crit_percent) {
  442. usage4
  443. (_("Warning percentage should be more than critical percentage"));
  444. }
  445. else if (warn_size_bytes < crit_size_bytes) {
  446. usage4
  447. (_("Warning free space should be more than critical free space"));
  448. }
  449. return OK;
  450. }
  451. void
  452. print_help (void)
  453. {
  454. print_revision (progname, revision);
  455. printf (_(COPYRIGHT), copyright, email);
  456. printf ("%s\n", _("Check swap space on local machine."));
  457. printf ("\n\n");
  458. print_usage ();
  459. printf (_(UT_HELP_VRSN));
  460. printf (" %s\n", "-w, --warning=INTEGER");
  461. printf (" %s\n", _("Exit with WARNING status if less than INTEGER bytes of swap space are free"));
  462. printf (" %s\n", "-w, --warning=PERCENT%%");
  463. printf (" %s\n", _("Exit with WARNING status if less than PERCENT of swap space is free"));
  464. printf (" %s\n", "-c, --critical=INTEGER");
  465. printf (" %s\n", _("Exit with CRITICAL status if less than INTEGER bytes of swap space are free"));
  466. printf (" %s\n", "-c, --critical=PERCENT%%");
  467. printf (" %s\n", _("Exit with CRITCAL status if less than PERCENT of swap space is free"));
  468. printf (" %s\n", "-a, --allswaps");
  469. printf (" %s\n", _("Conduct comparisons for all swap partitions, one by one"));
  470. printf (_(UT_VERBOSE));
  471. printf ("\n");
  472. printf ("%s\n", _("Notes:"));
  473. printf (" %s\n", _("On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.\n"));
  474. printf (_(UT_SUPPORT));
  475. }
  476. void
  477. print_usage (void)
  478. {
  479. printf (_("Usage:"));
  480. printf ("%s [-av] -w <percent_free>%% -c <percent_free>%%\n",progname);
  481. printf ("%s [-av] -w <bytes_free> -c <bytes_free>\n", progname);
  482. }