check_swap.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*****************************************************************************
  2. *
  3. * Monitoring check_swap 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_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-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. status = "- Swap is either disabled, not present, or of zero size. ";
  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. if (!free_swap_mb) return no_swap_state;
  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. {"no-swap", required_argument, 0, 'n'},
  353. {"verbose", no_argument, 0, 'v'},
  354. {"version", no_argument, 0, 'V'},
  355. {"help", no_argument, 0, 'h'},
  356. {0, 0, 0, 0}
  357. };
  358. if (argc < 2)
  359. return ERROR;
  360. while (1) {
  361. c = getopt_long (argc, argv, "+?Vvhac:w:n:", longopts, &option);
  362. if (c == -1 || c == EOF)
  363. break;
  364. switch (c) {
  365. case 'w': /* warning size threshold */
  366. if (is_intnonneg (optarg)) {
  367. warn_size_bytes = (float) atoi (optarg);
  368. break;
  369. }
  370. else if (strstr (optarg, ",") &&
  371. strstr (optarg, "%") &&
  372. sscanf (optarg, "%f,%d%%", &warn_size_bytes, &warn_percent) == 2) {
  373. warn_size_bytes = floorf(warn_size_bytes);
  374. break;
  375. }
  376. else if (strstr (optarg, "%") &&
  377. sscanf (optarg, "%d%%", &warn_percent) == 1) {
  378. break;
  379. }
  380. else {
  381. usage4 (_("Warning threshold must be integer or percentage!"));
  382. }
  383. case 'c': /* critical size threshold */
  384. if (is_intnonneg (optarg)) {
  385. crit_size_bytes = (float) atoi (optarg);
  386. break;
  387. }
  388. else if (strstr (optarg, ",") &&
  389. strstr (optarg, "%") &&
  390. sscanf (optarg, "%f,%d%%", &crit_size_bytes, &crit_percent) == 2) {
  391. crit_size_bytes = floorf(crit_size_bytes);
  392. break;
  393. }
  394. else if (strstr (optarg, "%") &&
  395. sscanf (optarg, "%d%%", &crit_percent) == 1) {
  396. break;
  397. }
  398. else {
  399. usage4 (_("Critical threshold must be integer or percentage!"));
  400. }
  401. case 'a': /* all swap */
  402. allswaps = TRUE;
  403. break;
  404. case 'n':
  405. if ((no_swap_state = mp_translate_state(optarg)) == ERROR) {
  406. usage4 (_("no-swap result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
  407. }
  408. case 'v': /* verbose */
  409. verbose++;
  410. break;
  411. case 'V': /* version */
  412. print_revision (progname, NP_VERSION);
  413. exit (STATE_UNKNOWN);
  414. case 'h': /* help */
  415. print_help ();
  416. exit (STATE_UNKNOWN);
  417. case '?': /* error */
  418. usage5 ();
  419. }
  420. }
  421. c = optind;
  422. if (c == argc)
  423. return validate_arguments ();
  424. if (warn_percent == 0 && is_intnonneg (argv[c]))
  425. warn_percent = atoi (argv[c++]);
  426. if (c == argc)
  427. return validate_arguments ();
  428. if (crit_percent == 0 && is_intnonneg (argv[c]))
  429. crit_percent = atoi (argv[c++]);
  430. if (c == argc)
  431. return validate_arguments ();
  432. if (warn_size_bytes == 0 && is_intnonneg (argv[c]))
  433. warn_size_bytes = (float) atoi (argv[c++]);
  434. if (c == argc)
  435. return validate_arguments ();
  436. if (crit_size_bytes == 0 && is_intnonneg (argv[c]))
  437. crit_size_bytes = (float) atoi (argv[c++]);
  438. return validate_arguments ();
  439. }
  440. int
  441. validate_arguments (void)
  442. {
  443. if (warn_percent == 0 && crit_percent == 0 && warn_size_bytes == 0
  444. && crit_size_bytes == 0) {
  445. return ERROR;
  446. }
  447. else if (warn_percent < crit_percent) {
  448. usage4
  449. (_("Warning percentage should be more than critical percentage"));
  450. }
  451. else if (warn_size_bytes < crit_size_bytes) {
  452. usage4
  453. (_("Warning free space should be more than critical free space"));
  454. }
  455. return OK;
  456. }
  457. void
  458. print_help (void)
  459. {
  460. print_revision (progname, NP_VERSION);
  461. printf (_(COPYRIGHT), copyright, email);
  462. printf ("%s\n", _("Check swap space on local machine."));
  463. printf ("\n\n");
  464. print_usage ();
  465. printf (UT_HELP_VRSN);
  466. printf (UT_EXTRA_OPTS);
  467. printf (" %s\n", "-w, --warning=INTEGER");
  468. printf (" %s\n", _("Exit with WARNING status if less than INTEGER bytes of swap space are free"));
  469. printf (" %s\n", "-w, --warning=PERCENT%%");
  470. printf (" %s\n", _("Exit with WARNING status if less than PERCENT of swap space is free"));
  471. printf (" %s\n", "-c, --critical=INTEGER");
  472. printf (" %s\n", _("Exit with CRITICAL status if less than INTEGER bytes of swap space are free"));
  473. printf (" %s\n", "-c, --critical=PERCENT%%");
  474. printf (" %s\n", _("Exit with CRITICAL status if less than PERCENT of swap space is free"));
  475. printf (" %s\n", "-a, --allswaps");
  476. printf (" %s\n", _("Conduct comparisons for all swap partitions, one by one"));
  477. printf (" %s\n", "-n, --no-swap=<ok|warning|critical|unknown>");
  478. printf (" %s %s\n", _("Resulting state when there is no swap regardless of thresholds. Default:"), state_text(no_swap_state));
  479. printf (UT_VERBOSE);
  480. printf ("\n");
  481. printf ("%s\n", _("Notes:"));
  482. printf (" %s\n", _("Both INTEGER and PERCENT thresholds can be specified, they are all checked."));
  483. printf (" %s\n", _("On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s."));
  484. printf (UT_SUPPORT);
  485. }
  486. void
  487. print_usage (void)
  488. {
  489. printf ("%s\n", _("Usage:"));
  490. printf (" %s [-av] -w <percent_free>%% -c <percent_free>%%\n",progname);
  491. printf (" -w <bytes_free> -c <bytes_free> [-n <state>]\n");
  492. }