check_disk.c 12 KB

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