check_disk.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. #ifdef HAVE_GETOPT_H
  183. int option_index = 0;
  184. static struct option long_options[] = {
  185. {"warning", required_argument, 0, 'w'},
  186. {"critical", required_argument, 0, 'c'},
  187. {"timeout", required_argument, 0, 't'},
  188. {"path", required_argument, 0, 'p'},
  189. {"partition", required_argument, 0, 'p'},
  190. {"verbose", no_argument, 0, 'v'},
  191. {"version", no_argument, 0, 'V'},
  192. {"errors-only", no_argument, 0, 'e'},
  193. {"help", no_argument, 0, 'h'},
  194. {"mountpoint", no_argument, 0, 'm'},
  195. {"exclude_device", required_argument, 0, 'x'},
  196. {"quiet", no_argument, 0, 'q'},
  197. {0, 0, 0, 0}
  198. };
  199. #endif
  200. if (argc < 2)
  201. return ERROR;
  202. for (c = 1; c < argc; c++)
  203. if (strcmp ("-to", argv[c]) == 0)
  204. strcpy (argv[c], "-t");
  205. while (1) {
  206. #ifdef HAVE_GETOPT_H
  207. c =
  208. getopt_long (argc, argv, "+?Vqhvet:c:w:p:x:m", long_options, &option_index);
  209. #else
  210. c = getopt (argc, argv, "+?Vqhvet:c:w:p:x:m");
  211. #endif
  212. if (c == -1 || c == EOF)
  213. break;
  214. switch (c) {
  215. case 'w': /* warning time threshold */
  216. if (is_intnonneg (optarg)) {
  217. w_df = atoi (optarg);
  218. break;
  219. }
  220. else if (strpbrk (optarg, ",:") &&
  221. strstr (optarg, "%") &&
  222. sscanf (optarg, "%d%*[:,]%f%%", &w_df, &w_dfp) == 2) {
  223. break;
  224. }
  225. else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &w_dfp) == 1) {
  226. break;
  227. }
  228. else {
  229. usage ("Warning threshold must be integer or percentage!\n");
  230. }
  231. case 'c': /* critical time threshold */
  232. if (is_intnonneg (optarg)) {
  233. c_df = atoi (optarg);
  234. break;
  235. }
  236. else if (strpbrk (optarg, ",:") &&
  237. strstr (optarg, "%") &&
  238. sscanf (optarg, "%d%*[,:]%f%%", &c_df, &c_dfp) == 2) {
  239. break;
  240. }
  241. else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &c_dfp) == 1) {
  242. break;
  243. }
  244. else {
  245. usage ("Critical threshold must be integer or percentage!\n");
  246. }
  247. case 't': /* timeout period */
  248. if (is_integer (optarg)) {
  249. timeout_interval = atoi (optarg);
  250. break;
  251. }
  252. else {
  253. usage ("Timeout Interval must be an integer!\n");
  254. }
  255. case 'p': /* path or partition */
  256. path = optarg;
  257. break;
  258. case 'v': /* verbose */
  259. verbose++;
  260. break;
  261. case 'q': /* verbose */
  262. verbose--;
  263. break;
  264. case 'e':
  265. erronly = TRUE;
  266. break;
  267. case 'm': /* display mountpoint */
  268. display_mntp = TRUE;
  269. break;
  270. case 'x': /* exclude path or partition */
  271. exclude_device = optarg;
  272. break;
  273. case 'V': /* version */
  274. print_revision (progname, REVISION);
  275. exit (STATE_OK);
  276. case 'h': /* help */
  277. print_help ();
  278. exit (STATE_OK);
  279. case '?': /* help */
  280. usage ("check_disk: unrecognized option\n");
  281. break;
  282. }
  283. }
  284. c = optind;
  285. if (w_dfp == -1 && argc > c && is_intnonneg (argv[c]))
  286. w_dfp = (100.0 - atof (argv[c++]));
  287. if (c_dfp == -1 && argc > c && is_intnonneg (argv[c]))
  288. c_dfp = (100.0 - atof (argv[c++]));
  289. if (argc > c && strlen (path) == 0)
  290. path = argv[c++];
  291. return validate_arguments ();
  292. }
  293. int
  294. validate_arguments ()
  295. {
  296. if (w_df < 0 && c_df < 0 && w_dfp < 0 && c_dfp < 0) {
  297. printf ("INPUT ERROR: Unable to parse command line\n");
  298. return ERROR;
  299. }
  300. else if ((w_dfp >= 0 || c_dfp >= 0)
  301. && (w_dfp < 0 || c_dfp < 0 || w_dfp > 100 || c_dfp > 100
  302. || c_dfp > w_dfp)) {
  303. printf
  304. ("INPUT ERROR: C_DFP (%f) should be less than W_DFP (%f) and both should be between zero and 100 percent, inclusive\n",
  305. c_dfp, w_dfp);
  306. return ERROR;
  307. }
  308. else if ((w_df > 0 || c_df > 0) && (w_df < 0 || c_df < 0 || c_df > w_df)) {
  309. printf
  310. ("INPUT ERROR: C_DF (%d) should be less than W_DF (%d) and both should be greater than zero\n",
  311. c_df, w_df);
  312. return ERROR;
  313. }
  314. else {
  315. return OK;
  316. }
  317. }
  318. int
  319. check_disk (usp, free_disk)
  320. {
  321. int result = STATE_UNKNOWN;
  322. /* check the percent used space against thresholds */
  323. if (usp >= 0 && usp >= (100.0 - c_dfp))
  324. result = STATE_CRITICAL;
  325. else if (c_df >= 0 && free_disk <= c_df)
  326. result = STATE_CRITICAL;
  327. else if (usp >= 0 && usp >= (100.0 - w_dfp))
  328. result = STATE_WARNING;
  329. else if (w_df >= 0 && free_disk <= w_df)
  330. result = STATE_WARNING;
  331. else if (usp >= 0.0)
  332. result = STATE_OK;
  333. return result;
  334. }
  335. void
  336. print_help (void)
  337. {
  338. print_revision (progname, REVISION);
  339. printf
  340. ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
  341. "This plugin will check the percent of used disk space on a mounted\n"
  342. "file system and generate an alert if percentage is above one of the\n"
  343. "threshold values.\n\n");
  344. print_usage ();
  345. printf
  346. ("\nOptions:\n"
  347. " -w, --warning=INTEGER\n"
  348. " Exit with WARNING status if less than INTEGER kilobytes of disk are free\n"
  349. " -w, --warning=PERCENT%%\n"
  350. " Exit with WARNING status if less than PERCENT of disk space is free\n"
  351. " -c, --critical=INTEGER\n"
  352. " Exit with CRITICAL status if less than INTEGER kilobytes of disk are free\n"
  353. " -c, --critical=PERCENT%%\n"
  354. " Exit with CRITCAL status if less than PERCENT of disk space is free\n"
  355. " -p, --path=PATH, --partition=PARTTION\n"
  356. " Path or partition (checks all mounted partitions if unspecified)\n"
  357. " -m, --mountpoint\n"
  358. " Display the mountpoint instead of the partition\n"
  359. " -x, --exclude_device=PATH\n"
  360. " Ignore device (only works if -p unspecified)\n"
  361. " -e, --errors-only\n"
  362. " Display only devices/mountpoints with errors\n"
  363. " -v, --verbose\n"
  364. " Show details for command-line debugging (do not use with nagios server)\n"
  365. " -h, --help\n"
  366. " Print detailed help screen\n"
  367. " -V, --version\n" " Print version information\n\n");
  368. support ();
  369. }
  370. void
  371. print_usage (void)
  372. {
  373. printf
  374. ("Usage: %s -w limit -c limit [-p path | -x device] [-t timeout] [-m] [-e] [--verbose]\n"
  375. " %s (-h|--help)\n"
  376. " %s (-V|--version)\n", progname, progname, progname);
  377. }