check_disk.c 14 KB

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