check_disk.c 17 KB

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