check_disk.c 10 KB

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