check_disk.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 name_list
  82. {
  83. char *name;
  84. struct name_list *name_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 name_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 name_list *fs_exclude_list;
  99. static struct name_list *path_select_list;
  100. static struct name_list *dev_select_list;
  101. /* Linked list of mounted filesystems. */
  102. static struct mount_entry *mount_list;
  103. /* For long options that have no equivalent short option, use a
  104. non-character as a pseudo short option, starting with CHAR_MAX + 1. */
  105. enum
  106. {
  107. SYNC_OPTION = CHAR_MAX + 1,
  108. NO_SYNC_OPTION,
  109. BLOCK_SIZE_OPTION
  110. };
  111. #ifdef _AIX
  112. #pragma alloca
  113. #endif
  114. int process_arguments (int, char **);
  115. int validate_arguments (void);
  116. int check_disk (int usp, int free_disk);
  117. void print_help (void);
  118. void print_usage (void);
  119. int w_df = -1;
  120. int c_df = -1;
  121. float w_dfp = -1.0;
  122. float c_dfp = -1.0;
  123. char *path = "";
  124. char *exclude_device = "";
  125. int verbose = 0;
  126. int erronly = FALSE;
  127. int display_mntp = FALSE;
  128. /* Linked list of mounted filesystems. */
  129. static struct mount_entry *mount_list;
  130. int
  131. main (int argc, char **argv)
  132. {
  133. int usp = -1;
  134. int total_disk = -1;
  135. int used_disk = -1;
  136. int free_disk = -1;
  137. int result = STATE_UNKNOWN;
  138. int disk_result = STATE_UNKNOWN;
  139. char *command_line = "";
  140. char input_buffer[MAX_INPUT_BUFFER];
  141. char file_system[MAX_INPUT_BUFFER];
  142. char mntp[MAX_INPUT_BUFFER];
  143. char *output = "";
  144. struct mount_entry *me;
  145. struct fs_usage fsp;
  146. char *disk;
  147. mount_list = read_filesystem_list (0);
  148. if (process_arguments (argc, argv) != OK)
  149. usage ("Could not parse arguments\n");
  150. for (me = mount_list; me; me = me->me_next) {
  151. if ((dev_select_list &&
  152. ! strcmp (dev_select_list->name, me->me_devname)) ||
  153. (path_select_list &&
  154. ! strcmp (path_select_list->name, me->me_mountdir)))
  155. get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
  156. else if (dev_select_list || path_select_list)
  157. continue;
  158. else
  159. get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
  160. if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
  161. usp = (fsp.fsu_blocks - fsp.fsu_bavail) * 100 / fsp.fsu_blocks;
  162. disk_result = check_disk (usp, fsp.fsu_bavail);
  163. result = max_state (disk_result, result);
  164. asprintf (&output, "%s %llu of %llu MB (%2.0f%%) free on %s\n",
  165. output,
  166. fsp.fsu_bavail*fsp.fsu_blocksize/1024/1024,
  167. fsp.fsu_blocks*fsp.fsu_blocksize/1024/1024,
  168. (double)fsp.fsu_bavail*100/fsp.fsu_blocks,
  169. display_mntp ? me->me_devname : me->me_mountdir);
  170. }
  171. }
  172. terminate (result, "DISK %s %s\n", state_text (result), output);
  173. }
  174. /* process command-line arguments */
  175. int
  176. process_arguments (int argc, char **argv)
  177. {
  178. int c;
  179. struct name_list *se;
  180. struct name_list **pathtail = &path_select_list;
  181. struct name_list **devtail = &dev_select_list;
  182. int option_index = 0;
  183. static struct option long_options[] = {
  184. {"warning", required_argument, 0, 'w'},
  185. {"critical", required_argument, 0, 'c'},
  186. {"timeout", required_argument, 0, 't'},
  187. {"path", required_argument, 0, 'p'},
  188. {"partition", required_argument, 0, 'p'},
  189. {"verbose", no_argument, 0, 'v'},
  190. {"version", no_argument, 0, 'V'},
  191. {"errors-only", no_argument, 0, 'e'},
  192. {"help", no_argument, 0, 'h'},
  193. {"mountpoint", no_argument, 0, 'm'},
  194. {"device", no_argument, 0, 'd'},
  195. {"exclude_device", required_argument, 0, 'x'},
  196. {"quiet", no_argument, 0, 'q'},
  197. {0, 0, 0, 0}
  198. };
  199. if (argc < 2)
  200. return ERROR;
  201. for (c = 1; c < argc; c++)
  202. if (strcmp ("-to", argv[c]) == 0)
  203. strcpy (argv[c], "-t");
  204. while (1) {
  205. c = getopt_long (argc, argv, "+?Vqhvet:c:w:p:d:x:m", long_options, &option_index);
  206. if (c == -1 || c == EOF)
  207. break;
  208. switch (c) {
  209. case 'w': /* warning time threshold */
  210. if (is_intnonneg (optarg)) {
  211. w_df = atoi (optarg);
  212. break;
  213. }
  214. else if (strpbrk (optarg, ",:") &&
  215. strstr (optarg, "%") &&
  216. sscanf (optarg, "%d%*[:,]%f%%", &w_df, &w_dfp) == 2) {
  217. break;
  218. }
  219. else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &w_dfp) == 1) {
  220. break;
  221. }
  222. else {
  223. usage ("Warning threshold must be integer or percentage!\n");
  224. }
  225. case 'c': /* critical time threshold */
  226. if (is_intnonneg (optarg)) {
  227. c_df = atoi (optarg);
  228. break;
  229. }
  230. else if (strpbrk (optarg, ",:") &&
  231. strstr (optarg, "%") &&
  232. sscanf (optarg, "%d%*[,:]%f%%", &c_df, &c_dfp) == 2) {
  233. break;
  234. }
  235. else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &c_dfp) == 1) {
  236. break;
  237. }
  238. else {
  239. usage ("Critical threshold must be integer or percentage!\n");
  240. }
  241. case 't': /* timeout period */
  242. if (is_integer (optarg)) {
  243. timeout_interval = atoi (optarg);
  244. break;
  245. }
  246. else {
  247. usage ("Timeout Interval must be an integer!\n");
  248. }
  249. case 'p': /* path or partition */
  250. se = (struct name_list *) malloc (sizeof (struct name_list));
  251. se->name = strdup (optarg);
  252. *pathtail = se;
  253. pathtail = &se->name_next;
  254. break;
  255. case 'd': /* path or partition */
  256. se = (struct name_list *) malloc (sizeof (struct name_list));
  257. se->name = strdup (optarg);
  258. *devtail = se;
  259. devtail = &se->name_next;
  260. break;
  261. case 'v': /* verbose */
  262. verbose++;
  263. break;
  264. case 'q': /* verbose */
  265. verbose--;
  266. break;
  267. case 'e':
  268. erronly = TRUE;
  269. break;
  270. case 'm': /* display mountpoint */
  271. display_mntp = TRUE;
  272. break;
  273. case 'x': /* exclude path or partition */
  274. exclude_device = optarg;
  275. break;
  276. case 'V': /* version */
  277. print_revision (progname, revision);
  278. exit (STATE_OK);
  279. case 'h': /* help */
  280. print_help ();
  281. exit (STATE_OK);
  282. case '?': /* help */
  283. usage ("check_disk: unrecognized option\n");
  284. break;
  285. }
  286. }
  287. c = optind;
  288. if (w_dfp == -1 && argc > c && is_intnonneg (argv[c]))
  289. w_dfp = (100.0 - atof (argv[c++]));
  290. if (c_dfp == -1 && argc > c && is_intnonneg (argv[c]))
  291. c_dfp = (100.0 - atof (argv[c++]));
  292. if (argc > c && strlen (path) == 0)
  293. path = argv[c++];
  294. return validate_arguments ();
  295. }
  296. int
  297. validate_arguments ()
  298. {
  299. if (w_df < 0 && c_df < 0 && w_dfp < 0 && c_dfp < 0) {
  300. printf ("INPUT ERROR: Unable to parse command line\n");
  301. return ERROR;
  302. }
  303. else if ((w_dfp >= 0 || c_dfp >= 0)
  304. && (w_dfp < 0 || c_dfp < 0 || w_dfp > 100 || c_dfp > 100
  305. || c_dfp > w_dfp)) {
  306. printf
  307. ("INPUT ERROR: C_DFP (%f) should be less than W_DFP (%f) and both should be between zero and 100 percent, inclusive\n",
  308. c_dfp, w_dfp);
  309. return ERROR;
  310. }
  311. else if ((w_df > 0 || c_df > 0) && (w_df < 0 || c_df < 0 || c_df > w_df)) {
  312. printf
  313. ("INPUT ERROR: C_DF (%d) should be less than W_DF (%d) and both should be greater than zero\n",
  314. c_df, w_df);
  315. return ERROR;
  316. }
  317. else {
  318. return OK;
  319. }
  320. }
  321. int
  322. check_disk (usp, free_disk)
  323. {
  324. int result = STATE_UNKNOWN;
  325. /* check the percent used space against thresholds */
  326. if (usp >= 0 && usp >= (100.0 - c_dfp))
  327. result = STATE_CRITICAL;
  328. else if (c_df >= 0 && free_disk <= c_df)
  329. result = STATE_CRITICAL;
  330. else if (usp >= 0 && usp >= (100.0 - w_dfp))
  331. result = STATE_WARNING;
  332. else if (w_df >= 0 && free_disk <= w_df)
  333. result = STATE_WARNING;
  334. else if (usp >= 0.0)
  335. result = STATE_OK;
  336. return result;
  337. }
  338. void
  339. print_help (void)
  340. {
  341. print_revision (progname, revision);
  342. printf ("Copyright (c) %s %s\n\t<%s>\n\n%s\n",
  343. copyright, authors, email, summary);
  344. print_usage ();
  345. printf ("\nOptions:\n");
  346. printf (options);
  347. support ();
  348. }
  349. void
  350. print_usage (void)
  351. {
  352. printf
  353. ("Usage: %s %s\n"
  354. " %s (-h|--help)\n"
  355. " %s (-V|--version)\n", progname, option_summary, progname, progname);
  356. }