check_disk.c 16 KB

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