check_disk.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /******************************************************************************
  2. *
  3. * CHECK_DISK.C
  4. *
  5. * Program: Disk space plugin for Nagios
  6. * License: GPL
  7. * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
  8. * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
  9. *
  10. * $Id$
  11. *
  12. * Description:
  13. *
  14. * This plugin will use the /bin/df command to check the free space on
  15. * currently mounted filesystems. If the percent used disk space is
  16. * above <c_dfp>, a STATE_CRITICAL is returned. If the percent used
  17. * disk space is above <w_dfp>, a STATE_WARNING is returned. If the
  18. * speicified filesystem cannot be read, a STATE_CRITICAL is returned,
  19. * other errors with reading the output result in a STATE_UNKNOWN
  20. * error.
  21. *
  22. * Notes:
  23. * - IRIX support added by Charlie Cook 4-16-1999
  24. * - Modifications by Karl DeBisschop 1999-11-24
  25. * reformat code to 80 char screen width
  26. * set STATE_WARNING if stderr is written or spclose status set
  27. * set default result to STAT_UNKNOWN
  28. * initailize usp to -1, eliminate 'found' variable
  29. * accept any filename/filesystem
  30. * use sscanf, drop while loop
  31. *
  32. *****************************************************************************/
  33. #include "common.h"
  34. #include "popen.h"
  35. #include "utils.h"
  36. #include <stdarg.h>
  37. #define PROGNAME "check_disk"
  38. int process_arguments (int, char **);
  39. int call_getopt (int, char **);
  40. int validate_arguments (void);
  41. int check_disk (int usp, int free_disk);
  42. void print_help (void);
  43. void print_usage (void);
  44. int w_df = -1;
  45. int c_df = -1;
  46. float w_dfp = -1.0;
  47. float c_dfp = -1.0;
  48. char *path = NULL;
  49. int verbose = FALSE;
  50. int
  51. main (int argc, char **argv)
  52. {
  53. int len;
  54. int usp = -1;
  55. int total_disk = -1;
  56. int used_disk = -1;
  57. int free_disk = -1;
  58. int result = STATE_UNKNOWN;
  59. char *command_line = NULL;
  60. char input_buffer[MAX_INPUT_BUFFER] = "";
  61. char file_system[MAX_INPUT_BUFFER] = "";
  62. char outbuf[MAX_INPUT_BUFFER] = "";
  63. char *output = NULL;
  64. if (process_arguments (argc, argv) != OK)
  65. usage ("Could not parse arguments\n");
  66. command_line = ssprintf (command_line, "%s %s", DF_COMMAND, path);
  67. if (verbose)
  68. printf ("%s ==> ", command_line);
  69. child_process = spopen (command_line);
  70. if (child_process == NULL) {
  71. printf ("Could not open pipe: %s\n", command_line);
  72. return STATE_UNKNOWN;
  73. }
  74. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  75. if (child_stderr == NULL) {
  76. printf ("Could not open stderr for %s\n", command_line);
  77. }
  78. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  79. if (!index (input_buffer, '/'))
  80. continue;
  81. if (sscanf
  82. (input_buffer, "%s %d %d %d %d%%", file_system, &total_disk,
  83. &used_disk, &free_disk, &usp) == 5
  84. || sscanf (input_buffer, "%s %*s %d %d %d %d%%", file_system,
  85. &total_disk, &used_disk, &free_disk, &usp) == 5) {
  86. result = max (result, check_disk (usp, free_disk));
  87. len =
  88. snprintf (outbuf, MAX_INPUT_BUFFER - 1,
  89. " [%d kB (%d%%) free on %s]", free_disk, 100 - usp,
  90. file_system);
  91. outbuf[len] = 0;
  92. output = strscat (output, outbuf);
  93. }
  94. else {
  95. printf ("Unable to read output:\n%s\n%s\n", command_line, input_buffer);
  96. return result;
  97. }
  98. }
  99. /* If we get anything on stderr, at least set warning */
  100. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  101. result = max (result, STATE_WARNING);
  102. /* close stderr */
  103. (void) fclose (child_stderr);
  104. /* close the pipe */
  105. if (spclose (child_process))
  106. result = max (result, STATE_WARNING);
  107. else if (usp < 0)
  108. printf ("Disk %s not mounted or nonexistant\n", argv[3]);
  109. else if (result == STATE_UNKNOWN)
  110. printf ("Unable to read output\n%s\n%s\n", command_line, input_buffer);
  111. else
  112. printf ("DISK %s -%s\n", state_text (result), output);
  113. return result;
  114. }
  115. /* process command-line arguments */
  116. int
  117. process_arguments (int argc, char **argv)
  118. {
  119. int c;
  120. if (argc < 2)
  121. return ERROR;
  122. for (c = 1; c < argc; c++) {
  123. if (strcmp ("-to", argv[c]) == 0) {
  124. strcpy (argv[c], "-t");
  125. }
  126. }
  127. c = 0;
  128. while ((c += (call_getopt (argc - c, &argv[c]))) < argc) {
  129. if (w_dfp == -1 && is_intnonneg (argv[c]))
  130. w_dfp = (100.0 - atof (argv[c]));
  131. else if (c_dfp == -1 && is_intnonneg (argv[c]))
  132. c_dfp = (100.0 - atof (argv[c]));
  133. else if (path == NULL || path[0] == 0)
  134. path = strscpy (path, argv[c]);
  135. }
  136. if (path == NULL) {
  137. path = malloc (1);
  138. if (path == NULL)
  139. terminate (STATE_UNKNOWN, "Could not malloc empty path\n");
  140. path[0] = 0;
  141. }
  142. return validate_arguments ();
  143. }
  144. int
  145. call_getopt (int argc, char **argv)
  146. {
  147. int c, i = 0;
  148. #ifdef HAVE_GETOPT_H
  149. int option_index = 0;
  150. static struct option long_options[] = {
  151. {"warning", required_argument, 0, 'w'},
  152. {"critical", required_argument, 0, 'c'},
  153. {"timeout", required_argument, 0, 't'},
  154. {"path", required_argument, 0, 'p'},
  155. {"partition", required_argument, 0, 'p'},
  156. {"verbose", no_argument, 0, 'v'},
  157. {"version", no_argument, 0, 'V'},
  158. {"help", no_argument, 0, 'h'},
  159. {0, 0, 0, 0}
  160. };
  161. #endif
  162. while (1) {
  163. #ifdef HAVE_GETOPT_H
  164. c =
  165. getopt_long (argc, argv, "+?Vhvt:c:w:p:", long_options, &option_index);
  166. #else
  167. c = getopt (argc, argv, "+?Vhvt:c:w:p:");
  168. #endif
  169. i++;
  170. if (c == -1 || c == EOF || c == 1)
  171. break;
  172. switch (c) {
  173. case 't':
  174. case 'c':
  175. case 'w':
  176. case 'p':
  177. i++;
  178. }
  179. switch (c) {
  180. case 'w': /* warning time threshold */
  181. if (is_intnonneg (optarg)) {
  182. w_df = atoi (optarg);
  183. break;
  184. }
  185. else if (strpbrk (optarg, ",:") &&
  186. strstr (optarg, "%") &&
  187. sscanf (optarg, "%d%*[:,]%f%%", &w_df, &w_dfp) == 2) {
  188. break;
  189. }
  190. else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &w_dfp) == 1) {
  191. break;
  192. }
  193. else {
  194. usage ("Warning threshold must be integer or percentage!\n");
  195. }
  196. case 'c': /* critical time threshold */
  197. if (is_intnonneg (optarg)) {
  198. c_df = atoi (optarg);
  199. break;
  200. }
  201. else if (strpbrk (optarg, ",:") &&
  202. strstr (optarg, "%") &&
  203. sscanf (optarg, "%d%*[,:]%f%%", &c_df, &c_dfp) == 2) {
  204. break;
  205. }
  206. else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &c_dfp) == 1) {
  207. break;
  208. }
  209. else {
  210. usage ("Critical threshold must be integer or percentage!\n");
  211. }
  212. case 't': /* timeout period */
  213. if (is_integer (optarg)) {
  214. timeout_interval = atoi (optarg);
  215. break;
  216. }
  217. else {
  218. usage ("Timeout Interval must be an integer!\n");
  219. }
  220. case 'p': /* path or partition */
  221. path = optarg;
  222. break;
  223. case 'v': /* verbose */
  224. verbose = TRUE;
  225. break;
  226. case 'V': /* version */
  227. print_revision (my_basename (argv[0]), "$Revision$");
  228. exit (STATE_OK);
  229. case 'h': /* help */
  230. print_help ();
  231. exit (STATE_OK);
  232. case '?': /* help */
  233. usage ("check_disk: unrecognized option\n");
  234. break;
  235. }
  236. }
  237. return i;
  238. }
  239. int
  240. validate_arguments ()
  241. {
  242. if (w_df < 0 && c_df < 0 && w_dfp < 0 && c_dfp < 0) {
  243. printf ("INPUT ERROR: Unable to parse command line\n");
  244. return ERROR;
  245. }
  246. else if ((w_dfp >= 0 || c_dfp >= 0)
  247. && (w_dfp < 0 || c_dfp < 0 || w_dfp > 100 || c_dfp > 100
  248. || c_dfp > w_dfp)) {
  249. printf
  250. ("INPUT ERROR: C_DFP (%f) should be less than W_DFP (%f) and both should be between zero and 100 percent, inclusive\n",
  251. c_dfp, w_dfp);
  252. return ERROR;
  253. }
  254. else if ((w_df > 0 || c_df > 0) && (w_df < 0 || c_df < 0 || c_df > w_df)) {
  255. printf
  256. ("INPUT ERROR: C_DF (%d) should be less than W_DF (%d) and both should be greater than zero\n",
  257. c_df, w_df);
  258. return ERROR;
  259. }
  260. else {
  261. return OK;
  262. }
  263. }
  264. int
  265. check_disk (usp, free_disk)
  266. {
  267. int result = STATE_UNKNOWN;
  268. /* check the percent used space against thresholds */
  269. if (usp >= 0 && usp >= (100.0 - c_dfp))
  270. result = STATE_CRITICAL;
  271. else if (c_df >= 0 && free_disk <= c_df)
  272. result = STATE_CRITICAL;
  273. else if (usp >= 0 && usp >= (100.0 - w_dfp))
  274. result = STATE_WARNING;
  275. else if (w_df >= 0 && free_disk <= w_df)
  276. result = STATE_WARNING;
  277. else if (usp >= 0.0)
  278. result = STATE_OK;
  279. return result;
  280. }
  281. void
  282. print_help (void)
  283. {
  284. print_revision (PROGNAME, "$Revision$");
  285. printf
  286. ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
  287. "This plugin will check the percent of used disk space on a mounted\n"
  288. "file system and generate an alert if percentage is above one of the\n"
  289. "threshold values.\n\n");
  290. print_usage ();
  291. printf
  292. ("\nOptions:\n"
  293. " -w, --warning=INTEGER\n"
  294. " Exit with WARNING status if less than INTEGER kilobytes of disk are free\n"
  295. " -w, --warning=PERCENT%%\n"
  296. " Exit with WARNING status if more than PERCENT of disk space is free\n"
  297. " -c, --critical=INTEGER\n"
  298. " Exit with CRITICAL status if less than INTEGER kilobytes of disk are free\n"
  299. " -c, --critical=PERCENT%%\n"
  300. " Exit with CRITCAL status if more than PERCENT of disk space is free\n"
  301. " -p, --path=PATH, --partition=PARTTION\n"
  302. " Path or partition (checks all mounted partitions if unspecified)\n"
  303. " -v, --verbose\n"
  304. " Show details for command-line debugging (do not use with nagios server)\n"
  305. " -h, --help\n"
  306. " Print detailed help screen\n"
  307. " -V, --version\n" " Print version information\n\n");
  308. support ();
  309. }
  310. void
  311. print_usage (void)
  312. {
  313. printf
  314. ("Usage: %s -w limit -c limit [-p path] [-t timeout] [--verbose]\n"
  315. " %s (-h|--help)\n"
  316. " %s (-V|--version)\n", PROGNAME, PROGNAME, PROGNAME);
  317. }