check_disk.c 24 KB

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