check_disk.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. * specified 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. /* Fixes AIX /proc fs which lists - for size values */
  85. if (strstr (input_buffer, "/proc ") == input_buffer)
  86. continue;
  87. if (sscanf (input_buffer, "%s %d %d %d %d%% %s", file_system,
  88. &total_disk, &used_disk, &free_disk, &usp, mntp) == 6 ||
  89. sscanf (input_buffer, "%s %*s %d %d %d %d%% %s", file_system,
  90. &total_disk, &used_disk, &free_disk, &usp, mntp) == 6) {
  91. if (strcmp(exclude_device,file_system) == 0 ||
  92. strcmp(exclude_device,mntp) == 0) {
  93. if (verbose>0)
  94. printf ("ignoring %s.", file_system);
  95. continue;
  96. }
  97. disk_result = check_disk (usp, free_disk);
  98. if (strcmp (file_system, "none") == 0)
  99. strncpy (file_system, mntp, MAX_INPUT_BUFFER-1);
  100. if (disk_result!=STATE_OK || verbose>=0)
  101. asprintf (&output, "%s [%d kB (%d%%) free on %s]", output,
  102. free_disk, 100 - usp, display_mntp ? mntp : file_system);
  103. result = max_state (result, disk_result);
  104. }
  105. else {
  106. printf ("Unable to read output:\n%s\n%s\n", command_line, input_buffer);
  107. return result;
  108. }
  109. }
  110. /* If we get anything on stderr, at least set warning */
  111. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  112. if (result != STATE_CRITICAL) {
  113. result = STATE_WARNING;
  114. }
  115. }
  116. /* close stderr */
  117. if (child_stderr)
  118. (void) fclose (child_stderr);
  119. /* close the pipe */
  120. if (spclose(child_process)!=0 && result!=STATE_CRITICAL)
  121. result = STATE_WARNING;
  122. if (usp < 0)
  123. printf ("Disk \"%s\" not mounted or nonexistant\n", path);
  124. else if (result == STATE_UNKNOWN)
  125. printf ("Unable to read output\n%s\n%s\n", command_line, input_buffer);
  126. else
  127. printf ("DISK %s%s\n", state_text (result), output);
  128. return result;
  129. }
  130. /* process command-line arguments */
  131. int
  132. process_arguments (int argc, char **argv)
  133. {
  134. int c;
  135. #ifdef HAVE_GETOPT_H
  136. int option_index = 0;
  137. static struct option long_options[] = {
  138. {"warning", required_argument, 0, 'w'},
  139. {"critical", required_argument, 0, 'c'},
  140. {"timeout", required_argument, 0, 't'},
  141. {"path", required_argument, 0, 'p'},
  142. {"partition", required_argument, 0, 'p'},
  143. {"verbose", no_argument, 0, 'v'},
  144. {"version", no_argument, 0, 'V'},
  145. {"help", no_argument, 0, 'h'},
  146. {"mountpoint", no_argument, 0, 'm'},
  147. {"exclude_device", required_argument, 0, 'x'},
  148. {"quiet", no_argument, 0, 'q'},
  149. {0, 0, 0, 0}
  150. };
  151. #endif
  152. if (argc < 2)
  153. return ERROR;
  154. for (c = 1; c < argc; c++)
  155. if (strcmp ("-to", argv[c]) == 0)
  156. strcpy (argv[c], "-t");
  157. while (1) {
  158. #ifdef HAVE_GETOPT_H
  159. c =
  160. getopt_long (argc, argv, "+?Vqhvt:c:w:p:x:m", long_options, &option_index);
  161. #else
  162. c = getopt (argc, argv, "+?Vqhvt:c:w:p:x:m");
  163. #endif
  164. if (c == -1 || c == EOF)
  165. break;
  166. switch (c) {
  167. case 'w': /* warning time threshold */
  168. if (is_intnonneg (optarg)) {
  169. w_df = atoi (optarg);
  170. break;
  171. }
  172. else if (strpbrk (optarg, ",:") &&
  173. strstr (optarg, "%") &&
  174. sscanf (optarg, "%d%*[:,]%f%%", &w_df, &w_dfp) == 2) {
  175. break;
  176. }
  177. else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &w_dfp) == 1) {
  178. break;
  179. }
  180. else {
  181. usage ("Warning threshold must be integer or percentage!\n");
  182. }
  183. case 'c': /* critical time threshold */
  184. if (is_intnonneg (optarg)) {
  185. c_df = atoi (optarg);
  186. break;
  187. }
  188. else if (strpbrk (optarg, ",:") &&
  189. strstr (optarg, "%") &&
  190. sscanf (optarg, "%d%*[,:]%f%%", &c_df, &c_dfp) == 2) {
  191. break;
  192. }
  193. else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &c_dfp) == 1) {
  194. break;
  195. }
  196. else {
  197. usage ("Critical threshold must be integer or percentage!\n");
  198. }
  199. case 't': /* timeout period */
  200. if (is_integer (optarg)) {
  201. timeout_interval = atoi (optarg);
  202. break;
  203. }
  204. else {
  205. usage ("Timeout Interval must be an integer!\n");
  206. }
  207. case 'p': /* path or partition */
  208. path = optarg;
  209. break;
  210. case 'v': /* verbose */
  211. verbose++;
  212. break;
  213. case 'q': /* verbose */
  214. verbose--;
  215. break;
  216. case 'm': /* display mountpoint */
  217. display_mntp = TRUE;
  218. break;
  219. case 'x': /* exclude path or partition */
  220. exclude_device = optarg;
  221. break;
  222. case 'V': /* version */
  223. print_revision (progname, REVISION);
  224. exit (STATE_OK);
  225. case 'h': /* help */
  226. print_help ();
  227. exit (STATE_OK);
  228. case '?': /* help */
  229. usage ("check_disk: unrecognized option\n");
  230. break;
  231. }
  232. }
  233. c = optind;
  234. if (w_dfp == -1 && argc > c && is_intnonneg (argv[c]))
  235. w_dfp = (100.0 - atof (argv[c++]));
  236. if (c_dfp == -1 && argc > c && is_intnonneg (argv[c]))
  237. c_dfp = (100.0 - atof (argv[c++]));
  238. if (argc > c && strlen (path) == 0)
  239. path = argv[c++];
  240. return validate_arguments ();
  241. }
  242. int
  243. validate_arguments ()
  244. {
  245. if (w_df < 0 && c_df < 0 && w_dfp < 0 && c_dfp < 0) {
  246. printf ("INPUT ERROR: Unable to parse command line\n");
  247. return ERROR;
  248. }
  249. else if ((w_dfp >= 0 || c_dfp >= 0)
  250. && (w_dfp < 0 || c_dfp < 0 || w_dfp > 100 || c_dfp > 100
  251. || c_dfp > w_dfp)) {
  252. printf
  253. ("INPUT ERROR: C_DFP (%f) should be less than W_DFP (%f) and both should be between zero and 100 percent, inclusive\n",
  254. c_dfp, w_dfp);
  255. return ERROR;
  256. }
  257. else if ((w_df > 0 || c_df > 0) && (w_df < 0 || c_df < 0 || c_df > w_df)) {
  258. printf
  259. ("INPUT ERROR: C_DF (%d) should be less than W_DF (%d) and both should be greater than zero\n",
  260. c_df, w_df);
  261. return ERROR;
  262. }
  263. else {
  264. return OK;
  265. }
  266. }
  267. int
  268. check_disk (usp, free_disk)
  269. {
  270. int result = STATE_UNKNOWN;
  271. /* check the percent used space against thresholds */
  272. if (usp >= 0 && usp >= (100.0 - c_dfp))
  273. result = STATE_CRITICAL;
  274. else if (c_df >= 0 && free_disk <= c_df)
  275. result = STATE_CRITICAL;
  276. else if (usp >= 0 && usp >= (100.0 - w_dfp))
  277. result = STATE_WARNING;
  278. else if (w_df >= 0 && free_disk <= w_df)
  279. result = STATE_WARNING;
  280. else if (usp >= 0.0)
  281. result = STATE_OK;
  282. return result;
  283. }
  284. void
  285. print_help (void)
  286. {
  287. print_revision (progname, REVISION);
  288. printf
  289. ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
  290. "This plugin will check the percent of used disk space on a mounted\n"
  291. "file system and generate an alert if percentage is above one of the\n"
  292. "threshold values.\n\n");
  293. print_usage ();
  294. printf
  295. ("\nOptions:\n"
  296. " -w, --warning=INTEGER\n"
  297. " Exit with WARNING status if less than INTEGER kilobytes of disk are free\n"
  298. " -w, --warning=PERCENT%%\n"
  299. " Exit with WARNING status if less than PERCENT of disk space is free\n"
  300. " -c, --critical=INTEGER\n"
  301. " Exit with CRITICAL status if less than INTEGER kilobytes of disk are free\n"
  302. " -c, --critical=PERCENT%%\n"
  303. " Exit with CRITCAL status if less than PERCENT of disk space is free\n"
  304. " -p, --path=PATH, --partition=PARTTION\n"
  305. " Path or partition (checks all mounted partitions if unspecified)\n"
  306. " -m, --mountpoint\n"
  307. " Display the mountpoint instead of the partition\n"
  308. " -x, --exclude_device=PATH\n"
  309. " Ignore device (only works if -p unspecified)\n"
  310. " -e, --errors-only\n"
  311. " Display only devices/mountpoints with errors\n"
  312. " -v, --verbose\n"
  313. " Show details for command-line debugging (do not use with nagios server)\n"
  314. " -h, --help\n"
  315. " Print detailed help screen\n"
  316. " -V, --version\n" " Print version information\n\n");
  317. support ();
  318. }
  319. void
  320. print_usage (void)
  321. {
  322. printf
  323. ("Usage: %s -w limit -c limit [-p path | -x device] [-t timeout] [-m] [-e] [--verbose]\n"
  324. " %s (-h|--help)\n"
  325. " %s (-V|--version)\n", progname, progname, progname);
  326. }