check_disk.c 9.4 KB

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