check_disk.c 17 KB

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