check_disk.c 13 KB

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