check_swap.c 14 KB

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