check_disk.c 12 KB

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