check_swap.c 15 KB

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