check_disk.c 13 KB

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