check_disk.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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-2014";
  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. #include <human.h>
  52. #ifdef __CYGWIN__
  53. # include <windows.h>
  54. # undef ERROR
  55. # define ERROR -1
  56. #endif
  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 nonzero, show only local filesystems but call stat() on remote ones. */
  65. static int stat_remote_fs = 0;
  66. /* If nonzero, skip "fake" filesystems created by the system */
  67. static int skip_fake_fs = 0;
  68. /* If nonzero and -w/-c are set on both percentile and raw units, alert when either unit's threshold is crossed */
  69. static int combined_thresholds = 0;
  70. /* If positive, the units to use when printing sizes;
  71. if negative, the human-readable base. */
  72. /* static int output_block_size; */
  73. /* If nonzero, invoke the `sync' system call before getting any usage data.
  74. Using this option can make df very slow, especially with many or very
  75. busy disks. Note that this may make a difference on some systems --
  76. SunOs4.1.3, for one. It is *not* necessary on Linux. */
  77. /* static int require_sync = 0; */
  78. /* Linked list of filesystem types to display.
  79. If `fs_select_list' is NULL, list all types.
  80. This table is generated dynamically from command-line options,
  81. rather than hardcoding into the program what it thinks are the
  82. valid filesystem types; let the user specify any filesystem type
  83. they want to, and if there are any filesystems of that type, they
  84. will be shown.
  85. Some filesystem types:
  86. 4.2 4.3 ufs nfs swap ignore io vm efs dbg */
  87. /* static struct parameter_list *fs_select_list; */
  88. /* Linked list of filesystem types to omit.
  89. If the list is empty, don't exclude any types. */
  90. static struct name_list *fs_exclude_list;
  91. /* Linked list of filesystem types to check.
  92. If the list is empty, include all types. */
  93. static struct name_list *fs_include_list;
  94. static struct name_list *dp_exclude_list;
  95. static struct parameter_list *path_select_list = NULL;
  96. /* Linked list of mounted filesystems. */
  97. static struct mount_entry *mount_list;
  98. static const char *always_exclude[] = { "iso9600", "fuse.gvfsd-fuse", NULL };
  99. #define MAX_HUMAN_COL_WIDTH 255
  100. typedef struct human_disk_entry {
  101. int disk_result;
  102. double free_pct;
  103. uintmax_t avail_bytes;
  104. uintmax_t total_bytes;
  105. char free_pct_str[10];
  106. char avail_bytes_str[10];
  107. char total_bytes_str[10];
  108. char disk_result_str[10];
  109. char *type;
  110. char *mount_dir;
  111. struct human_disk_entry* next;
  112. } human_disk_entry_t;
  113. typedef struct {
  114. unsigned int disk_result;
  115. unsigned int free_pct;
  116. unsigned int avail_bytes;
  117. unsigned int total_bytes;
  118. unsigned int type;
  119. unsigned int mount_dir;
  120. } human_column_widths_t;
  121. #define HUMAN_INTER_COLUMN_WIDTH 3
  122. #define HUMAN_HEADER_COUNT 6
  123. const char* human_column_header_names[HUMAN_HEADER_COUNT] = {
  124. "Status",
  125. "Free",
  126. "Avail",
  127. "Total",
  128. "Type",
  129. "Mount Point"};
  130. human_column_widths_t human_column_widths = { 0, 0, 0, 0, 0, 0 };
  131. void print_human_disk_entries(human_disk_entry_t* human_disk_entries, unsigned num_human_disk_entries);
  132. int human_disk_entry_comparer(const void*, const void*);
  133. /* For long options that have no equivalent short option, use a
  134. non-character as a pseudo short option, starting with CHAR_MAX + 1. */
  135. enum
  136. {
  137. SYNC_OPTION = CHAR_MAX + 1,
  138. NO_SYNC_OPTION,
  139. BLOCK_SIZE_OPTION
  140. };
  141. #ifdef _AIX
  142. #pragma alloca
  143. #endif
  144. int process_arguments (int, char **);
  145. void print_path (const char *mypath);
  146. void set_all_thresholds (struct parameter_list *path);
  147. int validate_arguments (uintmax_t, uintmax_t, double, double, double, double, char *);
  148. void print_help (void);
  149. void print_usage (void);
  150. double calculate_percent(uintmax_t, uintmax_t);
  151. void stat_path (struct parameter_list *p);
  152. void get_stats (struct parameter_list *p, struct fs_usage *fsp);
  153. void get_path_stats (struct parameter_list *p, struct fs_usage *fsp);
  154. double w_dfp = -1.0;
  155. double c_dfp = -1.0;
  156. char *path;
  157. char *exclude_device;
  158. char *units;
  159. uintmax_t mult = 1024 * 1024;
  160. int verbose = 0;
  161. int verbose_machine_output = 0;
  162. int newlines = FALSE;
  163. int erronly = FALSE;
  164. int display_mntp = FALSE;
  165. int exact_match = FALSE;
  166. int freespace_ignore_reserved = FALSE;
  167. char *warn_freespace_units = NULL;
  168. char *crit_freespace_units = NULL;
  169. char *warn_freespace_percent = NULL;
  170. char *crit_freespace_percent = NULL;
  171. char *warn_usedspace_units = NULL;
  172. char *crit_usedspace_units = NULL;
  173. char *warn_usedspace_percent = NULL;
  174. char *crit_usedspace_percent = NULL;
  175. char *warn_usedinodes_percent = NULL;
  176. char *crit_usedinodes_percent = NULL;
  177. char *warn_freeinodes_percent = NULL;
  178. char *crit_freeinodes_percent = NULL;
  179. int path_selected = FALSE;
  180. char *group = NULL;
  181. struct stat *stat_buf;
  182. struct name_list *seen = NULL;
  183. int human_output = 0;
  184. int inode_perfdata_enabled = 0;
  185. int
  186. main (int argc, char **argv)
  187. {
  188. int result = STATE_UNKNOWN;
  189. int disk_result = STATE_UNKNOWN;
  190. char *output = NULL;
  191. char *details;
  192. char *perf;
  193. char *preamble;
  194. char *flag_header = NULL;
  195. char *label_name;
  196. char *inode_label_name, *raw_used_inodes_name, *raw_free_inodes_name;
  197. int print_inode_perfdata_warning, print_inode_perfdata_critical;
  198. double inode_space_pct;
  199. double warning_high_tide;
  200. double critical_high_tide;
  201. int temp_result;
  202. int temp_result2;
  203. struct mount_entry *me;
  204. struct mount_entry *last_me;
  205. struct fs_usage fsp, tmpfsp;
  206. struct parameter_list *temp_list, *path;
  207. human_disk_entry_t* human_disk_entries = NULL;
  208. unsigned num_human_disk_entries = 0;
  209. #ifdef __CYGWIN__
  210. char mountdir[32];
  211. #endif
  212. preamble = strdup (" - free space:");
  213. output = strdup ("");
  214. details = strdup ("");
  215. perf = strdup ("");
  216. stat_buf = malloc(sizeof *stat_buf);
  217. setlocale (LC_ALL, "");
  218. bindtextdomain (PACKAGE, LOCALEDIR);
  219. textdomain (PACKAGE);
  220. mount_list = read_file_system_list (0);
  221. /* Parse extra opts if any */
  222. argv = np_extra_opts (&argc, argv, progname);
  223. if (process_arguments (argc, argv) == ERROR)
  224. usage4 (_("Could not parse arguments"));
  225. verbose_machine_output = (verbose >= 3 && !human_output);
  226. /* Set signal handling and alarm timeout */
  227. if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
  228. die (STATE_UNKNOWN, _("Cannot catch SIGALRM"));
  229. }
  230. (void) alarm ((unsigned) timeout_interval);
  231. /* If a list of paths has not been selected, find entire
  232. mount list and create list of paths
  233. */
  234. if (path_selected == FALSE) {
  235. for (me = mount_list; me; me = me->me_next) {
  236. if (strcmp(me->me_type, "autofs") == 0 && show_local_fs) {
  237. if (last_me == NULL)
  238. mount_list = me;
  239. else
  240. last_me->me_next = me->me_next;
  241. free_mount_entry (me);
  242. continue;
  243. }
  244. if (skip_fake_fs &&
  245. (strcmp(me->me_type, "sysfs") == 0 || strcmp(me->me_type, "proc") == 0
  246. || strcmp(me->me_type, "debugfs") == 0 || strcmp(me->me_type, "tracefs") == 0
  247. || strcmp(me->me_type, "fusectl") == 0 || strcmp(me->me_type, "fuse.gvfsd-fuse") == 0
  248. || strcmp(me->me_type, "cgroup") == 0 || strstr(me->me_type, "tmpfs") != NULL))
  249. {
  250. if (last_me == NULL)
  251. mount_list = me->me_next;
  252. else
  253. last_me->me_next = me->me_next;
  254. free_mount_entry (me);
  255. continue;
  256. }
  257. if (! (path = np_find_parameter(path_select_list, me->me_mountdir))) {
  258. path = np_add_parameter(&path_select_list, me->me_mountdir);
  259. }
  260. path->best_match = me;
  261. path->group = group;
  262. set_all_thresholds(path);
  263. last_me = me;
  264. }
  265. }
  266. np_set_best_match(path_select_list, mount_list, exact_match);
  267. /* Error if no match found for specified paths */
  268. temp_list = path_select_list;
  269. while (temp_list) {
  270. if (! temp_list->best_match) {
  271. die (STATE_CRITICAL, _("DISK %s: %s not found\n"), _("CRITICAL"), temp_list->name);
  272. }
  273. temp_list = temp_list->name_next;
  274. }
  275. /* Initialize the header lengths to be the header text, so each column is at minimum as wide as its header */
  276. if (human_output) {
  277. int i;
  278. for (i = 0; i < HUMAN_HEADER_COUNT; i++) {
  279. ((unsigned int *)&human_column_widths.disk_result)[i] = strlen(human_column_header_names[i]);
  280. }
  281. }
  282. /* Process for every path in list */
  283. for (path = path_select_list; path; path=path->name_next) {
  284. if (verbose_machine_output && path->freespace_percent->warning != NULL && path->freespace_percent->critical != NULL)
  285. printf("Thresholds(pct) for %s warn: %f crit %f\n",path->name, path->freespace_percent->warning->end,
  286. path->freespace_percent->critical->end);
  287. if (verbose_machine_output && path->group != NULL)
  288. printf("Group of %s: %s\n",path->name,path->group);
  289. /* reset disk result */
  290. disk_result = STATE_UNKNOWN;
  291. me = path->best_match;
  292. #ifdef __CYGWIN__
  293. if (strncmp(path->name, "/cygdrive/", 10) != 0 || strlen(path->name) > 11)
  294. continue;
  295. snprintf(mountdir, sizeof(mountdir), "%s:\\", me->me_mountdir + 10);
  296. if (GetDriveType(mountdir) != DRIVE_FIXED)
  297. me->me_remote = 1;
  298. #endif
  299. /* Filters */
  300. /* Remove filesystems already seen */
  301. if (np_seen_name(seen, me->me_mountdir)) {
  302. continue;
  303. }
  304. np_add_name(&seen, me->me_mountdir);
  305. if (path->group == NULL) {
  306. /* Skip remote filesystems if we're not interested in them */
  307. if (me->me_remote && show_local_fs) {
  308. if (stat_remote_fs)
  309. stat_path(path);
  310. continue;
  311. /* Skip pseudo fs's if we haven't asked for all fs's */
  312. } else if (me->me_dummy && !show_all_fs) {
  313. continue;
  314. /* Skip excluded fstypes */
  315. } else if (fs_exclude_list && np_find_name (fs_exclude_list, me->me_type)) {
  316. continue;
  317. /* Skip excluded fs's */
  318. } else if (dp_exclude_list &&
  319. (np_find_name (dp_exclude_list, me->me_devname) ||
  320. np_find_name (dp_exclude_list, me->me_mountdir))) {
  321. continue;
  322. /* Skip not included fstypes */
  323. } else if (fs_include_list && !np_find_name (fs_include_list, me->me_type)) {
  324. continue;
  325. }
  326. }
  327. stat_path(path);
  328. get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
  329. if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
  330. get_stats (path, &fsp);
  331. if (verbose_machine_output) {
  332. 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 fsp.fsu_blocksize=%llu mult=%llu\n",
  333. me->me_mountdir, path->dused_pct, path->dfree_pct, (double)path->dused_units, (double)path->dfree_units, (double)path->dtotal_units, path->dused_inodes_percent, path->dfree_inodes_percent, fsp.fsu_blocksize, mult);
  334. }
  335. /** Threshold comparisons
  336. * If combined_thresholds is set, the "units" and "percent" will
  337. * have their minimum state calculated, and the disk_result will be the
  338. * maximum of these minima.
  339. * Note that both "units" and "percent" MUST be set, otherwise
  340. * the check will always return OK.
  341. *
  342. * If combined_thresholds is not set, the maximum of all get_status()
  343. * calls will be used.
  344. */
  345. temp_result = get_status(path->dfree_units, path->freespace_units);
  346. if (verbose_machine_output) printf("Freespace_units result=%d\n", temp_result);
  347. temp_result2 = get_status(path->dfree_pct, path->freespace_percent);
  348. if (verbose_machine_output) printf("Freespace%% result=%d\n", temp_result2);
  349. if (combined_thresholds) {
  350. temp_result = min_state(temp_result, temp_result2);
  351. }
  352. else {
  353. temp_result = max_state(temp_result, temp_result2);
  354. }
  355. disk_result = max_state(disk_result, temp_result);
  356. temp_result = get_status(path->dused_units, path->usedspace_units);
  357. if (verbose_machine_output) printf("Usedspace_units result=%d\n", temp_result);
  358. temp_result2 = get_status(path->dused_pct, path->usedspace_percent);
  359. if (verbose_machine_output) printf("Usedspace_percent result=%d\n", temp_result2);
  360. if (combined_thresholds) {
  361. temp_result = min_state(temp_result, temp_result2);
  362. }
  363. else {
  364. temp_result = max_state(temp_result, temp_result2);
  365. }
  366. disk_result = max_state(disk_result, temp_result);
  367. temp_result = get_status(path->dused_inodes_percent, path->usedinodes_percent);
  368. if (verbose_machine_output) printf("Usedinodes_percent result=%d\n", temp_result);
  369. temp_result2 = get_status(path->dfree_inodes_percent, path->freeinodes_percent);
  370. if (verbose_machine_output) printf("Freeinodes_percent result=%d\n", temp_result2);
  371. if (combined_thresholds) {
  372. temp_result = min_state(temp_result, temp_result2);
  373. }
  374. else {
  375. temp_result = max_state(temp_result, temp_result2);
  376. }
  377. disk_result = max_state(disk_result, temp_result);
  378. result = max_state(result, disk_result);
  379. /* What a mess of units. The output shows free space, the perf data shows used space. Yikes!
  380. Hack here. Trying to get warn/crit levels from freespace_(units|percent) for perf
  381. data. Assumption that start=0. Roll on new syntax...
  382. */
  383. /* *_high_tide must be reinitialized at each run */
  384. warning_high_tide = ULONG_MAX;
  385. critical_high_tide = ULONG_MAX;
  386. if (path->freespace_units->warning != NULL) {
  387. warning_high_tide = path->dtotal_units - path->freespace_units->warning->end;
  388. }
  389. if (path->freespace_percent->warning != NULL) {
  390. warning_high_tide = fabs( min( (double) warning_high_tide, (double) (1.0 - path->freespace_percent->warning->end/100)*path->dtotal_units ));
  391. }
  392. if (path->freespace_units->critical != NULL) {
  393. critical_high_tide = path->dtotal_units - path->freespace_units->critical->end;
  394. }
  395. if (path->freespace_percent->critical != NULL) {
  396. critical_high_tide = fabs( min( (double) critical_high_tide, (double) (1.0 - path->freespace_percent->critical->end/100)*path->dtotal_units ));
  397. }
  398. if (human_output) {
  399. human_disk_entry_t* human_disk_entry = (human_disk_entry_t*)malloc(sizeof(struct human_disk_entry));
  400. human_disk_entry->mount_dir = me->me_mountdir;
  401. human_disk_entry->type = me->me_type;
  402. human_disk_entry->disk_result = disk_result;
  403. human_disk_entry->next = human_disk_entries;
  404. human_disk_entry->avail_bytes = path->dfree_units;
  405. human_disk_entry->free_pct = path->dfree_pct;
  406. human_disk_entry->total_bytes = path->dtotal_units;
  407. human_disk_entries = human_disk_entry;
  408. num_human_disk_entries++;
  409. snprintf(&human_disk_entry->free_pct_str[0], 9, "%2.1f%%", human_disk_entry->free_pct);
  410. int human_opts = human_autoscale | human_suppress_point_zero | human_SI | human_B;
  411. char human_buf[MAX_HUMAN_COL_WIDTH];
  412. const char *free_pct_str = &human_disk_entry->free_pct_str[0];
  413. const char *disk_result_str = state_text(human_disk_entry->disk_result);
  414. const char *avail_bytes_str = human_readable(human_disk_entry->avail_bytes, &human_buf[0], human_opts, 1, 1);
  415. strncpy(&human_disk_entry->avail_bytes_str[0], avail_bytes_str, sizeof(human_disk_entry->avail_bytes_str));
  416. const char *total_bytes_str = human_readable(human_disk_entry->total_bytes, &human_buf[0], human_opts, 1, 1);
  417. strncpy(&human_disk_entry->total_bytes_str[0], total_bytes_str, sizeof(human_disk_entry->total_bytes_str));
  418. strncpy(&human_disk_entry->disk_result_str[0], disk_result_str, sizeof(human_disk_entry->disk_result_str));
  419. if (human_column_widths.free_pct < strlen(free_pct_str)) human_column_widths.free_pct = strlen(free_pct_str);
  420. if (human_column_widths.avail_bytes < strlen(avail_bytes_str)) human_column_widths.avail_bytes = strlen(avail_bytes_str);
  421. if (human_column_widths.total_bytes < strlen(total_bytes_str)) human_column_widths.total_bytes = strlen(total_bytes_str);
  422. if (human_column_widths.disk_result < strlen(disk_result_str)) human_column_widths.disk_result = strlen(disk_result_str);
  423. if (human_column_widths.type < strlen(me->me_type)) human_column_widths.type = strlen(me->me_type);
  424. if (human_column_widths.mount_dir < strlen(me->me_mountdir)) human_column_widths.mount_dir = strlen(me->me_mountdir);
  425. } else {
  426. label_name = (!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir;
  427. /* Nb: *_high_tide are unset when == ULONG_MAX */
  428. xasprintf (&perf, "%s %s", perf,
  429. perfdata (label_name,
  430. path->dused_units, units,
  431. (warning_high_tide != ULONG_MAX ? TRUE : FALSE), warning_high_tide,
  432. (critical_high_tide != ULONG_MAX ? TRUE : FALSE), critical_high_tide,
  433. TRUE, 0,
  434. TRUE, path->dtotal_units));
  435. if (inode_perfdata_enabled) {
  436. inode_label_name = calloc(strlen(label_name) + 1 + 14, 1);
  437. inode_label_name = strcat(inode_label_name, label_name);
  438. inode_label_name = strcat(inode_label_name, "_inode_percent");
  439. print_inode_perfdata_warning = FALSE;
  440. if (path->freeinodes_percent != NULL && path->freeinodes_percent->warning != NULL) {
  441. print_inode_perfdata_warning = TRUE;
  442. }
  443. print_inode_perfdata_critical = FALSE;
  444. if (path->freeinodes_percent != NULL && path->freeinodes_percent->critical != NULL) {
  445. print_inode_perfdata_critical = TRUE;
  446. }
  447. xasprintf (&perf, "%s %s", perf,
  448. perfdata (inode_label_name,
  449. path->dused_inodes_percent, "%",
  450. print_inode_perfdata_warning, (print_inode_perfdata_warning ? path->freeinodes_percent->warning->end : 0),
  451. print_inode_perfdata_critical, (print_inode_perfdata_critical ? path->freeinodes_percent->critical->end : 0),
  452. TRUE, 0,
  453. TRUE, 100));
  454. raw_used_inodes_name = calloc(strlen(label_name) + 1 + 11, 1);
  455. raw_used_inodes_name = strcat(raw_used_inodes_name, label_name);
  456. raw_used_inodes_name = strcat(raw_used_inodes_name, "_inode_used");
  457. xasprintf(&perf, "%s %s", perf, perfdata(raw_used_inodes_name, path->inodes_total - path->inodes_free, "", FALSE, 0, FALSE, 0, TRUE, 0, TRUE, path->inodes_total));
  458. raw_free_inodes_name = calloc(strlen(label_name) + 1 + 11, 1);
  459. raw_free_inodes_name = strcat(raw_free_inodes_name, label_name);
  460. raw_free_inodes_name = strcat(raw_free_inodes_name, "_inode_free");
  461. xasprintf(&perf, "%s %s", perf, perfdata(raw_free_inodes_name, path->inodes_free, "", FALSE, 0, FALSE, 0, TRUE, 0, TRUE, path->inodes_total));
  462. }
  463. }
  464. if (disk_result==STATE_OK && erronly && !verbose)
  465. continue;
  466. if (!human_output) {
  467. if (disk_result && verbose) {
  468. xasprintf(&flag_header, " %s [", state_text (disk_result));
  469. }
  470. else {
  471. xasprintf(&flag_header, "");
  472. }
  473. xasprintf (&output, "%s %s %.0f %s (%.2f%%",
  474. output,
  475. (!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
  476. (double)path->dfree_units,
  477. units,
  478. path->dfree_pct);
  479. /* Whether or not to put all disks on new line */
  480. if (newlines) {
  481. if (path->dused_inodes_percent < 0) {
  482. xasprintf(&output, "%s inode=-)%s;\n", output, (disk_result ? "]" : ""));
  483. } else {
  484. xasprintf(&output, "%s inode=%.0f%%)%s;\n", output, path->dfree_inodes_percent, ((disk_result && verbose) ? "]" : ""));
  485. }
  486. } else {
  487. if (path->dused_inodes_percent < 0) {
  488. xasprintf(&output, "%s inode=-)%s;", output, (disk_result ? "]" : ""));
  489. } else {
  490. xasprintf(&output, "%s inode=%.0f%%)%s;", output, path->dfree_inodes_percent, ((disk_result && verbose) ? "]" : ""));
  491. }
  492. }
  493. free(flag_header);
  494. }
  495. /* TODO: Need to do a similar debug line
  496. xasprintf (&details, _("%s\n\
  497. %.0f of %.0f %s (%.0f%% inode=%.0f%%) free on %s (type %s mounted on %s) warn:%lu crit:%lu warn%%:%.0f%% crit%%:%.0f%%"),
  498. details, (double)dfree_units, (double)dtotal_units, units, dfree_pct, inode_space_pct,
  499. me->me_devname, me->me_type, me->me_mountdir,
  500. (unsigned long)w_df, (unsigned long)c_df, w_dfp, c_dfp);
  501. */
  502. }
  503. }
  504. if (human_output) {
  505. print_human_disk_entries(&human_disk_entries[0], num_human_disk_entries);
  506. } else {
  507. if (verbose >= 2)
  508. xasprintf (&output, "%s%s", output, details);
  509. if (newlines) {
  510. printf ("DISK %s%s\n%s|%s\n", state_text (result), (erronly && result==STATE_OK) ? "" : preamble, output, perf);
  511. } else {
  512. printf ("DISK %s%s%s|%s\n", state_text (result), (erronly && result==STATE_OK) ? "" : preamble, output, perf);
  513. }
  514. }
  515. return result;
  516. }
  517. double calculate_percent(uintmax_t value, uintmax_t total) {
  518. double pct = -1;
  519. /* I don't understand the below, but it is taken from coreutils' df */
  520. /* Seems to be calculating pct, in the best possible way */
  521. if (value <= TYPE_MAXIMUM(uintmax_t) / 10000
  522. && total != 0) {
  523. uintmax_t u100 = value * 10000;
  524. pct = (u100 / total + (u100 % total != 0)) / 100.0;
  525. } else {
  526. /* Possible rounding errors - see coreutils' df for more explanation */
  527. double u = value;
  528. double t = total;
  529. if (t) {
  530. long int lipct = pct = u * 10000 / t;
  531. double ipct = lipct / 100.0;
  532. /* Like 'pct = ceil (dpct);', but without ceil - from coreutils again */
  533. if (ipct - 1 < pct && pct <= ipct + 1)
  534. pct = ipct + (ipct < pct);
  535. }
  536. }
  537. return pct;
  538. }
  539. /* process command-line arguments */
  540. int
  541. process_arguments (int argc, char **argv)
  542. {
  543. int c, err, i;
  544. struct parameter_list *se;
  545. struct parameter_list *temp_list = NULL, *previous = NULL;
  546. struct parameter_list *temp_path_select_list = NULL;
  547. struct mount_entry *me, *temp_me;
  548. int result = OK;
  549. regex_t re;
  550. int cflags = REG_NOSUB | REG_EXTENDED;
  551. int default_cflags = cflags;
  552. char errbuf[MAX_INPUT_BUFFER];
  553. int fnd = 0;
  554. enum {
  555. SKIP_FAKE_FS = CHAR_MAX + 1,
  556. INODE_PERFDATA_ENABLED,
  557. COMBINED_THRESHOLDS,
  558. };
  559. int option = 0;
  560. static struct option longopts[] = {
  561. {"timeout", required_argument, 0, 't'},
  562. {"warning", required_argument, 0, 'w'},
  563. {"critical", required_argument, 0, 'c'},
  564. {"combined-thresholds", no_argument, 0, COMBINED_THRESHOLDS},
  565. {"iwarning", required_argument, 0, 'W'},
  566. /* Dang, -C is taken. We might want to reshuffle this. */
  567. {"icritical", required_argument, 0, 'K'},
  568. {"kilobytes", no_argument, 0, 'k'},
  569. {"megabytes", no_argument, 0, 'm'},
  570. {"units", required_argument, 0, 'u'},
  571. {"human", no_argument, 0, 'H'},
  572. {"path", required_argument, 0, 'p'},
  573. {"partition", required_argument, 0, 'p'},
  574. {"exclude_device", required_argument, 0, 'x'},
  575. {"exclude-type", required_argument, 0, 'X'},
  576. {"include-type", required_argument, 0, 'N'},
  577. {"newlines", no_argument, 0, 'n'},
  578. {"group", required_argument, 0, 'g'},
  579. {"eregi-path", required_argument, 0, 'R'},
  580. {"eregi-partition", required_argument, 0, 'R'},
  581. {"ereg-path", required_argument, 0, 'r'},
  582. {"ereg-partition", required_argument, 0, 'r'},
  583. {"freespace-ignore-reserved", no_argument, 0, 'f'},
  584. {"ignore-ereg-path", required_argument, 0, 'i'},
  585. {"ignore-ereg-partition", required_argument, 0, 'i'},
  586. {"ignore-eregi-path", required_argument, 0, 'I'},
  587. {"ignore-eregi-partition", required_argument, 0, 'I'},
  588. {"local", no_argument, 0, 'l'},
  589. {"skip-fake-fs", no_argument, 0, SKIP_FAKE_FS},
  590. {"inode-perfdata", no_argument, 0, INODE_PERFDATA_ENABLED},
  591. {"stat-remote-fs", no_argument, 0, 'L'},
  592. {"mountpoint", no_argument, 0, 'M'},
  593. {"errors-only", no_argument, 0, 'e'},
  594. {"exact-match", no_argument, 0, 'E'},
  595. {"all", no_argument, 0, 'A'},
  596. {"verbose", no_argument, 0, 'v'},
  597. {"quiet", no_argument, 0, 'q'},
  598. {"clear", no_argument, 0, 'C'},
  599. {"version", no_argument, 0, 'V'},
  600. {"help", no_argument, 0, 'h'},
  601. {0, 0, 0, 0}
  602. };
  603. if (argc < 2)
  604. return ERROR;
  605. for (i = 0; always_exclude[i]; ++i)
  606. np_add_name(&fs_exclude_list, always_exclude[i]);
  607. for (c = 1; c < argc; c++)
  608. if (strcmp ("-to", argv[c]) == 0)
  609. strcpy (argv[c], "-t");
  610. while (1) {
  611. c = getopt_long (argc, argv, "+?VqhHvefCt:c:w:K:W:u:p:x:X:N:mklLg:R:r:i:I:MEAn", longopts, &option);
  612. if (c == -1 || c == EOF)
  613. break;
  614. switch (c) {
  615. case 't': /* timeout period */
  616. timeout_interval = parse_timeout_string(optarg);
  617. break;
  618. /* See comments for 'c' */
  619. case 'w': /* warning threshold */
  620. if (strstr(optarg, "%")) {
  621. if (*optarg == '@') {
  622. warn_freespace_percent = optarg;
  623. } else {
  624. xasprintf(&warn_freespace_percent, "@%s", optarg);
  625. }
  626. } else {
  627. if (*optarg == '@') {
  628. warn_freespace_units = optarg;
  629. } else {
  630. xasprintf(&warn_freespace_units, "@%s", optarg);
  631. }
  632. }
  633. break;
  634. /* Awful mistake where the range values do not make sense. Normally,
  635. you alert if the value is within the range, but since we are using
  636. freespace, we have to alert if outside the range. Thus we artificially
  637. force @ at the beginning of the range, so that it is backwards compatible
  638. */
  639. case 'c': /* critical threshold */
  640. if (strstr(optarg, "%")) {
  641. if (*optarg == '@') {
  642. crit_freespace_percent = optarg;
  643. } else {
  644. xasprintf(&crit_freespace_percent, "@%s", optarg);
  645. }
  646. } else {
  647. if (*optarg == '@') {
  648. crit_freespace_units = optarg;
  649. } else {
  650. xasprintf(&crit_freespace_units, "@%s", optarg);
  651. }
  652. }
  653. break;
  654. case COMBINED_THRESHOLDS:
  655. combined_thresholds = 1;
  656. break;
  657. case 'W': /* warning inode threshold */
  658. if (*optarg == '@') {
  659. warn_freeinodes_percent = optarg;
  660. } else {
  661. xasprintf(&warn_freeinodes_percent, "@%s", optarg);
  662. }
  663. break;
  664. case 'K': /* critical inode threshold */
  665. if (*optarg == '@') {
  666. crit_freeinodes_percent = optarg;
  667. } else {
  668. xasprintf(&crit_freeinodes_percent, "@%s", optarg);
  669. }
  670. break;
  671. case 'u':
  672. if (units)
  673. free(units);
  674. if (! strcmp (optarg, "bytes")) {
  675. mult = (uintmax_t)1;
  676. units = strdup ("B");
  677. } else if (! strcmp (optarg, "kB")) {
  678. mult = (uintmax_t)1024;
  679. units = strdup ("kB");
  680. } else if (! strcmp (optarg, "MB")) {
  681. mult = (uintmax_t)1024 * 1024;
  682. units = strdup ("MB");
  683. } else if (! strcmp (optarg, "GB")) {
  684. mult = (uintmax_t)1024 * 1024 * 1024;
  685. units = strdup ("GB");
  686. } else if (! strcmp (optarg, "TB")) {
  687. mult = (uintmax_t)1024 * 1024 * 1024 * 1024;
  688. units = strdup ("TB");
  689. } else if (! strcmp (optarg, "KiB")) {
  690. mult = (uintmax_t)1024;
  691. units = strdup ("KiB");
  692. } else if (! strcmp (optarg, "MiB")) {
  693. mult = (uintmax_t)1024 * 1024;
  694. units = strdup ("MiB");
  695. } else if (! strcmp (optarg, "GiB")) {
  696. mult = (uintmax_t)1024 * 1024 * 1024;
  697. units = strdup ("GiB");
  698. } else if (! strcmp (optarg, "TiB")) {
  699. mult = (uintmax_t)1024 * 1024 * 1024 * 1024;
  700. units = strdup ("TiB");
  701. } else {
  702. die (STATE_UNKNOWN, _("unit type %s not known\n"), optarg);
  703. }
  704. if (units == NULL)
  705. die (STATE_UNKNOWN, _("failed allocating storage for '%s'\n"), "units");
  706. break;
  707. case 'H': /* Human display */
  708. human_output = 1;
  709. break;
  710. case 'k': /* display mountpoint */
  711. mult = (uintmax_t)1024;
  712. if (units)
  713. free(units);
  714. units = strdup ("kB");
  715. break;
  716. case 'm': /* display mountpoint */
  717. mult = (uintmax_t)1024 * 1024;
  718. if (units)
  719. free(units);
  720. units = strdup ("MB");
  721. break;
  722. case 'L': /* show local filesystems, but stat remote filesystems for accessibility */
  723. stat_remote_fs = 1;
  724. show_local_fs = 1;
  725. break;
  726. case 'l':
  727. show_local_fs = 1;
  728. break;
  729. case SKIP_FAKE_FS:
  730. skip_fake_fs = 1;
  731. break;
  732. case INODE_PERFDATA_ENABLED:
  733. inode_perfdata_enabled = 1;
  734. break;
  735. case 'p': /* select path */
  736. if (! (warn_freespace_units || crit_freespace_units || warn_freespace_percent ||
  737. crit_freespace_percent || warn_usedspace_units || crit_usedspace_units ||
  738. warn_usedspace_percent || crit_usedspace_percent || warn_usedinodes_percent ||
  739. crit_usedinodes_percent || warn_freeinodes_percent || crit_freeinodes_percent )) {
  740. die (STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set a threshold value before using -p\n"));
  741. }
  742. /* add parameter if not found. overwrite thresholds if path has already been added */
  743. if (! (se = np_find_parameter(path_select_list, optarg))) {
  744. se = np_add_parameter(&path_select_list, optarg);
  745. }
  746. se->group = group;
  747. set_all_thresholds(se);
  748. /* With autofs, it is required to stat() the path before re-populating the mount_list */
  749. stat_path(se);
  750. /* NB: We can't free the old mount_list "just like that": both list pointers and struct
  751. * pointers are copied around. One of the reason it wasn't done yet is that other parts
  752. * of check_disk need the same kind of cleanup so it'd better be done as a whole */
  753. mount_list = read_file_system_list (0);
  754. np_set_best_match(se, mount_list, exact_match);
  755. path_selected = TRUE;
  756. break;
  757. case 'x': /* exclude path or partition */
  758. np_add_name(&dp_exclude_list, optarg);
  759. break;
  760. case 'X': /* exclude file system type */
  761. np_add_name(&fs_exclude_list, optarg);
  762. break;
  763. case 'N': /* include file system type */
  764. np_add_name(&fs_include_list, optarg);
  765. break;
  766. case 'n': /* show each disk on a new line */
  767. newlines = TRUE;
  768. break;
  769. case 'v': /* verbose */
  770. verbose++;
  771. break;
  772. case 'q': /* TODO: this function should eventually go away (removed 2007-09-20) */
  773. /* verbose--; **replaced by line below**. -q was only a broken way of implementing -e */
  774. erronly = TRUE;
  775. break;
  776. case 'e':
  777. erronly = TRUE;
  778. break;
  779. case 'E':
  780. if (path_selected)
  781. die (STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set -E before selecting paths\n"));
  782. exact_match = TRUE;
  783. break;
  784. case 'f':
  785. freespace_ignore_reserved = TRUE;
  786. break;
  787. case 'g':
  788. if (path_selected)
  789. die (STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set group value before selecting paths\n"));
  790. group = optarg;
  791. break;
  792. case 'I':
  793. cflags |= REG_ICASE;
  794. case 'i':
  795. if (!path_selected)
  796. die (STATE_UNKNOWN, "DISK %s: %s\n", _("UNKNOWN"), _("Paths need to be selected before using -i/-I. Use -A to select all paths explicitly"));
  797. err = regcomp(&re, optarg, cflags);
  798. if (err != 0) {
  799. regerror (err, &re, errbuf, MAX_INPUT_BUFFER);
  800. die (STATE_UNKNOWN, "DISK %s: %s - %s\n",_("UNKNOWN"), _("Could not compile regular expression"), errbuf);
  801. }
  802. temp_list = path_select_list;
  803. previous = NULL;
  804. while (temp_list) {
  805. if (temp_list->best_match) {
  806. if (np_regex_match_mount_entry(temp_list->best_match, &re)) {
  807. if (verbose >= 3)
  808. printf("ignoring %s matching regex\n", temp_list->name);
  809. temp_list = np_del_parameter(temp_list, previous);
  810. /* pointer to first element needs to be updated if first item gets deleted */
  811. if (previous == NULL)
  812. path_select_list = temp_list;
  813. } else {
  814. previous = temp_list;
  815. temp_list = temp_list->name_next;
  816. }
  817. } else {
  818. previous = temp_list;
  819. temp_list = temp_list->name_next;
  820. }
  821. }
  822. cflags = default_cflags;
  823. break;
  824. case 'A':
  825. optarg = strdup(".*");
  826. case 'R':
  827. cflags |= REG_ICASE;
  828. case 'r':
  829. if (! (warn_freespace_units || crit_freespace_units || warn_freespace_percent ||
  830. crit_freespace_percent || warn_usedspace_units || crit_usedspace_units ||
  831. warn_usedspace_percent || crit_usedspace_percent || warn_usedinodes_percent ||
  832. crit_usedinodes_percent || warn_freeinodes_percent || crit_freeinodes_percent )) {
  833. die (STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set a threshold value before using -r/-R\n"));
  834. }
  835. err = regcomp(&re, optarg, cflags);
  836. if (err != 0) {
  837. regerror (err, &re, errbuf, MAX_INPUT_BUFFER);
  838. die (STATE_UNKNOWN, "DISK %s: %s - %s\n",_("UNKNOWN"), _("Could not compile regular expression"), errbuf);
  839. }
  840. for (me = mount_list; me; me = me->me_next) {
  841. if (np_regex_match_mount_entry(me, &re)) {
  842. fnd = TRUE;
  843. if (verbose >= 3)
  844. printf("%s %s matching expression %s\n", me->me_devname, me->me_mountdir, optarg);
  845. /* add parameter if not found. overwrite thresholds if path has already been added */
  846. if (! (se = np_find_parameter(path_select_list, me->me_mountdir))) {
  847. se = np_add_parameter(&path_select_list, me->me_mountdir);
  848. }
  849. se->group = group;
  850. set_all_thresholds(se);
  851. }
  852. }
  853. if (!fnd)
  854. die (STATE_UNKNOWN, "DISK %s: %s - %s\n",_("UNKNOWN"),
  855. _("Regular expression did not match any path or disk"), optarg);
  856. fnd = FALSE;
  857. path_selected = TRUE;
  858. np_set_best_match(path_select_list, mount_list, exact_match);
  859. cflags = default_cflags;
  860. break;
  861. case 'M': /* display mountpoint */
  862. display_mntp = TRUE;
  863. break;
  864. case 'C':
  865. /* add all mount entries to path_select list if no partitions have been explicitly defined using -p */
  866. if (path_selected == FALSE) {
  867. struct parameter_list *path;
  868. for (me = mount_list; me; me = me->me_next) {
  869. if (! (path = np_find_parameter(path_select_list, me->me_mountdir)))
  870. path = np_add_parameter(&path_select_list, me->me_mountdir);
  871. path->best_match = me;
  872. path->group = group;
  873. set_all_thresholds(path);
  874. }
  875. }
  876. warn_freespace_units = NULL;
  877. crit_freespace_units = NULL;
  878. warn_usedspace_units = NULL;
  879. crit_usedspace_units = NULL;
  880. warn_freespace_percent = NULL;
  881. crit_freespace_percent = NULL;
  882. warn_usedspace_percent = NULL;
  883. crit_usedspace_percent = NULL;
  884. warn_usedinodes_percent = NULL;
  885. crit_usedinodes_percent = NULL;
  886. warn_freeinodes_percent = NULL;
  887. crit_freeinodes_percent = NULL;
  888. path_selected = FALSE;
  889. group = NULL;
  890. break;
  891. case 'V': /* version */
  892. print_revision (progname, NP_VERSION);
  893. exit (STATE_OK);
  894. case 'h': /* help */
  895. print_help ();
  896. exit (STATE_OK);
  897. case '?': /* help */
  898. usage (_("Unknown argument"));
  899. }
  900. }
  901. /* Support for "check_disk warn crit [fs]" with thresholds at used% level */
  902. c = optind;
  903. if (warn_usedspace_percent == NULL && argc > c && is_intnonneg (argv[c]))
  904. warn_usedspace_percent = argv[c++];
  905. if (crit_usedspace_percent == NULL && argc > c && is_intnonneg (argv[c]))
  906. crit_usedspace_percent = argv[c++];
  907. if (argc > c && path == NULL) {
  908. se = np_add_parameter(&path_select_list, strdup(argv[c++]));
  909. path_selected = TRUE;
  910. set_all_thresholds(se);
  911. }
  912. if (units == NULL) {
  913. units = strdup ("MiB");
  914. mult = (uintmax_t)1024 * 1024;
  915. }
  916. return TRUE;
  917. }
  918. void
  919. print_path (const char *mypath)
  920. {
  921. if (mypath == NULL)
  922. printf ("\n");
  923. else
  924. printf (_(" for %s\n"), mypath);
  925. }
  926. void
  927. set_all_thresholds (struct parameter_list *path)
  928. {
  929. if (path->freespace_units != NULL) free(path->freespace_units);
  930. set_thresholds(&path->freespace_units, warn_freespace_units, crit_freespace_units);
  931. if (path->freespace_percent != NULL) free (path->freespace_percent);
  932. set_thresholds(&path->freespace_percent, warn_freespace_percent, crit_freespace_percent);
  933. if (path->usedspace_units != NULL) free (path->usedspace_units);
  934. set_thresholds(&path->usedspace_units, warn_usedspace_units, crit_usedspace_units);
  935. if (path->usedspace_percent != NULL) free (path->usedspace_percent);
  936. set_thresholds(&path->usedspace_percent, warn_usedspace_percent, crit_usedspace_percent);
  937. if (path->usedinodes_percent != NULL) free (path->usedinodes_percent);
  938. set_thresholds(&path->usedinodes_percent, warn_usedinodes_percent, crit_usedinodes_percent);
  939. if (path->freeinodes_percent != NULL) free (path->freeinodes_percent);
  940. set_thresholds(&path->freeinodes_percent, warn_freeinodes_percent, crit_freeinodes_percent);
  941. }
  942. /* TODO: Remove?
  943. int
  944. validate_arguments (uintmax_t w, uintmax_t c, double wp, double cp, double iwp, double icp, char *mypath)
  945. {
  946. if (w < 0 && c < 0 && wp < 0.0 && cp < 0.0) {
  947. printf (_("INPUT ERROR: No thresholds specified"));
  948. print_path (mypath);
  949. return ERROR;
  950. }
  951. else if ((wp >= 0.0 || cp >= 0.0) &&
  952. (wp < 0.0 || cp < 0.0 || wp > 100.0 || cp > 100.0 || cp > wp)) {
  953. printf (_("\
  954. INPUT ERROR: C_DFP (%f) should be less than W_DFP (%.1f) and both should be between zero and 100 percent, inclusive"),
  955. cp, wp);
  956. print_path (mypath);
  957. return ERROR;
  958. }
  959. else if ((iwp >= 0.0 || icp >= 0.0) &&
  960. (iwp < 0.0 || icp < 0.0 || iwp > 100.0 || icp > 100.0 || icp > iwp)) {
  961. printf (_("\
  962. INPUT ERROR: C_IDFP (%f) should be less than W_IDFP (%.1f) and both should be between zero and 100 percent, inclusive"),
  963. icp, iwp);
  964. print_path (mypath);
  965. return ERROR;
  966. }
  967. else if ((w > 0 || c > 0) && (w == 0 || c == 0 || c > w)) {
  968. printf (_("\
  969. INPUT ERROR: C_DF (%lu) should be less than W_DF (%lu) and both should be greater than zero"),
  970. (unsigned long)c, (unsigned long)w);
  971. print_path (mypath);
  972. return ERROR;
  973. }
  974. return OK;
  975. }
  976. */
  977. void
  978. print_help (void)
  979. {
  980. print_revision (progname, NP_VERSION);
  981. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  982. printf (COPYRIGHT, copyright, email);
  983. printf ("%s\n", _("This plugin checks the amount of used disk space on a mounted file system"));
  984. printf ("%s\n", _("and generates an alert if free space is less than one of the threshold values"));
  985. printf ("\n\n");
  986. print_usage ();
  987. printf (UT_HELP_VRSN);
  988. printf (UT_EXTRA_OPTS);
  989. printf (" %s\n", "-w, --warning=INTEGER");
  990. printf (" %s\n", _("Exit with WARNING status if less than INTEGER units of disk are free"));
  991. printf (" %s\n", "-w, --warning=PERCENT%");
  992. printf (" %s\n", _("Exit with WARNING status if less than PERCENT of disk space is free"));
  993. printf (" %s\n", "-c, --critical=INTEGER");
  994. printf (" %s\n", _("Exit with CRITICAL status if less than INTEGER units of disk are free"));
  995. printf (" %s\n", "-c, --critical=PERCENT%");
  996. printf (" %s\n", _("Exit with CRITICAL status if less than PERCENT of disk space is free"));
  997. printf (" %s\n", " --combined-thresholds");
  998. printf (" %s\n", _("Do not alert at a given level unless \"PERCENT\" AND \"INTEGER units\""));
  999. printf (" %s\n", _("thresholds are met. For an OR condition, don't use this flag."));
  1000. printf (" %s\n", "-W, --iwarning=PERCENT%");
  1001. printf (" %s\n", _("Exit with WARNING status if less than PERCENT of inode space is free"));
  1002. printf (" %s\n", "-K, --icritical=PERCENT%");
  1003. printf (" %s\n", _("Exit with CRITICAL status if less than PERCENT of inode space is free"));
  1004. printf (" %s\n", "-p, --path=PATH, --partition=PARTITION");
  1005. printf (" %s\n", _("Mount point or block device as emitted by the mount(8) command (may be repeated)"));
  1006. printf (" %s\n", "-x, --exclude_device=PATH <STRING>");
  1007. printf (" %s\n", _("Ignore device (only works if -p unspecified)"));
  1008. printf (" %s\n", "-C, --clear");
  1009. printf (" %s\n", _("Clear thresholds"));
  1010. printf (" %s\n", "-E, --exact-match");
  1011. printf (" %s\n", _("For paths or partitions specified with -p, only check for exact paths"));
  1012. printf (" %s\n", "-e, --errors-only");
  1013. printf (" %s\n", _("Display only devices/mountpoints with errors"));
  1014. printf (" %s\n", "-f, --freespace-ignore-reserved");
  1015. printf (" %s\n", _("Don't account root-reserved blocks into freespace in perfdata"));
  1016. printf (" %s\n", "-g, --group=NAME");
  1017. printf (" %s\n", _("Group paths. Thresholds apply to (free-)space of all partitions together"));
  1018. printf (" %s\n", "-H, --human");
  1019. printf (" %s\n", _("Produce human-readable output."));
  1020. printf (" %s\n", "-k, --kilobytes");
  1021. printf (" %s\n", _("Same as '--units kB'"));
  1022. printf (" %s\n", "-l, --local");
  1023. printf (" %s\n", _("Only check local filesystems"));
  1024. printf (" %s\n", " --skip-fake-fs");
  1025. printf (" %s\n", _("Skip 'fake' mountpoints created by the system"));
  1026. printf (" %s\n", "--inode-perfdata");
  1027. printf (" %s\n", _("Enable performance data for inode-based statistics"));
  1028. printf (" %s\n", "-L, --stat-remote-fs");
  1029. printf (" %s\n", _("Only check local filesystems against thresholds. Yet call stat on remote filesystems"));
  1030. printf (" %s\n", _("to test if they are accessible (e.g. to detect Stale NFS Handles)"));
  1031. printf (" %s\n", "-M, --mountpoint");
  1032. printf (" %s\n", _("Display the mountpoint instead of the partition"));
  1033. printf (" %s\n", "-m, --megabytes");
  1034. printf (" %s\n", _("Same as '--units MB'"));
  1035. printf (" %s\n", "-A, --all");
  1036. printf (" %s\n", _("Explicitly select all paths. This is equivalent to -R '.*'"));
  1037. printf (" %s\n", "-R, --eregi-path=PATH, --eregi-partition=PARTITION");
  1038. printf (" %s\n", _("Case insensitive regular expression for path/partition (may be repeated)"));
  1039. printf (" %s\n", "-r, --ereg-path=PATH, --ereg-partition=PARTITION");
  1040. printf (" %s\n", _("Regular expression for path or partition (may be repeated)"));
  1041. printf (" %s\n", "-I, --ignore-eregi-path=PATH, --ignore-eregi-partition=PARTITION");
  1042. printf (" %s\n", _("Regular expression to ignore selected path/partition (case insensitive) (may be repeated)"));
  1043. printf (" %s\n", "-i, --ignore-ereg-path=PATH, --ignore-ereg-partition=PARTITION");
  1044. printf (" %s\n", _("Regular expression to ignore selected path or partition (may be repeated)"));
  1045. printf (UT_PLUG_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
  1046. printf (" %s\n", "-u, --units=STRING");
  1047. printf (" %s\n", _("Choose bytes, kB, MB, GB, TB, KiB, MiB, GiB, TiB (default: MiB)"));
  1048. printf (" %s\n", _("Note: kB/MB/GB/TB are still calculated as their respective binary"));
  1049. printf (" %s\n", _("units due to backward compatibility issues."));
  1050. printf (UT_VERBOSE);
  1051. printf (" %s\n", "-X, --exclude-type=TYPE");
  1052. printf (" %s\n", _("Ignore all filesystems of indicated type (may be repeated)"));
  1053. printf (" %s\n", "-N, --include-type=TYPE");
  1054. printf (" %s\n", _("Check only filesystems of indicated type (may be repeated)"));
  1055. printf (" %s\n", "-n, --newlines");
  1056. printf (" %s\n", _("Multi-line output of each disk's status information on a new line"));
  1057. printf ("\n");
  1058. printf ("%s\n", _("Examples:"));
  1059. printf (" %s\n", "check_disk -w 10% -c 5% -p /tmp -p /var -C -w 100000 -c 50000 -p /");
  1060. printf (" %s\n", _("Checks /tmp and /var at 10% and 5%, and / at 100MB and 50MB"));
  1061. printf (" %s\n", "check_disk -w 100 -c 50 -C -w 1000 -c 500 -g sidDATA -r '^/oracle/SID/data.*$'");
  1062. printf (" %s\n", _("Checks all filesystems not matching -r at 100M and 50M. The fs matching the -r regex"));
  1063. printf (" %s\n", _("are grouped which means the freespace thresholds are applied to all disks together"));
  1064. printf (" %s\n", "check_disk -w 100 -c 50 -C -w 1000 -c 500 -p /foo -C -w 5% -c 3% -p /bar");
  1065. printf (" %s\n", _("Checks /foo for 1000M/500M and /bar for 5/3%. All remaining volumes use 100M/50M"));
  1066. printf (UT_SUPPORT);
  1067. }
  1068. void
  1069. print_usage (void)
  1070. {
  1071. printf ("%s\n", _("Usage:"));
  1072. printf (" %s -w limit -c limit [-W limit] [-K limit] {-p path | -x device}\n", progname);
  1073. printf ("[-C] [-E] [-e] [-f] [-g group ] [-H] [-k] [-l] [-M] [-m] [-R path ] [-r path ]\n");
  1074. printf ("[-t timeout] [-u unit] [-v] [-X type] [-N type] [-n] [--combined-thresholds ]\n");
  1075. }
  1076. void
  1077. stat_path (struct parameter_list *p)
  1078. {
  1079. /* Stat entry to check that dir exists and is accessible */
  1080. if (verbose >= 3)
  1081. printf("calling stat on %s\n", p->name);
  1082. if (stat (p->name, &stat_buf[0])) {
  1083. if (verbose >= 3)
  1084. printf("stat failed on %s\n", p->name);
  1085. if (!human_output)
  1086. printf("DISK %s - ", _("CRITICAL"));
  1087. die (STATE_CRITICAL, _("%s %s: %s\n"), p->name, _("is not accessible"), strerror(errno));
  1088. }
  1089. }
  1090. void
  1091. get_stats (struct parameter_list *p, struct fs_usage *fsp) {
  1092. struct parameter_list *p_list;
  1093. struct fs_usage tmpfsp;
  1094. int first = 1;
  1095. if (p->group == NULL) {
  1096. get_path_stats(p,fsp);
  1097. } else {
  1098. /* find all group members */
  1099. for (p_list = path_select_list; p_list; p_list=p_list->name_next) {
  1100. #ifdef __CYGWIN__
  1101. if (strncmp(p_list->name, "/cygdrive/", 10) != 0)
  1102. continue;
  1103. #endif
  1104. if (p_list->group && ! (strcmp(p_list->group, p->group))) {
  1105. stat_path(p_list);
  1106. get_fs_usage (p_list->best_match->me_mountdir, p_list->best_match->me_devname, &tmpfsp);
  1107. get_path_stats(p_list, &tmpfsp);
  1108. if (verbose >= 3)
  1109. printf("Group %s: adding %llu blocks sized %llu, (%s) used_units=%g free_units=%g total_units=%g fsu_blocksize=%llu mult=%llu\n",
  1110. p_list->group, tmpfsp.fsu_bavail, tmpfsp.fsu_blocksize, p_list->best_match->me_mountdir, (double)p_list->dused_units, (double)p_list->dfree_units,
  1111. (double)p_list->dtotal_units, mult);
  1112. /* prevent counting the first FS of a group twice since its parameter_list entry
  1113. * is used to carry the information of all file systems of the entire group */
  1114. if (! first) {
  1115. p->total += p_list->total;
  1116. p->available += p_list->available;
  1117. p->available_to_root += p_list->available_to_root;
  1118. p->used += p_list->used;
  1119. p->dused_units += p_list->dused_units;
  1120. p->dfree_units += p_list->dfree_units;
  1121. p->dtotal_units += p_list->dtotal_units;
  1122. p->inodes_total += p_list->inodes_total;
  1123. p->inodes_free += p_list->inodes_free;
  1124. }
  1125. first = 0;
  1126. }
  1127. if (verbose >= 3)
  1128. printf("Group %s now has: used_units=%g free_units=%g total_units=%g fsu_blocksize=%llu mult=%llu\n",
  1129. p->group, tmpfsp.fsu_bavail, tmpfsp.fsu_blocksize, p->best_match->me_mountdir, (double)p->dused_units,
  1130. (double)p->dfree_units, (double)p->dtotal_units, mult);
  1131. }
  1132. /* modify devname and mountdir for output */
  1133. p->best_match->me_mountdir = p->best_match->me_devname = p->group;
  1134. }
  1135. /* finally calculate percentages for either plain FS or summed up group */
  1136. p->dused_pct = calculate_percent( p->used, p->used + p->available ); /* used + available can never be > uintmax */
  1137. p->dfree_pct = 100 - p->dused_pct;
  1138. p->dused_inodes_percent = calculate_percent(p->inodes_total - p->inodes_free, p->inodes_total);
  1139. p->dfree_inodes_percent = 100 - p->dused_inodes_percent;
  1140. }
  1141. void
  1142. get_path_stats (struct parameter_list *p, struct fs_usage *fsp) {
  1143. #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(OpenBSD )
  1144. /* 2007-12-08 - Workaround for Gnulib reporting insanely high available
  1145. * space on BSD (the actual value should be negative but fsp->fsu_bavail
  1146. * is unsigned) */
  1147. p->available = fsp->fsu_bavail > fsp->fsu_bfree ? 0 : fsp->fsu_bavail;
  1148. #else
  1149. p->available = fsp->fsu_bavail;
  1150. #endif
  1151. p->available_to_root = fsp->fsu_bfree;
  1152. p->used = fsp->fsu_blocks - fsp->fsu_bfree;
  1153. if (freespace_ignore_reserved) {
  1154. /* option activated : we subtract the root-reserved space from the total */
  1155. p->total = fsp->fsu_blocks - p->available_to_root + p->available;
  1156. } else {
  1157. /* default behaviour : take all the blocks into account */
  1158. p->total = fsp->fsu_blocks;
  1159. }
  1160. p->dused_units = p->used*fsp->fsu_blocksize/mult;
  1161. p->dfree_units = p->available*fsp->fsu_blocksize/mult;
  1162. p->dtotal_units = p->total*fsp->fsu_blocksize/mult;
  1163. p->inodes_total = fsp->fsu_files; /* Total file nodes. */
  1164. p->inodes_free = fsp->fsu_ffree; /* Free file nodes. */
  1165. np_add_name(&seen, p->best_match->me_mountdir);
  1166. }
  1167. void
  1168. print_human_disk_entries(human_disk_entry_t* human_disk_entries, unsigned num_human_disk_entries) {
  1169. char avail_bytes_buf[10], total_bytes_buf[10];
  1170. const human_disk_entry_t* human_disk_entry = human_disk_entries;
  1171. unsigned int separator_length =
  1172. human_column_widths.disk_result +
  1173. human_column_widths.free_pct +
  1174. human_column_widths.avail_bytes +
  1175. human_column_widths.total_bytes +
  1176. human_column_widths.type +
  1177. human_column_widths.mount_dir +
  1178. HUMAN_INTER_COLUMN_WIDTH * 3 + 6;
  1179. char sep_buf[separator_length];
  1180. memset(&sep_buf[0], '-', separator_length);
  1181. sep_buf[separator_length] = 0;
  1182. const human_disk_entry_t** entries_table = malloc(sizeof(human_disk_entry_t*) * num_human_disk_entries);
  1183. int i = 0;
  1184. int num_warn = 0, num_critical = 0;
  1185. while (human_disk_entry != NULL) {
  1186. if (human_disk_entry->disk_result == STATE_CRITICAL) num_critical++;
  1187. if (human_disk_entry->disk_result == STATE_WARNING) num_warn++;
  1188. entries_table[i++] = human_disk_entry;
  1189. human_disk_entry = human_disk_entry->next;
  1190. };
  1191. if (num_critical > 0) {
  1192. range* pct = parse_range_string(crit_freespace_percent);
  1193. printf("Critical: less than %2.1f%% is free on one or more file systems\n\n", pct->end);
  1194. } else if (num_warn > 0) {
  1195. range* pct = parse_range_string(warn_freespace_percent);
  1196. printf("Warning: less than %2.1f%% is free on one or more file systems\n\n", pct->end);
  1197. }
  1198. const char *row_fmt = "%-*s%*s%*s%*s %*s %-*s\n";
  1199. printf(row_fmt,
  1200. human_column_widths.disk_result, human_column_header_names[0],
  1201. human_column_widths.free_pct + HUMAN_INTER_COLUMN_WIDTH, human_column_header_names[1],
  1202. human_column_widths.avail_bytes + HUMAN_INTER_COLUMN_WIDTH, human_column_header_names[2],
  1203. human_column_widths.total_bytes + HUMAN_INTER_COLUMN_WIDTH, human_column_header_names[3],
  1204. human_column_widths.type , human_column_header_names[4],
  1205. human_column_widths.mount_dir, human_column_header_names[5]);
  1206. printf("%s\n", &sep_buf[0]);
  1207. qsort(entries_table, num_human_disk_entries, sizeof(human_disk_entry_t*), human_disk_entry_comparer);
  1208. for (i = 0; i < num_human_disk_entries; i++) {
  1209. human_disk_entry = entries_table[i];
  1210. printf(row_fmt,
  1211. human_column_widths.disk_result, &human_disk_entry->disk_result_str[0],
  1212. human_column_widths.free_pct + HUMAN_INTER_COLUMN_WIDTH, &human_disk_entry->free_pct_str[0],
  1213. human_column_widths.avail_bytes + HUMAN_INTER_COLUMN_WIDTH, &human_disk_entry->avail_bytes_str[0],
  1214. human_column_widths.total_bytes + HUMAN_INTER_COLUMN_WIDTH, &human_disk_entry->total_bytes_str[0],
  1215. human_column_widths.type , human_disk_entry->type,
  1216. human_column_widths.mount_dir, human_disk_entry->mount_dir);
  1217. };
  1218. free(entries_table);
  1219. }
  1220. int
  1221. human_disk_entry_comparer(const void* _lhs, const void* _rhs) {
  1222. const human_disk_entry_t* lhs = *((human_disk_entry_t**)_lhs);
  1223. const human_disk_entry_t* rhs = *((human_disk_entry_t**)_rhs);
  1224. if (lhs->disk_result == rhs->disk_result) {
  1225. return lhs->avail_bytes > rhs->avail_bytes ? 1 : -1;
  1226. } else {
  1227. return lhs->disk_result < rhs->disk_result ? 1 : -1;
  1228. }
  1229. }