check_disk.c 13 KB

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