check_disk.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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 (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\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. unsigned long l;
  191. int result = OK;
  192. int option_index = 0;
  193. static struct option long_options[] = {
  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", long_options, &option_index);
  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 (! strcmp (optarg, "bytes")) {
  273. mult = (uintmax_t)1;
  274. units = strdup ("B");
  275. } else if (! strcmp (optarg, "kB")) {
  276. mult = (uintmax_t)1024;
  277. units = strdup ("kB");
  278. } else if (! strcmp (optarg, "MB")) {
  279. mult = (uintmax_t)1024 * 1024;
  280. units = strdup ("MB");
  281. } else if (! strcmp (optarg, "GB")) {
  282. mult = (uintmax_t)1024 * 1024 * 1024;
  283. units = strdup ("GB");
  284. } else if (! strcmp (optarg, "TB")) {
  285. mult = (uintmax_t)1024 * 1024 * 1024 * 1024;
  286. units = strdup ("TB");
  287. } else {
  288. die (STATE_UNKNOWN, _("unit type %s not known\n"), optarg);
  289. }
  290. break;
  291. case 'k': /* display mountpoint */
  292. mult = 1024;
  293. units = strdup ("kB");
  294. break;
  295. case 'm': /* display mountpoint */
  296. mult = 1024 * 1024;
  297. units = strdup ("kB");
  298. break;
  299. case 'l':
  300. show_local_fs = 1;
  301. break;
  302. case 'p': /* select path */
  303. se = (struct name_list *) malloc (sizeof (struct name_list));
  304. se->name = strdup (optarg);
  305. se->name_next = NULL;
  306. se->w_df = w_df;
  307. se->c_df = c_df;
  308. se->w_dfp = w_dfp;
  309. se->c_dfp = c_dfp;
  310. *pathtail = se;
  311. pathtail = &se->name_next;
  312. break;
  313. case 'x': /* exclude path or partition */
  314. se = (struct name_list *) malloc (sizeof (struct name_list));
  315. se->name = strdup (optarg);
  316. se->name_next = NULL;
  317. *dptail = se;
  318. dptail = &se->name_next;
  319. break;
  320. case 'X': /* exclude file system type */
  321. se = (struct name_list *) malloc (sizeof (struct name_list));
  322. se->name = strdup (optarg);
  323. se->name_next = NULL;
  324. *fstail = se;
  325. fstail = &se->name_next;
  326. break;
  327. case 'v': /* verbose */
  328. verbose++;
  329. break;
  330. case 'q': /* verbose */
  331. verbose--;
  332. break;
  333. case 'e':
  334. erronly = TRUE;
  335. break;
  336. case 'M': /* display mountpoint */
  337. display_mntp = TRUE;
  338. break;
  339. case 'C':
  340. w_df = 0;
  341. c_df = 0;
  342. w_dfp = -1.0;
  343. c_dfp = -1.0;
  344. break;
  345. case 'V': /* version */
  346. print_revision (progname, revision);
  347. exit (STATE_OK);
  348. case 'h': /* help */
  349. print_help ();
  350. exit (STATE_OK);
  351. case '?': /* help */
  352. usage (_("check_disk: unrecognized option\n"));
  353. break;
  354. }
  355. }
  356. /* Support for "check_disk warn crit [fs]" with thresholds at used level */
  357. c = optind;
  358. if (w_dfp < 0 && argc > c && is_intnonneg (argv[c]))
  359. w_dfp = (100.0 - atof (argv[c++]));
  360. if (c_dfp < 0 && argc > c && is_intnonneg (argv[c]))
  361. c_dfp = (100.0 - atof (argv[c++]));
  362. if (argc > c && path == NULL) {
  363. se = (struct name_list *) malloc (sizeof (struct name_list));
  364. se->name = strdup (argv[c++]);
  365. se->name_next = NULL;
  366. se->w_df = w_df;
  367. se->c_df = c_df;
  368. se->w_dfp = w_dfp;
  369. se->c_dfp = c_dfp;
  370. *pathtail = se;
  371. }
  372. if (path_select_list) {
  373. temp_list = path_select_list;
  374. while (temp_list) {
  375. if (validate_arguments (temp_list->w_df,
  376. temp_list->c_df,
  377. temp_list->w_dfp,
  378. temp_list->c_dfp,
  379. temp_list->name) == ERROR)
  380. result = ERROR;
  381. temp_list = temp_list->name_next;
  382. }
  383. return result;
  384. } else {
  385. return validate_arguments (w_df, c_df, w_dfp, c_dfp, NULL);
  386. }
  387. }
  388. void print_path (char *mypath)
  389. {
  390. if (mypath)
  391. printf (" for %s", mypath);
  392. printf ("\n");
  393. }
  394. int
  395. validate_arguments (uintmax_t w, uintmax_t c, double wp, double cp, char *mypath)
  396. {
  397. if (w == 0 && c == 0 && wp < 0.0 && cp < 0.0) {
  398. printf (_("INPUT ERROR: No thresholds specified"));
  399. print_path (mypath);
  400. return ERROR;
  401. }
  402. else if ((wp >= 0.0 || cp >= 0.0) &&
  403. (wp < 0.0 || cp < 0.0 || wp > 100.0 || cp > 100.0 || cp > wp)) {
  404. printf (_("\
  405. INPUT ERROR: C_DFP (%f) should be less than W_DFP (%.1f) and both should be between zero and 100 percent, inclusive"),
  406. cp, wp);
  407. print_path (path);
  408. return ERROR;
  409. }
  410. else if ((w > 0 || c > 0) && (w == 0 || c == 0 || c > w)) {
  411. printf (_("\
  412. INPUT ERROR: C_DF (%lu) should be less than W_DF (%lu) and both should be greater than zero"),
  413. (unsigned long)c, (unsigned long)w);
  414. print_path (path);
  415. return ERROR;
  416. }
  417. if (units == NULL)
  418. units = strdup ("MB");
  419. return OK;
  420. }
  421. int
  422. check_disk (double usp, uintmax_t free_disk)
  423. {
  424. int result = STATE_UNKNOWN;
  425. /* check the percent used space against thresholds */
  426. if (usp >= 0.0 && usp >= (100.0 - c_dfp))
  427. result = STATE_CRITICAL;
  428. else if (c_df > 0 && free_disk <= c_df)
  429. result = STATE_CRITICAL;
  430. else if (usp >= 0.0 && usp >= (100.0 - w_dfp))
  431. result = STATE_WARNING;
  432. else if (w_df > 0 && free_disk <= w_df)
  433. result = STATE_WARNING;
  434. else if (usp >= 0.0)
  435. result = STATE_OK;
  436. return result;
  437. }
  438. int
  439. walk_name_list (struct name_list *list, const char *name)
  440. {
  441. while (list) {
  442. if (! strcmp(list->name, name)) {
  443. list->found = 1;
  444. /* if required for name_lists that have not saved w_df, etc (eg exclude lists) */
  445. if (list->w_df) w_df = list->w_df;
  446. if (list->c_df) c_df = list->c_df;
  447. if (list->w_dfp>=0.0) w_dfp = list->w_dfp;
  448. if (list->c_dfp>=0.0) c_dfp = list->c_dfp;
  449. return TRUE;
  450. }
  451. list = list->name_next;
  452. }
  453. return FALSE;
  454. }
  455. void
  456. print_help (void)
  457. {
  458. print_revision (progname, revision);
  459. printf (_(COPYRIGHT), copyright, email);
  460. printf (_("\
  461. This plugin checks the amount of used disk space on a mounted file system\n\
  462. and generates an alert if free space is less than one of the threshold values."));
  463. print_usage ();
  464. printf (_(UT_HELP_VRSN));
  465. printf (_("\
  466. -w, --warning=INTEGER\n\
  467. Exit with WARNING status if less than INTEGER kilobytes of disk are free\n\
  468. -w, --warning=PERCENT%%\n\
  469. Exit with WARNING status if less than PERCENT of disk space is free\n\
  470. -c, --critical=INTEGER\n\
  471. Exit with CRITICAL status if less than INTEGER kilobytes of disk are free\n\
  472. -c, --critical=PERCENT%%\n\
  473. Exit with CRITCAL status if less than PERCENT of disk space is free\n\
  474. -C, --clear\n\
  475. Clear thresholds\n"));
  476. printf (_("\
  477. -u, --units=STRING\n\
  478. Choose bytes, kB, MB, GB, TB (default: MB)\n\
  479. -k, --kilobytes\n\
  480. Same as '--units kB'\n\
  481. -m, --megabytes\n\
  482. Same as '--units MB'\n"));
  483. printf (_("\
  484. -l, --local\n\
  485. Only check local filesystems\n\
  486. -p, --path=PATH, --partition=PARTITION\n\
  487. Path or partition (may be repeated)\n\
  488. -x, --exclude_device=PATH <STRING>\n\
  489. Ignore device (only works if -p unspecified)\n\
  490. -X, --exclude-type=TYPE <STRING>\n\
  491. Ignore all filesystems of indicated type (may be repeated)\n\
  492. -M, --mountpoint\n\
  493. Display the mountpoint instead of the partition\n\
  494. -e, --errors-only\n\
  495. Display only devices/mountpoints with errors\n"));
  496. printf (_(UT_WARN_CRIT));
  497. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  498. printf (_(UT_VERBOSE));
  499. printf ("%s", _("Examples:\n\
  500. check_disk -w 10% -c 5% -p /tmp -p /var -C -w 100000 -c 50000 -p /\n\
  501. Checks /tmp and /var at 10%,5% and / at 100MB, 50MB\n"));
  502. support ();
  503. }
  504. void
  505. print_usage (void)
  506. {
  507. printf (_("\
  508. Usage: %s -w limit -c limit [-p path | -x device] [-t timeout] [-m] [-e]\n\
  509. [-v] [-q]\n\
  510. %s (-h|--help)\n\
  511. %s (-V|--version)\n"),
  512. progname, progname, progname);
  513. }