check_disk.c 16 KB

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