check_disk.c 25 KB

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