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. #include "common.h"
  28. #include "popen.h"
  29. #include "utils.h"
  30. const char *progname = "check_swap";
  31. const char *revision = "$Revision$";
  32. const char *copyright = "2000-2003";
  33. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  34. int check_swap (int usp, long unsigned int 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. unsigned long long warn_size = 0;
  42. unsigned long long crit_size = 0;
  43. int verbose;
  44. int allswaps;
  45. int
  46. main (int argc, char **argv)
  47. {
  48. int percent_used, percent;
  49. unsigned long long total_swap = 0, used_swap = 0, free_swap = 0;
  50. unsigned long long dsktotal = 0, dskused = 0, dskfree = 0, tmp = 0;
  51. int result = STATE_OK;
  52. char input_buffer[MAX_INPUT_BUFFER];
  53. char *perf;
  54. int conv_factor = SWAP_CONVERSION;
  55. #ifdef HAVE_PROC_MEMINFO
  56. FILE *fp;
  57. #else
  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) != OK)
  85. usage (_("Incorrect arguments supplied\n"));
  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]%*[:] %llu %llu %llu", &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 [%llu (%d%%)]", status, dskfree, 100 - percent);
  104. }
  105. }
  106. else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFre]%*[:] %llu %*[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", "%d%*s %d");
  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=%d, used=%d, free=%d\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=%d, free=%d\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 [%llu (%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 = tbl->swt_ent[i].ste_pages / SWAP_CONVERSION;
  224. dskfree = tbl->swt_ent[i].ste_free / SWAP_CONVERSION;
  225. dskused = ( dsktotal - dskfree );
  226. if(allswaps && dsktotal > 0){
  227. percent = 100 * (((double) dskused) / ((double) dsktotal));
  228. result = max_state (result, check_swap (percent, dskfree));
  229. if (verbose) {
  230. asprintf (&status, "%s [%d (%d%%)]", status, (int)dskfree, 100 - percent);
  231. }
  232. }
  233. total_swap += dsktotal;
  234. free_swap += dskfree;
  235. used_swap += dskused;
  236. }
  237. /* and clean up after ourselves */
  238. for(i=0;i<nswaps;i++){
  239. free(tbl->swt_ent[i].ste_path);
  240. }
  241. free(tbl);
  242. # else
  243. # ifdef CHECK_SWAP_SWAPCTL_BSD
  244. /* get the number of active swap devices */
  245. nswaps=swapctl(SWAP_NSWAP, NULL, 0);
  246. /* initialize swap table + entries */
  247. ent=(struct swapent*)malloc(sizeof(struct swapent)*nswaps);
  248. /* and now, tally 'em up */
  249. swapctl_res=swapctl(SWAP_STATS, ent, nswaps);
  250. if(swapctl_res < 0){
  251. perror("swapctl failed: ");
  252. result = STATE_WARNING;
  253. }
  254. for(i=0;i<nswaps;i++){
  255. dsktotal = ent->se_nblks / conv_factor;
  256. dskused = ent->se_inuse / conv_factor;
  257. dskfree = ( dsktotal - dskused );
  258. if(allswaps && dsktotal > 0){
  259. percent = 100 * (((double) dskused) / ((double) dsktotal));
  260. result = max_state (result, check_swap (percent, dskfree));
  261. if (verbose) {
  262. asprintf (&status, "%s [%d (%d%%)]", status, (int)dskfree, 100 - percent);
  263. }
  264. }
  265. total_swap += dsktotal;
  266. free_swap += dskfree;
  267. used_swap += dskused;
  268. }
  269. /* and clean up after ourselves */
  270. free(ent);
  271. # endif /* CHECK_SWAP_SWAPCTL_BSD */
  272. # endif /* CHECK_SWAP_SWAPCTL_SVR4 */
  273. # endif /* HAVE_SWAP */
  274. #endif /* HAVE_PROC_MEMINFO */
  275. percent_used = 100 * ((double) used_swap) / ((double) total_swap);
  276. result = max_state (result, check_swap (percent_used, free_swap));
  277. /* broken into two steps because of funkiness with builtin asprintf */
  278. asprintf (&tmp_status, _(" %d%% free (%llu MB out of %llu MB)"),
  279. (100 - percent_used), free_swap, total_swap);
  280. asprintf (&status, "%s%s", tmp_status, status);
  281. asprintf (&perf, "%s", perfdata ("swap", (long) free_swap, "MB",
  282. TRUE, (long) max (warn_size/1024, warn_percent/100.0*total_swap),
  283. TRUE, (long) max (crit_size/1024, crit_percent/100.0*total_swap),
  284. TRUE, 0,
  285. TRUE, (long) total_swap));
  286. printf ("SWAP %s:%s |%s\n", state_text (result), status, perf);
  287. return result;
  288. }
  289. int
  290. check_swap (int usp, long unsigned int free_swap)
  291. {
  292. int result = STATE_UNKNOWN;
  293. free_swap = free_swap * 1024; /* Convert back to bytes as warn and crit specified in bytes */
  294. if (usp >= 0 && crit_percent != 0 && usp >= (100.0 - crit_percent))
  295. result = STATE_CRITICAL;
  296. else if (crit_size > 0 && free_swap <= crit_size)
  297. result = STATE_CRITICAL;
  298. else if (usp >= 0 && warn_percent != 0 && usp >= (100.0 - warn_percent))
  299. result = STATE_WARNING;
  300. else if (warn_size > 0 && free_swap <= warn_size)
  301. result = STATE_WARNING;
  302. else if (usp >= 0.0)
  303. result = STATE_OK;
  304. return result;
  305. }
  306. /* process command-line arguments */
  307. int
  308. process_arguments (int argc, char **argv)
  309. {
  310. int c = 0; /* option character */
  311. int option = 0;
  312. static struct option longopts[] = {
  313. {"warning", required_argument, 0, 'w'},
  314. {"critical", required_argument, 0, 'c'},
  315. {"allswaps", no_argument, 0, 'a'},
  316. {"verbose", no_argument, 0, 'v'},
  317. {"version", no_argument, 0, 'V'},
  318. {"help", no_argument, 0, 'h'},
  319. {0, 0, 0, 0}
  320. };
  321. if (argc < 2)
  322. return ERROR;
  323. while (1) {
  324. c = getopt_long (argc, argv, "+?Vvhac:w:", longopts, &option);
  325. if (c == -1 || c == EOF)
  326. break;
  327. switch (c) {
  328. case 'w': /* warning size threshold */
  329. if (is_intnonneg (optarg)) {
  330. warn_size = atoi (optarg);
  331. break;
  332. }
  333. else if (strstr (optarg, ",") &&
  334. strstr (optarg, "%") &&
  335. sscanf (optarg, "%llu,%d%%", &warn_size, &warn_percent) == 2) {
  336. break;
  337. }
  338. else if (strstr (optarg, "%") &&
  339. sscanf (optarg, "%d%%", &warn_percent) == 1) {
  340. break;
  341. }
  342. else {
  343. usage (_("Warning threshold must be integer or percentage!\n"));
  344. }
  345. case 'c': /* critical size threshold */
  346. if (is_intnonneg (optarg)) {
  347. crit_size = atoi (optarg);
  348. break;
  349. }
  350. else if (strstr (optarg, ",") &&
  351. strstr (optarg, "%") &&
  352. sscanf (optarg, "%llu,%d%%", &crit_size, &crit_percent) == 2) {
  353. break;
  354. }
  355. else if (strstr (optarg, "%") &&
  356. sscanf (optarg, "%d%%", &crit_percent) == 1) {
  357. break;
  358. }
  359. else {
  360. usage (_("Critical threshold must be integer or percentage!\n"));
  361. }
  362. case 'a': /* all swap */
  363. allswaps = TRUE;
  364. break;
  365. case 'v': /* verbose */
  366. verbose++;
  367. break;
  368. case 'V': /* version */
  369. print_revision (progname, revision);
  370. exit (STATE_OK);
  371. case 'h': /* help */
  372. print_help ();
  373. exit (STATE_OK);
  374. case '?': /* error */
  375. printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
  376. print_usage ();
  377. exit (STATE_UNKNOWN);
  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 = 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. usage
  408. (_("Warning percentage should be more than critical percentage\n"));
  409. }
  410. else if (warn_size < crit_size) {
  411. usage
  412. (_("Warning free space should be more than critical free space\n"));
  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 server.\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 (_("Usage:\n\
  449. %s [-av] -w <percent_free>%% -c <percent_free>%%\n\
  450. %s [-av] -w <bytes_free> -c <bytes_free>\n\
  451. %s (-h | --help) for detailed help\n\
  452. %s (-V | --version) for version information\n"),
  453. progname, progname, progname, progname);
  454. }