check_disk.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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. #include "common.h"
  39. #ifdef HAVE_SYS_STAT_H
  40. # include <sys/stat.h>
  41. #endif
  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 "utils_disk.h"
  49. #include <stdarg.h>
  50. #include "fsusage.h"
  51. #include "mountlist.h"
  52. #include "intprops.h" /* necessary for TYPE_MAXIMUM */
  53. #if HAVE_LIMITS_H
  54. # include <limits.h>
  55. #endif
  56. #include "regex.h"
  57. /* If nonzero, show inode information. */
  58. static int inode_format = 1;
  59. /* If nonzero, show even filesystems with zero size or
  60. uninteresting types. */
  61. static int show_all_fs = 1;
  62. /* If nonzero, show only local filesystems. */
  63. static int show_local_fs = 0;
  64. /* If positive, the units to use when printing sizes;
  65. if negative, the human-readable base. */
  66. /* static int output_block_size; */
  67. /* If nonzero, invoke the `sync' system call before getting any usage data.
  68. Using this option can make df very slow, especially with many or very
  69. busy disks. Note that this may make a difference on some systems --
  70. SunOs4.1.3, for one. It is *not* necessary on Linux. */
  71. /* static int require_sync = 0; */
  72. /* Linked list of filesystem types to display.
  73. If `fs_select_list' is NULL, list all types.
  74. This table is generated dynamically from command-line options,
  75. rather than hardcoding into the program what it thinks are the
  76. valid filesystem types; let the user specify any filesystem type
  77. they want to, and if there are any filesystems of that type, they
  78. will be shown.
  79. Some filesystem types:
  80. 4.2 4.3 ufs nfs swap ignore io vm efs dbg */
  81. /* static struct parameter_list *fs_select_list; */
  82. /* Linked list of filesystem types to omit.
  83. If the list is empty, don't exclude any types. */
  84. static struct name_list *fs_exclude_list;
  85. static struct name_list *dp_exclude_list;
  86. static struct parameter_list *path_select_list = NULL;
  87. /* Linked list of mounted filesystems. */
  88. static struct mount_entry *mount_list;
  89. /* For long options that have no equivalent short option, use a
  90. non-character as a pseudo short option, starting with CHAR_MAX + 1. */
  91. enum
  92. {
  93. SYNC_OPTION = CHAR_MAX + 1,
  94. NO_SYNC_OPTION,
  95. BLOCK_SIZE_OPTION
  96. };
  97. #ifdef _AIX
  98. #pragma alloca
  99. #endif
  100. /* Linked list of mounted filesystems. */
  101. static struct mount_entry *mount_list;
  102. int process_arguments (int, char **);
  103. void print_path (const char *mypath);
  104. void set_all_thresholds (struct parameter_list *path);
  105. int validate_arguments (uintmax_t, uintmax_t, double, double, double, double, char *);
  106. void print_help (void);
  107. void print_usage (void);
  108. double calculate_percent(uintmax_t, uintmax_t);
  109. double w_dfp = -1.0;
  110. double c_dfp = -1.0;
  111. char *path;
  112. char *exclude_device;
  113. char *units;
  114. uintmax_t mult = 1024 * 1024;
  115. int verbose = 0;
  116. int erronly = FALSE;
  117. int display_mntp = FALSE;
  118. int exact_match = FALSE;
  119. char *warn_freespace_units = NULL;
  120. char *crit_freespace_units = NULL;
  121. char *warn_freespace_percent = NULL;
  122. char *crit_freespace_percent = NULL;
  123. char *warn_usedspace_units = NULL;
  124. char *crit_usedspace_units = NULL;
  125. char *warn_usedspace_percent = NULL;
  126. char *crit_usedspace_percent = NULL;
  127. char *warn_usedinodes_percent = NULL;
  128. char *crit_usedinodes_percent = NULL;
  129. char *warn_freeinodes_percent = NULL;
  130. char *crit_freeinodes_percent = NULL;
  131. bool path_selected = false;
  132. char *group = NULL;
  133. int
  134. main (int argc, char **argv)
  135. {
  136. int result = STATE_UNKNOWN;
  137. int disk_result = STATE_UNKNOWN;
  138. char *output;
  139. char *details;
  140. char *perf;
  141. char *preamble;
  142. double inode_space_pct;
  143. uintmax_t total, available, available_to_root, used;
  144. double dfree_pct = -1, dused_pct = -1;
  145. double dused_units, dfree_units, dtotal_units;
  146. double dused_inodes_percent, dfree_inodes_percent;
  147. double warning_high_tide;
  148. double critical_high_tide;
  149. int temp_result;
  150. struct mount_entry *me;
  151. struct fs_usage fsp, tmpfsp;
  152. struct parameter_list *temp_list, *path;
  153. struct name_list *seen = NULL;
  154. preamble = strdup (" - free space:");
  155. output = strdup ("");
  156. details = strdup ("");
  157. perf = strdup ("");
  158. setlocale (LC_ALL, "");
  159. bindtextdomain (PACKAGE, LOCALEDIR);
  160. textdomain (PACKAGE);
  161. mount_list = read_file_system_list (0);
  162. if (process_arguments (argc, argv) == ERROR)
  163. usage4 (_("Could not parse arguments"));
  164. /* If a list of paths has not been selected, find entire
  165. mount list and create list of paths
  166. */
  167. if (path_selected == false) {
  168. for (me = mount_list; me; me = me->me_next) {
  169. if (! (path = np_find_parameter(path_select_list, me->me_mountdir))) {
  170. path = np_add_parameter(&path_select_list, me->me_mountdir);
  171. }
  172. path->best_match = me;
  173. path->group = group;
  174. set_all_thresholds(path);
  175. }
  176. }
  177. np_set_best_match(path_select_list, mount_list, exact_match);
  178. /* Error if no match found for specified paths */
  179. temp_list = path_select_list;
  180. while (temp_list) {
  181. if (! temp_list->best_match) {
  182. die (STATE_CRITICAL, _("DISK %s: %s not found\n"), _("CRITICAL"), temp_list->name);
  183. }
  184. temp_list = temp_list->name_next;
  185. }
  186. /* Process for every path in list */
  187. for (path = path_select_list; path; path=path->name_next) {
  188. if (verbose > 3 && path->freespace_percent->warning != NULL && path->freespace_percent->critical != NULL)
  189. printf("Thresholds(pct) for %s warn: %f crit %f\n",path->name, path->freespace_percent->warning->end,
  190. path->freespace_percent->critical->end);
  191. if (verbose > 3 && path->group != NULL)
  192. printf("Group of %s: %s\n",path->name,path->group);
  193. /* reset disk result */
  194. disk_result = STATE_UNKNOWN;
  195. me = path->best_match;
  196. /* Filters */
  197. /* Remove filesystems already seen */
  198. if (np_seen_name(seen, me->me_mountdir)) {
  199. continue;
  200. } else {
  201. if (path->group != NULL) {
  202. /* find all group members */
  203. fsp.fsu_blocksize = 0;
  204. fsp.fsu_blocks = 0;
  205. fsp.fsu_bfree = 0;
  206. fsp.fsu_bavail = 0;
  207. fsp.fsu_files = 0;
  208. fsp.fsu_ffree = 0;
  209. for (temp_list = path_select_list; temp_list; temp_list=temp_list->name_next) {
  210. if (temp_list->group && ! (strcmp(temp_list->group, path->group))) {
  211. get_fs_usage (temp_list->best_match->me_mountdir, temp_list->best_match->me_devname, &tmpfsp);
  212. /* possibly differing blocksizes if disks are grouped. Calculating average */
  213. fsp.fsu_blocksize = (fsp.fsu_blocksize * fsp.fsu_blocks + tmpfsp.fsu_blocksize * tmpfsp.fsu_blocks) / \
  214. (fsp.fsu_blocks + tmpfsp.fsu_blocks); /* Size of a block. */
  215. fsp.fsu_blocks += tmpfsp.fsu_blocks; /* Total blocks. */
  216. fsp.fsu_bfree += tmpfsp.fsu_bfree; /* Free blocks available to superuser. */
  217. fsp.fsu_bavail += tmpfsp.fsu_bavail; /* Free blocks available to non-superuser. */
  218. fsp.fsu_files += tmpfsp.fsu_files; /* Total file nodes. */
  219. fsp.fsu_ffree += tmpfsp.fsu_ffree; /* Free file nodes. */
  220. if (verbose > 3)
  221. printf("Group %s: add %llu blocks (%s) \n", path->group, tmpfsp.fsu_bavail, temp_list->name);
  222. // printf("Group %s: add %u blocks (%s)\n", temp_list->name); // path->group, tmpfsp.fsu_bavail, temp_list->name);
  223. np_add_name(&seen, temp_list->best_match->me_mountdir);
  224. }
  225. }
  226. /* modify devname and mountdir for output */
  227. me->me_mountdir = me->me_devname = path->group;
  228. } else
  229. np_add_name(&seen, me->me_mountdir);
  230. }
  231. if (path->group == NULL) {
  232. /* Skip remote filesystems if we're not interested in them */
  233. if (me->me_remote && show_local_fs) {
  234. continue;
  235. /* Skip pseudo fs's if we haven't asked for all fs's */
  236. } else if (me->me_dummy && !show_all_fs) {
  237. continue;
  238. /* Skip excluded fstypes */
  239. } else if (fs_exclude_list && np_find_name (fs_exclude_list, me->me_type)) {
  240. continue;
  241. /* Skip excluded fs's */
  242. } else if (dp_exclude_list &&
  243. (np_find_name (dp_exclude_list, me->me_devname) ||
  244. np_find_name (dp_exclude_list, me->me_mountdir))) {
  245. continue;
  246. }
  247. get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
  248. }
  249. if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
  250. total = fsp.fsu_blocks;
  251. available = fsp.fsu_bavail;
  252. available_to_root = fsp.fsu_bfree;
  253. used = total - available_to_root;
  254. dused_pct = calculate_percent( used, used + available ); /* used + available can never be > uintmax */
  255. dfree_pct = 100 - dused_pct;
  256. dused_units = used*fsp.fsu_blocksize/mult;
  257. dfree_units = available*fsp.fsu_blocksize/mult;
  258. dtotal_units = total*fsp.fsu_blocksize/mult;
  259. dused_inodes_percent = calculate_percent(fsp.fsu_files - fsp.fsu_ffree, fsp.fsu_files);
  260. dfree_inodes_percent = 100 - dused_inodes_percent;
  261. if (verbose >= 3) {
  262. printf ("For %s, used_pct=%g free_pct=%g used_units=%g free_units=%g total_units=%g used_inodes_pct=%g free_inodes_pct=%g\n",
  263. me->me_mountdir, dused_pct, dfree_pct, dused_units, dfree_units, dtotal_units, dused_inodes_percent, dfree_inodes_percent);
  264. }
  265. /* Threshold comparisons */
  266. temp_result = get_status(dfree_units, path->freespace_units);
  267. if (verbose >=3) printf("Freespace_units result=%d\n", temp_result);
  268. disk_result = max_state( disk_result, temp_result );
  269. temp_result = get_status(dfree_pct, path->freespace_percent);
  270. if (verbose >=3) printf("Freespace%% result=%d\n", temp_result);
  271. disk_result = max_state( disk_result, temp_result );
  272. temp_result = get_status(dused_units, path->usedspace_units);
  273. if (verbose >=3) printf("Usedspace_units result=%d\n", temp_result);
  274. disk_result = max_state( disk_result, temp_result );
  275. temp_result = get_status(dused_pct, path->usedspace_percent);
  276. if (verbose >=3) printf("Usedspace_percent result=%d\n", temp_result);
  277. disk_result = max_state( disk_result, temp_result );
  278. temp_result = get_status(dused_inodes_percent, path->usedinodes_percent);
  279. if (verbose >=3) printf("Usedinodes_percent result=%d\n", temp_result);
  280. disk_result = max_state( disk_result, temp_result );
  281. temp_result = get_status(dfree_inodes_percent, path->freeinodes_percent);
  282. if (verbose >=3) printf("Freeinodes_percent result=%d\n", temp_result);
  283. disk_result = max_state( disk_result, temp_result );
  284. result = max_state(result, disk_result);
  285. /* What a mess of units. The output shows free space, the perf data shows used space. Yikes!
  286. Hack here. Trying to get warn/crit levels from freespace_(units|percent) for perf
  287. data. Assumption that start=0. Roll on new syntax...
  288. */
  289. /* *_high_tide must be reinitialized at each run */
  290. warning_high_tide = UINT_MAX;
  291. critical_high_tide = UINT_MAX;
  292. if (path->freespace_units->warning != NULL) {
  293. warning_high_tide = dtotal_units - path->freespace_units->warning->end;
  294. }
  295. if (path->freespace_percent->warning != NULL) {
  296. warning_high_tide = abs( min( (double) warning_high_tide, (double) (1.0 - path->freespace_percent->warning->end/100)*dtotal_units ));
  297. }
  298. if (path->freespace_units->critical != NULL) {
  299. critical_high_tide = dtotal_units - path->freespace_units->critical->end;
  300. }
  301. if (path->freespace_percent->critical != NULL) {
  302. critical_high_tide = abs( min( (double) critical_high_tide, (double) (1.0 - path->freespace_percent->critical->end/100)*dtotal_units ));
  303. }
  304. asprintf (&perf, "%s %s", perf,
  305. perfdata ((!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
  306. dused_units, units,
  307. (warning_high_tide != UINT_MAX ? TRUE : FALSE), warning_high_tide,
  308. (critical_high_tide != UINT_MAX ? TRUE : FALSE), critical_high_tide,
  309. TRUE, 0,
  310. TRUE, dtotal_units));
  311. if (disk_result==STATE_OK && erronly && !verbose)
  312. continue;
  313. if (disk_result!=STATE_OK || verbose>=0) {
  314. asprintf (&output, "%s %s %.0f %s (%.0f%%",
  315. output,
  316. (!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
  317. dfree_units,
  318. units,
  319. dfree_pct);
  320. if (dused_inodes_percent < 0) {
  321. asprintf(&output, "%s inode=-);", output);
  322. } else {
  323. asprintf(&output, "%s inode=%.0f%%);", output, dfree_inodes_percent );
  324. }
  325. }
  326. /* TODO: Need to do a similar debug line
  327. asprintf (&details, _("%s\n\
  328. %.0f of %.0f %s (%.0f%% inode=%.0f%%) free on %s (type %s mounted on %s) warn:%lu crit:%lu warn%%:%.0f%% crit%%:%.0f%%"),
  329. details, dfree_units, dtotal_units, units, dfree_pct, inode_space_pct,
  330. me->me_devname, me->me_type, me->me_mountdir,
  331. (unsigned long)w_df, (unsigned long)c_df, w_dfp, c_dfp);
  332. */
  333. }
  334. }
  335. if (verbose > 2)
  336. asprintf (&output, "%s%s", output, details);
  337. printf ("DISK %s%s%s|%s\n", state_text (result), (erronly && result==STATE_OK) ? "" : preamble, output, perf);
  338. return result;
  339. }
  340. double calculate_percent(uintmax_t value, uintmax_t total) {
  341. double pct = -1;
  342. /* I don't understand the below, but it is taken from coreutils' df */
  343. /* Seems to be calculating pct, in the best possible way */
  344. if (value <= TYPE_MAXIMUM(uintmax_t) / 100
  345. && total != 0) {
  346. uintmax_t u100 = value * 100;
  347. pct = u100 / total + (u100 % total != 0);
  348. } else {
  349. /* Possible rounding errors - see coreutils' df for more explanation */
  350. double u = value;
  351. double t = total;
  352. if (t) {
  353. long int lipct = pct = u * 100 / t;
  354. double ipct = lipct;
  355. /* Like 'pct = ceil (dpct);', but without ceil - from coreutils again */
  356. if (ipct - 1 < pct && pct <= ipct + 1)
  357. pct = ipct + (ipct < pct);
  358. }
  359. }
  360. return pct;
  361. }
  362. /* process command-line arguments */
  363. int
  364. process_arguments (int argc, char **argv)
  365. {
  366. int c, err;
  367. struct parameter_list *se, *se2;
  368. struct parameter_list *temp_list;
  369. struct parameter_list *temp_path_select_list = NULL;
  370. struct mount_entry *me;
  371. int result = OK;
  372. struct stat *stat_buf;
  373. regex_t re;
  374. int cflags = REG_NOSUB | REG_EXTENDED;
  375. char errbuf[MAX_INPUT_BUFFER];
  376. bool fnd = false;
  377. int option = 0;
  378. static struct option longopts[] = {
  379. {"timeout", required_argument, 0, 't'},
  380. {"warning", required_argument, 0, 'w'},
  381. {"critical", required_argument, 0, 'c'},
  382. {"iwarning", required_argument, 0, 'W'},
  383. /* Dang, -C is taken. We might want to reshuffle this. */
  384. {"icritical", required_argument, 0, 'K'},
  385. {"local", required_argument, 0, 'l'},
  386. {"kilobytes", required_argument, 0, 'k'},
  387. {"megabytes", required_argument, 0, 'm'},
  388. {"units", required_argument, 0, 'u'},
  389. {"path", required_argument, 0, 'p'},
  390. {"partition", required_argument, 0, 'p'},
  391. {"exclude_device", required_argument, 0, 'x'},
  392. {"exclude-type", required_argument, 0, 'X'},
  393. {"group", required_argument, 0, 'g'},
  394. {"eregi-path", required_argument, 0, 'R'},
  395. {"eregi-partition", required_argument, 0, 'R'},
  396. {"ereg-path", required_argument, 0, 'r'},
  397. {"ereg-partition", required_argument, 0, 'r'},
  398. {"mountpoint", no_argument, 0, 'M'},
  399. {"errors-only", no_argument, 0, 'e'},
  400. {"exact-match", no_argument, 0, 'E'},
  401. {"verbose", no_argument, 0, 'v'},
  402. {"quiet", no_argument, 0, 'q'},
  403. {"clear", no_argument, 0, 'C'},
  404. {"version", no_argument, 0, 'V'},
  405. {"help", no_argument, 0, 'h'},
  406. {0, 0, 0, 0}
  407. };
  408. if (argc < 2)
  409. return ERROR;
  410. np_add_name(&fs_exclude_list, "iso9660");
  411. for (c = 1; c < argc; c++)
  412. if (strcmp ("-to", argv[c]) == 0)
  413. strcpy (argv[c], "-t");
  414. while (1) {
  415. c = getopt_long (argc, argv, "+?VqhveCt:c:w:K:W:u:p:x:X:mklg:R:r:ME", longopts, &option);
  416. if (c == -1 || c == EOF)
  417. break;
  418. switch (c) {
  419. case 't': /* timeout period */
  420. if (is_integer (optarg)) {
  421. timeout_interval = atoi (optarg);
  422. break;
  423. }
  424. else {
  425. usage2 (_("Timeout interval must be a positive integer"), optarg);
  426. }
  427. /* See comments for 'c' */
  428. case 'w': /* warning threshold */
  429. if (strstr(optarg, "%")) {
  430. if (*optarg == '@') {
  431. warn_freespace_percent = optarg;
  432. } else {
  433. asprintf(&warn_freespace_percent, "@%s", optarg);
  434. }
  435. } else {
  436. if (*optarg == '@') {
  437. warn_freespace_units = optarg;
  438. } else {
  439. asprintf(&warn_freespace_units, "@%s", optarg);
  440. }
  441. }
  442. break;
  443. /* Awful mistake where the range values do not make sense. Normally,
  444. you alert if the value is within the range, but since we are using
  445. freespace, we have to alert if outside the range. Thus we artifically
  446. force @ at the beginning of the range, so that it is backwards compatible
  447. */
  448. case 'c': /* critical threshold */
  449. if (strstr(optarg, "%")) {
  450. if (*optarg == '@') {
  451. crit_freespace_percent = optarg;
  452. } else {
  453. asprintf(&crit_freespace_percent, "@%s", optarg);
  454. }
  455. } else {
  456. if (*optarg == '@') {
  457. crit_freespace_units = optarg;
  458. } else {
  459. asprintf(&crit_freespace_units, "@%s", optarg);
  460. }
  461. }
  462. break;
  463. case 'W': /* warning inode threshold */
  464. if (*optarg == '@') {
  465. warn_freeinodes_percent = optarg;
  466. } else {
  467. asprintf(&warn_freeinodes_percent, "@%s", optarg);
  468. }
  469. break;
  470. case 'K': /* critical inode threshold */
  471. if (*optarg == '@') {
  472. crit_freeinodes_percent = optarg;
  473. } else {
  474. asprintf(&crit_freeinodes_percent, "@%s", optarg);
  475. }
  476. break;
  477. case 'u':
  478. if (units)
  479. free(units);
  480. if (! strcmp (optarg, "bytes")) {
  481. mult = (uintmax_t)1;
  482. units = strdup ("B");
  483. } else if (! strcmp (optarg, "kB")) {
  484. mult = (uintmax_t)1024;
  485. units = strdup ("kB");
  486. } else if (! strcmp (optarg, "MB")) {
  487. mult = (uintmax_t)1024 * 1024;
  488. units = strdup ("MB");
  489. } else if (! strcmp (optarg, "GB")) {
  490. mult = (uintmax_t)1024 * 1024 * 1024;
  491. units = strdup ("GB");
  492. } else if (! strcmp (optarg, "TB")) {
  493. mult = (uintmax_t)1024 * 1024 * 1024 * 1024;
  494. units = strdup ("TB");
  495. } else {
  496. die (STATE_UNKNOWN, _("unit type %s not known\n"), optarg);
  497. }
  498. if (units == NULL)
  499. die (STATE_UNKNOWN, _("failed allocating storage for '%s'\n"), "units");
  500. break;
  501. case 'k': /* display mountpoint */
  502. mult = 1024;
  503. if (units)
  504. free(units);
  505. units = strdup ("kB");
  506. break;
  507. case 'm': /* display mountpoint */
  508. mult = 1024 * 1024;
  509. if (units)
  510. free(units);
  511. units = strdup ("MB");
  512. break;
  513. case 'l':
  514. show_local_fs = 1;
  515. break;
  516. case 'p': /* select path */
  517. if (! (warn_freespace_units || crit_freespace_units || warn_freespace_percent ||
  518. crit_freespace_percent || warn_usedspace_units || crit_usedspace_units ||
  519. warn_usedspace_percent || crit_usedspace_percent || warn_usedinodes_percent ||
  520. crit_usedinodes_percent || warn_freeinodes_percent || crit_freeinodes_percent )) {
  521. die (STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set a threshold value before using -p\n"));
  522. }
  523. /* get the real mountdir of the specified path. np_find_parameter won't find an entry if -p is not
  524. * exactly the same string as the mountdir */
  525. se2 = np_add_parameter(&temp_path_select_list, optarg);
  526. np_set_best_match(se2, mount_list, FALSE);
  527. /* add parameter if not found. overwrite thresholds if path has already been added */
  528. if (! (se = np_find_parameter(path_select_list, optarg))) {
  529. se = np_add_parameter(&path_select_list, optarg);
  530. }
  531. se->group = group;
  532. set_all_thresholds(se);
  533. path_selected = true;
  534. break;
  535. case 'x': /* exclude path or partition */
  536. np_add_name(&dp_exclude_list, optarg);
  537. break;
  538. case 'X': /* exclude file system type */
  539. np_add_name(&fs_exclude_list, optarg);
  540. break;
  541. case 'v': /* verbose */
  542. verbose++;
  543. break;
  544. case 'q': /* verbose */
  545. verbose--;
  546. break;
  547. case 'e':
  548. erronly = TRUE;
  549. break;
  550. case 'E':
  551. exact_match = TRUE;
  552. break;
  553. case 'g':
  554. if (path_selected)
  555. die (STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set group value before using -p\n"));
  556. group = optarg;
  557. break;
  558. case 'R':
  559. cflags |= REG_ICASE;
  560. case 'r':
  561. if (! (warn_freespace_units || crit_freespace_units || warn_freespace_percent ||
  562. crit_freespace_percent || warn_usedspace_units || crit_usedspace_units ||
  563. warn_usedspace_percent || crit_usedspace_percent || warn_usedinodes_percent ||
  564. crit_usedinodes_percent || warn_freeinodes_percent || crit_freeinodes_percent )) {
  565. die (STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set a threshold value before using -r/-R\n"));
  566. }
  567. err = regcomp(&re, optarg, cflags);
  568. if (err != 0) {
  569. regerror (err, &re, errbuf, MAX_INPUT_BUFFER);
  570. die (STATE_UNKNOWN, "DISK %s: %s - %s\n",_("UNKNOWN"), _("Could not compile regular expression"), errbuf);
  571. }
  572. for (me = mount_list; me; me = me->me_next) {
  573. if (np_regex_match_mount_entry(me, &re)) {
  574. fnd = true;
  575. if (verbose > 3)
  576. printf("%s %s matching expression %s\n", me->me_devname, me->me_mountdir, optarg);
  577. /* add parameter if not found. overwrite thresholds if path has already been added */
  578. if (! (se = np_find_parameter(path_select_list, me->me_mountdir))) {
  579. se = np_add_parameter(&path_select_list, me->me_mountdir);
  580. }
  581. se->group = group;
  582. set_all_thresholds(se);
  583. }
  584. }
  585. if (!fnd)
  586. die (STATE_UNKNOWN, "DISK %s: %s - %s\n",_("UNKNOWN"),
  587. _("Regular expression did not match any path or disk"), optarg);
  588. fnd = false;
  589. path_selected = true;
  590. break;
  591. case 'M': /* display mountpoint */
  592. display_mntp = TRUE;
  593. break;
  594. case 'C':
  595. /* add all mount entries to path_select list if no partitions have been explicitly defined using -p */
  596. if (path_selected == false) {
  597. struct mount_entry *me;
  598. struct parameter_list *path;
  599. for (me = mount_list; me; me = me->me_next) {
  600. if (! (path = np_find_parameter(path_select_list, me->me_mountdir)))
  601. path = np_add_parameter(&path_select_list, me->me_mountdir);
  602. path->best_match = me;
  603. path->group = group;
  604. set_all_thresholds(path);
  605. }
  606. }
  607. warn_freespace_units = NULL;
  608. crit_freespace_units = NULL;
  609. warn_usedspace_units = NULL;
  610. crit_usedspace_units = NULL;
  611. warn_freespace_percent = NULL;
  612. crit_freespace_percent = NULL;
  613. warn_usedspace_percent = NULL;
  614. crit_usedspace_percent = NULL;
  615. warn_usedinodes_percent = NULL;
  616. crit_usedinodes_percent = NULL;
  617. warn_freeinodes_percent = NULL;
  618. crit_freeinodes_percent = NULL;
  619. path_selected = false;
  620. group = NULL;
  621. break;
  622. case 'V': /* version */
  623. print_revision (progname, revision);
  624. exit (STATE_OK);
  625. case 'h': /* help */
  626. print_help ();
  627. exit (STATE_OK);
  628. case '?': /* help */
  629. usage (_("Unknown argument"));
  630. }
  631. }
  632. /* Support for "check_disk warn crit [fs]" with thresholds at used% level */
  633. c = optind;
  634. if (warn_usedspace_percent == NULL && argc > c && is_intnonneg (argv[c]))
  635. warn_usedspace_percent = argv[c++];
  636. if (crit_usedspace_percent == NULL && argc > c && is_intnonneg (argv[c]))
  637. crit_usedspace_percent = argv[c++];
  638. if (argc > c && path == NULL) {
  639. se = np_add_parameter(&path_select_list, strdup(argv[c++]));
  640. path_selected = true;
  641. set_all_thresholds(se);
  642. }
  643. if (units == NULL) {
  644. units = strdup ("MB");
  645. mult = (uintmax_t)1024 * 1024;
  646. }
  647. if (path_select_list) {
  648. temp_list = path_select_list;
  649. stat_buf = malloc(sizeof *stat_buf);
  650. while (temp_list) {
  651. /* Stat each entry to check that dir exists */
  652. if (stat (temp_list->name, &stat_buf[0])) {
  653. printf("DISK %s - ", _("CRITICAL"));
  654. die (STATE_CRITICAL, _("%s does not exist\n"), temp_list->name);
  655. }
  656. /* if (validate_arguments (temp_list->w_df,
  657. temp_list->c_df,
  658. temp_list->w_dfp,
  659. temp_list->c_dfp,
  660. temp_list->w_idfp,
  661. temp_list->c_idfp,
  662. temp_list->name) == ERROR)
  663. result = ERROR;
  664. */
  665. temp_list = temp_list->name_next;
  666. }
  667. free(stat_buf);
  668. return result;
  669. } else {
  670. return TRUE;
  671. /* return validate_arguments (w_df, c_df, w_dfp, c_dfp, w_idfp, c_idfp, NULL); */
  672. }
  673. }
  674. void
  675. print_path (const char *mypath)
  676. {
  677. if (mypath == NULL)
  678. printf ("\n");
  679. else
  680. printf (_(" for %s\n"), mypath);
  681. }
  682. void
  683. set_all_thresholds (struct parameter_list *path)
  684. {
  685. set_thresholds(&path->freespace_units, warn_freespace_units, crit_freespace_units);
  686. set_thresholds(&path->freespace_percent, warn_freespace_percent, crit_freespace_percent);
  687. set_thresholds(&path->usedspace_units, warn_usedspace_units, crit_usedspace_units);
  688. set_thresholds(&path->usedspace_percent, warn_usedspace_percent, crit_usedspace_percent);
  689. set_thresholds(&path->usedinodes_percent, warn_usedinodes_percent, crit_usedinodes_percent);
  690. set_thresholds(&path->freeinodes_percent, warn_freeinodes_percent, crit_freeinodes_percent);
  691. }
  692. /* TODO: Remove?
  693. int
  694. validate_arguments (uintmax_t w, uintmax_t c, double wp, double cp, double iwp, double icp, char *mypath)
  695. {
  696. if (w < 0 && c < 0 && wp < 0.0 && cp < 0.0) {
  697. printf (_("INPUT ERROR: No thresholds specified"));
  698. print_path (mypath);
  699. return ERROR;
  700. }
  701. else if ((wp >= 0.0 || cp >= 0.0) &&
  702. (wp < 0.0 || cp < 0.0 || wp > 100.0 || cp > 100.0 || cp > wp)) {
  703. printf (_("\
  704. INPUT ERROR: C_DFP (%f) should be less than W_DFP (%.1f) and both should be between zero and 100 percent, inclusive"),
  705. cp, wp);
  706. print_path (mypath);
  707. return ERROR;
  708. }
  709. else if ((iwp >= 0.0 || icp >= 0.0) &&
  710. (iwp < 0.0 || icp < 0.0 || iwp > 100.0 || icp > 100.0 || icp > iwp)) {
  711. printf (_("\
  712. INPUT ERROR: C_IDFP (%f) should be less than W_IDFP (%.1f) and both should be between zero and 100 percent, inclusive"),
  713. icp, iwp);
  714. print_path (mypath);
  715. return ERROR;
  716. }
  717. else if ((w > 0 || c > 0) && (w == 0 || c == 0 || c > w)) {
  718. printf (_("\
  719. INPUT ERROR: C_DF (%lu) should be less than W_DF (%lu) and both should be greater than zero"),
  720. (unsigned long)c, (unsigned long)w);
  721. print_path (mypath);
  722. return ERROR;
  723. }
  724. return OK;
  725. }
  726. */
  727. void
  728. print_help (void)
  729. {
  730. print_revision (progname, revision);
  731. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  732. printf (COPYRIGHT, copyright, email);
  733. printf ("%s\n", _("This plugin checks the amount of used disk space on a mounted file system"));
  734. printf ("%s\n", _("and generates an alert if free space is less than one of the threshold values"));
  735. printf ("\n\n");
  736. print_usage ();
  737. printf (_(UT_HELP_VRSN));
  738. printf (" %s\n", "-w, --warning=INTEGER");
  739. printf (" %s\n", _("Exit with WARNING status if less than INTEGER units of disk are free"));
  740. printf (" %s\n", "-w, --warning=PERCENT%");
  741. printf (" %s\n", _("Exit with WARNING status if less than PERCENT of disk space is free"));
  742. printf (" %s\n", "-W, --iwarning=PERCENT%");
  743. printf (" %s\n", _("Exit with WARNING status if less than PERCENT of inode space is free"));
  744. printf (" %s\n", "-K, --icritical=PERCENT%");
  745. printf (" %s\n", _("Exit with CRITICAL status if less than PERCENT of inode space is free"));
  746. printf (" %s\n", "-c, --critical=INTEGER");
  747. printf (" %s\n", _("Exit with CRITICAL status if less than INTEGER units of disk are free"));
  748. printf (" %s\n", "-c, --critical=PERCENT%");
  749. printf (" %s\n", _("Exit with CRITCAL status if less than PERCENT of disk space is free"));
  750. printf (" %s\n", "-C, --clear");
  751. printf (" %s\n", _("Clear thresholds"));
  752. printf (" %s\n", "-u, --units=STRING");
  753. printf (" %s\n", _("Choose bytes, kB, MB, GB, TB (default: MB)"));
  754. printf (" %s\n", "-k, --kilobytes");
  755. printf (" %s\n", _("Same as '--units kB'"));
  756. printf (" %s\n", "-m, --megabytes");
  757. printf (" %s\n", _("Same as '--units MB'"));
  758. printf (" %s\n", "-l, --local");
  759. printf (" %s\n", _("Only check local filesystems"));
  760. printf (" %s\n", "-p, --path=PATH, --partition=PARTITION");
  761. printf (" %s\n", _("Path or partition (may be repeated)"));
  762. printf (" %s\n", "-r, --ereg-path=PATH, --ereg-partition=PARTITION");
  763. printf (" %s\n", _("Regular expression for path or partition (may be repeated)"));
  764. printf (" %s\n", "-R, --eregi-path=PATH, --eregi-partition=PARTITION");
  765. printf (" %s\n", _("Case insensitive regular expression for path/partition (may be repeated)"));
  766. printf (" %s\n", "-g, --group=NAME");
  767. printf (" %s\n", _("Group pathes. Thresholds apply to (free-)space of all partitions together"));
  768. printf (" %s\n", "-x, --exclude_device=PATH <STRING>");
  769. printf (" %s\n", _("Ignore device (only works if -p unspecified)"));
  770. printf (" %s\n", "-X, --exclude-type=TYPE <STRING>");
  771. printf (" %s\n", _("Ignore all filesystems of indicated type (may be repeated)"));
  772. printf (" %s\n", "-M, --mountpoint");
  773. printf (" %s\n", _("Display the mountpoint instead of the partition"));
  774. printf (" %s\n", "-E, --exact-match");
  775. printf (" %s\n", _("For paths or partitions specified with -p, only check for exact paths"));
  776. printf (" %s\n", "-e, --errors-only");
  777. printf (" %s\n", _("Display only devices/mountpoints with errors"));
  778. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  779. printf (_(UT_VERBOSE));
  780. printf ("\n");
  781. printf ("%s\n", _("Examples:"));
  782. printf (" %s\n", "check_disk -w 10% -c 5% -p /tmp -p /var -C -w 100000 -c 50000 -p /");
  783. printf (" %s\n", _("Checks /tmp and /var at 10% and 5%, and / at 100MB and 50MB"));
  784. printf (_(UT_SUPPORT));
  785. }
  786. void
  787. print_usage (void)
  788. {
  789. printf (_("Usage:"));
  790. printf (" %s -w limit -c limit [-p path | -x device] [-t timeout]", progname);
  791. printf ("[-m] [-e] [-W limit] [-K limit] [-v] [-q] [-E]\n");
  792. }