check_disk.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 *program_name = "check_disk"; // Required for coreutils libs
  16. const char *revision = "$Revision$";
  17. const char *copyright = "1999-2003";
  18. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  19. #include "common.h"
  20. #if HAVE_INTTYPES_H
  21. # include <inttypes.h>
  22. #endif
  23. #include <assert.h>
  24. #include "popen.h"
  25. #include "utils.h"
  26. #include <stdarg.h>
  27. #include "../lib/fsusage.h"
  28. #include "../lib/mountlist.h"
  29. #if HAVE_LIMITS_H
  30. # include <limits.h>
  31. #endif
  32. /* If nonzero, show inode information. */
  33. /* static int inode_format; */
  34. /* If nonzero, show even filesystems with zero size or
  35. uninteresting types. */
  36. static int show_all_fs = 1;
  37. /* If nonzero, show only local filesystems. */
  38. static int show_local_fs = 0;
  39. /* If positive, the units to use when printing sizes;
  40. if negative, the human-readable base. */
  41. /* static int output_block_size; */
  42. /* If nonzero, invoke the `sync' system call before getting any usage data.
  43. Using this option can make df very slow, especially with many or very
  44. busy disks. Note that this may make a difference on some systems --
  45. SunOs4.1.3, for one. It is *not* necessary on Linux. */
  46. /* static int require_sync = 0; */
  47. /* A filesystem type to display. */
  48. struct name_list
  49. {
  50. char *name;
  51. int found;
  52. uintmax_t w_df;
  53. uintmax_t c_df;
  54. double w_dfp;
  55. double c_dfp;
  56. struct name_list *name_next;
  57. };
  58. /* Linked list of filesystem types to display.
  59. If `fs_select_list' is NULL, list all types.
  60. This table is generated dynamically from command-line options,
  61. rather than hardcoding into the program what it thinks are the
  62. valid filesystem types; let the user specify any filesystem type
  63. they want to, and if there are any filesystems of that type, they
  64. will be shown.
  65. Some filesystem types:
  66. 4.2 4.3 ufs nfs swap ignore io vm efs dbg */
  67. /* static struct name_list *fs_select_list; */
  68. /* Linked list of filesystem types to omit.
  69. If the list is empty, don't exclude any types. */
  70. static struct name_list *fs_exclude_list;
  71. static struct name_list *dp_exclude_list;
  72. static struct name_list *path_select_list;
  73. static struct name_list *dev_select_list;
  74. /* Linked list of mounted filesystems. */
  75. static struct mount_entry *mount_list;
  76. /* For long options that have no equivalent short option, use a
  77. non-character as a pseudo short option, starting with CHAR_MAX + 1. */
  78. enum
  79. {
  80. SYNC_OPTION = CHAR_MAX + 1,
  81. NO_SYNC_OPTION,
  82. BLOCK_SIZE_OPTION
  83. };
  84. #ifdef _AIX
  85. #pragma alloca
  86. #endif
  87. int process_arguments (int, char **);
  88. void print_path (const char *mypath);
  89. int validate_arguments (uintmax_t, uintmax_t, double, double, char *);
  90. int check_disk (double usp, uintmax_t free_disk);
  91. int walk_name_list (struct name_list *list, const char *name);
  92. void print_help (void);
  93. void print_usage (void);
  94. uintmax_t w_df = 0;
  95. uintmax_t c_df = 0;
  96. double w_dfp = -1.0;
  97. double c_dfp = -1.0;
  98. char *path;
  99. char *exclude_device;
  100. char *units;
  101. uintmax_t mult = 1024 * 1024;
  102. int verbose = 0;
  103. int erronly = FALSE;
  104. int display_mntp = FALSE;
  105. /* Linked list of mounted filesystems. */
  106. static struct mount_entry *mount_list;
  107. int
  108. main (int argc, char **argv)
  109. {
  110. double usp = -1.0;
  111. int result = STATE_UNKNOWN;
  112. int disk_result = STATE_UNKNOWN;
  113. char file_system[MAX_INPUT_BUFFER];
  114. char *output;
  115. char *details;
  116. char *perf;
  117. uintmax_t psize;
  118. float free_space, free_space_pct, total_space;
  119. struct mount_entry *me;
  120. struct fs_usage fsp;
  121. struct name_list *temp_list;
  122. output = strdup (" - free space:");
  123. details = strdup ("");
  124. perf = strdup ("");
  125. setlocale (LC_ALL, "");
  126. bindtextdomain (PACKAGE, LOCALEDIR);
  127. textdomain (PACKAGE);
  128. mount_list = read_filesystem_list (0);
  129. if (process_arguments (argc, argv) != OK)
  130. usage (_("Could not parse arguments\n"));
  131. for (me = mount_list; me; me = me->me_next) {
  132. if (path_select_list &&
  133. (walk_name_list (path_select_list, me->me_mountdir) ||
  134. walk_name_list (path_select_list, me->me_devname) ) )
  135. get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
  136. else if (dev_select_list || path_select_list)
  137. continue;
  138. else if (me->me_remote && show_local_fs)
  139. continue;
  140. else if (me->me_dummy && !show_all_fs)
  141. continue;
  142. else if (fs_exclude_list && walk_name_list (fs_exclude_list, me->me_type))
  143. continue;
  144. else if (dp_exclude_list &&
  145. (walk_name_list (dp_exclude_list, me->me_devname) ||
  146. walk_name_list (dp_exclude_list, me->me_mountdir)))
  147. continue;
  148. else
  149. get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
  150. if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
  151. usp = (double)(fsp.fsu_blocks - fsp.fsu_bavail) * 100 / fsp.fsu_blocks;
  152. disk_result = check_disk (usp, fsp.fsu_bavail * fsp.fsu_blocksize);
  153. result = max_state (disk_result, result);
  154. psize = fsp.fsu_blocks*fsp.fsu_blocksize/mult;
  155. asprintf (&perf, "%s %s", perf,
  156. perfdata ((!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
  157. psize-(fsp.fsu_bavail*fsp.fsu_blocksize/mult), units,
  158. TRUE, min ((uintmax_t)psize-(uintmax_t)w_df, (uintmax_t)((1.0-w_dfp/100.0)*psize)),
  159. TRUE, min ((uintmax_t)psize-(uintmax_t)c_df, (uintmax_t)((1.0-c_dfp/100.0)*psize)),
  160. TRUE, 0,
  161. TRUE, psize));
  162. if (disk_result==STATE_OK && erronly && !verbose)
  163. continue;
  164. free_space = (float)fsp.fsu_bavail*fsp.fsu_blocksize/mult;
  165. free_space_pct = (float)fsp.fsu_bavail*100/fsp.fsu_blocks;
  166. total_space = (float)fsp.fsu_blocks*fsp.fsu_blocksize/mult;
  167. if (disk_result!=STATE_OK || verbose>=0)
  168. asprintf (&output, ("%s %s %.0f %s (%.0f%%);"),
  169. output,
  170. (!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
  171. free_space,
  172. units,
  173. free_space_pct);
  174. asprintf (&details, _("%s\n\
  175. %.0f of %.0f %s (%.0f%%) free on %s (type %s mounted on %s) warn:%lu crit:%lu warn%%:%.0f%% crit%%:%.0f%%"),
  176. details, free_space, total_space, units, free_space_pct,
  177. me->me_devname, me->me_type, me->me_mountdir,
  178. (unsigned long)w_df, (unsigned long)c_df, w_dfp, c_dfp);
  179. }
  180. }
  181. asprintf (&output, "%s|%s", output, perf);
  182. if (verbose > 2)
  183. asprintf (&output, "%s%s", output, details);
  184. /* Override result if paths specified and not found */
  185. temp_list = path_select_list;
  186. while (temp_list) {
  187. if (temp_list->found != TRUE) {
  188. asprintf (&output, _("%s [%s not found]"), output, temp_list->name);
  189. result = STATE_CRITICAL;
  190. }
  191. temp_list = temp_list->name_next;
  192. }
  193. printf ("DISK %s%s\n", state_text (result), output);
  194. return result;
  195. }
  196. /* process command-line arguments */
  197. int
  198. process_arguments (int argc, char **argv)
  199. {
  200. int c;
  201. struct name_list *se;
  202. struct name_list **pathtail = &path_select_list;
  203. struct name_list **fstail = &fs_exclude_list;
  204. struct name_list **dptail = &dp_exclude_list;
  205. struct name_list *temp_list;
  206. int result = OK;
  207. unsigned long l;
  208. int option = 0;
  209. static struct option longopts[] = {
  210. {"timeout", required_argument, 0, 't'},
  211. {"warning", required_argument, 0, 'w'},
  212. {"critical", required_argument, 0, 'c'},
  213. {"local", required_argument, 0, 'l'},
  214. {"kilobytes", required_argument, 0, 'k'},
  215. {"megabytes", required_argument, 0, 'm'},
  216. {"units", required_argument, 0, 'u'},
  217. {"path", required_argument, 0, 'p'},
  218. {"partition", required_argument, 0, 'p'},
  219. {"exclude_device", required_argument, 0, 'x'},
  220. {"exclude-type", required_argument, 0, 'X'},
  221. {"mountpoint", no_argument, 0, 'M'},
  222. {"errors-only", no_argument, 0, 'e'},
  223. {"verbose", no_argument, 0, 'v'},
  224. {"quiet", no_argument, 0, 'q'},
  225. {"clear", no_argument, 0, 'C'},
  226. {"version", no_argument, 0, 'V'},
  227. {"help", no_argument, 0, 'h'},
  228. {0, 0, 0, 0}
  229. };
  230. if (argc < 2)
  231. return ERROR;
  232. se = (struct name_list *) malloc (sizeof (struct name_list));
  233. se->name = strdup ("iso9660");
  234. se->name_next = NULL;
  235. *fstail = se;
  236. fstail = &se->name_next;
  237. for (c = 1; c < argc; c++)
  238. if (strcmp ("-to", argv[c]) == 0)
  239. strcpy (argv[c], "-t");
  240. while (1) {
  241. c = getopt_long (argc, argv, "+?VqhveCt:c:w:u:p:x:X:mklM", longopts, &option);
  242. if (c == -1 || c == EOF)
  243. break;
  244. switch (c) {
  245. case 't': /* timeout period */
  246. if (is_integer (optarg)) {
  247. timeout_interval = atoi (optarg);
  248. break;
  249. }
  250. else {
  251. usage (_("Timeout Interval must be an integer!\n"));
  252. }
  253. case 'w': /* warning threshold */
  254. if (is_intnonneg (optarg)) {
  255. w_df = atoi (optarg);
  256. break;
  257. }
  258. else if (strpbrk (optarg, ",:") &&
  259. strstr (optarg, "%") &&
  260. sscanf (optarg, "%lu%*[:,]%lf%%", &l, &w_dfp) == 2) {
  261. w_df = (uintmax_t)l;
  262. break;
  263. }
  264. else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &w_dfp) == 1) {
  265. break;
  266. }
  267. else {
  268. usage (_("Warning threshold must be integer or percentage!\n"));
  269. }
  270. case 'c': /* critical threshold */
  271. if (is_intnonneg (optarg)) {
  272. c_df = atoi (optarg);
  273. break;
  274. }
  275. else if (strpbrk (optarg, ",:") &&
  276. strstr (optarg, "%") &&
  277. sscanf (optarg, "%lu%*[,:]%lf%%", &l, &c_dfp) == 2) {
  278. c_df = (uintmax_t)l;
  279. break;
  280. }
  281. else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &c_dfp) == 1) {
  282. break;
  283. }
  284. else {
  285. usage (_("Critical threshold must be integer or percentage!\n"));
  286. }
  287. case 'u':
  288. if (units)
  289. free(units);
  290. if (! strcmp (optarg, "bytes")) {
  291. mult = (uintmax_t)1;
  292. units = strdup ("B");
  293. } else if (! strcmp (optarg, "kB")) {
  294. mult = (uintmax_t)1024;
  295. units = strdup ("kB");
  296. } else if (! strcmp (optarg, "MB")) {
  297. mult = (uintmax_t)1024 * 1024;
  298. units = strdup ("MB");
  299. } else if (! strcmp (optarg, "GB")) {
  300. mult = (uintmax_t)1024 * 1024 * 1024;
  301. units = strdup ("GB");
  302. } else if (! strcmp (optarg, "TB")) {
  303. mult = (uintmax_t)1024 * 1024 * 1024 * 1024;
  304. units = strdup ("TB");
  305. } else {
  306. die (STATE_UNKNOWN, _("unit type %s not known\n"), optarg);
  307. }
  308. if (units == NULL)
  309. die (STATE_UNKNOWN, _("failed allocating storage for '%s'\n"), "units");
  310. break;
  311. case 'k': /* display mountpoint */
  312. mult = 1024;
  313. if (units)
  314. free(units);
  315. units = strdup ("kB");
  316. break;
  317. case 'm': /* display mountpoint */
  318. mult = 1024 * 1024;
  319. if (units)
  320. free(units);
  321. units = strdup ("MB");
  322. break;
  323. case 'l':
  324. show_local_fs = 1;
  325. break;
  326. case 'p': /* select path */
  327. se = (struct name_list *) malloc (sizeof (struct name_list));
  328. se->name = optarg;
  329. se->name_next = NULL;
  330. se->w_df = w_df;
  331. se->c_df = c_df;
  332. se->w_dfp = w_dfp;
  333. se->c_dfp = c_dfp;
  334. *pathtail = se;
  335. pathtail = &se->name_next;
  336. break;
  337. case 'x': /* exclude path or partition */
  338. se = (struct name_list *) malloc (sizeof (struct name_list));
  339. se->name = optarg;
  340. se->name_next = NULL;
  341. se->w_df = 0;
  342. se->c_df = 0;
  343. se->w_dfp = -1.0;
  344. se->c_dfp = -1.0;
  345. *dptail = se;
  346. dptail = &se->name_next;
  347. break;
  348. case 'X': /* exclude file system type */
  349. se = (struct name_list *) malloc (sizeof (struct name_list));
  350. se->name = optarg;
  351. se->name_next = NULL;
  352. se->w_df = 0;
  353. se->c_df = 0;
  354. se->w_dfp = -1.0;
  355. se->c_dfp = -1.0;
  356. *fstail = se;
  357. fstail = &se->name_next;
  358. break;
  359. case 'v': /* verbose */
  360. verbose++;
  361. break;
  362. case 'q': /* verbose */
  363. verbose--;
  364. break;
  365. case 'e':
  366. erronly = TRUE;
  367. break;
  368. case 'M': /* display mountpoint */
  369. display_mntp = TRUE;
  370. break;
  371. case 'C':
  372. w_df = 0;
  373. c_df = 0;
  374. w_dfp = -1.0;
  375. c_dfp = -1.0;
  376. break;
  377. case 'V': /* version */
  378. print_revision (progname, revision);
  379. exit (STATE_OK);
  380. case 'h': /* help */
  381. print_help ();
  382. exit (STATE_OK);
  383. case '?': /* help */
  384. usage (_("check_disk: unrecognized option\n"));
  385. break;
  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, uintmax_t 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."));
  501. print_usage ();
  502. printf (_(UT_HELP_VRSN));
  503. printf (_("\
  504. -w, --warning=INTEGER\n\
  505. Exit with WARNING status if less than INTEGER kilobytes 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 kilobytes 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\
  548. %s (-h|--help)\n\
  549. %s (-V|--version)\n"),
  550. progname, progname, progname);
  551. }