check_disk.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. /*****************************************************************************
  2. *
  3. * Nagios check_disk plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999-2014 Nagios Plugins Development Team
  7. *
  8. * Description:
  9. *
  10. * This file contains the check_disk plugin
  11. *
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. *
  27. *****************************************************************************/
  28. const char *progname = "check_disk";
  29. const char *program_name = "check_disk"; /* Required for coreutils libs */
  30. const char *copyright = "1999-2018";
  31. const char *email = "devel@nagios-plugins.org";
  32. #include "common.h"
  33. #ifdef HAVE_SYS_STAT_H
  34. # include <sys/stat.h>
  35. #endif
  36. #if HAVE_INTTYPES_H
  37. # include <inttypes.h>
  38. #endif
  39. #include <assert.h>
  40. #include "popen.h"
  41. #include "utils.h"
  42. #include "utils_disk.h"
  43. #include <stdarg.h>
  44. #include "fsusage.h"
  45. #include "mountlist.h"
  46. #include "intprops.h" /* necessary for TYPE_MAXIMUM */
  47. #if HAVE_LIMITS_H
  48. # include <limits.h>
  49. #endif
  50. #include "regex.h"
  51. #ifdef __CYGWIN__
  52. # include <windows.h>
  53. # undef ERROR
  54. # define ERROR -1
  55. #endif
  56. /* If nonzero, show inode information. */
  57. // static int inode_format = 1;
  58. /* If nonzero, show even filesystems with zero size or
  59. uninteresting types. */
  60. static int show_all_fs = 1;
  61. /* If nonzero, show only local filesystems. */
  62. static int show_local_fs = 0;
  63. /* If nonzero, show only local filesystems but call stat() on remote ones. */
  64. static int stat_remote_fs = 0;
  65. /* If positive, the units to use when printing sizes;
  66. if negative, the human-readable base. */
  67. /* static int output_block_size; */
  68. /* If nonzero, invoke the `sync' system call before getting any usage data.
  69. Using this option can make df very slow, especially with many or very
  70. busy disks. Note that this may make a difference on some systems --
  71. SunOs4.1.3, for one. It is *not* necessary on Linux. */
  72. /* static int require_sync = 0; */
  73. /* Linked list of filesystem types to display.
  74. If `fs_select_list' is NULL, list all types.
  75. This table is generated dynamically from command-line options,
  76. rather than hardcoding into the program what it thinks are the
  77. valid filesystem types; let the user specify any filesystem type
  78. they want to, and if there are any filesystems of that type, they
  79. will be shown.
  80. Some filesystem types:
  81. 4.2 4.3 ufs nfs swap ignore io vm efs dbg */
  82. /* static struct parameter_list *fs_select_list; */
  83. /* Linked list of filesystem types to omit.
  84. If the list is empty, don't exclude any types. */
  85. static struct name_list *fs_exclude_list;
  86. /* Linked list of filesystem types to check.
  87. If the list is empty, include all types. */
  88. static struct name_list *fs_include_list;
  89. static struct name_list *dp_exclude_list;
  90. static struct parameter_list *path_select_list = NULL;
  91. /* Linked list of mounted filesystems. */
  92. static struct mount_entry *mount_list;
  93. static const char *always_exclude[] = { "iso9600", "fuse.gvfsd-fuse", NULL };
  94. /* For long options that have no equivalent short option, use a
  95. non-character as a pseudo short option, starting with CHAR_MAX + 1. */
  96. enum
  97. {
  98. SYNC_OPTION = CHAR_MAX + 1,
  99. NO_SYNC_OPTION,
  100. BLOCK_SIZE_OPTION
  101. };
  102. #ifdef _AIX
  103. #pragma alloca
  104. #endif
  105. int process_arguments (int, char **);
  106. void print_path (const char *mypath);
  107. void set_all_thresholds (struct parameter_list *path);
  108. void print_help (void);
  109. void print_usage (void);
  110. double calculate_percent(uintmax_t, uintmax_t);
  111. void stat_path (struct parameter_list *p);
  112. void get_stats (struct parameter_list *p, struct fs_usage *fsp);
  113. void get_path_stats (struct parameter_list *p, struct fs_usage *fsp);
  114. double w_dfp = -1.0;
  115. double c_dfp = -1.0;
  116. char *path;
  117. char *exclude_device;
  118. char *units;
  119. uintmax_t mult = 1024 * 1024;
  120. int verbose = 0;
  121. int newlines = FALSE;
  122. int erronly = FALSE;
  123. int display_mntp = FALSE;
  124. int exact_match = FALSE;
  125. int freespace_ignore_reserved = FALSE;
  126. int show_status = FALSE;
  127. char *warn_freespace_units = NULL;
  128. char *crit_freespace_units = NULL;
  129. char *warn_freespace_percent = NULL;
  130. char *crit_freespace_percent = NULL;
  131. char *warn_usedspace_units = NULL;
  132. char *crit_usedspace_units = NULL;
  133. char *warn_usedspace_percent = NULL;
  134. char *crit_usedspace_percent = NULL;
  135. char *warn_usedinodes_percent = NULL;
  136. char *crit_usedinodes_percent = NULL;
  137. char *warn_freeinodes_percent = NULL;
  138. char *crit_freeinodes_percent = NULL;
  139. int path_selected = FALSE;
  140. char *group = NULL;
  141. struct stat *stat_buf;
  142. struct name_list *seen = NULL;
  143. int
  144. main (int argc, char **argv)
  145. {
  146. int result = STATE_UNKNOWN;
  147. int disk_result = STATE_UNKNOWN;
  148. char *output;
  149. char status_lit[4];
  150. char *perf;
  151. char *preamble;
  152. char *flag_header;
  153. //double inode_space_pct;
  154. double warning_high_tide;
  155. double critical_high_tide;
  156. int temp_result;
  157. struct mount_entry *me
  158. struct fs_usage fsp;
  159. struct parameter_list *temp_list, *path;
  160. #ifdef __CYGWIN__
  161. char mountdir[32];
  162. #endif
  163. preamble = strdup (" - free space:");
  164. output = strdup ("");
  165. perf = strdup ("");
  166. stat_buf = malloc(sizeof *stat_buf);
  167. setlocale (LC_ALL, "");
  168. bindtextdomain (PACKAGE, LOCALEDIR);
  169. textdomain (PACKAGE);
  170. mount_list = read_file_system_list (0);
  171. /* Parse extra opts if any */
  172. argv = np_extra_opts (&argc, argv, progname);
  173. if (process_arguments (argc, argv) == ERROR)
  174. usage4 (_("Could not parse arguments"));
  175. if (show_status)
  176. sprintf(status_lit, "x:");
  177. else
  178. status_lit[0] = '\0';
  179. /* If a list of paths has not been selected, find entire
  180. mount list and create list of paths
  181. */
  182. if (path_selected == FALSE) {
  183. for (me = mount_list; me; me = me->me_next) {
  184. if (strcmp(me->me_type, "autofs") == 0 && show_local_fs) {
  185. if (last_me == NULL)
  186. mount_list = me;
  187. else
  188. last_me->me_next = me->me_next;
  189. free_mount_entry (me);
  190. continue;
  191. }
  192. if (strcmp(me->me_type, "sysfs") == 0 || strcmp(me->me_type, "proc") == 0
  193. || strcmp(me->me_type, "debugfs") == 0 || strcmp(me->me_type, "tracefs") == 0
  194. || strcmp(me->me_type, "fusectl") == 0 || strcmp(me->me_type, "fuse.gvfsd-fuse") == 0
  195. || strcmp(me->me_type, "cgroup") == 0 || strstr(me->me_type, "tmpfs") != NULL)
  196. {
  197. if (last_me == NULL)
  198. mount_list = me->me_next;
  199. else
  200. last_me->me_next = me->me_next;
  201. free_mount_entry (me);
  202. continue;
  203. }
  204. if (! (path = np_find_parameter(path_select_list, me->me_mountdir))) {
  205. path = np_add_parameter(&path_select_list, me->me_mountdir);
  206. }
  207. path->best_match = me;
  208. path->group = group;
  209. set_all_thresholds(path);
  210. }
  211. }
  212. np_set_best_match(path_select_list, mount_list, exact_match);
  213. /* Error if no match found for specified paths */
  214. temp_list = path_select_list;
  215. while (temp_list) {
  216. if (! temp_list->best_match) {
  217. die (STATE_CRITICAL, _("DISK %s: %s not found\n"), _("CRITICAL"), temp_list->name);
  218. }
  219. temp_list = temp_list->name_next;
  220. }
  221. /* Process for every path in list */
  222. for (path = path_select_list; path; path=path->name_next) {
  223. if (verbose >= 3 && path->freespace_percent->warning != NULL && path->freespace_percent->critical != NULL)
  224. printf("Thresholds(pct) for %s warn: %f crit %f\n",path->name, path->freespace_percent->warning->end,
  225. path->freespace_percent->critical->end);
  226. if (verbose >= 3 && path->group != NULL)
  227. printf("Group of %s: %s\n",path->name,path->group);
  228. /* reset disk result */
  229. disk_result = STATE_UNKNOWN;
  230. me = path->best_match;
  231. #ifdef __CYGWIN__
  232. if (strncmp(path->name, "/cygdrive/", 10) != 0 || strlen(path->name) > 11)
  233. continue;
  234. snprintf(mountdir, sizeof(mountdir), "%s:\\", me->me_mountdir + 10);
  235. if (GetDriveType(mountdir) != DRIVE_FIXED)
  236. me->me_remote = 1;
  237. #endif
  238. /* Filters */
  239. /* Remove filesystems already seen */
  240. if (np_seen_name(seen, me->me_mountdir)) {
  241. continue;
  242. }
  243. np_add_name(&seen, me->me_mountdir);
  244. if (path->group == NULL) {
  245. /* Skip remote filesystems if we're not interested in them */
  246. if (me->me_remote && show_local_fs) {
  247. if (stat_remote_fs)
  248. stat_path(path);
  249. continue;
  250. /* Skip pseudo fs's if we haven't asked for all fs's */
  251. } else if (me->me_dummy && !show_all_fs) {
  252. continue;
  253. /* Skip excluded fstypes */
  254. } else if (fs_exclude_list && np_find_name (fs_exclude_list, me->me_type)) {
  255. continue;
  256. /* Skip excluded fs's */
  257. } else if (dp_exclude_list &&
  258. (np_find_name (dp_exclude_list, me->me_devname) ||
  259. np_find_name (dp_exclude_list, me->me_mountdir))) {
  260. continue;
  261. /* Skip not included fstypes */
  262. } else if (fs_include_list && !np_find_name (fs_include_list, me->me_type)) {
  263. continue;
  264. }
  265. }
  266. stat_path(path);
  267. get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
  268. if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
  269. get_stats (path, &fsp);
  270. if (verbose >= 3) {
  271. printf ("For %s, used_pct=%g free_pct=%g used_units=%i free_units=%i total_units=%i used_inodes_pct=%g free_inodes_pct=%g fsp.fsu_blocksize=%llu mult=%llu\n",
  272. me->me_mountdir, path->dused_pct, path->dfree_pct, path->dused_units, path->dfree_units, path->dtotal_units, path->dused_inodes_percent, path->dfree_inodes_percent, fsp.fsu_blocksize, mult);
  273. }
  274. /* Threshold comparisons */
  275. temp_result = get_status(path->dfree_units, path->freespace_units);
  276. if (verbose >=3) printf("Freespace_units result=%d\n", temp_result);
  277. disk_result = max_state( disk_result, temp_result );
  278. temp_result = get_status(path->dfree_pct, path->freespace_percent);
  279. if (verbose >=3) printf("Freespace%% result=%d\n", temp_result);
  280. disk_result = max_state( disk_result, temp_result );
  281. temp_result = get_status(path->dused_units, path->usedspace_units);
  282. if (verbose >=3) printf("Usedspace_units result=%d\n", temp_result);
  283. disk_result = max_state( disk_result, temp_result );
  284. temp_result = get_status(path->dused_pct, path->usedspace_percent);
  285. if (verbose >=3) printf("Usedspace_percent result=%d\n", temp_result);
  286. disk_result = max_state( disk_result, temp_result );
  287. temp_result = get_status(path->dused_inodes_percent, path->usedinodes_percent);
  288. if (verbose >=3) printf("Usedinodes_percent result=%d\n", temp_result);
  289. disk_result = max_state( disk_result, temp_result );
  290. temp_result = get_status(path->dfree_inodes_percent, path->freeinodes_percent);
  291. if (verbose >=3) printf("Freeinodes_percent result=%d\n", temp_result);
  292. disk_result = max_state( disk_result, temp_result );
  293. if (show_status)
  294. status_lit[0] = state_text(disk_result)[0];
  295. result = max_state(result, disk_result);
  296. /* What a mess of units. The output shows free space, the perf data shows used space. Yikes!
  297. Hack here. Trying to get warn/crit levels from freespace_(units|percent) for perf
  298. data. Assumption that start=0. Roll on new syntax...
  299. */
  300. /* *_high_tide must be reinitialized at each run */
  301. warning_high_tide = ULONG_MAX;
  302. critical_high_tide = ULONG_MAX;
  303. if (path->freespace_units->warning != NULL) {
  304. warning_high_tide = path->dtotal_units - path->freespace_units->warning->end;
  305. }
  306. if (path->freespace_percent->warning != NULL) {
  307. warning_high_tide = labs( min( (double) warning_high_tide, (double) (1.0 - path->freespace_percent->warning->end/100)*path->dtotal_units ));
  308. }
  309. if (path->freespace_units->critical != NULL) {
  310. critical_high_tide = path->dtotal_units - path->freespace_units->critical->end;
  311. }
  312. if (path->freespace_percent->critical != NULL) {
  313. critical_high_tide = labs( min( (double) critical_high_tide, (double) (1.0 - path->freespace_percent->critical->end/100)*path->dtotal_units ));
  314. }
  315. /* Nb: *_high_tide are unset when == ULONG_MAX */
  316. xasprintf (&perf, "%s %s", perf,
  317. perfdata ((!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
  318. path->dused_units, units,
  319. (warning_high_tide != ULONG_MAX ? TRUE : FALSE), warning_high_tide,
  320. (critical_high_tide != ULONG_MAX ? TRUE : FALSE), critical_high_tide,
  321. TRUE, 0,
  322. TRUE, path->dtotal_units));
  323. if (disk_result==STATE_OK && erronly && !verbose)
  324. continue;
  325. xasprintf (&output, "%s%s%s%s %i %s (%.2f%%",
  326. output,
  327. newlines ? "" : " ",
  328. status_lit,
  329. (!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
  330. path->dfree_units,
  331. units,
  332. path->dfree_pct);
  333. /* Whether or not to put all disks on new line */
  334. if (newlines) {
  335. if (path->dused_inodes_percent < 0) {
  336. xasprintf(&output, "%s inode=-)%s;\n", output, (disk_result ? "]" : ""));
  337. } else {
  338. xasprintf(&output, "%s inode=%.0f%%)%s;\n", output, path->dfree_inodes_percent, ((disk_result && verbose) ? "]" : ""));
  339. }
  340. } else {
  341. if (path->dused_inodes_percent < 0) {
  342. xasprintf(&output, "%s inode=-)%s;", output, (disk_result ? "]" : ""));
  343. } else {
  344. xasprintf(&output, "%s inode=%.0f%%)%s;", output, path->dfree_inodes_percent, ((disk_result && verbose) ? "]" : ""));
  345. }
  346. }
  347. /* TODO: Need to do a similar debug line
  348. xasprintf (&details, _("%s\n\
  349. %.0f of %.0f %s (%.0f%% inode=%.0f%%) free on %s (type %s mounted on %s) warn:%lu crit:%lu warn%%:%.0f%% crit%%:%.0f%%"),
  350. details, dfree_units, dtotal_units, units, dfree_pct, inode_space_pct,
  351. me->me_devname, me->me_type, me->me_mountdir,
  352. (unsigned long)w_df, (unsigned long)c_df, w_dfp, c_dfp);
  353. */
  354. }
  355. }
  356. if (newlines) {
  357. printf ("DISK %s%s\n%s|%s\n", state_text (result), (erronly && result==STATE_OK) ? "" : preamble, output, perf);
  358. } else {
  359. printf ("DISK %s%s%s|%s\n", state_text (result), (erronly && result==STATE_OK) ? "" : preamble, output, perf);
  360. }
  361. return result;
  362. }
  363. double calculate_percent(uintmax_t value, uintmax_t total) {
  364. double pct = -1;
  365. /* I don't understand the below, but it is taken from coreutils' df */
  366. /* Seems to be calculating pct, in the best possible way */
  367. if (value <= TYPE_MAXIMUM(uintmax_t) / 10000
  368. && total != 0) {
  369. uintmax_t u100 = value * 10000;
  370. pct = (u100 / total + (u100 % total != 0)) / 100.0;
  371. } else {
  372. /* Possible rounding errors - see coreutils' df for more explanation */
  373. double u = value;
  374. double t = total;
  375. if (t) {
  376. long int lipct = pct = u * 10000 / t;
  377. double ipct = lipct / 100.0;
  378. /* Like 'pct = ceil (dpct);', but without ceil - from coreutils again */
  379. if (ipct - 1 < pct && pct <= ipct + 1)
  380. pct = ipct + (ipct < pct);
  381. }
  382. }
  383. return pct;
  384. }
  385. /* process command-line arguments */
  386. int
  387. process_arguments (int argc, char **argv)
  388. {
  389. int c, err, i;
  390. struct parameter_list *se;
  391. struct parameter_list *temp_list = NULL, *previous = NULL;
  392. struct parameter_list *temp_path_select_list = NULL;
  393. struct mount_entry *me;
  394. //int result = OK;
  395. regex_t re;
  396. int cflags = REG_NOSUB | REG_EXTENDED;
  397. int default_cflags = cflags;
  398. char errbuf[MAX_INPUT_BUFFER];
  399. int fnd = 0;
  400. int option = 0;
  401. static struct option longopts[] = {
  402. {"timeout", required_argument, 0, 't'},
  403. {"warning", required_argument, 0, 'w'},
  404. {"critical", required_argument, 0, 'c'},
  405. {"iwarning", required_argument, 0, 'W'},
  406. /* Dang, -C is taken. We might want to reshuffle this. */
  407. {"icritical", required_argument, 0, 'K'},
  408. {"kilobytes", no_argument, 0, 'k'},
  409. {"megabytes", no_argument, 0, 'm'},
  410. {"units", required_argument, 0, 'u'},
  411. {"path", required_argument, 0, 'p'},
  412. {"partition", required_argument, 0, 'p'},
  413. {"exclude_device", required_argument, 0, 'x'},
  414. {"exclude-type", required_argument, 0, 'X'},
  415. {"include-type", required_argument, 0, 'N'},
  416. {"newlines", no_argument, 0, 'n'},
  417. {"group", required_argument, 0, 'g'},
  418. {"eregi-path", required_argument, 0, 'R'},
  419. {"eregi-partition", required_argument, 0, 'R'},
  420. {"ereg-path", required_argument, 0, 'r'},
  421. {"ereg-partition", required_argument, 0, 'r'},
  422. {"freespace-ignore-reserved", no_argument, 0, 'f'},
  423. {"ignore-ereg-path", required_argument, 0, 'i'},
  424. {"ignore-ereg-partition", required_argument, 0, 'i'},
  425. {"ignore-eregi-path", required_argument, 0, 'I'},
  426. {"ignore-eregi-partition", required_argument, 0, 'I'},
  427. {"local", no_argument, 0, 'l'},
  428. {"stat-remote-fs", no_argument, 0, 'L'},
  429. {"mountpoint", no_argument, 0, 'M'},
  430. {"errors-only", no_argument, 0, 'e'},
  431. {"exact-match", no_argument, 0, 'E'},
  432. {"all", no_argument, 0, 'A'},
  433. {"show-status", no_argument, 0, 's'},
  434. {"verbose", no_argument, 0, 'v'},
  435. {"quiet", no_argument, 0, 'q'},
  436. {"clear", no_argument, 0, 'C'},
  437. {"version", no_argument, 0, 'V'},
  438. {"help", no_argument, 0, 'h'},
  439. {0, 0, 0, 0}
  440. };
  441. if (argc < 2)
  442. return ERROR;
  443. for (i = 0; always_exclude[i]; ++i)
  444. np_add_name(&fs_exclude_list, always_exclude[i]);
  445. for (c = 1; c < argc; c++)
  446. if (strcmp ("-to", argv[c]) == 0)
  447. strcpy (argv[c], "-t");
  448. while (1) {
  449. c = getopt_long (argc, argv, "+?VqhvefCt:c:w:K:W:u:p:x:X:N:mklLg:R:r:i:I:MEAns", longopts, &option);
  450. if (c == -1 || c == EOF)
  451. break;
  452. switch (c) {
  453. case 't': /* timeout period */
  454. timeout_interval = parse_timeout_string(optarg);
  455. break;
  456. /* See comments for 'c' */
  457. case 'w': /* warning threshold */
  458. if (strstr(optarg, "%")) {
  459. if (*optarg == '@') {
  460. warn_freespace_percent = optarg;
  461. } else {
  462. xasprintf(&warn_freespace_percent, "@%s", optarg);
  463. }
  464. } else {
  465. if (*optarg == '@') {
  466. warn_freespace_units = optarg;
  467. } else {
  468. xasprintf(&warn_freespace_units, "@%s", optarg);
  469. }
  470. }
  471. break;
  472. /* Awful mistake where the range values do not make sense. Normally,
  473. you alert if the value is within the range, but since we are using
  474. freespace, we have to alert if outside the range. Thus we artificially
  475. force @ at the beginning of the range, so that it is backwards compatible
  476. */
  477. case 'c': /* critical threshold */
  478. if (strstr(optarg, "%")) {
  479. if (*optarg == '@') {
  480. crit_freespace_percent = optarg;
  481. } else {
  482. xasprintf(&crit_freespace_percent, "@%s", optarg);
  483. }
  484. } else {
  485. if (*optarg == '@') {
  486. crit_freespace_units = optarg;
  487. } else {
  488. xasprintf(&crit_freespace_units, "@%s", optarg);
  489. }
  490. }
  491. break;
  492. case 'W': /* warning inode threshold */
  493. if (*optarg == '@') {
  494. warn_freeinodes_percent = optarg;
  495. } else {
  496. xasprintf(&warn_freeinodes_percent, "@%s", optarg);
  497. }
  498. break;
  499. case 'K': /* critical inode threshold */
  500. if (*optarg == '@') {
  501. crit_freeinodes_percent = optarg;
  502. } else {
  503. xasprintf(&crit_freeinodes_percent, "@%s", optarg);
  504. }
  505. break;
  506. case 'u':
  507. if (units)
  508. free(units);
  509. if (! strcmp (optarg, "bytes")) {
  510. mult = (uintmax_t)1;
  511. units = strdup ("B");
  512. } else if (! strcmp (optarg, "kB")) {
  513. mult = (uintmax_t)1024;
  514. units = strdup ("kB");
  515. } else if (! strcmp (optarg, "MB")) {
  516. mult = (uintmax_t)1024 * 1024;
  517. units = strdup ("MB");
  518. } else if (! strcmp (optarg, "GB")) {
  519. mult = (uintmax_t)1024 * 1024 * 1024;
  520. units = strdup ("GB");
  521. } else if (! strcmp (optarg, "TB")) {
  522. mult = (uintmax_t)1024 * 1024 * 1024 * 1024;
  523. units = strdup ("TB");
  524. } else {
  525. die (STATE_UNKNOWN, _("unit type %s not known\n"), optarg);
  526. }
  527. if (units == NULL)
  528. die (STATE_UNKNOWN, _("failed allocating storage for '%s'\n"), "units");
  529. break;
  530. case 'k': /* display mountpoint */
  531. mult = 1024;
  532. if (units)
  533. free(units);
  534. units = strdup ("kB");
  535. break;
  536. case 'm': /* display mountpoint */
  537. mult = 1024 * 1024;
  538. if (units)
  539. free(units);
  540. units = strdup ("MB");
  541. break;
  542. case 'L':
  543. stat_remote_fs = 1;
  544. case 'l':
  545. show_local_fs = 1;
  546. break;
  547. case 'p': /* select path */
  548. if (! (warn_freespace_units || crit_freespace_units || warn_freespace_percent ||
  549. crit_freespace_percent || warn_usedspace_units || crit_usedspace_units ||
  550. warn_usedspace_percent || crit_usedspace_percent || warn_usedinodes_percent ||
  551. crit_usedinodes_percent || warn_freeinodes_percent || crit_freeinodes_percent )) {
  552. die (STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set a threshold value before using -p\n"));
  553. }
  554. /* add parameter if not found. overwrite thresholds if path has already been added */
  555. if (! (se = np_find_parameter(path_select_list, optarg))) {
  556. se = np_add_parameter(&path_select_list, optarg);
  557. }
  558. se->group = group;
  559. set_all_thresholds(se);
  560. /* With autofs, it is required to stat() the path before re-populating the mount_list */
  561. stat_path(se);
  562. /* NB: We can't free the old mount_list "just like that": both list pointers and struct
  563. * pointers are copied around. One of the reason it wasn't done yet is that other parts
  564. * of check_disk need the same kind of cleanup so it'd better be done as a whole */
  565. mount_list = read_file_system_list (0);
  566. np_set_best_match(se, mount_list, exact_match);
  567. path_selected = TRUE;
  568. break;
  569. case 'x': /* exclude path or partition */
  570. np_add_name(&dp_exclude_list, optarg);
  571. break;
  572. case 'X': /* exclude file system type */
  573. np_add_name(&fs_exclude_list, optarg);
  574. break;
  575. case 'N': /* include file system type */
  576. np_add_name(&fs_include_list, optarg);
  577. break;
  578. case 'n': /* show each disk on a new line */
  579. newlines = TRUE;
  580. break;
  581. case 'v': /* verbose */
  582. verbose++;
  583. break;
  584. case 'q': /* TODO: this function should eventually go away (removed 2007-09-20) */
  585. /* verbose--; **replaced by line below**. -q was only a broken way of implementing -e */
  586. erronly = TRUE;
  587. break;
  588. case 'e':
  589. erronly = TRUE;
  590. break;
  591. case 'E':
  592. if (path_selected)
  593. die (STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set -E before selecting paths\n"));
  594. exact_match = TRUE;
  595. break;
  596. case 'f':
  597. freespace_ignore_reserved = TRUE;
  598. break;
  599. case 'g':
  600. if (path_selected)
  601. die (STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set group value before selecting paths\n"));
  602. group = optarg;
  603. break;
  604. case 'I':
  605. cflags |= REG_ICASE;
  606. case 'i':
  607. if (!path_selected)
  608. die (STATE_UNKNOWN, "DISK %s: %s\n", _("UNKNOWN"), _("Paths need to be selected before using -i/-I. Use -A to select all paths explicitly"));
  609. err = regcomp(&re, optarg, cflags);
  610. if (err != 0) {
  611. regerror (err, &re, errbuf, MAX_INPUT_BUFFER);
  612. die (STATE_UNKNOWN, "DISK %s: %s - %s\n",_("UNKNOWN"), _("Could not compile regular expression"), errbuf);
  613. }
  614. temp_list = path_select_list;
  615. previous = NULL;
  616. while (temp_list) {
  617. if (temp_list->best_match) {
  618. if (np_regex_match_mount_entry(temp_list->best_match, &re)) {
  619. if (verbose >=3)
  620. printf("ignoring %s matching regex\n", temp_list->name);
  621. temp_list = np_del_parameter(temp_list, previous);
  622. /* pointer to first element needs to be updated if first item gets deleted */
  623. if (previous == NULL)
  624. path_select_list = temp_list;
  625. } else {
  626. previous = temp_list;
  627. temp_list = temp_list->name_next;
  628. }
  629. } else {
  630. previous = temp_list;
  631. temp_list = temp_list->name_next;
  632. }
  633. }
  634. cflags = default_cflags;
  635. break;
  636. case 's':
  637. show_status = TRUE;
  638. break;
  639. case 'A':
  640. optarg = strdup(".*");
  641. case 'R':
  642. cflags |= REG_ICASE;
  643. case 'r':
  644. if (! (warn_freespace_units || crit_freespace_units || warn_freespace_percent ||
  645. crit_freespace_percent || warn_usedspace_units || crit_usedspace_units ||
  646. warn_usedspace_percent || crit_usedspace_percent || warn_usedinodes_percent ||
  647. crit_usedinodes_percent || warn_freeinodes_percent || crit_freeinodes_percent )) {
  648. die (STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set a threshold value before using -r/-R\n"));
  649. }
  650. err = regcomp(&re, optarg, cflags);
  651. if (err != 0) {
  652. regerror (err, &re, errbuf, MAX_INPUT_BUFFER);
  653. die (STATE_UNKNOWN, "DISK %s: %s - %s\n",_("UNKNOWN"), _("Could not compile regular expression"), errbuf);
  654. }
  655. for (me = mount_list; me; me = me->me_next) {
  656. if (np_regex_match_mount_entry(me, &re)) {
  657. fnd = TRUE;
  658. if (verbose >= 3)
  659. printf("%s %s matching expression %s\n", me->me_devname, me->me_mountdir, optarg);
  660. /* add parameter if not found. overwrite thresholds if path has already been added */
  661. if (! (se = np_find_parameter(path_select_list, me->me_mountdir))) {
  662. se = np_add_parameter(&path_select_list, me->me_mountdir);
  663. }
  664. se->group = group;
  665. set_all_thresholds(se);
  666. }
  667. }
  668. if (!fnd)
  669. die (STATE_UNKNOWN, "DISK %s: %s - %s\n",_("UNKNOWN"),
  670. _("Regular expression did not match any path or disk"), optarg);
  671. fnd = FALSE;
  672. path_selected = TRUE;
  673. np_set_best_match(path_select_list, mount_list, exact_match);
  674. cflags = default_cflags;
  675. break;
  676. case 'M': /* display mountpoint */
  677. display_mntp = TRUE;
  678. break;
  679. case 'C':
  680. /* add all mount entries to path_select list if no partitions have been explicitly defined using -p */
  681. if (path_selected == FALSE) {
  682. struct parameter_list *path;
  683. for (me = mount_list; me; me = me->me_next) {
  684. if (! (path = np_find_parameter(path_select_list, me->me_mountdir)))
  685. path = np_add_parameter(&path_select_list, me->me_mountdir);
  686. path->best_match = me;
  687. path->group = group;
  688. set_all_thresholds(path);
  689. }
  690. }
  691. warn_freespace_units = NULL;
  692. crit_freespace_units = NULL;
  693. warn_usedspace_units = NULL;
  694. crit_usedspace_units = NULL;
  695. warn_freespace_percent = NULL;
  696. crit_freespace_percent = NULL;
  697. warn_usedspace_percent = NULL;
  698. crit_usedspace_percent = NULL;
  699. warn_usedinodes_percent = NULL;
  700. crit_usedinodes_percent = NULL;
  701. warn_freeinodes_percent = NULL;
  702. crit_freeinodes_percent = NULL;
  703. path_selected = FALSE;
  704. group = NULL;
  705. break;
  706. case 'V': /* version */
  707. print_revision (progname, NP_VERSION);
  708. exit (STATE_OK);
  709. case 'h': /* help */
  710. print_help ();
  711. exit (STATE_OK);
  712. case '?': /* help */
  713. usage (_("Unknown argument"));
  714. }
  715. }
  716. /* Support for "check_disk warn crit [fs]" with thresholds at used% level */
  717. c = optind;
  718. if (warn_usedspace_percent == NULL && argc > c && is_intnonneg (argv[c]))
  719. warn_usedspace_percent = argv[c++];
  720. if (crit_usedspace_percent == NULL && argc > c && is_intnonneg (argv[c]))
  721. crit_usedspace_percent = argv[c++];
  722. if (argc > c && path == NULL) {
  723. se = np_add_parameter(&path_select_list, strdup(argv[c++]));
  724. path_selected = TRUE;
  725. set_all_thresholds(se);
  726. }
  727. if (units == NULL) {
  728. units = strdup ("MB");
  729. mult = (uintmax_t)1024 * 1024;
  730. }
  731. return TRUE;
  732. }
  733. void
  734. print_path (const char *mypath)
  735. {
  736. if (mypath == NULL)
  737. printf ("\n");
  738. else
  739. printf (_(" for %s\n"), mypath);
  740. }
  741. void
  742. set_all_thresholds (struct parameter_list *path)
  743. {
  744. if (path->freespace_units != NULL) free(path->freespace_units);
  745. set_thresholds(&path->freespace_units, warn_freespace_units, crit_freespace_units);
  746. if (path->freespace_percent != NULL) free (path->freespace_percent);
  747. set_thresholds(&path->freespace_percent, warn_freespace_percent, crit_freespace_percent);
  748. if (path->usedspace_units != NULL) free (path->usedspace_units);
  749. set_thresholds(&path->usedspace_units, warn_usedspace_units, crit_usedspace_units);
  750. if (path->usedspace_percent != NULL) free (path->usedspace_percent);
  751. set_thresholds(&path->usedspace_percent, warn_usedspace_percent, crit_usedspace_percent);
  752. if (path->usedinodes_percent != NULL) free (path->usedinodes_percent);
  753. set_thresholds(&path->usedinodes_percent, warn_usedinodes_percent, crit_usedinodes_percent);
  754. if (path->freeinodes_percent != NULL) free (path->freeinodes_percent);
  755. set_thresholds(&path->freeinodes_percent, warn_freeinodes_percent, crit_freeinodes_percent);
  756. }
  757. void
  758. print_help (void)
  759. {
  760. print_revision (progname, NP_VERSION);
  761. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  762. printf (COPYRIGHT, copyright, email);
  763. printf ("%s\n", _("This plugin checks the amount of used disk space on a mounted file system"));
  764. printf ("%s\n", _("and generates an alert if free space is less than one of the threshold values"));
  765. printf ("\n\n");
  766. print_usage ();
  767. printf (UT_HELP_VRSN);
  768. printf (UT_EXTRA_OPTS);
  769. printf (" %s\n", "-w, --warning=INTEGER");
  770. printf (" %s\n", _("Exit with WARNING status if less than INTEGER units of disk are free"));
  771. printf (" %s\n", "-w, --warning=PERCENT%");
  772. printf (" %s\n", _("Exit with WARNING status if less than PERCENT of disk space is free"));
  773. printf (" %s\n", "-c, --critical=INTEGER");
  774. printf (" %s\n", _("Exit with CRITICAL status if less than INTEGER units of disk are free"));
  775. printf (" %s\n", "-c, --critical=PERCENT%");
  776. printf (" %s\n", _("Exit with CRITICAL status if less than PERCENT of disk space is free"));
  777. printf (" %s\n", "-W, --iwarning=PERCENT%");
  778. printf (" %s\n", _("Exit with WARNING status if less than PERCENT of inode space is free"));
  779. printf (" %s\n", "-K, --icritical=PERCENT%");
  780. printf (" %s\n", _("Exit with CRITICAL status if less than PERCENT of inode space is free"));
  781. printf (" %s\n", "-p, --path=PATH, --partition=PARTITION");
  782. printf (" %s\n", _("Mount point or block device as emitted by the mount(8) command (may be repeated)"));
  783. printf (" %s\n", "-x, --exclude_device=PATH <STRING>");
  784. printf (" %s\n", _("Ignore device (only works if -p unspecified)"));
  785. printf (" %s\n", "-C, --clear");
  786. printf (" %s\n", _("Clear thresholds"));
  787. printf (" %s\n", "-E, --exact-match");
  788. printf (" %s\n", _("For paths or partitions specified with -p, only check for exact paths"));
  789. printf (" %s\n", "-e, --errors-only");
  790. printf (" %s\n", _("Display only devices/mountpoints with errors"));
  791. printf (" %s\n", "-f, --freespace-ignore-reserved");
  792. printf (" %s\n", _("Don't account root-reserved blocks into freespace in perfdata"));
  793. printf (" %s\n", "-g, --group=NAME");
  794. printf (" %s\n", _("Group paths. Thresholds apply to (free-)space of all partitions together"));
  795. printf (" %s\n", "-k, --kilobytes");
  796. printf (" %s\n", _("Same as '--units kB'"));
  797. printf (" %s\n", "-l, --local");
  798. printf (" %s\n", _("Only check local filesystems"));
  799. printf (" %s\n", "-L, --stat-remote-fs");
  800. printf (" %s\n", _("Only check local filesystems against thresholds. Yet call stat on remote filesystems"));
  801. printf (" %s\n", _("to test if they are accessible (e.g. to detect Stale NFS Handles)"));
  802. printf (" %s\n", "-M, --mountpoint");
  803. printf (" %s\n", _("Display the mountpoint instead of the partition"));
  804. printf (" %s\n", "-m, --megabytes");
  805. printf (" %s\n", _("Same as '--units MB'"));
  806. printf (" %s\n", "-A, --all");
  807. printf (" %s\n", _("Explicitly select all paths. This is equivalent to -R '.*'"));
  808. printf (" %s\n", "-R, --eregi-path=PATH, --eregi-partition=PARTITION");
  809. printf (" %s\n", _("Case insensitive regular expression for path/partition (may be repeated)"));
  810. printf (" %s\n", "-r, --ereg-path=PATH, --ereg-partition=PARTITION");
  811. printf (" %s\n", _("Regular expression for path or partition (may be repeated)"));
  812. printf (" %s\n", "-I, --ignore-eregi-path=PATH, --ignore-eregi-partition=PARTITION");
  813. printf (" %s\n", _("Regular expression to ignore selected path/partition (case insensitive) (may be repeated)"));
  814. printf (" %s\n", "-i, --ignore-ereg-path=PATH, --ignore-ereg-partition=PARTITION");
  815. printf (" %s\n", _("Regular expression to ignore selected path or partition (may be repeated)"));
  816. printf (" %s\n", "-s, --show-status");
  817. printf (" %s\n", _("Show status for each path/partition"));
  818. printf (UT_PLUG_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
  819. printf (" %s\n", "-u, --units=STRING");
  820. printf (" %s\n", _("Choose bytes, kB, MB, GB, TB (default: MB)"));
  821. printf (UT_VERBOSE);
  822. printf (" %s\n", "-X, --exclude-type=TYPE");
  823. printf (" %s\n", _("Ignore all filesystems of indicated type (may be repeated)"));
  824. printf (" %s\n", "-N, --include-type=TYPE");
  825. printf (" %s\n", _("Check only filesystems of indicated type (may be repeated)"));
  826. printf (" %s\n", "-n, --newlines");
  827. printf (" %s\n", _("Multi-line output of each disk's status information on a new line"));
  828. printf ("\n");
  829. printf ("%s\n", _("Examples:"));
  830. printf (" %s\n", "check_disk -w 10% -c 5% -p /tmp -p /var -C -w 100000 -c 50000 -p /");
  831. printf (" %s\n", _("Checks /tmp and /var at 10% and 5%, and / at 100MB and 50MB"));
  832. printf (" %s\n", "check_disk -w 100 -c 50 -C -w 1000 -c 500 -g sidDATA -r '^/oracle/SID/data.*$'");
  833. printf (" %s\n", _("Checks all filesystems not matching -r at 100M and 50M. The fs matching the -r regex"));
  834. printf (" %s\n", _("are grouped which means the freespace thresholds are applied to all disks together"));
  835. printf (" %s\n", "check_disk -w 100 -c 50 -C -w 1000 -c 500 -p /foo -C -w 5% -c 3% -p /bar");
  836. printf (" %s\n", _("Checks /foo for 1000M/500M and /bar for 5/3%. All remaining volumes use 100M/50M"));
  837. printf (UT_SUPPORT);
  838. }
  839. void
  840. print_usage (void)
  841. {
  842. printf ("%s\n", _("Usage:"));
  843. printf (" %s -w limit -c limit [-W limit] [-K limit] {-p path | -x device}\n", progname);
  844. printf ("[-C] [-E] [-e] [-f] [-g group ] [-k] [-l] [-M] [-m] {-A | [-R path] [-r path]}\n");
  845. printf ("[-s] [-t timeout] [-u unit] [-v] [-X type] [-N type] [-n]\n");
  846. }
  847. void
  848. stat_path (struct parameter_list *p)
  849. {
  850. /* Stat entry to check that dir exists and is accessible */
  851. if (verbose >= 3) {
  852. if (p->best_match)
  853. printf("calling stat on %s (%s %s)\n", p->name, p->best_match->me_devname, p->best_match->me_type);
  854. else
  855. printf("calling stat on %s\n", p->name);
  856. }
  857. if (stat (p->name, &stat_buf[0])) {
  858. if (verbose >= 3)
  859. printf("stat failed on %s\n", p->name);
  860. printf("DISK %s - ", _("CRITICAL"));
  861. die (STATE_CRITICAL, _("%s %s: %s\n"), p->name, _("is not accessible"), strerror(errno));
  862. }
  863. }
  864. void
  865. get_stats (struct parameter_list *p, struct fs_usage *fsp) {
  866. struct parameter_list *p_list;
  867. struct fs_usage tmpfsp;
  868. int first = 1;
  869. if (p->group == NULL) {
  870. get_path_stats(p,fsp);
  871. } else {
  872. /* find all group members */
  873. for (p_list = path_select_list; p_list; p_list=p_list->name_next) {
  874. #ifdef __CYGWIN__
  875. if (strncmp(p_list->name, "/cygdrive/", 10) != 0)
  876. continue;
  877. #endif
  878. if (p_list->group && ! (strcmp(p_list->group, p->group))) {
  879. stat_path(p_list);
  880. get_fs_usage (p_list->best_match->me_mountdir, p_list->best_match->me_devname, &tmpfsp);
  881. get_path_stats(p_list, &tmpfsp);
  882. if (verbose >= 3)
  883. printf("Group %s: adding %llu blocks sized %llu, (%s) used_units=%i free_units=%i total_units=%i fsu_blocksize=%llu mult=%llu\n",
  884. p_list->group, tmpfsp.fsu_bavail, tmpfsp.fsu_blocksize, p_list->best_match->me_mountdir, p_list->dused_units, p_list->dfree_units,
  885. p_list->dtotal_units, mult);
  886. /* prevent counting the first FS of a group twice since its parameter_list entry
  887. * is used to carry the information of all file systems of the entire group */
  888. if (! first) {
  889. p->total += p_list->total;
  890. p->available += p_list->available;
  891. p->available_to_root += p_list->available_to_root;
  892. p->used += p_list->used;
  893. p->dused_units += p_list->dused_units;
  894. p->dfree_units += p_list->dfree_units;
  895. p->dtotal_units += p_list->dtotal_units;
  896. p->inodes_total += p_list->inodes_total;
  897. p->inodes_free += p_list->inodes_free;
  898. }
  899. first = 0;
  900. }
  901. if (verbose >= 3)
  902. printf("Group %s now has: used_units=%1 free_units=%1 total_units=%1 fsu_blocksize=%llu mult=%llu\n",
  903. p->group, tmpfsp.fsu_bavail, tmpfsp.fsu_blocksize, p->best_match->me_mountdir, p->dused_units,
  904. p->dfree_units, p->dtotal_units, mult);
  905. }
  906. /* modify devname and mountdir for output */
  907. p->best_match->me_mountdir = p->best_match->me_devname = p->group;
  908. }
  909. /* finally calculate percentages for either plain FS or summed up group */
  910. p->dused_pct = calculate_percent( p->used, p->used + p->available ); /* used + available can never be > uintmax */
  911. p->dfree_pct = 100 - p->dused_pct;
  912. p->dused_inodes_percent = calculate_percent(p->inodes_total - p->inodes_free, p->inodes_total);
  913. p->dfree_inodes_percent = 100 - p->dused_inodes_percent;
  914. }
  915. void
  916. get_path_stats (struct parameter_list *p, struct fs_usage *fsp) {
  917. /* 2007-12-08 - Workaround for Gnulib reporting insanely high available
  918. * space on BSD (the actual value should be negative but fsp->fsu_bavail
  919. * is unsigned) */
  920. p->available = fsp->fsu_bavail > fsp->fsu_bfree ? 0 : fsp->fsu_bavail;
  921. p->available_to_root = fsp->fsu_bfree;
  922. p->used = fsp->fsu_blocks - fsp->fsu_bfree;
  923. if (freespace_ignore_reserved) {
  924. /* option activated : we subtract the root-reserved space from the total */
  925. p->total = fsp->fsu_blocks - p->available_to_root + p->available;
  926. } else {
  927. /* default behaviour : take all the blocks into account */
  928. p->total = fsp->fsu_blocks;
  929. }
  930. p->dused_units = p->used*fsp->fsu_blocksize/mult;
  931. p->dfree_units = p->available*fsp->fsu_blocksize/mult;
  932. p->dtotal_units = p->total*fsp->fsu_blocksize/mult;
  933. p->inodes_total = fsp->fsu_files; /* Total file nodes. */
  934. p->inodes_free = fsp->fsu_ffree; /* Free file nodes. */
  935. np_add_name(&seen, p->best_match->me_mountdir);
  936. }