check_disk.c 11 KB

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