check_swap.c 14 KB

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