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