check_disk.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 REVISION "$Revision$"
  38. #define COPYRIGHT "2000-2002"
  39. int process_arguments (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. const char *progname = "check_disk";
  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. char *exclude_device = "";
  51. int verbose = 0;
  52. int display_mntp = FALSE;
  53. int
  54. main (int argc, char **argv)
  55. {
  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 disk_result = STATE_UNKNOWN;
  62. char *command_line = "";
  63. char input_buffer[MAX_INPUT_BUFFER];
  64. char file_system[MAX_INPUT_BUFFER];
  65. char mntp[MAX_INPUT_BUFFER];
  66. char *output = "";
  67. if (process_arguments (argc, argv) != OK)
  68. usage ("Could not parse arguments\n");
  69. asprintf (&command_line, "%s %s", DF_COMMAND, path);
  70. if (verbose>0)
  71. printf ("%s ==> ", command_line);
  72. child_process = spopen (command_line);
  73. if (child_process == NULL) {
  74. printf ("Could not open pipe: %s\n", command_line);
  75. return STATE_UNKNOWN;
  76. }
  77. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  78. if (child_stderr == NULL) {
  79. printf ("Could not open stderr for %s\n", command_line);
  80. }
  81. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  82. if (!index (input_buffer, '/'))
  83. continue;
  84. if (sscanf (input_buffer, "%s %d %d %d %d%% %s", file_system,
  85. &total_disk, &used_disk, &free_disk, &usp, mntp) == 6 ||
  86. sscanf (input_buffer, "%s %*s %d %d %d %d%% %s", file_system,
  87. &total_disk, &used_disk, &free_disk, &usp, mntp) == 6) {
  88. if (strcmp(exclude_device,file_system) == 0 ||
  89. strcmp(exclude_device,mntp) == 0) {
  90. if (verbose>0)
  91. printf ("ignoring %s.", file_system);
  92. continue;
  93. }
  94. disk_result = check_disk (usp, free_disk);
  95. if (strcmp (file_system, "none") == 0)
  96. strncpy (file_system, mntp, MAX_INPUT_BUFFER-1);
  97. if (disk_result!=STATE_OK || verbose>=0)
  98. asprintf (&output, "%s [%d kB (%d%%) free on %s]", output,
  99. free_disk, 100 - usp, display_mntp ? mntp : file_system);
  100. result = max_state (result, disk_result);
  101. }
  102. else {
  103. printf ("Unable to read output:\n%s\n%s\n", command_line, input_buffer);
  104. return result;
  105. }
  106. }
  107. /* If we get anything on stderr, at least set warning */
  108. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  109. if (result != STATE_CRITICAL) {
  110. result = STATE_WARNING;
  111. }
  112. }
  113. /* close stderr */
  114. if (child_stderr)
  115. (void) fclose (child_stderr);
  116. /* close the pipe */
  117. if (spclose(child_process)!=0 && result!=STATE_CRITICAL)
  118. result = STATE_WARNING;
  119. if (usp < 0)
  120. printf ("Disk \"%s\" not mounted or nonexistant\n", path);
  121. else if (result == STATE_UNKNOWN)
  122. printf ("Unable to read output\n%s\n%s\n", command_line, input_buffer);
  123. else
  124. printf ("DISK %s%s\n", state_text (result), output);
  125. return result;
  126. }
  127. /* process command-line arguments */
  128. int
  129. process_arguments (int argc, char **argv)
  130. {
  131. int c;
  132. #ifdef HAVE_GETOPT_H
  133. int option_index = 0;
  134. static struct option long_options[] = {
  135. {"warning", required_argument, 0, 'w'},
  136. {"critical", required_argument, 0, 'c'},
  137. {"timeout", required_argument, 0, 't'},
  138. {"path", required_argument, 0, 'p'},
  139. {"partition", required_argument, 0, 'p'},
  140. {"verbose", no_argument, 0, 'v'},
  141. {"version", no_argument, 0, 'V'},
  142. {"help", no_argument, 0, 'h'},
  143. {"mountpoint", no_argument, 0, 'm'},
  144. {"exclude_device", required_argument, 0, 'x'},
  145. {"quiet", no_argument, 0, 'q'},
  146. {0, 0, 0, 0}
  147. };
  148. #endif
  149. if (argc < 2)
  150. return ERROR;
  151. for (c = 1; c < argc; c++)
  152. if (strcmp ("-to", argv[c]) == 0)
  153. strcpy (argv[c], "-t");
  154. while (1) {
  155. #ifdef HAVE_GETOPT_H
  156. c =
  157. getopt_long (argc, argv, "+?Vqhvt:c:w:p:x:m", long_options, &option_index);
  158. #else
  159. c = getopt (argc, argv, "+?Vqhvt:c:w:p:x:m");
  160. #endif
  161. if (c == -1 || c == EOF)
  162. break;
  163. switch (c) {
  164. case 'w': /* warning time threshold */
  165. if (is_intnonneg (optarg)) {
  166. w_df = atoi (optarg);
  167. break;
  168. }
  169. else if (strpbrk (optarg, ",:") &&
  170. strstr (optarg, "%") &&
  171. sscanf (optarg, "%d%*[:,]%f%%", &w_df, &w_dfp) == 2) {
  172. break;
  173. }
  174. else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &w_dfp) == 1) {
  175. break;
  176. }
  177. else {
  178. usage ("Warning threshold must be integer or percentage!\n");
  179. }
  180. case 'c': /* critical time threshold */
  181. if (is_intnonneg (optarg)) {
  182. c_df = atoi (optarg);
  183. break;
  184. }
  185. else if (strpbrk (optarg, ",:") &&
  186. strstr (optarg, "%") &&
  187. sscanf (optarg, "%d%*[,:]%f%%", &c_df, &c_dfp) == 2) {
  188. break;
  189. }
  190. else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &c_dfp) == 1) {
  191. break;
  192. }
  193. else {
  194. usage ("Critical threshold must be integer or percentage!\n");
  195. }
  196. case 't': /* timeout period */
  197. if (is_integer (optarg)) {
  198. timeout_interval = atoi (optarg);
  199. break;
  200. }
  201. else {
  202. usage ("Timeout Interval must be an integer!\n");
  203. }
  204. case 'p': /* path or partition */
  205. path = optarg;
  206. break;
  207. case 'v': /* verbose */
  208. verbose++;
  209. break;
  210. case 'q': /* verbose */
  211. verbose--;
  212. break;
  213. case 'm': /* display mountpoint */
  214. display_mntp = TRUE;
  215. break;
  216. case 'x': /* exclude path or partition */
  217. exclude_device = optarg;
  218. break;
  219. case 'V': /* version */
  220. print_revision (progname, REVISION);
  221. exit (STATE_OK);
  222. case 'h': /* help */
  223. print_help ();
  224. exit (STATE_OK);
  225. case '?': /* help */
  226. usage ("check_disk: unrecognized option\n");
  227. break;
  228. }
  229. }
  230. c = optind;
  231. if (w_dfp == -1 && argc > c && is_intnonneg (argv[c]))
  232. w_dfp = (100.0 - atof (argv[c++]));
  233. if (c_dfp == -1 && argc > c && is_intnonneg (argv[c]))
  234. c_dfp = (100.0 - atof (argv[c++]));
  235. if (argc > c && strlen (path) == 0)
  236. path = argv[c++];
  237. return validate_arguments ();
  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 less 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 less 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. " -m, --mountpoint\n"
  304. " Display the mountpoint instead of the partition\n"
  305. " -x, --exclude_device=PATH\n"
  306. " Ignore device (only works if -p unspecified)\n"
  307. " -e, --errors-only\n"
  308. " Display only devices/mountpoints with errors\n"
  309. " -v, --verbose\n"
  310. " Show details for command-line debugging (do not use with nagios server)\n"
  311. " -h, --help\n"
  312. " Print detailed help screen\n"
  313. " -V, --version\n" " Print version information\n\n");
  314. support ();
  315. }
  316. void
  317. print_usage (void)
  318. {
  319. printf
  320. ("Usage: %s -w limit -c limit [-p path | -x device] [-t timeout] [-m] [-e] [--verbose]\n"
  321. " %s (-h|--help)\n"
  322. " %s (-V|--version)\n", progname, progname, progname);
  323. }