check_disk.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. #if HAVE_INTTYPES_H
  54. # include <inttypes.h>
  55. #endif
  56. #include <assert.h>
  57. #include "popen.h"
  58. #include "utils.h"
  59. #include <stdarg.h>
  60. #include "../lib/fsusage.h"
  61. /* If nonzero, show inode information. */
  62. static int inode_format;
  63. /* If nonzero, show even filesystems with zero size or
  64. uninteresting types. */
  65. static int show_all_fs;
  66. /* If nonzero, show only local filesystems. */
  67. static int show_local_fs;
  68. /* If nonzero, output data for each filesystem corresponding to a
  69. command line argument -- even if it's a dummy (automounter) entry. */
  70. static int show_listed_fs;
  71. /* If positive, the units to use when printing sizes;
  72. if negative, the human-readable base. */
  73. static int output_block_size;
  74. /* If nonzero, invoke the `sync' system call before getting any usage data.
  75. Using this option can make df very slow, especially with many or very
  76. busy disks. Note that this may make a difference on some systems --
  77. SunOs4.1.3, for one. It is *not* necessary on Linux. */
  78. static int require_sync = 0;
  79. /* A filesystem type to display. */
  80. struct fs_type_list
  81. {
  82. char *fs_name;
  83. struct fs_type_list *fs_next;
  84. };
  85. /* Linked list of filesystem types to display.
  86. If `fs_select_list' is NULL, list all types.
  87. This table is generated dynamically from command-line options,
  88. rather than hardcoding into the program what it thinks are the
  89. valid filesystem types; let the user specify any filesystem type
  90. they want to, and if there are any filesystems of that type, they
  91. will be shown.
  92. Some filesystem types:
  93. 4.2 4.3 ufs nfs swap ignore io vm efs dbg */
  94. static struct fs_type_list *fs_select_list;
  95. /* Linked list of filesystem types to omit.
  96. If the list is empty, don't exclude any types. */
  97. static struct fs_type_list *fs_exclude_list;
  98. /* Linked list of mounted filesystems. */
  99. static struct mount_entry *mount_list;
  100. /* For long options that have no equivalent short option, use a
  101. non-character as a pseudo short option, starting with CHAR_MAX + 1. */
  102. enum
  103. {
  104. SYNC_OPTION = CHAR_MAX + 1,
  105. NO_SYNC_OPTION,
  106. BLOCK_SIZE_OPTION
  107. };
  108. #ifdef _AIX
  109. #pragma alloca
  110. #endif
  111. int process_arguments (int, char **);
  112. int validate_arguments (void);
  113. int check_disk (int usp, int free_disk);
  114. void print_help (void);
  115. void print_usage (void);
  116. int w_df = -1;
  117. int c_df = -1;
  118. float w_dfp = -1.0;
  119. float c_dfp = -1.0;
  120. char *path = "";
  121. char *exclude_device = "";
  122. int verbose = 0;
  123. int erronly = FALSE;
  124. int display_mntp = FALSE;
  125. int
  126. main (int argc, char **argv)
  127. {
  128. int usp = -1;
  129. int total_disk = -1;
  130. int used_disk = -1;
  131. int free_disk = -1;
  132. int result = STATE_UNKNOWN;
  133. int disk_result = STATE_UNKNOWN;
  134. char *command_line = "";
  135. char input_buffer[MAX_INPUT_BUFFER];
  136. char file_system[MAX_INPUT_BUFFER];
  137. char mntp[MAX_INPUT_BUFFER];
  138. char *output = "";
  139. struct fs_usage fsp;
  140. char *disk;
  141. if (process_arguments (argc, argv) != OK)
  142. usage ("Could not parse arguments\n");
  143. get_fs_usage (path, disk, &fsp);
  144. usp = (fsp.fsu_blocks - fsp.fsu_bavail) / fsp.fsu_blocks;
  145. disk_result = check_disk (usp, fsp.fsu_bavail);
  146. result = disk_result;
  147. asprintf (&output, "%llu of %llu kB (%2.0f%%) free (%d-byte blocks)",
  148. fsp.fsu_bavail*fsp.fsu_blocksize/1024,
  149. fsp.fsu_blocks*fsp.fsu_blocksize/1024,
  150. (double)fsp.fsu_bavail*100/fsp.fsu_blocks,
  151. fsp.fsu_blocksize);
  152. terminate (result, "DISK %s %s\n", state_text (result), output);
  153. }
  154. /* process command-line arguments */
  155. int
  156. process_arguments (int argc, char **argv)
  157. {
  158. int c;
  159. int option_index = 0;
  160. static struct option long_options[] = {
  161. {"warning", required_argument, 0, 'w'},
  162. {"critical", required_argument, 0, 'c'},
  163. {"timeout", required_argument, 0, 't'},
  164. {"path", required_argument, 0, 'p'},
  165. {"partition", required_argument, 0, 'p'},
  166. {"verbose", no_argument, 0, 'v'},
  167. {"version", no_argument, 0, 'V'},
  168. {"errors-only", no_argument, 0, 'e'},
  169. {"help", no_argument, 0, 'h'},
  170. {"mountpoint", no_argument, 0, 'm'},
  171. {"exclude_device", required_argument, 0, 'x'},
  172. {"quiet", no_argument, 0, 'q'},
  173. {0, 0, 0, 0}
  174. };
  175. if (argc < 2)
  176. return ERROR;
  177. for (c = 1; c < argc; c++)
  178. if (strcmp ("-to", argv[c]) == 0)
  179. strcpy (argv[c], "-t");
  180. while (1) {
  181. c = getopt_long (argc, argv, "+?Vqhvet:c:w:p:x:m", long_options, &option_index);
  182. if (c == -1 || c == EOF)
  183. break;
  184. switch (c) {
  185. case 'w': /* warning time threshold */
  186. if (is_intnonneg (optarg)) {
  187. w_df = atoi (optarg);
  188. break;
  189. }
  190. else if (strpbrk (optarg, ",:") &&
  191. strstr (optarg, "%") &&
  192. sscanf (optarg, "%d%*[:,]%f%%", &w_df, &w_dfp) == 2) {
  193. break;
  194. }
  195. else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &w_dfp) == 1) {
  196. break;
  197. }
  198. else {
  199. usage ("Warning threshold must be integer or percentage!\n");
  200. }
  201. case 'c': /* critical time threshold */
  202. if (is_intnonneg (optarg)) {
  203. c_df = atoi (optarg);
  204. break;
  205. }
  206. else if (strpbrk (optarg, ",:") &&
  207. strstr (optarg, "%") &&
  208. sscanf (optarg, "%d%*[,:]%f%%", &c_df, &c_dfp) == 2) {
  209. break;
  210. }
  211. else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &c_dfp) == 1) {
  212. break;
  213. }
  214. else {
  215. usage ("Critical threshold must be integer or percentage!\n");
  216. }
  217. case 't': /* timeout period */
  218. if (is_integer (optarg)) {
  219. timeout_interval = atoi (optarg);
  220. break;
  221. }
  222. else {
  223. usage ("Timeout Interval must be an integer!\n");
  224. }
  225. case 'p': /* path or partition */
  226. path = optarg;
  227. break;
  228. case 'v': /* verbose */
  229. verbose++;
  230. break;
  231. case 'q': /* verbose */
  232. verbose--;
  233. break;
  234. case 'e':
  235. erronly = TRUE;
  236. break;
  237. case 'm': /* display mountpoint */
  238. display_mntp = TRUE;
  239. break;
  240. case 'x': /* exclude path or partition */
  241. exclude_device = optarg;
  242. break;
  243. case 'V': /* version */
  244. print_revision (progname, revision);
  245. exit (STATE_OK);
  246. case 'h': /* help */
  247. print_help ();
  248. exit (STATE_OK);
  249. case '?': /* help */
  250. usage ("check_disk: unrecognized option\n");
  251. break;
  252. }
  253. }
  254. c = optind;
  255. if (w_dfp == -1 && argc > c && is_intnonneg (argv[c]))
  256. w_dfp = (100.0 - atof (argv[c++]));
  257. if (c_dfp == -1 && argc > c && is_intnonneg (argv[c]))
  258. c_dfp = (100.0 - atof (argv[c++]));
  259. if (argc > c && strlen (path) == 0)
  260. path = argv[c++];
  261. return validate_arguments ();
  262. }
  263. int
  264. validate_arguments ()
  265. {
  266. if (w_df < 0 && c_df < 0 && w_dfp < 0 && c_dfp < 0) {
  267. printf ("INPUT ERROR: Unable to parse command line\n");
  268. return ERROR;
  269. }
  270. else if ((w_dfp >= 0 || c_dfp >= 0)
  271. && (w_dfp < 0 || c_dfp < 0 || w_dfp > 100 || c_dfp > 100
  272. || c_dfp > w_dfp)) {
  273. printf
  274. ("INPUT ERROR: C_DFP (%f) should be less than W_DFP (%f) and both should be between zero and 100 percent, inclusive\n",
  275. c_dfp, w_dfp);
  276. return ERROR;
  277. }
  278. else if ((w_df > 0 || c_df > 0) && (w_df < 0 || c_df < 0 || c_df > w_df)) {
  279. printf
  280. ("INPUT ERROR: C_DF (%d) should be less than W_DF (%d) and both should be greater than zero\n",
  281. c_df, w_df);
  282. return ERROR;
  283. }
  284. else {
  285. return OK;
  286. }
  287. }
  288. int
  289. check_disk (usp, free_disk)
  290. {
  291. int result = STATE_UNKNOWN;
  292. /* check the percent used space against thresholds */
  293. if (usp >= 0 && usp >= (100.0 - c_dfp))
  294. result = STATE_CRITICAL;
  295. else if (c_df >= 0 && free_disk <= c_df)
  296. result = STATE_CRITICAL;
  297. else if (usp >= 0 && usp >= (100.0 - w_dfp))
  298. result = STATE_WARNING;
  299. else if (w_df >= 0 && free_disk <= w_df)
  300. result = STATE_WARNING;
  301. else if (usp >= 0.0)
  302. result = STATE_OK;
  303. return result;
  304. }
  305. void
  306. print_help (void)
  307. {
  308. print_revision (progname, revision);
  309. printf ("Copyright (c) %s %s\n\t<%s>\n\n%s\n",
  310. copyright, authors, email, summary);
  311. print_usage ();
  312. printf ("\nOptions:\n");
  313. printf (options);
  314. support ();
  315. }
  316. void
  317. print_usage (void)
  318. {
  319. printf
  320. ("Usage: %s %s\n"
  321. " %s (-h|--help)\n"
  322. " %s (-V|--version)\n", progname, option_summary, progname, progname);
  323. }