check_swap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 = 200;
  40. int crit_percent = 200;
  41. long unsigned int warn_size = 0;
  42. long unsigned int crit_size = 0;
  43. int verbose;
  44. int allswaps;
  45. int
  46. main (int argc, char **argv)
  47. {
  48. int percent_used, percent;
  49. long unsigned int total_swap = 0, used_swap = 0, free_swap = 0;
  50. long unsigned int dsktotal, dskused, dskfree;
  51. int result = STATE_OK;
  52. char input_buffer[MAX_INPUT_BUFFER];
  53. #ifdef HAVE_PROC_MEMINFO
  54. FILE *fp;
  55. #else
  56. # ifdef HAVE_SWAP
  57. int conv_factor; /* Convert to MBs */
  58. char *temp_buffer;
  59. char *swap_command;
  60. char *swap_format;
  61. # endif
  62. #endif
  63. char str[32];
  64. char *status;
  65. setlocale (LC_ALL, "");
  66. bindtextdomain (PACKAGE, LOCALEDIR);
  67. textdomain (PACKAGE);
  68. status = strdup("");
  69. if (process_arguments (argc, argv) != OK)
  70. usage (_("Invalid command arguments supplied\n"));
  71. #ifdef HAVE_PROC_MEMINFO
  72. fp = fopen (PROC_MEMINFO, "r");
  73. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  74. if (sscanf (input_buffer, " %s %lu %lu %lu", str, &dsktotal, &dskused, &dskfree) == 4 &&
  75. strstr (str, "Swap")) {
  76. dsktotal = dsktotal / 1048576;
  77. dskused = dskused / 1048576;
  78. dskfree = dskfree / 1048576;
  79. total_swap += dsktotal;
  80. used_swap += dskused;
  81. free_swap += dskfree;
  82. if (allswaps) {
  83. percent = 100 * (((double) dskused) / ((double) dsktotal));
  84. result = max_state (result, check_swap (percent, dskfree));
  85. if (verbose)
  86. asprintf (&status, "%s [%lu (%d%%)]", status, dskfree, 100 - percent);
  87. }
  88. }
  89. }
  90. fclose(fp);
  91. #else
  92. # ifdef HAVE_SWAP
  93. if (!allswaps) {
  94. #ifdef _AIX
  95. asprintf(&swap_command, "%s", "/usr/sbin/lsps -s");
  96. asprintf(&swap_format, "%s", "%d%*s %d");
  97. conv_factor = 1;
  98. #else
  99. # ifdef sun
  100. asprintf(&swap_command, "%s", "/usr/sbin/swap -s");
  101. asprintf(&swap_format, "%s", "%*s %*dk %*s %*s + %*dk %*s = %dk %*s %dk %*s");
  102. conv_factor = 2048;
  103. # endif
  104. #endif
  105. } else {
  106. asprintf(&swap_command, "%s", SWAP_COMMAND);
  107. asprintf(&swap_format, "%s", SWAP_FORMAT);
  108. conv_factor = SWAP_CONVERSION;
  109. }
  110. if (verbose >= 2)
  111. printf (_("Command: %s\n"), swap_command);
  112. if (verbose >= 3)
  113. printf (_("Format: %s\n"), swap_format);
  114. child_process = spopen (swap_command);
  115. if (child_process == NULL) {
  116. printf (_("Could not open pipe: %s\n"), swap_command);
  117. return STATE_UNKNOWN;
  118. }
  119. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  120. if (child_stderr == NULL)
  121. printf (_("Could not open stderr for %s\n"), swap_command);
  122. sprintf (str, "%s", "");
  123. /* read 1st line */
  124. fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
  125. if (strcmp (swap_format, "") == 0) {
  126. temp_buffer = strtok (input_buffer, " \n");
  127. while (temp_buffer) {
  128. if (strstr (temp_buffer, "blocks"))
  129. sprintf (str, "%s %s", str, "%f");
  130. else if (strstr (temp_buffer, "dskfree"))
  131. sprintf (str, "%s %s", str, "%f");
  132. else
  133. sprintf (str, "%s %s", str, "%*s");
  134. temp_buffer = strtok (NULL, " \n");
  135. }
  136. }
  137. if (!allswaps) {
  138. #ifdef _AIX
  139. fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); /* Ignore first line */
  140. sscanf (input_buffer, swap_format, &total_swap, &used_swap);
  141. free_swap = total_swap * (100 - used_swap) /100;
  142. used_swap = total_swap - free_swap;
  143. if (verbose >= 3)
  144. printf (_("total=%d, used=%d, free=%d\n"), total_swap, used_swap, free_swap);
  145. #else
  146. # ifdef sun
  147. sscanf (input_buffer, swap_format, &used_swap, &free_swap);
  148. used_swap = used_swap / 1024;
  149. free_swap = free_swap / 1024;
  150. total_swap = used_swap + free_swap;
  151. # endif
  152. #endif
  153. } else {
  154. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  155. sscanf (input_buffer, swap_format, &dsktotal, &dskfree);
  156. dsktotal = dsktotal / conv_factor;
  157. /* AIX lists percent used, so this converts to dskfree in MBs */
  158. #ifdef _AIX
  159. dskfree = dsktotal * (100 - dskfree) / 100;
  160. #else
  161. dskfree = dskfree / conv_factor;
  162. #endif
  163. if (verbose >= 3)
  164. printf (_("total=%d, free=%d\n"), dsktotal, dskfree);
  165. dskused = dsktotal - dskfree;
  166. total_swap += dsktotal;
  167. used_swap += dskused;
  168. free_swap += dskfree;
  169. if (allswaps) {
  170. percent = 100 * (((double) dskused) / ((double) dsktotal));
  171. result = max_state (result, check_swap (percent, dskfree));
  172. if (verbose)
  173. asprintf (&status, "%s [%lu (%d%%)]", status, dskfree, 100 - percent);
  174. }
  175. }
  176. }
  177. /* If we get anything on STDERR, at least set warning */
  178. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  179. result = max_state (result, STATE_WARNING);
  180. /* close stderr */
  181. (void) fclose (child_stderr);
  182. /* close the pipe */
  183. if (spclose (child_process))
  184. result = max_state (result, STATE_WARNING);
  185. # endif
  186. #endif
  187. percent_used = 100 * ((double) used_swap) / ((double) total_swap);
  188. result = max_state (result, check_swap (percent_used, free_swap));
  189. asprintf (&status, _(" %d%% free (%lu MB out of %lu MB)%s"),
  190. (100 - percent_used), free_swap, total_swap, status);
  191. die (result, "SWAP %s:%s\n", state_text (result), status);
  192. return STATE_UNKNOWN;
  193. }
  194. int
  195. check_swap (int usp, long unsigned int free_swap)
  196. {
  197. int result = STATE_UNKNOWN;
  198. if (usp >= 0 && usp >= (100.0 - crit_percent))
  199. result = STATE_CRITICAL;
  200. else if (crit_size > 0 && free_swap <= crit_size)
  201. result = STATE_CRITICAL;
  202. else if (usp >= 0 && usp >= (100.0 - warn_percent))
  203. result = STATE_WARNING;
  204. else if (warn_size > 0 && free_swap <= warn_size)
  205. result = STATE_WARNING;
  206. else if (usp >= 0.0)
  207. result = STATE_OK;
  208. return result;
  209. }
  210. /* process command-line arguments */
  211. int
  212. process_arguments (int argc, char **argv)
  213. {
  214. int c = 0; /* option character */
  215. int option = 0;
  216. static struct option longopts[] = {
  217. {"warning", required_argument, 0, 'w'},
  218. {"critical", required_argument, 0, 'c'},
  219. {"allswaps", no_argument, 0, 'a'},
  220. {"verbose", no_argument, 0, 'v'},
  221. {"version", no_argument, 0, 'V'},
  222. {"help", no_argument, 0, 'h'},
  223. {0, 0, 0, 0}
  224. };
  225. if (argc < 2)
  226. return ERROR;
  227. while (1) {
  228. c = getopt_long (argc, argv, "+?Vvhac:w:", longopts, &option);
  229. if (c == -1 || c == EOF)
  230. break;
  231. switch (c) {
  232. case 'w': /* warning size threshold */
  233. if (is_intnonneg (optarg)) {
  234. warn_size = atoi (optarg);
  235. break;
  236. }
  237. else if (strstr (optarg, ",") &&
  238. strstr (optarg, "%") &&
  239. sscanf (optarg, "%lu,%d%%", &warn_size, &warn_percent) == 2) {
  240. break;
  241. }
  242. else if (strstr (optarg, "%") &&
  243. sscanf (optarg, "%d%%", &warn_percent) == 1) {
  244. break;
  245. }
  246. else {
  247. usage (_("Warning threshold must be integer or percentage!\n"));
  248. }
  249. case 'c': /* critical size threshold */
  250. if (is_intnonneg (optarg)) {
  251. crit_size = atoi (optarg);
  252. break;
  253. }
  254. else if (strstr (optarg, ",") &&
  255. strstr (optarg, "%") &&
  256. sscanf (optarg, "%lu,%d%%", &crit_size, &crit_percent) == 2) {
  257. break;
  258. }
  259. else if (strstr (optarg, "%") &&
  260. sscanf (optarg, "%d%%", &crit_percent) == 1) {
  261. break;
  262. }
  263. else {
  264. usage (_("Critical threshold must be integer or percentage!\n"));
  265. }
  266. case 'a': /* all swap */
  267. allswaps = TRUE;
  268. break;
  269. case 'v': /* verbose */
  270. verbose++;
  271. break;
  272. case 'V': /* version */
  273. print_revision (progname, revision);
  274. exit (STATE_OK);
  275. case 'h': /* help */
  276. print_help ();
  277. exit (STATE_OK);
  278. case '?': /* help */
  279. usage (_("Invalid argument\n"));
  280. }
  281. }
  282. c = optind;
  283. if (c == argc)
  284. return validate_arguments ();
  285. if (warn_percent > 100 && is_intnonneg (argv[c]))
  286. warn_percent = atoi (argv[c++]);
  287. if (c == argc)
  288. return validate_arguments ();
  289. if (crit_percent > 100 && is_intnonneg (argv[c]))
  290. crit_percent = atoi (argv[c++]);
  291. if (c == argc)
  292. return validate_arguments ();
  293. if (warn_size == 0 && is_intnonneg (argv[c]))
  294. warn_size = atoi (argv[c++]);
  295. if (c == argc)
  296. return validate_arguments ();
  297. if (crit_size == 0 && is_intnonneg (argv[c]))
  298. crit_size = atoi (argv[c++]);
  299. return validate_arguments ();
  300. }
  301. int
  302. validate_arguments (void)
  303. {
  304. if (warn_percent > 100 && crit_percent > 100 && warn_size == 0
  305. && crit_size == 0) {
  306. return ERROR;
  307. }
  308. else if (warn_percent < crit_percent) {
  309. usage
  310. (_("Warning percentage should be more than critical percentage\n"));
  311. }
  312. else if (warn_size < crit_size) {
  313. usage
  314. (_("Warning free space should be more than critical free space\n"));
  315. }
  316. return OK;
  317. }
  318. void
  319. print_help (void)
  320. {
  321. print_revision (progname, revision);
  322. printf (_(COPYRIGHT), copyright, email);
  323. printf (_("Check swap space on local server.\n\n"));
  324. print_usage ();
  325. printf (_(UT_HELP_VRSN));
  326. printf (_("\n\
  327. -w, --warning=INTEGER\n\
  328. Exit with WARNING status if less than INTEGER bytes of swap space are free\n\
  329. -w, --warning=PERCENT%%\n\
  330. Exit with WARNING status if less than PERCENT of swap space has been used\n\
  331. -c, --critical=INTEGER\n\
  332. Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n\
  333. -c, --critical=PERCENT%%\n\
  334. Exit with CRITCAL status if less than PERCENT of swap space has been used\n\
  335. -a, --allswaps\n\
  336. Conduct comparisons for all swap partitions, one by one\n"));
  337. #ifdef sun
  338. printf (_("\n\
  339. On Solaris, if -a specified, uses swap -l, otherwise uses swap -s.\n\
  340. Will be discrepencies because swap -s counts allocated swap and includes\n\
  341. real memory\n"));
  342. #endif
  343. #ifdef _AIX
  344. printf (_("\n\
  345. On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.\n"));
  346. #endif
  347. printf (_(UT_SUPPORT));
  348. }
  349. void
  350. print_usage (void)
  351. {
  352. printf (_("Usage:\n\
  353. %s [-a] -w <used_percentage>%% -c <used_percentage>%%\n\
  354. %s [-a] -w <bytes_free> -c <bytes_free>\n\
  355. %s (-h | --help) for detailed help\n\
  356. %s (-V | --version) for version information\n"),
  357. progname, progname, progname, progname);
  358. }