check_disk.c 10 KB

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