check_disk.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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. struct stat *stat_buf;
  251. unsigned long l;
  252. int option = 0;
  253. static struct option longopts[] = {
  254. {"timeout", required_argument, 0, 't'},
  255. {"warning", required_argument, 0, 'w'},
  256. {"critical", required_argument, 0, 'c'},
  257. {"iwarning", required_argument, 0, 'W'},
  258. /* Dang, -C is taken. We might want to reshuffle this. */
  259. {"icritical", required_argument, 0, 'K'},
  260. {"local", required_argument, 0, 'l'},
  261. {"kilobytes", required_argument, 0, 'k'},
  262. {"megabytes", required_argument, 0, 'm'},
  263. {"units", required_argument, 0, 'u'},
  264. {"path", required_argument, 0, 'p'},
  265. {"partition", required_argument, 0, 'p'},
  266. {"exclude_device", required_argument, 0, 'x'},
  267. {"exclude-type", required_argument, 0, 'X'},
  268. {"mountpoint", no_argument, 0, 'M'},
  269. {"errors-only", no_argument, 0, 'e'},
  270. {"verbose", no_argument, 0, 'v'},
  271. {"quiet", no_argument, 0, 'q'},
  272. {"clear", no_argument, 0, 'C'},
  273. {"version", no_argument, 0, 'V'},
  274. {"help", no_argument, 0, 'h'},
  275. {0, 0, 0, 0}
  276. };
  277. if (argc < 2)
  278. return ERROR;
  279. se = (struct name_list *) malloc (sizeof (struct name_list));
  280. se->name = strdup ("iso9660");
  281. se->name_next = NULL;
  282. se->found = 0;
  283. se->found_len = 0;
  284. *fstail = se;
  285. fstail = &se->name_next;
  286. for (c = 1; c < argc; c++)
  287. if (strcmp ("-to", argv[c]) == 0)
  288. strcpy (argv[c], "-t");
  289. while (1) {
  290. c = getopt_long (argc, argv, "+?VqhveCt:c:w:K:W:u:p:x:X:mklM", longopts, &option);
  291. if (c == -1 || c == EOF)
  292. break;
  293. switch (c) {
  294. case 't': /* timeout period */
  295. if (is_integer (optarg)) {
  296. timeout_interval = atoi (optarg);
  297. break;
  298. }
  299. else {
  300. usage2 (_("Timeout interval must be a positive integer"), optarg);
  301. }
  302. case 'w': /* warning threshold */
  303. if (is_intnonneg (optarg)) {
  304. w_df = atoi (optarg);
  305. break;
  306. }
  307. else if (strpbrk (optarg, ",:") &&
  308. strstr (optarg, "%") &&
  309. sscanf (optarg, "%lu%*[:,]%lf%%", &l, &w_dfp) == 2) {
  310. w_df = (uintmax_t)l;
  311. break;
  312. }
  313. else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &w_dfp) == 1) {
  314. break;
  315. }
  316. else {
  317. usage4 (_("Warning threshold must be integer or percentage!"));
  318. }
  319. case 'c': /* critical threshold */
  320. if (is_intnonneg (optarg)) {
  321. c_df = atoi (optarg);
  322. break;
  323. }
  324. else if (strpbrk (optarg, ",:") &&
  325. strstr (optarg, "%") &&
  326. sscanf (optarg, "%lu%*[,:]%lf%%", &l, &c_dfp) == 2) {
  327. c_df = (uintmax_t)l;
  328. break;
  329. }
  330. else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &c_dfp) == 1) {
  331. break;
  332. }
  333. else {
  334. usage4 (_("Critical threshold must be integer or percentage!"));
  335. }
  336. case 'W': /* warning inode threshold */
  337. if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &w_idfp) == 1) {
  338. break;
  339. }
  340. else {
  341. usage (_("Warning inode threshold must be percentage!\n"));
  342. }
  343. case 'K': /* kritical inode threshold */
  344. if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &c_idfp) == 1) {
  345. break;
  346. }
  347. else {
  348. usage (_("Critical inode threshold must be percentage!\n"));
  349. }
  350. case 'u':
  351. if (units)
  352. free(units);
  353. if (! strcmp (optarg, "bytes")) {
  354. mult = (uintmax_t)1;
  355. units = strdup ("B");
  356. } else if (! strcmp (optarg, "kB")) {
  357. mult = (uintmax_t)1024;
  358. units = strdup ("kB");
  359. } else if (! strcmp (optarg, "MB")) {
  360. mult = (uintmax_t)1024 * 1024;
  361. units = strdup ("MB");
  362. } else if (! strcmp (optarg, "GB")) {
  363. mult = (uintmax_t)1024 * 1024 * 1024;
  364. units = strdup ("GB");
  365. } else if (! strcmp (optarg, "TB")) {
  366. mult = (uintmax_t)1024 * 1024 * 1024 * 1024;
  367. units = strdup ("TB");
  368. } else {
  369. die (STATE_UNKNOWN, _("unit type %s not known\n"), optarg);
  370. }
  371. if (units == NULL)
  372. die (STATE_UNKNOWN, _("failed allocating storage for '%s'\n"), "units");
  373. break;
  374. case 'k': /* display mountpoint */
  375. mult = 1024;
  376. if (units)
  377. free(units);
  378. units = strdup ("kB");
  379. break;
  380. case 'm': /* display mountpoint */
  381. mult = 1024 * 1024;
  382. if (units)
  383. free(units);
  384. units = strdup ("MB");
  385. break;
  386. case 'l':
  387. show_local_fs = 1;
  388. break;
  389. case 'p': /* select path */
  390. se = (struct name_list *) malloc (sizeof (struct name_list));
  391. se->name = optarg;
  392. se->name_next = NULL;
  393. se->w_df = w_df;
  394. se->c_df = c_df;
  395. se->w_dfp = w_dfp;
  396. se->c_dfp = c_dfp;
  397. se->found = 0;
  398. se->found_len = 0;
  399. *pathtail = se;
  400. pathtail = &se->name_next;
  401. break;
  402. case 'x': /* exclude path or partition */
  403. se = (struct name_list *) malloc (sizeof (struct name_list));
  404. se->name = optarg;
  405. se->name_next = NULL;
  406. /* If you don't clear the w_fd etc values here, they
  407. * get processed when you walk the list and assigned
  408. * to the global w_df!
  409. */
  410. se->w_df = 0;
  411. se->c_df = 0;
  412. se->w_dfp = 0;
  413. se->c_dfp = 0;
  414. se->found = 0;
  415. se->found_len = 0;
  416. *dptail = se;
  417. dptail = &se->name_next;
  418. break;
  419. case 'X': /* exclude file system type */
  420. se = (struct name_list *) malloc (sizeof (struct name_list));
  421. se->name = optarg;
  422. se->name_next = NULL;
  423. /* If you don't clear the w_fd etc values here, they
  424. * get processed when you walk the list and assigned
  425. * to the global w_df!
  426. */
  427. se->w_df = 0;
  428. se->c_df = 0;
  429. se->w_dfp = 0;
  430. se->c_dfp = 0;
  431. se->found = 0;
  432. se->found_len = 0;
  433. *fstail = se;
  434. fstail = &se->name_next;
  435. break;
  436. case 'v': /* verbose */
  437. verbose++;
  438. break;
  439. case 'q': /* verbose */
  440. verbose--;
  441. break;
  442. case 'e':
  443. erronly = TRUE;
  444. break;
  445. case 'M': /* display mountpoint */
  446. display_mntp = TRUE;
  447. break;
  448. case 'C':
  449. w_df = 0;
  450. c_df = 0;
  451. w_dfp = -1.0;
  452. c_dfp = -1.0;
  453. break;
  454. case 'V': /* version */
  455. print_revision (progname, revision);
  456. exit (STATE_OK);
  457. case 'h': /* help */
  458. print_help ();
  459. exit (STATE_OK);
  460. case '?': /* help */
  461. usage (_("Unknown argument"));
  462. }
  463. }
  464. /* Support for "check_disk warn crit [fs]" with thresholds at used level */
  465. c = optind;
  466. if (w_dfp < 0 && argc > c && is_intnonneg (argv[c]))
  467. w_dfp = (100.0 - atof (argv[c++]));
  468. if (c_dfp < 0 && argc > c && is_intnonneg (argv[c]))
  469. c_dfp = (100.0 - atof (argv[c++]));
  470. if (argc > c && path == NULL) {
  471. se = (struct name_list *) malloc (sizeof (struct name_list));
  472. se->name = strdup (argv[c++]);
  473. se->name_next = NULL;
  474. se->w_df = w_df;
  475. se->c_df = c_df;
  476. se->w_dfp = w_dfp;
  477. se->c_dfp = c_dfp;
  478. se->found =0;
  479. se->found_len = 0;
  480. *pathtail = se;
  481. }
  482. if (path_select_list) {
  483. temp_list = path_select_list;
  484. stat_buf = malloc(sizeof stat_buf);
  485. while (temp_list) {
  486. /* Stat each entry to check that dir exists */
  487. if (stat (temp_list->name, &stat_buf[0])) {
  488. printf("DISK %s - ", _("CRITICAL"));
  489. die (STATE_CRITICAL, _("%s does not exist\n"), temp_list->name);
  490. }
  491. if (validate_arguments (temp_list->w_df,
  492. temp_list->c_df,
  493. temp_list->w_dfp,
  494. temp_list->c_dfp,
  495. temp_list->w_idfp,
  496. temp_list->c_idfp,
  497. temp_list->name) == ERROR)
  498. result = ERROR;
  499. temp_list = temp_list->name_next;
  500. }
  501. free(stat_buf);
  502. return result;
  503. } else {
  504. return validate_arguments (w_df, c_df, w_dfp, c_dfp, w_idfp, c_idfp, NULL);
  505. }
  506. }
  507. void
  508. print_path (const char *mypath)
  509. {
  510. if (mypath == NULL)
  511. printf ("\n");
  512. else
  513. printf (_(" for %s\n"), mypath);
  514. return;
  515. }
  516. int
  517. validate_arguments (uintmax_t w, uintmax_t c, double wp, double cp, double iwp, double icp, char *mypath)
  518. {
  519. if (w < 0 && c < 0 && wp < 0.0 && cp < 0.0) {
  520. printf (_("INPUT ERROR: No thresholds specified"));
  521. print_path (mypath);
  522. return ERROR;
  523. }
  524. else if ((wp >= 0.0 || cp >= 0.0) &&
  525. (wp < 0.0 || cp < 0.0 || wp > 100.0 || cp > 100.0 || cp > wp)) {
  526. printf (_("\
  527. INPUT ERROR: C_DFP (%f) should be less than W_DFP (%.1f) and both should be between zero and 100 percent, inclusive"),
  528. cp, wp);
  529. print_path (mypath);
  530. return ERROR;
  531. }
  532. else if ((iwp >= 0.0 || icp >= 0.0) &&
  533. (iwp < 0.0 || icp < 0.0 || iwp > 100.0 || icp > 100.0 || icp > iwp)) {
  534. printf (_("\
  535. INPUT ERROR: C_IDFP (%f) should be less than W_IDFP (%.1f) and both should be between zero and 100 percent, inclusive"),
  536. icp, iwp);
  537. print_path (mypath);
  538. return ERROR;
  539. }
  540. else if ((w > 0 || c > 0) && (w == 0 || c == 0 || c > w)) {
  541. printf (_("\
  542. INPUT ERROR: C_DF (%lu) should be less than W_DF (%lu) and both should be greater than zero"),
  543. (unsigned long)c, (unsigned long)w);
  544. print_path (mypath);
  545. return ERROR;
  546. }
  547. if (units == NULL) {
  548. units = strdup ("MB");
  549. mult = (uintmax_t)1024 * 1024;
  550. }
  551. return OK;
  552. }
  553. int
  554. check_disk (double usp, uintmax_t free_disk, double uisp)
  555. {
  556. int result = STATE_UNKNOWN;
  557. /* check the percent used space against thresholds */
  558. if (usp >= 0.0 && c_dfp >=0.0 && usp >= (100.0 - c_dfp))
  559. result = STATE_CRITICAL;
  560. else if (uisp >= 0.0 && c_idfp >=0.0 && uisp >= (100.0 - c_idfp))
  561. result = STATE_CRITICAL;
  562. else if (c_df > 0 && free_disk <= c_df)
  563. result = STATE_CRITICAL;
  564. else if (usp >= 0.0 && w_dfp >=0.0 && usp >= (100.0 - w_dfp))
  565. result = STATE_WARNING;
  566. else if (uisp >= 0.0 && w_idfp >=0.0 && uisp >= (100.0 - w_idfp))
  567. result = STATE_WARNING;
  568. else if (w_df > 0 && free_disk <= w_df)
  569. result = STATE_WARNING;
  570. else if (usp >= 0.0)
  571. result = STATE_OK;
  572. return result;
  573. }
  574. int
  575. walk_name_list (struct name_list *list, const char *name)
  576. {
  577. int name_len;
  578. name_len = strlen(name);
  579. while (list) {
  580. /* if the paths match up to the length of the mount path,
  581. * AND if the mount path name is longer than the longest
  582. * found match, we have a new winner */
  583. if (name_len >= list->found_len &&
  584. ! strncmp(list->name, name, name_len)) {
  585. list->found = 1;
  586. list->found_len = name_len;
  587. /* if required for name_lists that have not saved w_df, etc (eg exclude lists) */
  588. if (list->w_df) w_df = list->w_df;
  589. if (list->c_df) c_df = list->c_df;
  590. if (list->w_dfp>=0.0) w_dfp = list->w_dfp;
  591. if (list->c_dfp>=0.0) c_dfp = list->c_dfp;
  592. return TRUE;
  593. }
  594. list = list->name_next;
  595. }
  596. return FALSE;
  597. }
  598. void
  599. print_help (void)
  600. {
  601. print_revision (progname, revision);
  602. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  603. printf (COPYRIGHT, copyright, email);
  604. printf (_("This plugin checks the amount of used disk space on a mounted file system"));
  605. printf (_("and generates an alert if free space is less than one of the threshold values"));
  606. printf ("\n\n");
  607. print_usage ();
  608. printf (_(UT_HELP_VRSN));
  609. printf (" %s\n", "-w, --warning=INTEGER");
  610. printf (" %s\n", _("Exit with WARNING status if less than INTEGER units of disk are free"));
  611. printf (" %s\n", "-w, --warning=PERCENT%");
  612. printf (" %s\n", _("Exit with WARNING status if less than PERCENT of disk space is free"));
  613. printf (" %s\n", "-W, --iwarning=PERCENT%");
  614. printf (" %s\n", _("Exit with WARNING status if less than PERCENT of inode space is free"));
  615. printf (" %s\n", "-K, --icritical=PERCENT%");
  616. printf (" %s\n", _("Exit with CRITICAL status if less than PERCENT of inode space is free"));
  617. printf (" %s\n", "-c, --critical=INTEGER");
  618. printf (" %s\n", _("Exit with CRITICAL status if less than INTEGER units of disk are free"));
  619. printf (" %s\n", "-c, --critical=PERCENT%");
  620. printf (" %s\n", _("Exit with CRITCAL status if less than PERCENT of disk space is free"));
  621. printf (" %s\n", "-C, --clear");
  622. printf (" %s\n", _("Clear thresholds"));
  623. printf (" %s\n", "-u, --units=STRING");
  624. printf (" %s\n", _("Choose bytes, kB, MB, GB, TB (default: MB)"));
  625. printf (" %s\n", "-k, --kilobytes");
  626. printf (" %s\n", _("Same as '--units kB'"));
  627. printf (" %s\n", "-m, --megabytes");
  628. printf (" %s\n", _("Same as '--units MB'"));
  629. printf (" %s\n", "-l, --local");
  630. printf (" %s\n", _("Only check local filesystems"));
  631. printf (" %s\n", "-p, --path=PATH, --partition=PARTITION");
  632. printf (" %s\n", _("Path or partition (may be repeated)"));
  633. printf (" %s\n", "-x, --exclude_device=PATH <STRING>");
  634. printf (" %s\n", _("Ignore device (only works if -p unspecified)"));
  635. printf (" %s\n", _("-X, --exclude-type=TYPE <STRING>"));
  636. printf (" %s\n", _("Ignore all filesystems of indicated type (may be repeated)"));
  637. printf (" %s\n", "-m, --mountpoint");
  638. printf (" %s\n", _("Display the mountpoint instead of the partition"));
  639. printf (" %s\n", "-e, --errors-only");
  640. printf (" %s\n", _("Display only devices/mountpoints with errors"));
  641. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  642. printf (_(UT_VERBOSE));
  643. printf ("\n");
  644. printf ("%s\n", _("Examples:"));
  645. printf (" %s\n", "check_disk -w 10% -c 5% -p /tmp -p /var -C -w 100000 -c 50000 -p /");
  646. printf (" %s\n", _("Checks /tmp and /var at 10% and 5%, and / at 100MB and 50MB"));
  647. printf (_(UT_SUPPORT));
  648. }
  649. void
  650. print_usage (void)
  651. {
  652. printf (_("Usage:"));
  653. printf (" %s -w limit -c limit [-p path | -x device] [-t timeout]", progname);
  654. printf ("[-m] [-e] [-W limit] [-K limit] [-v] [-q]\n");
  655. }