check_disk.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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%.0f of %.0f %s (%.0f%%) free on %s (type %s mounted on %s) warn:%d crit:%d warn%%:%.0f%% crit%%:%.0f%%"),
  160. details,
  161. free_space,
  162. total_space,
  163. units,
  164. free_space_pct,
  165. me->me_devname,
  166. me->me_type,
  167. me->me_mountdir,
  168. w_df, c_df, w_dfp, c_dfp);
  169. }
  170. }
  171. if (verbose > 2)
  172. asprintf (&output, "%s%s", output, details);
  173. /* Override result if paths specified and not found */
  174. temp_list = path_select_list;
  175. while (temp_list) {
  176. if (temp_list->found != TRUE) {
  177. asprintf (&output, _("%s [%s not found]"), output, temp_list->name);
  178. result = STATE_CRITICAL;
  179. }
  180. temp_list = temp_list->name_next;
  181. }
  182. die (result, "DISK %s%s\n", state_text (result), output, details);
  183. return STATE_UNKNOWN;
  184. }
  185. /* process command-line arguments */
  186. int
  187. process_arguments (int argc, char **argv)
  188. {
  189. int c;
  190. struct name_list *se;
  191. struct name_list **pathtail = &path_select_list;
  192. struct name_list **fstail = &fs_exclude_list;
  193. struct name_list **dptail = &dp_exclude_list;
  194. struct name_list *temp_list;
  195. int result = OK;
  196. int option_index = 0;
  197. static struct option long_options[] = {
  198. {"timeout", required_argument, 0, 't'},
  199. {"warning", required_argument, 0, 'w'},
  200. {"critical", required_argument, 0, 'c'},
  201. {"local", required_argument, 0, 'l'},
  202. {"kilobytes", required_argument, 0, 'k'},
  203. {"megabytes", required_argument, 0, 'm'},
  204. {"units", required_argument, 0, 'u'},
  205. {"path", required_argument, 0, 'p'},
  206. {"partition", required_argument, 0, 'p'},
  207. {"exclude_device", required_argument, 0, 'x'},
  208. {"exclude-type", required_argument, 0, 'X'},
  209. {"mountpoint", no_argument, 0, 'M'},
  210. {"errors-only", no_argument, 0, 'e'},
  211. {"verbose", no_argument, 0, 'v'},
  212. {"quiet", no_argument, 0, 'q'},
  213. {"clear", no_argument, 0, 'C'},
  214. {"version", no_argument, 0, 'V'},
  215. {"help", no_argument, 0, 'h'},
  216. {0, 0, 0, 0}
  217. };
  218. if (argc < 2)
  219. return ERROR;
  220. se = (struct name_list *) malloc (sizeof (struct name_list));
  221. se->name = strdup ("iso9660");
  222. se->name_next = NULL;
  223. *fstail = se;
  224. fstail = &se->name_next;
  225. for (c = 1; c < argc; c++)
  226. if (strcmp ("-to", argv[c]) == 0)
  227. strcpy (argv[c], "-t");
  228. while (1) {
  229. c = getopt_long (argc, argv, "+?VqhveCt:c:w:u:p:x:X:mklM", long_options, &option_index);
  230. if (c == -1 || c == EOF)
  231. break;
  232. switch (c) {
  233. case 't': /* timeout period */
  234. if (is_integer (optarg)) {
  235. timeout_interval = atoi (optarg);
  236. break;
  237. }
  238. else {
  239. usage (_("Timeout Interval must be an integer!\n"));
  240. }
  241. case 'w': /* warning threshold */
  242. if (is_intnonneg (optarg)) {
  243. w_df = atoi (optarg);
  244. break;
  245. }
  246. else if (strpbrk (optarg, ",:") &&
  247. strstr (optarg, "%") &&
  248. sscanf (optarg, "%ul%*[:,]%lf%%", &w_df, &w_dfp) == 2) {
  249. break;
  250. }
  251. else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &w_dfp) == 1) {
  252. break;
  253. }
  254. else {
  255. usage (_("Warning threshold must be integer or percentage!\n"));
  256. }
  257. case 'c': /* critical threshold */
  258. if (is_intnonneg (optarg)) {
  259. c_df = atoi (optarg);
  260. break;
  261. }
  262. else if (strpbrk (optarg, ",:") &&
  263. strstr (optarg, "%") &&
  264. sscanf (optarg, "%ul%*[,:]%lf%%", &c_df, &c_dfp) == 2) {
  265. break;
  266. }
  267. else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &c_dfp) == 1) {
  268. break;
  269. }
  270. else {
  271. usage (_("Critical threshold must be integer or percentage!\n"));
  272. }
  273. case 'u':
  274. if (! strcmp (optarg, "bytes")) {
  275. mult = (uintmax_t)1;
  276. units = "B";
  277. } else if (! strcmp (optarg, "kB")) {
  278. mult = (uintmax_t)1024;
  279. units = "kB";
  280. } else if (! strcmp (optarg, "MB")) {
  281. mult = (uintmax_t)1024 * 1024;
  282. units = "MB";
  283. } else if (! strcmp (optarg, "GB")) {
  284. mult = (uintmax_t)1024 * 1024 * 1024;
  285. units = "GB";
  286. } else if (! strcmp (optarg, "TB")) {
  287. mult = (uintmax_t)1024 * 1024 * 1024 * 1024;
  288. units = "TB";
  289. } else {
  290. die (STATE_UNKNOWN, _("unit type %s not known\n"), optarg);
  291. }
  292. break;
  293. case 'k': /* display mountpoint */
  294. mult = 1024;
  295. units = "kB";
  296. break;
  297. case 'm': /* display mountpoint */
  298. mult = 1024 * 1024;
  299. units = "MB";
  300. break;
  301. case 'l':
  302. show_local_fs = 1;
  303. break;
  304. case 'p': /* select path */
  305. se = (struct name_list *) malloc (sizeof (struct name_list));
  306. se->name = strdup (optarg);
  307. se->name_next = NULL;
  308. se->w_df = w_df;
  309. se->c_df = c_df;
  310. se->w_dfp = w_dfp;
  311. se->c_dfp = c_dfp;
  312. *pathtail = se;
  313. pathtail = &se->name_next;
  314. break;
  315. case 'x': /* exclude path or partition */
  316. se = (struct name_list *) malloc (sizeof (struct name_list));
  317. se->name = strdup (optarg);
  318. se->name_next = NULL;
  319. *dptail = se;
  320. dptail = &se->name_next;
  321. break;
  322. case 'X': /* exclude file system type */
  323. se = (struct name_list *) malloc (sizeof (struct name_list));
  324. se->name = strdup (optarg);
  325. se->name_next = NULL;
  326. *fstail = se;
  327. fstail = &se->name_next;
  328. break;
  329. case 'v': /* verbose */
  330. verbose++;
  331. break;
  332. case 'q': /* verbose */
  333. verbose--;
  334. break;
  335. case 'e':
  336. erronly = TRUE;
  337. break;
  338. case 'M': /* display mountpoint */
  339. display_mntp = TRUE;
  340. break;
  341. case 'C':
  342. w_df = 0;
  343. c_df = 0;
  344. w_dfp = -1.0;
  345. c_dfp = -1.0;
  346. break;
  347. case 'V': /* version */
  348. print_revision (progname, revision);
  349. exit (STATE_OK);
  350. case 'h': /* help */
  351. print_help ();
  352. exit (STATE_OK);
  353. case '?': /* help */
  354. usage (_("check_disk: unrecognized option\n"));
  355. break;
  356. }
  357. }
  358. /* Support for "check_disk warn crit [fs]" with thresholds at used level */
  359. c = optind;
  360. if (w_dfp < 0 && argc > c && is_intnonneg (argv[c]))
  361. w_dfp = (100.0 - atof (argv[c++]));
  362. if (c_dfp < 0 && argc > c && is_intnonneg (argv[c]))
  363. c_dfp = (100.0 - atof (argv[c++]));
  364. if (argc > c && path == NULL) {
  365. se = (struct name_list *) malloc (sizeof (struct name_list));
  366. se->name = strdup (argv[c++]);
  367. se->name_next = NULL;
  368. se->w_df = w_df;
  369. se->c_df = c_df;
  370. se->w_dfp = w_dfp;
  371. se->c_dfp = c_dfp;
  372. *pathtail = se;
  373. }
  374. if (path_select_list) {
  375. temp_list = path_select_list;
  376. while (temp_list) {
  377. if (validate_arguments (temp_list->w_df,
  378. temp_list->c_df,
  379. temp_list->w_dfp,
  380. temp_list->c_dfp,
  381. temp_list->name) == ERROR)
  382. result = ERROR;
  383. temp_list = temp_list->name_next;
  384. }
  385. return result;
  386. } else {
  387. return validate_arguments (w_df, c_df, w_dfp, c_dfp, NULL);
  388. }
  389. }
  390. void print_path (char *mypath)
  391. {
  392. if (mypath)
  393. printf (" for %s", mypath);
  394. printf ("\n");
  395. }
  396. int
  397. validate_arguments (uintmax_t w, uintmax_t c, double wp, double cp, char *mypath)
  398. {
  399. if (w == 0 && c == 0 && wp < 0.0 && cp < 0.0) {
  400. printf (_("INPUT ERROR: No thresholds specified"));
  401. print_path (mypath);
  402. return ERROR;
  403. }
  404. else if ((wp >= 0.0 || cp >= 0.0) &&
  405. (wp < 0.0 || cp < 0.0 || wp > 100.0 || cp > 100.0 || cp > wp)) {
  406. printf (_("\
  407. INPUT ERROR: C_DFP (%f) should be less than W_DFP (%.1f) and both should be between zero and 100 percent, inclusive"),
  408. cp, wp);
  409. print_path (path);
  410. return ERROR;
  411. }
  412. else if ((w > 0 || c > 0) && (w == 0 || c == 0 || c > w)) {
  413. printf (_("\
  414. INPUT ERROR: C_DF (%lu) should be less than W_DF (%lu) and both should be greater than zero"),
  415. (unsigned long)c, (unsigned long)w);
  416. print_path (path);
  417. return ERROR;
  418. }
  419. else {
  420. return OK;
  421. }
  422. }
  423. int
  424. check_disk (double usp, uintmax_t free_disk)
  425. {
  426. int result = STATE_UNKNOWN;
  427. /* check the percent used space against thresholds */
  428. if (usp >= 0.0 && usp >= (100.0 - c_dfp))
  429. result = STATE_CRITICAL;
  430. else if (c_df > 0 && free_disk <= c_df)
  431. result = STATE_CRITICAL;
  432. else if (usp >= 0.0 && usp >= (100.0 - w_dfp))
  433. result = STATE_WARNING;
  434. else if (w_df > 0 && free_disk <= w_df)
  435. result = STATE_WARNING;
  436. else if (usp >= 0.0)
  437. result = STATE_OK;
  438. return result;
  439. }
  440. int
  441. walk_name_list (struct name_list *list, const char *name)
  442. {
  443. while (list) {
  444. if (! strcmp(list->name, name)) {
  445. list->found = 1;
  446. /* if required for name_lists that have not saved w_df, etc (eg exclude lists) */
  447. if (list->w_df) w_df = list->w_df;
  448. if (list->c_df) c_df = list->c_df;
  449. if (list->w_dfp>=0.0) w_dfp = list->w_dfp;
  450. if (list->c_dfp>=0.0) c_dfp = list->c_dfp;
  451. return TRUE;
  452. }
  453. list = list->name_next;
  454. }
  455. return FALSE;
  456. }
  457. void
  458. print_help (void)
  459. {
  460. print_revision (progname, revision);
  461. printf (_(COPYRIGHT), copyright, email);
  462. printf (_("\
  463. This plugin checks the amount of used disk space on a mounted file system\n\
  464. and generates an alert if free space is less than one of the threshold values."));
  465. print_usage ();
  466. printf (_(UT_HELP_VRSN));
  467. printf (_("\
  468. -w, --warning=INTEGER\n\
  469. Exit with WARNING status if less than INTEGER kilobytes of disk are free\n\
  470. -w, --warning=PERCENT%%\n\
  471. Exit with WARNING status if less than PERCENT of disk space is free\n\
  472. -c, --critical=INTEGER\n\
  473. Exit with CRITICAL status if less than INTEGER kilobytes of disk are free\n\
  474. -c, --critical=PERCENT%%\n\
  475. Exit with CRITCAL status if less than PERCENT of disk space is free\n\
  476. -C, --clear\n\
  477. Clear thresholds\n"));
  478. printf (_("\
  479. -u, --units=STRING\n\
  480. Choose bytes, kB, MB, GB, TB (default: MB)\n\
  481. -k, --kilobytes\n\
  482. Same as '--units kB'\n\
  483. -m, --megabytes\n\
  484. Same as '--units MB'\n"));
  485. printf (_("\
  486. -l, --local\n\
  487. Only check local filesystems\n\
  488. -p, --path=PATH, --partition=PARTITION\n\
  489. Path or partition (may be repeated)\n\
  490. -x, --exclude_device=PATH <STRING>\n\
  491. Ignore device (only works if -p unspecified)\n\
  492. -X, --exclude-type=TYPE <STRING>\n\
  493. Ignore all filesystems of indicated type (may be repeated)\n\
  494. -M, --mountpoint\n\
  495. Display the mountpoint instead of the partition\n\
  496. -e, --errors-only\n\
  497. Display only devices/mountpoints with errors\n"));
  498. printf (_(UT_WARN_CRIT));
  499. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  500. printf (_(UT_VERBOSE));
  501. printf ("%s", _("Examples:\n\
  502. check_disk -w 10% -c 5% -p /tmp -p /var -C -w 100000 -c 50000 -p /\n\
  503. Checks /tmp and /var at 10%,5% and / at 100MB, 50MB\n"));
  504. support ();
  505. }
  506. void
  507. print_usage (void)
  508. {
  509. printf (_("\
  510. Usage: %s -w limit -c limit [-p path | -x device] [-t timeout] [-m] [-e]\n\
  511. [-v] [-q]\n\
  512. %s (-h|--help)\n\
  513. %s (-V|--version)\n"),
  514. progname, progname, progname);
  515. }