check_swap.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /*****************************************************************************
  2. *
  3. * Monitoring check_disk plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
  7. * Copyright (c) 2000-2007 Monitoring Plugins Development Team
  8. *
  9. * Description:
  10. *
  11. * This file contains the check_disk 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-2007";
  31. const char *email = "devel@monitoring-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 no_swap_state = STATE_CRITICAL;
  61. int
  62. main (int argc, char **argv)
  63. {
  64. int percent_used, percent;
  65. float total_swap_mb = 0, used_swap_mb = 0, free_swap_mb = 0;
  66. float dsktotal_mb = 0, dskused_mb = 0, dskfree_mb = 0, tmp_mb = 0;
  67. int result = STATE_UNKNOWN;
  68. char input_buffer[MAX_INPUT_BUFFER];
  69. #ifdef HAVE_PROC_MEMINFO
  70. FILE *fp;
  71. #else
  72. int conv_factor = SWAP_CONVERSION;
  73. # ifdef HAVE_SWAP
  74. char *temp_buffer;
  75. char *swap_command;
  76. char *swap_format;
  77. # else
  78. # ifdef HAVE_DECL_SWAPCTL
  79. int i=0, nswaps=0, swapctl_res=0;
  80. # ifdef CHECK_SWAP_SWAPCTL_SVR4
  81. swaptbl_t *tbl=NULL;
  82. swapent_t *ent=NULL;
  83. # else
  84. # ifdef CHECK_SWAP_SWAPCTL_BSD
  85. struct swapent *ent;
  86. # endif /* CHECK_SWAP_SWAPCTL_BSD */
  87. # endif /* CHECK_SWAP_SWAPCTL_SVR4 */
  88. # endif /* HAVE_DECL_SWAPCTL */
  89. # endif
  90. #endif
  91. char str[32];
  92. char *status;
  93. setlocale (LC_ALL, "");
  94. bindtextdomain (PACKAGE, LOCALEDIR);
  95. textdomain (PACKAGE);
  96. status = strdup ("");
  97. /* Parse extra opts if any */
  98. argv=np_extra_opts (&argc, argv, progname);
  99. if (process_arguments (argc, argv) == ERROR)
  100. usage4 (_("Could not parse arguments"));
  101. #ifdef HAVE_PROC_MEMINFO
  102. if (verbose >= 3) {
  103. printf("Reading PROC_MEMINFO at %s\n", PROC_MEMINFO);
  104. }
  105. fp = fopen (PROC_MEMINFO, "r");
  106. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  107. if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %f %f %f", &dsktotal_mb, &dskused_mb, &dskfree_mb) == 3) {
  108. dsktotal_mb = dsktotal_mb / 1048576; /* Apply conversion */
  109. dskused_mb = dskused_mb / 1048576;
  110. dskfree_mb = dskfree_mb / 1048576;
  111. total_swap_mb += dsktotal_mb;
  112. used_swap_mb += dskused_mb;
  113. free_swap_mb += dskfree_mb;
  114. if (allswaps) {
  115. if (dsktotal_mb == 0)
  116. percent=100.0;
  117. else
  118. percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb));
  119. result = max_state (result, check_swap (percent, dskfree_mb));
  120. if (verbose)
  121. xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent);
  122. }
  123. }
  124. else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFre]%*[:] %f %*[k]%*[B]", str, &tmp_mb)) {
  125. if (verbose >= 3) {
  126. printf("Got %s with %f\n", str, tmp_mb);
  127. }
  128. /* I think this part is always in Kb, so convert to mb */
  129. if (strcmp ("Total", str) == 0) {
  130. dsktotal_mb = tmp_mb / 1024;
  131. }
  132. else if (strcmp ("Free", str) == 0) {
  133. dskfree_mb = tmp_mb / 1024;
  134. }
  135. }
  136. }
  137. fclose(fp);
  138. dskused_mb = dsktotal_mb - dskfree_mb;
  139. total_swap_mb = dsktotal_mb;
  140. used_swap_mb = dskused_mb;
  141. free_swap_mb = dskfree_mb;
  142. #else
  143. # ifdef HAVE_SWAP
  144. xasprintf(&swap_command, "%s", SWAP_COMMAND);
  145. xasprintf(&swap_format, "%s", SWAP_FORMAT);
  146. /* These override the command used if a summary (and thus ! allswaps) is required */
  147. /* The summary flag returns more accurate information about swap usage on these OSes */
  148. # ifdef _AIX
  149. if (!allswaps) {
  150. xasprintf(&swap_command, "%s", "/usr/sbin/lsps -s");
  151. xasprintf(&swap_format, "%s", "%f%*s %f");
  152. conv_factor = 1;
  153. }
  154. # endif
  155. if (verbose >= 2)
  156. printf (_("Command: %s\n"), swap_command);
  157. if (verbose >= 3)
  158. printf (_("Format: %s\n"), swap_format);
  159. child_process = spopen (swap_command);
  160. if (child_process == NULL) {
  161. printf (_("Could not open pipe: %s\n"), swap_command);
  162. return STATE_UNKNOWN;
  163. }
  164. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  165. if (child_stderr == NULL)
  166. printf (_("Could not open stderr for %s\n"), swap_command);
  167. sprintf (str, "%s", "");
  168. /* read 1st line */
  169. fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
  170. if (strcmp (swap_format, "") == 0) {
  171. temp_buffer = strtok (input_buffer, " \n");
  172. while (temp_buffer) {
  173. if (strstr (temp_buffer, "blocks"))
  174. sprintf (str, "%s %s", str, "%f");
  175. else if (strstr (temp_buffer, "dskfree"))
  176. sprintf (str, "%s %s", str, "%f");
  177. else
  178. sprintf (str, "%s %s", str, "%*s");
  179. temp_buffer = strtok (NULL, " \n");
  180. }
  181. }
  182. /* If different swap command is used for summary switch, need to read format differently */
  183. # ifdef _AIX
  184. if (!allswaps) {
  185. fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); /* Ignore first line */
  186. sscanf (input_buffer, swap_format, &total_swap_mb, &used_swap_mb);
  187. free_swap_mb = total_swap_mb * (100 - used_swap_mb) /100;
  188. used_swap_mb = total_swap_mb - free_swap_mb;
  189. if (verbose >= 3)
  190. printf (_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, used_swap_mb, free_swap_mb);
  191. } else {
  192. # endif
  193. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  194. sscanf (input_buffer, swap_format, &dsktotal_mb, &dskfree_mb);
  195. dsktotal_mb = dsktotal_mb / conv_factor;
  196. /* AIX lists percent used, so this converts to dskfree in MBs */
  197. # ifdef _AIX
  198. dskfree_mb = dsktotal_mb * (100 - dskfree_mb) / 100;
  199. # else
  200. dskfree_mb = dskfree_mb / conv_factor;
  201. # endif
  202. if (verbose >= 3)
  203. printf (_("total=%.0f, free=%.0f\n"), dsktotal_mb, dskfree_mb);
  204. dskused_mb = dsktotal_mb - dskfree_mb;
  205. total_swap_mb += dsktotal_mb;
  206. used_swap_mb += dskused_mb;
  207. free_swap_mb += dskfree_mb;
  208. if (allswaps) {
  209. percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb));
  210. result = max_state (result, check_swap (percent, dskfree_mb));
  211. if (verbose)
  212. xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent);
  213. }
  214. }
  215. # ifdef _AIX
  216. }
  217. # endif
  218. /* If we get anything on STDERR, at least set warning */
  219. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  220. result = max_state (result, STATE_WARNING);
  221. /* close stderr */
  222. (void) fclose (child_stderr);
  223. /* close the pipe */
  224. if (spclose (child_process))
  225. result = max_state (result, STATE_WARNING);
  226. # else
  227. # ifdef CHECK_SWAP_SWAPCTL_SVR4
  228. /* get the number of active swap devices */
  229. if((nswaps=swapctl(SC_GETNSWP, NULL))== -1)
  230. die(STATE_UNKNOWN, _("Error getting swap devices\n") );
  231. if(nswaps == 0)
  232. die(STATE_OK, _("SWAP OK: No swap devices defined\n"));
  233. if(verbose >= 3)
  234. printf("Found %d swap device(s)\n", nswaps);
  235. /* initialize swap table + entries */
  236. tbl=(swaptbl_t*)malloc(sizeof(swaptbl_t)+(sizeof(swapent_t)*nswaps));
  237. if(tbl==NULL)
  238. die(STATE_UNKNOWN, _("malloc() failed!\n"));
  239. memset(tbl, 0, sizeof(swaptbl_t)+(sizeof(swapent_t)*nswaps));
  240. tbl->swt_n=nswaps;
  241. for(i=0;i<nswaps;i++){
  242. if((tbl->swt_ent[i].ste_path=(char*)malloc(sizeof(char)*MAXPATHLEN)) == NULL)
  243. die(STATE_UNKNOWN, _("malloc() failed!\n"));
  244. }
  245. /* and now, tally 'em up */
  246. swapctl_res=swapctl(SC_LIST, tbl);
  247. if(swapctl_res < 0){
  248. perror(_("swapctl failed: "));
  249. die(STATE_UNKNOWN, _("Error in swapctl call\n"));
  250. }
  251. for(i=0;i<nswaps;i++){
  252. dsktotal_mb = (float) tbl->swt_ent[i].ste_pages / SWAP_CONVERSION;
  253. dskfree_mb = (float) tbl->swt_ent[i].ste_free / SWAP_CONVERSION;
  254. dskused_mb = ( dsktotal_mb - dskfree_mb );
  255. if (verbose >= 3)
  256. printf ("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", dsktotal_mb, dskfree_mb, dskused_mb);
  257. if(allswaps && dsktotal_mb > 0){
  258. percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb));
  259. result = max_state (result, check_swap (percent, dskfree_mb));
  260. if (verbose) {
  261. xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent);
  262. }
  263. }
  264. total_swap_mb += dsktotal_mb;
  265. free_swap_mb += dskfree_mb;
  266. used_swap_mb += dskused_mb;
  267. }
  268. /* and clean up after ourselves */
  269. for(i=0;i<nswaps;i++){
  270. free(tbl->swt_ent[i].ste_path);
  271. }
  272. free(tbl);
  273. # else
  274. # ifdef CHECK_SWAP_SWAPCTL_BSD
  275. /* get the number of active swap devices */
  276. nswaps=swapctl(SWAP_NSWAP, NULL, 0);
  277. /* initialize swap table + entries */
  278. ent=(struct swapent*)malloc(sizeof(struct swapent)*nswaps);
  279. /* and now, tally 'em up */
  280. swapctl_res=swapctl(SWAP_STATS, ent, nswaps);
  281. if(swapctl_res < 0){
  282. perror(_("swapctl failed: "));
  283. die(STATE_UNKNOWN, _("Error in swapctl call\n"));
  284. }
  285. for(i=0;i<nswaps;i++){
  286. dsktotal_mb = (float) ent[i].se_nblks / conv_factor;
  287. dskused_mb = (float) ent[i].se_inuse / conv_factor;
  288. dskfree_mb = ( dsktotal_mb - dskused_mb );
  289. if(allswaps && dsktotal_mb > 0){
  290. percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb));
  291. result = max_state (result, check_swap (percent, dskfree_mb));
  292. if (verbose) {
  293. xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent);
  294. }
  295. }
  296. total_swap_mb += dsktotal_mb;
  297. free_swap_mb += dskfree_mb;
  298. used_swap_mb += dskused_mb;
  299. }
  300. /* and clean up after ourselves */
  301. free(ent);
  302. # endif /* CHECK_SWAP_SWAPCTL_BSD */
  303. # endif /* CHECK_SWAP_SWAPCTL_SVR4 */
  304. # endif /* HAVE_SWAP */
  305. #endif /* HAVE_PROC_MEMINFO */
  306. /* if total_swap_mb == 0, let's not divide by 0 */
  307. if(total_swap_mb) {
  308. percent_used = 100 * ((double) used_swap_mb) / ((double) total_swap_mb);
  309. } else {
  310. percent_used = 100;
  311. }
  312. result = max_state (result, check_swap (percent_used, free_swap_mb));
  313. printf (_("SWAP %s - %d%% free (%d MB out of %d MB) %s|"),
  314. state_text (result),
  315. (100 - percent_used), (int) free_swap_mb, (int) total_swap_mb, status);
  316. puts (perfdata ("swap", (long) free_swap_mb, "MB",
  317. TRUE, (long) max (warn_size_bytes/(1024 * 1024), warn_percent/100.0*total_swap_mb),
  318. TRUE, (long) max (crit_size_bytes/(1024 * 1024), crit_percent/100.0*total_swap_mb),
  319. TRUE, 0,
  320. TRUE, (long) total_swap_mb));
  321. return result;
  322. }
  323. int
  324. check_swap (int usp, float free_swap_mb)
  325. {
  326. if (!free_swap_mb) return no_swap_state;
  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. {"no-swap", required_argument, 0, 'n'},
  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:n:", 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 'n':
  404. if ((no_swap_state = mp_translate_state(optarg)) == ERROR) {
  405. usage4 (_("no-swap result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
  406. }
  407. case 'v': /* verbose */
  408. verbose++;
  409. break;
  410. case 'V': /* version */
  411. print_revision (progname, NP_VERSION);
  412. exit (STATE_OK);
  413. case 'h': /* help */
  414. print_help ();
  415. exit (STATE_OK);
  416. case '?': /* error */
  417. usage5 ();
  418. }
  419. }
  420. c = optind;
  421. if (c == argc)
  422. return validate_arguments ();
  423. if (warn_percent == 0 && is_intnonneg (argv[c]))
  424. warn_percent = atoi (argv[c++]);
  425. if (c == argc)
  426. return validate_arguments ();
  427. if (crit_percent == 0 && is_intnonneg (argv[c]))
  428. crit_percent = atoi (argv[c++]);
  429. if (c == argc)
  430. return validate_arguments ();
  431. if (warn_size_bytes == 0 && is_intnonneg (argv[c]))
  432. warn_size_bytes = (float) atoi (argv[c++]);
  433. if (c == argc)
  434. return validate_arguments ();
  435. if (crit_size_bytes == 0 && is_intnonneg (argv[c]))
  436. crit_size_bytes = (float) atoi (argv[c++]);
  437. return validate_arguments ();
  438. }
  439. int
  440. validate_arguments (void)
  441. {
  442. if (warn_percent == 0 && crit_percent == 0 && warn_size_bytes == 0
  443. && crit_size_bytes == 0) {
  444. return ERROR;
  445. }
  446. else if (warn_percent < crit_percent) {
  447. usage4
  448. (_("Warning percentage should be more than critical percentage"));
  449. }
  450. else if (warn_size_bytes < crit_size_bytes) {
  451. usage4
  452. (_("Warning free space should be more than critical free space"));
  453. }
  454. return OK;
  455. }
  456. void
  457. print_help (void)
  458. {
  459. print_revision (progname, NP_VERSION);
  460. printf (_(COPYRIGHT), copyright, email);
  461. printf ("%s\n", _("Check swap space on local machine."));
  462. printf ("\n\n");
  463. print_usage ();
  464. printf (UT_HELP_VRSN);
  465. printf (UT_EXTRA_OPTS);
  466. printf (" %s\n", "-w, --warning=INTEGER");
  467. printf (" %s\n", _("Exit with WARNING status if less than INTEGER bytes of swap space are free"));
  468. printf (" %s\n", "-w, --warning=PERCENT%%");
  469. printf (" %s\n", _("Exit with WARNING status if less than PERCENT of swap space is free"));
  470. printf (" %s\n", "-c, --critical=INTEGER");
  471. printf (" %s\n", _("Exit with CRITICAL status if less than INTEGER bytes of swap space are free"));
  472. printf (" %s\n", "-c, --critical=PERCENT%%");
  473. printf (" %s\n", _("Exit with CRITICAL status if less than PERCENT of swap space is free"));
  474. printf (" %s\n", "-a, --allswaps");
  475. printf (" %s\n", _("Conduct comparisons for all swap partitions, one by one"));
  476. printf (" %s\n", "-n, --no-swap=<ok|warning|critical|unknown>");
  477. printf (" %s %s\n", _("Resulting state when there is no swap regardless of thresholds. Default:"), state_text(no_swap_state));
  478. printf (UT_VERBOSE);
  479. printf ("\n");
  480. printf ("%s\n", _("Notes:"));
  481. printf (" %s\n", _("Both INTEGER and PERCENT thresholds can be specified, they are all checked."));
  482. printf (" %s\n", _("On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s."));
  483. printf (UT_SUPPORT);
  484. }
  485. void
  486. print_usage (void)
  487. {
  488. printf ("%s\n", _("Usage:"));
  489. printf (" %s [-av] -w <percent_free>%% -c <percent_free>%%\n",progname);
  490. printf (" -w <bytes_free> -c <bytes_free> [-n <state>]\n");
  491. }