check_disk.c 17 KB

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