check_disk.c 10.0 KB

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