check_disk.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. #ifdef _AIX
  38. #pragma alloca
  39. #endif
  40. #if HAVE_INTTYPES_H
  41. # include <inttypes.h>
  42. #endif
  43. #define REVISION "$Revision$"
  44. #define COPYRIGHT "2000-2002"
  45. int process_arguments (int, char **);
  46. int validate_arguments (void);
  47. int check_disk (int usp, int free_disk);
  48. void print_help (void);
  49. void print_usage (void);
  50. const char *progname = "check_disk";
  51. int w_df = -1;
  52. int c_df = -1;
  53. float w_dfp = -1.0;
  54. float c_dfp = -1.0;
  55. char *path = "";
  56. char *exclude_device = "";
  57. int verbose = 0;
  58. int erronly = FALSE;
  59. int display_mntp = FALSE;
  60. int
  61. main (int argc, char **argv)
  62. {
  63. int usp = -1;
  64. int total_disk = -1;
  65. int used_disk = -1;
  66. int free_disk = -1;
  67. int result = STATE_UNKNOWN;
  68. int disk_result = STATE_UNKNOWN;
  69. char *command_line = "";
  70. char input_buffer[MAX_INPUT_BUFFER];
  71. char file_system[MAX_INPUT_BUFFER];
  72. char mntp[MAX_INPUT_BUFFER];
  73. char *output = "";
  74. #ifdef HAVE_STRUCT_STATFS
  75. #ifdef HAVE_SYS_VFS_H
  76. #include <sys/vfs.h>
  77. #else
  78. #include <sys/param.h>
  79. #include <sys/mount.h>
  80. #endif
  81. struct statfs buf;
  82. #endif
  83. if (process_arguments (argc, argv) != OK)
  84. usage ("Could not parse arguments\n");
  85. #ifdef HAVE_STRUCT_STATFS
  86. if (statfs (path, &buf) == -1) {
  87. switch (errno)
  88. {
  89. case ENOTDIR:
  90. terminate (STATE_UNKNOWN, "A component of the path prefix is not a directory.\n");
  91. case ENAMETOOLONG:
  92. terminate (STATE_UNKNOWN, "path is too long.\n");
  93. case ENOENT:
  94. terminate (STATE_UNKNOWN, "The file referred to by path does not exist.\n");
  95. case EACCES:
  96. terminate (STATE_UNKNOWN, "Search permission is denied for a component of the path prefix of path.\n");
  97. case ELOOP:
  98. terminate (STATE_UNKNOWN, "Too many symbolic links were encountered in translating path.\n");
  99. case EFAULT:
  100. terminate (STATE_UNKNOWN, "Buf or path points to an invalid address.\n");
  101. case EIO:
  102. terminate (STATE_UNKNOWN, "An I/O error occurred while reading from or writing to the file system.\n");
  103. case ENOMEM:
  104. terminate (STATE_UNKNOWN, "Insufficient kernel memory was available.\n");
  105. case ENOSYS:
  106. terminate (STATE_UNKNOWN, "The filesystem path is on does not support statfs.\n");
  107. }
  108. }
  109. usp = (buf.f_blocks - buf.f_bavail) / buf.f_blocks;
  110. disk_result = check_disk (usp, buf.f_bavail);
  111. result = disk_result;
  112. asprintf (&output, "%ld of %ld kB free (%ld-byte blocks)",
  113. buf.f_bavail*buf.f_bsize/1024, buf.f_blocks*buf.f_bsize/1024, buf.f_bsize);
  114. #else
  115. asprintf (&command_line, "%s %s", DF_COMMAND, path);
  116. if (verbose>0)
  117. printf ("%s ==> ", command_line);
  118. child_process = spopen (command_line);
  119. if (child_process == NULL) {
  120. printf ("Could not open pipe: %s\n", command_line);
  121. return STATE_UNKNOWN;
  122. }
  123. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  124. if (child_stderr == NULL) {
  125. printf ("Could not open stderr for %s\n", command_line);
  126. }
  127. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  128. if (!index (input_buffer, '/'))
  129. continue;
  130. /* Fixes AIX /proc fs which lists - for size values */
  131. if (strstr (input_buffer, "/proc ") == input_buffer)
  132. continue;
  133. if (sscanf (input_buffer, "%s %d %d %d %d%% %s", file_system,
  134. &total_disk, &used_disk, &free_disk, &usp, mntp) == 6 ||
  135. sscanf (input_buffer, "%s %*s %d %d %d %d%% %s", file_system,
  136. &total_disk, &used_disk, &free_disk, &usp, mntp) == 6) {
  137. if (strcmp(exclude_device,file_system) == 0 ||
  138. strcmp(exclude_device,mntp) == 0) {
  139. if (verbose>0)
  140. printf ("ignoring %s.", file_system);
  141. continue;
  142. }
  143. disk_result = check_disk (usp, free_disk);
  144. if (strcmp (file_system, "none") == 0)
  145. strncpy (file_system, mntp, MAX_INPUT_BUFFER-1);
  146. if (disk_result==STATE_OK && erronly && !verbose)
  147. continue;
  148. if (disk_result!=STATE_OK || verbose>=0)
  149. asprintf (&output, "%s [%d kB (%d%%) free on %s]", output,
  150. free_disk, 100 - usp, display_mntp ? mntp : file_system);
  151. result = max_state (result, disk_result);
  152. }
  153. else {
  154. printf ("Unable to read output:\n%s\n%s\n", command_line, input_buffer);
  155. return result;
  156. }
  157. }
  158. /* If we get anything on stderr, at least set warning */
  159. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  160. if (result != STATE_CRITICAL) {
  161. result = STATE_WARNING;
  162. }
  163. }
  164. /* close stderr */
  165. if (child_stderr)
  166. (void) fclose (child_stderr);
  167. /* close the pipe */
  168. if (spclose(child_process)!=0 && result!=STATE_CRITICAL)
  169. result = STATE_WARNING;
  170. if (usp < 0)
  171. terminate (result, "Disk \"%s\" not mounted or nonexistant\n", path);
  172. else if (result == STATE_UNKNOWN)
  173. terminate (result, "Unable to read output\n%s\n%s\n", command_line, input_buffer);
  174. #endif
  175. terminate (result, "DISK %s %s\n", state_text (result), output);
  176. }
  177. /* process command-line arguments */
  178. int
  179. process_arguments (int argc, char **argv)
  180. {
  181. int c;
  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. {"errors-only", no_argument, 0, 'e'},
  192. {"help", no_argument, 0, 'h'},
  193. {"mountpoint", no_argument, 0, 'm'},
  194. {"exclude_device", required_argument, 0, 'x'},
  195. {"quiet", no_argument, 0, 'q'},
  196. {0, 0, 0, 0}
  197. };
  198. if (argc < 2)
  199. return ERROR;
  200. for (c = 1; c < argc; c++)
  201. if (strcmp ("-to", argv[c]) == 0)
  202. strcpy (argv[c], "-t");
  203. while (1) {
  204. c = getopt_long (argc, argv, "+?Vqhvet:c:w:p:x:m", long_options, &option_index);
  205. if (c == -1 || c == EOF)
  206. break;
  207. switch (c) {
  208. case 'w': /* warning time threshold */
  209. if (is_intnonneg (optarg)) {
  210. w_df = atoi (optarg);
  211. break;
  212. }
  213. else if (strpbrk (optarg, ",:") &&
  214. strstr (optarg, "%") &&
  215. sscanf (optarg, "%d%*[:,]%f%%", &w_df, &w_dfp) == 2) {
  216. break;
  217. }
  218. else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &w_dfp) == 1) {
  219. break;
  220. }
  221. else {
  222. usage ("Warning threshold must be integer or percentage!\n");
  223. }
  224. case 'c': /* critical time threshold */
  225. if (is_intnonneg (optarg)) {
  226. c_df = atoi (optarg);
  227. break;
  228. }
  229. else if (strpbrk (optarg, ",:") &&
  230. strstr (optarg, "%") &&
  231. sscanf (optarg, "%d%*[,:]%f%%", &c_df, &c_dfp) == 2) {
  232. break;
  233. }
  234. else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &c_dfp) == 1) {
  235. break;
  236. }
  237. else {
  238. usage ("Critical threshold must be integer or percentage!\n");
  239. }
  240. case 't': /* timeout period */
  241. if (is_integer (optarg)) {
  242. timeout_interval = atoi (optarg);
  243. break;
  244. }
  245. else {
  246. usage ("Timeout Interval must be an integer!\n");
  247. }
  248. case 'p': /* path or partition */
  249. path = optarg;
  250. break;
  251. case 'v': /* verbose */
  252. verbose++;
  253. break;
  254. case 'q': /* verbose */
  255. verbose--;
  256. break;
  257. case 'e':
  258. erronly = TRUE;
  259. break;
  260. case 'm': /* display mountpoint */
  261. display_mntp = TRUE;
  262. break;
  263. case 'x': /* exclude path or partition */
  264. exclude_device = optarg;
  265. break;
  266. case 'V': /* version */
  267. print_revision (progname, REVISION);
  268. exit (STATE_OK);
  269. case 'h': /* help */
  270. print_help ();
  271. exit (STATE_OK);
  272. case '?': /* help */
  273. usage ("check_disk: unrecognized option\n");
  274. break;
  275. }
  276. }
  277. c = optind;
  278. if (w_dfp == -1 && argc > c && is_intnonneg (argv[c]))
  279. w_dfp = (100.0 - atof (argv[c++]));
  280. if (c_dfp == -1 && argc > c && is_intnonneg (argv[c]))
  281. c_dfp = (100.0 - atof (argv[c++]));
  282. if (argc > c && strlen (path) == 0)
  283. path = argv[c++];
  284. return validate_arguments ();
  285. }
  286. int
  287. validate_arguments ()
  288. {
  289. if (w_df < 0 && c_df < 0 && w_dfp < 0 && c_dfp < 0) {
  290. printf ("INPUT ERROR: Unable to parse command line\n");
  291. return ERROR;
  292. }
  293. else if ((w_dfp >= 0 || c_dfp >= 0)
  294. && (w_dfp < 0 || c_dfp < 0 || w_dfp > 100 || c_dfp > 100
  295. || c_dfp > w_dfp)) {
  296. printf
  297. ("INPUT ERROR: C_DFP (%f) should be less than W_DFP (%f) and both should be between zero and 100 percent, inclusive\n",
  298. c_dfp, w_dfp);
  299. return ERROR;
  300. }
  301. else if ((w_df > 0 || c_df > 0) && (w_df < 0 || c_df < 0 || c_df > w_df)) {
  302. printf
  303. ("INPUT ERROR: C_DF (%d) should be less than W_DF (%d) and both should be greater than zero\n",
  304. c_df, w_df);
  305. return ERROR;
  306. }
  307. else {
  308. return OK;
  309. }
  310. }
  311. int
  312. check_disk (usp, free_disk)
  313. {
  314. int result = STATE_UNKNOWN;
  315. /* check the percent used space against thresholds */
  316. if (usp >= 0 && usp >= (100.0 - c_dfp))
  317. result = STATE_CRITICAL;
  318. else if (c_df >= 0 && free_disk <= c_df)
  319. result = STATE_CRITICAL;
  320. else if (usp >= 0 && usp >= (100.0 - w_dfp))
  321. result = STATE_WARNING;
  322. else if (w_df >= 0 && free_disk <= w_df)
  323. result = STATE_WARNING;
  324. else if (usp >= 0.0)
  325. result = STATE_OK;
  326. return result;
  327. }
  328. void
  329. print_help (void)
  330. {
  331. print_revision (progname, REVISION);
  332. printf
  333. ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
  334. "This plugin will check the percent of used disk space on a mounted\n"
  335. "file system and generate an alert if percentage is above one of the\n"
  336. "threshold values.\n\n");
  337. print_usage ();
  338. printf
  339. ("\nOptions:\n"
  340. " -w, --warning=INTEGER\n"
  341. " Exit with WARNING status if less than INTEGER kilobytes of disk are free\n"
  342. " -w, --warning=PERCENT%%\n"
  343. " Exit with WARNING status if less than PERCENT of disk space is free\n"
  344. " -c, --critical=INTEGER\n"
  345. " Exit with CRITICAL status if less than INTEGER kilobytes of disk are free\n"
  346. " -c, --critical=PERCENT%%\n"
  347. " Exit with CRITCAL status if less than PERCENT of disk space is free\n"
  348. " -p, --path=PATH, --partition=PARTTION\n"
  349. " Path or partition (checks all mounted partitions if unspecified)\n"
  350. " -m, --mountpoint\n"
  351. " Display the mountpoint instead of the partition\n"
  352. " -x, --exclude_device=PATH\n"
  353. " Ignore device (only works if -p unspecified)\n"
  354. " -e, --errors-only\n"
  355. " Display only devices/mountpoints with errors\n"
  356. " -v, --verbose\n"
  357. " Show details for command-line debugging (do not use with nagios server)\n"
  358. " -h, --help\n"
  359. " Print detailed help screen\n"
  360. " -V, --version\n" " Print version information\n\n");
  361. support ();
  362. }
  363. void
  364. print_usage (void)
  365. {
  366. printf
  367. ("Usage: %s -w limit -c limit [-p path | -x device] [-t timeout] [-m] [-e] [--verbose]\n"
  368. " %s (-h|--help)\n"
  369. " %s (-V|--version)\n", progname, progname, progname);
  370. }