check_disk.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. #define REVISION "$Revision$"
  39. #define COPYRIGHT "2000-2002"
  40. int process_arguments (int, char **);
  41. int validate_arguments (void);
  42. int check_disk (int usp, int free_disk);
  43. void print_help (void);
  44. void print_usage (void);
  45. int w_df = -1;
  46. int c_df = -1;
  47. float w_dfp = -1.0;
  48. float c_dfp = -1.0;
  49. char *path = "";
  50. int verbose = FALSE;
  51. int display_mntp = FALSE;
  52. int
  53. main (int argc, char **argv)
  54. {
  55. int len;
  56. int usp = -1;
  57. int total_disk = -1;
  58. int used_disk = -1;
  59. int free_disk = -1;
  60. int result = STATE_UNKNOWN;
  61. int temp_result = STATE_UNKNOWN;
  62. char *command_line = NULL;
  63. char input_buffer[MAX_INPUT_BUFFER] = "";
  64. char file_system[MAX_INPUT_BUFFER] = "";
  65. char mntp[MAX_INPUT_BUFFER] = "";
  66. char outbuf[MAX_INPUT_BUFFER] = "";
  67. char *output = "";
  68. if (process_arguments (argc, argv) != OK)
  69. usage ("Could not parse arguments\n");
  70. asprintf (&command_line, "%s %s", DF_COMMAND, path);
  71. if (verbose)
  72. printf ("%s ==> ", command_line);
  73. child_process = spopen (command_line);
  74. if (child_process == NULL) {
  75. printf ("Could not open pipe: %s\n", command_line);
  76. return STATE_UNKNOWN;
  77. }
  78. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  79. if (child_stderr == NULL) {
  80. printf ("Could not open stderr for %s\n", command_line);
  81. }
  82. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  83. if (!index (input_buffer, '/'))
  84. continue;
  85. if (sscanf
  86. (input_buffer, "%s %d %d %d %d%% %s", file_system, &total_disk,
  87. &used_disk, &free_disk, &usp, &mntp) == 6
  88. || sscanf (input_buffer, "%s %*s %d %d %d %d%% %s", file_system,
  89. &total_disk, &used_disk, &free_disk, &usp, &mntp) == 6) {
  90. asprintf (&output, "%s [%d kB (%d%%) free on %s]", output, free_disk,
  91. 100 - usp, display_mntp ? mntp : file_system);
  92. result = max_state (result, check_disk (usp, free_disk));
  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. if( !( result == STATE_CRITICAL) ) {
  103. result = STATE_WARNING;
  104. }
  105. /* close stderr */
  106. (void) fclose (child_stderr);
  107. /* close the pipe */
  108. if (spclose (child_process))
  109. /*result = max (result, STATE_WARNING); */
  110. if( !( result == STATE_CRITICAL) ) {
  111. result = STATE_WARNING;
  112. }
  113. if (usp < 0)
  114. printf ("Disk \"%s\" not mounted or nonexistant\n", path);
  115. else if (result == STATE_UNKNOWN)
  116. printf ("Unable to read output\n%s\n%s\n", command_line, input_buffer);
  117. else
  118. printf ("DISK %s -%s\n", state_text (result), output);
  119. return result;
  120. }
  121. /* process command-line arguments */
  122. int
  123. process_arguments (int argc, char **argv)
  124. {
  125. int c;
  126. #ifdef HAVE_GETOPT_H
  127. int option_index = 0;
  128. static struct option long_options[] = {
  129. {"warning", required_argument, 0, 'w'},
  130. {"critical", required_argument, 0, 'c'},
  131. {"timeout", required_argument, 0, 't'},
  132. {"path", required_argument, 0, 'p'},
  133. {"partition", required_argument, 0, 'p'},
  134. {"verbose", no_argument, 0, 'v'},
  135. {"version", no_argument, 0, 'V'},
  136. {"help", no_argument, 0, 'h'},
  137. {"mountpoint", no_argument, 0, 'm'},
  138. {0, 0, 0, 0}
  139. };
  140. #endif
  141. if (argc < 2)
  142. return ERROR;
  143. for (c = 1; c < argc; c++)
  144. if (strcmp ("-to", argv[c]) == 0)
  145. strcpy (argv[c], "-t");
  146. while (1) {
  147. #ifdef HAVE_GETOPT_H
  148. c =
  149. getopt_long (argc, argv, "Vhvt:c:w:p:m", long_options, &option_index);
  150. #else
  151. c = getopt (argc, argv, "Vhvt:c:w:p:m");
  152. #endif
  153. if (c == -1 || c == EOF)
  154. break;
  155. switch (c) {
  156. case 'w': /* warning time threshold */
  157. if (is_intnonneg (optarg)) {
  158. w_df = atoi (optarg);
  159. break;
  160. }
  161. else if (strpbrk (optarg, ",:") &&
  162. strstr (optarg, "%") &&
  163. sscanf (optarg, "%d%*[:,]%f%%", &w_df, &w_dfp) == 2) {
  164. break;
  165. }
  166. else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &w_dfp) == 1) {
  167. break;
  168. }
  169. else {
  170. usage ("Warning threshold must be integer or percentage!\n");
  171. }
  172. case 'c': /* critical time threshold */
  173. if (is_intnonneg (optarg)) {
  174. c_df = atoi (optarg);
  175. break;
  176. }
  177. else if (strpbrk (optarg, ",:") &&
  178. strstr (optarg, "%") &&
  179. sscanf (optarg, "%d%*[,:]%f%%", &c_df, &c_dfp) == 2) {
  180. break;
  181. }
  182. else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &c_dfp) == 1) {
  183. break;
  184. }
  185. else {
  186. usage ("Critical threshold must be integer or percentage!\n");
  187. }
  188. case 't': /* timeout period */
  189. if (is_integer (optarg)) {
  190. timeout_interval = atoi (optarg);
  191. break;
  192. }
  193. else {
  194. usage ("Timeout Interval must be an integer!\n");
  195. }
  196. case 'p': /* path or partition */
  197. path = optarg;
  198. break;
  199. case 'v': /* verbose */
  200. verbose = TRUE;
  201. break;
  202. case 'm': /* display mountpoint */
  203. display_mntp = TRUE;
  204. break;
  205. case 'V': /* version */
  206. print_revision (PROGNAME, REVISION);
  207. exit (STATE_OK);
  208. case 'h': /* help */
  209. print_help ();
  210. exit (STATE_OK);
  211. case '?': /* help */
  212. usage ("check_disk: unrecognized option\n");
  213. break;
  214. }
  215. }
  216. c = optind;
  217. if (w_dfp == -1 && argc > c && is_intnonneg (argv[c]))
  218. w_dfp = (100.0 - atof (argv[c++]));
  219. if (c_dfp == -1 && argc > c && is_intnonneg (argv[c]))
  220. c_dfp = (100.0 - atof (argv[c++]));
  221. if (argc > c && strlen (path) == 0)
  222. path = argv[c++];
  223. return validate_arguments ();
  224. }
  225. int
  226. validate_arguments ()
  227. {
  228. if (w_df < 0 && c_df < 0 && w_dfp < 0 && c_dfp < 0) {
  229. printf ("INPUT ERROR: Unable to parse command line\n");
  230. return ERROR;
  231. }
  232. else if ((w_dfp >= 0 || c_dfp >= 0)
  233. && (w_dfp < 0 || c_dfp < 0 || w_dfp > 100 || c_dfp > 100
  234. || c_dfp > w_dfp)) {
  235. printf
  236. ("INPUT ERROR: C_DFP (%f) should be less than W_DFP (%f) and both should be between zero and 100 percent, inclusive\n",
  237. c_dfp, w_dfp);
  238. return ERROR;
  239. }
  240. else if ((w_df > 0 || c_df > 0) && (w_df < 0 || c_df < 0 || c_df > w_df)) {
  241. printf
  242. ("INPUT ERROR: C_DF (%d) should be less than W_DF (%d) and both should be greater than zero\n",
  243. c_df, w_df);
  244. return ERROR;
  245. }
  246. else {
  247. return OK;
  248. }
  249. }
  250. int
  251. check_disk (usp, free_disk)
  252. {
  253. int result = STATE_UNKNOWN;
  254. /* check the percent used space against thresholds */
  255. if (usp >= 0 && usp >= (100.0 - c_dfp))
  256. result = STATE_CRITICAL;
  257. else if (c_df >= 0 && free_disk <= c_df)
  258. result = STATE_CRITICAL;
  259. else if (usp >= 0 && usp >= (100.0 - w_dfp))
  260. result = STATE_WARNING;
  261. else if (w_df >= 0 && free_disk <= w_df)
  262. result = STATE_WARNING;
  263. else if (usp >= 0.0)
  264. result = STATE_OK;
  265. return result;
  266. }
  267. void
  268. print_help (void)
  269. {
  270. print_revision (PROGNAME, REVISION);
  271. printf
  272. ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
  273. "This plugin will check the percent of used disk space on a mounted\n"
  274. "file system and generate an alert if percentage is above one of the\n"
  275. "threshold values.\n\n");
  276. print_usage ();
  277. printf
  278. ("\nOptions:\n"
  279. " -w, --warning=INTEGER\n"
  280. " Exit with WARNING status if less than INTEGER kilobytes of disk are free\n"
  281. " -w, --warning=PERCENT%%\n"
  282. " Exit with WARNING status if more than PERCENT of disk space is free\n"
  283. " -c, --critical=INTEGER\n"
  284. " Exit with CRITICAL status if less than INTEGER kilobytes of disk are free\n"
  285. " -c, --critical=PERCENT%%\n"
  286. " Exit with CRITCAL status if more than PERCENT of disk space is free\n"
  287. " -p, --path=PATH, --partition=PARTTION\n"
  288. " Path or partition (checks all mounted partitions if unspecified)\n"
  289. " -m, --mountpoint\n"
  290. " Display the mountpoint instead of the partition\n"
  291. " -v, --verbose\n"
  292. " Show details for command-line debugging (do not use with nagios server)\n"
  293. " -h, --help\n"
  294. " Print detailed help screen\n"
  295. " -V, --version\n" " Print version information\n\n");
  296. support ();
  297. }
  298. void
  299. print_usage (void)
  300. {
  301. printf
  302. ("Usage: %s -w limit -c limit [-p path] [-t timeout] [-m] [--verbose]\n"
  303. " %s (-h|--help)\n"
  304. " %s (-V|--version)\n", PROGNAME, PROGNAME, PROGNAME);
  305. }