diff options
Diffstat (limited to 'plugins/check_disk.c')
| -rw-r--r-- | plugins/check_disk.c | 1299 |
1 files changed, 703 insertions, 596 deletions
diff --git a/plugins/check_disk.c b/plugins/check_disk.c index 037a6f7a..515ddff0 100644 --- a/plugins/check_disk.c +++ b/plugins/check_disk.c | |||
| @@ -31,24 +31,35 @@ const char *program_name = "check_disk"; /* Required for coreutils libs */ | |||
| 31 | const char *copyright = "1999-2024"; | 31 | const char *copyright = "1999-2024"; |
| 32 | const char *email = "devel@monitoring-plugins.org"; | 32 | const char *email = "devel@monitoring-plugins.org"; |
| 33 | 33 | ||
| 34 | #include "states.h" | ||
| 34 | #include "common.h" | 35 | #include "common.h" |
| 36 | #include "output.h" | ||
| 37 | #include "perfdata.h" | ||
| 38 | #include "utils_base.h" | ||
| 39 | #include "lib/thresholds.h" | ||
| 40 | |||
| 35 | #ifdef HAVE_SYS_STAT_H | 41 | #ifdef HAVE_SYS_STAT_H |
| 36 | # include <sys/stat.h> | 42 | # include <sys/stat.h> |
| 37 | #endif | 43 | #endif |
| 44 | |||
| 38 | #if HAVE_INTTYPES_H | 45 | #if HAVE_INTTYPES_H |
| 39 | # include <inttypes.h> | 46 | # include <inttypes.h> |
| 40 | #endif | 47 | #endif |
| 48 | |||
| 41 | #include <assert.h> | 49 | #include <assert.h> |
| 42 | #include "popen.h" | ||
| 43 | #include "utils.h" | ||
| 44 | #include "utils_disk.h" | ||
| 45 | #include <stdarg.h> | 50 | #include <stdarg.h> |
| 46 | #include "fsusage.h" | 51 | #include <stdint.h> |
| 47 | #include "mountlist.h" | ||
| 48 | #include <float.h> | 52 | #include <float.h> |
| 53 | #include "./popen.h" | ||
| 54 | #include "./utils.h" | ||
| 55 | #include "../gl/fsusage.h" | ||
| 56 | #include "../gl/mountlist.h" | ||
| 57 | #include "./check_disk.d/utils_disk.h" | ||
| 58 | |||
| 49 | #if HAVE_LIMITS_H | 59 | #if HAVE_LIMITS_H |
| 50 | # include <limits.h> | 60 | # include <limits.h> |
| 51 | #endif | 61 | #endif |
| 62 | |||
| 52 | #include "regex.h" | 63 | #include "regex.h" |
| 53 | 64 | ||
| 54 | #ifdef __CYGWIN__ | 65 | #ifdef __CYGWIN__ |
| @@ -57,424 +68,310 @@ const char *email = "devel@monitoring-plugins.org"; | |||
| 57 | # define ERROR -1 | 68 | # define ERROR -1 |
| 58 | #endif | 69 | #endif |
| 59 | 70 | ||
| 60 | /* If nonzero, show even filesystems with zero size or | ||
| 61 | uninteresting types. */ | ||
| 62 | static int show_all_fs = 1; | ||
| 63 | |||
| 64 | /* If nonzero, show only local filesystems. */ | ||
| 65 | static int show_local_fs = 0; | ||
| 66 | |||
| 67 | /* If nonzero, show only local filesystems but call stat() on remote ones. */ | ||
| 68 | static int stat_remote_fs = 0; | ||
| 69 | |||
| 70 | /* If positive, the units to use when printing sizes; | ||
| 71 | if negative, the human-readable base. */ | ||
| 72 | /* static int output_block_size; */ | ||
| 73 | |||
| 74 | /* If nonzero, invoke the `sync' system call before getting any usage data. | ||
| 75 | Using this option can make df very slow, especially with many or very | ||
| 76 | busy disks. Note that this may make a difference on some systems -- | ||
| 77 | SunOs4.1.3, for one. It is *not* necessary on Linux. */ | ||
| 78 | /* static int require_sync = 0; */ | ||
| 79 | |||
| 80 | /* Linked list of filesystem types to display. | ||
| 81 | If `fs_select_list' is NULL, list all types. | ||
| 82 | This table is generated dynamically from command-line options, | ||
| 83 | rather than hardcoding into the program what it thinks are the | ||
| 84 | valid filesystem types; let the user specify any filesystem type | ||
| 85 | they want to, and if there are any filesystems of that type, they | ||
| 86 | will be shown. | ||
| 87 | |||
| 88 | Some filesystem types: | ||
| 89 | 4.2 4.3 ufs nfs swap ignore io vm efs dbg */ | ||
| 90 | |||
| 91 | /* static struct parameter_list *fs_select_list; */ | ||
| 92 | |||
| 93 | /* Linked list of filesystem types to omit. | ||
| 94 | If the list is empty, don't exclude any types. */ | ||
| 95 | static struct regex_list *fs_exclude_list = NULL; | ||
| 96 | |||
| 97 | /* Linked list of filesystem types to check. | ||
| 98 | If the list is empty, include all types. */ | ||
| 99 | static struct regex_list *fs_include_list; | ||
| 100 | |||
| 101 | static struct name_list *dp_exclude_list; | ||
| 102 | |||
| 103 | static struct parameter_list *path_select_list = NULL; | ||
| 104 | |||
| 105 | /* Linked list of mounted filesystems. */ | ||
| 106 | static struct mount_entry *mount_list; | ||
| 107 | |||
| 108 | /* For long options that have no equivalent short option, use a | ||
| 109 | non-character as a pseudo short option, starting with CHAR_MAX + 1. */ | ||
| 110 | enum { | ||
| 111 | SYNC_OPTION = CHAR_MAX + 1, | ||
| 112 | NO_SYNC_OPTION, | ||
| 113 | BLOCK_SIZE_OPTION | ||
| 114 | }; | ||
| 115 | |||
| 116 | #ifdef _AIX | 71 | #ifdef _AIX |
| 117 | # pragma alloca | 72 | # pragma alloca |
| 118 | #endif | 73 | #endif |
| 119 | 74 | ||
| 120 | static int process_arguments(int /*argc*/, char ** /*argv*/); | 75 | typedef struct { |
| 121 | static void set_all_thresholds(struct parameter_list *path); | 76 | int errorcode; |
| 122 | static void print_help(void); | 77 | check_disk_config config; |
| 78 | } check_disk_config_wrapper; | ||
| 79 | static check_disk_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
| 80 | |||
| 81 | static void set_all_thresholds(parameter_list_elem *path, char *warn_freespace_units, char *crit_freespace_units, | ||
| 82 | char *warn_freespace_percent, char *crit_freespace_percent, char *warn_freeinodes_percent, | ||
| 83 | char *crit_freeinodes_percent); | ||
| 84 | static double calculate_percent(uintmax_t /*value*/, uintmax_t /*total*/); | ||
| 85 | static bool stat_path(parameter_list_elem * /*parameters*/, bool /*ignore_missing*/); | ||
| 86 | |||
| 87 | /* | ||
| 88 | * Puts the values from a struct fs_usage into a parameter_list with an additional flag to control how reserved | ||
| 89 | * and inodes should be judged (ignored or not) | ||
| 90 | */ | ||
| 91 | static parameter_list_elem get_path_stats(parameter_list_elem parameters, struct fs_usage fsp, bool freespace_ignore_reserved); | ||
| 92 | static mp_subcheck evaluate_filesystem(measurement_unit measurement_unit, bool display_inodes_perfdata, byte_unit unit); | ||
| 93 | |||
| 123 | void print_usage(void); | 94 | void print_usage(void); |
| 124 | static double calculate_percent(uintmax_t, uintmax_t); | 95 | static void print_help(void); |
| 125 | static bool stat_path(struct parameter_list *p); | ||
| 126 | static void get_stats(struct parameter_list *p, struct fs_usage *fsp); | ||
| 127 | static void get_path_stats(struct parameter_list *p, struct fs_usage *fsp); | ||
| 128 | 96 | ||
| 129 | static char *units; | ||
| 130 | static uintmax_t mult = 1024 * 1024; | ||
| 131 | static int verbose = 0; | 97 | static int verbose = 0; |
| 132 | static bool erronly = false; | ||
| 133 | static bool display_mntp = false; | ||
| 134 | static bool exact_match = false; | ||
| 135 | static bool ignore_missing = false; | ||
| 136 | static bool freespace_ignore_reserved = false; | ||
| 137 | static bool display_inodes_perfdata = false; | ||
| 138 | static char *warn_freespace_units = NULL; | ||
| 139 | static char *crit_freespace_units = NULL; | ||
| 140 | static char *warn_freespace_percent = NULL; | ||
| 141 | static char *crit_freespace_percent = NULL; | ||
| 142 | static char *warn_usedspace_units = NULL; | ||
| 143 | static char *crit_usedspace_units = NULL; | ||
| 144 | static char *warn_usedspace_percent = NULL; | ||
| 145 | static char *crit_usedspace_percent = NULL; | ||
| 146 | static char *warn_usedinodes_percent = NULL; | ||
| 147 | static char *crit_usedinodes_percent = NULL; | ||
| 148 | static char *warn_freeinodes_percent = NULL; | ||
| 149 | static char *crit_freeinodes_percent = NULL; | ||
| 150 | static bool path_selected = false; | ||
| 151 | static bool path_ignored = false; | ||
| 152 | static char *group = NULL; | ||
| 153 | static struct stat *stat_buf; | ||
| 154 | static struct name_list *seen = NULL; | ||
| 155 | 98 | ||
| 156 | int main(int argc, char **argv) { | 99 | // This would not be necessary in C23!! |
| 157 | int result = STATE_UNKNOWN; | 100 | const byte_unit Bytes_Factor = 1; |
| 158 | int disk_result = STATE_UNKNOWN; | 101 | const byte_unit KibiBytes_factor = 1024; |
| 159 | char *output = NULL; | 102 | const byte_unit MebiBytes_factor = 1048576; |
| 160 | char *ignored = NULL; | 103 | const byte_unit GibiBytes_factor = 1073741824; |
| 161 | char *details = NULL; | 104 | const byte_unit TebiBytes_factor = 1099511627776; |
| 162 | char *perf = NULL; | 105 | const byte_unit PebiBytes_factor = 1125899906842624; |
| 163 | char *perf_ilabel = NULL; | 106 | const byte_unit ExbiBytes_factor = 1152921504606846976; |
| 164 | char *preamble = " - free space:"; | 107 | const byte_unit KiloBytes_factor = 1000; |
| 165 | char *ignored_preamble = " - ignored paths:"; | 108 | const byte_unit MegaBytes_factor = 1000000; |
| 166 | char *flag_header = NULL; | 109 | const byte_unit GigaBytes_factor = 1000000000; |
| 167 | int temp_result = STATE_UNKNOWN; | 110 | const byte_unit TeraBytes_factor = 1000000000000; |
| 168 | 111 | const byte_unit PetaBytes_factor = 1000000000000000; | |
| 169 | struct mount_entry *me = NULL; | 112 | const byte_unit ExaBytes_factor = 1000000000000000000; |
| 170 | struct fs_usage fsp = {0}; | ||
| 171 | struct parameter_list *temp_list = NULL; | ||
| 172 | struct parameter_list *path = NULL; | ||
| 173 | |||
| 174 | #ifdef __CYGWIN__ | ||
| 175 | char mountdir[32]; | ||
| 176 | #endif | ||
| 177 | 113 | ||
| 178 | output = strdup(""); | ||
| 179 | ignored = strdup(""); | ||
| 180 | details = strdup(""); | ||
| 181 | perf = strdup(""); | ||
| 182 | perf_ilabel = strdup(""); | ||
| 183 | stat_buf = malloc(sizeof *stat_buf); | ||
| 184 | 114 | ||
| 115 | int main(int argc, char **argv) { | ||
| 185 | setlocale(LC_ALL, ""); | 116 | setlocale(LC_ALL, ""); |
| 186 | bindtextdomain(PACKAGE, LOCALEDIR); | 117 | bindtextdomain(PACKAGE, LOCALEDIR); |
| 187 | textdomain(PACKAGE); | 118 | textdomain(PACKAGE); |
| 188 | 119 | ||
| 189 | mount_list = read_file_system_list(0); | 120 | #ifdef __CYGWIN__ |
| 121 | char mountdir[32]; | ||
| 122 | #endif | ||
| 190 | 123 | ||
| 191 | /* Parse extra opts if any */ | 124 | // Parse extra opts if any |
| 192 | argv = np_extra_opts(&argc, argv, progname); | 125 | argv = np_extra_opts(&argc, argv, progname); |
| 193 | 126 | ||
| 194 | if (process_arguments(argc, argv) == ERROR) | 127 | check_disk_config_wrapper tmp_config = process_arguments(argc, argv); |
| 128 | if (tmp_config.errorcode == ERROR) { | ||
| 195 | usage4(_("Could not parse arguments")); | 129 | usage4(_("Could not parse arguments")); |
| 130 | } | ||
| 196 | 131 | ||
| 197 | /* If a list of paths has not been selected, find entire | 132 | check_disk_config config = tmp_config.config; |
| 198 | mount list and create list of paths | 133 | |
| 199 | */ | 134 | if (config.output_format_is_set) { |
| 200 | if (path_selected == false && path_ignored == false) { | 135 | mp_set_format(config.output_format); |
| 201 | for (me = mount_list; me; me = me->me_next) { | ||
| 202 | if (!(path = np_find_parameter(path_select_list, me->me_mountdir))) { | ||
| 203 | path = np_add_parameter(&path_select_list, me->me_mountdir); | ||
| 204 | } | ||
| 205 | path->best_match = me; | ||
| 206 | path->group = group; | ||
| 207 | set_all_thresholds(path); | ||
| 208 | } | ||
| 209 | } | 136 | } |
| 210 | 137 | ||
| 211 | if (path_ignored == false) { | 138 | if (config.erronly) { |
| 212 | np_set_best_match(path_select_list, mount_list, exact_match); | 139 | mp_set_level_of_detail(MP_DETAIL_NON_OK_ONLY); |
| 213 | } | 140 | } |
| 214 | 141 | ||
| 215 | /* Error if no match found for specified paths */ | 142 | if (!config.path_ignored) { |
| 216 | temp_list = path_select_list; | 143 | mp_int_fs_list_set_best_match(config.path_select_list, config.mount_list, config.exact_match); |
| 144 | } | ||
| 217 | 145 | ||
| 218 | while (path_select_list) { | 146 | // Error if no match found for specified paths |
| 219 | if (!path_select_list->best_match && ignore_missing == true) { | 147 | for (parameter_list_elem *elem = config.path_select_list.first; elem;) { |
| 220 | /* If the first element will be deleted, the temp_list must be updated with the new start address as well */ | 148 | if (!elem->best_match && config.ignore_missing) { |
| 221 | if (path_select_list == temp_list) { | ||
| 222 | temp_list = path_select_list->name_next; | ||
| 223 | } | ||
| 224 | /* Add path argument to list of ignored paths to inform about missing paths being ignored and not alerted */ | ||
| 225 | xasprintf(&ignored, "%s %s;", ignored, path_select_list->name); | ||
| 226 | /* Delete the path from the list so that it is not stat-checked later in the code. */ | 149 | /* Delete the path from the list so that it is not stat-checked later in the code. */ |
| 227 | path_select_list = np_del_parameter(path_select_list, path_select_list->name_prev); | 150 | elem = mp_int_fs_list_del(&config.path_select_list, elem); |
| 228 | } else if (!path_select_list->best_match) { | 151 | continue; |
| 152 | } | ||
| 153 | if (!elem->best_match) { | ||
| 229 | /* Without --ignore-missing option, exit with Critical state. */ | 154 | /* Without --ignore-missing option, exit with Critical state. */ |
| 230 | die(STATE_CRITICAL, _("DISK %s: %s not found\n"), _("CRITICAL"), path_select_list->name); | 155 | die(STATE_CRITICAL, _("DISK %s: %s not found\n"), _("CRITICAL"), elem->name); |
| 231 | } else { | ||
| 232 | /* Continue jumping through the list */ | ||
| 233 | path_select_list = path_select_list->name_next; | ||
| 234 | } | 156 | } |
| 235 | } | ||
| 236 | 157 | ||
| 237 | path_select_list = temp_list; | 158 | elem = mp_int_fs_list_get_next(elem); |
| 159 | } | ||
| 238 | 160 | ||
| 239 | if (!path_select_list && ignore_missing == true) { | 161 | mp_check overall = mp_check_init(); |
| 240 | result = STATE_OK; | 162 | if (config.path_select_list.length == 0) { |
| 241 | if (verbose >= 2) { | 163 | mp_subcheck none_sc = mp_subcheck_init(); |
| 242 | printf("None of the provided paths were found\n"); | 164 | xasprintf(&none_sc.output, "No filesystems were found for the provided parameters"); |
| 165 | if (config.ignore_missing) { | ||
| 166 | none_sc = mp_set_subcheck_state(none_sc, STATE_OK); | ||
| 167 | } else { | ||
| 168 | none_sc = mp_set_subcheck_state(none_sc, STATE_UNKNOWN); | ||
| 169 | if (verbose >= 2) { | ||
| 170 | printf("None of the provided paths were found\n"); | ||
| 171 | } | ||
| 243 | } | 172 | } |
| 173 | mp_add_subcheck_to_check(&overall, none_sc); | ||
| 174 | mp_exit(overall); | ||
| 244 | } | 175 | } |
| 245 | 176 | ||
| 246 | /* Process for every path in list */ | 177 | // Filter list first |
| 247 | for (path = path_select_list; path; path = path->name_next) { | 178 | for (parameter_list_elem *path = config.path_select_list.first; path;) { |
| 248 | if (verbose >= 3 && path->freespace_percent->warning != NULL && path->freespace_percent->critical != NULL) | 179 | if (!path->best_match) { |
| 249 | printf("Thresholds(pct) for %s warn: %f crit %f\n", path->name, path->freespace_percent->warning->end, | 180 | path = mp_int_fs_list_del(&config.path_select_list, path); |
| 250 | path->freespace_percent->critical->end); | ||
| 251 | |||
| 252 | if (verbose >= 3 && path->group != NULL) | ||
| 253 | printf("Group of %s: %s\n", path->name, path->group); | ||
| 254 | |||
| 255 | /* reset disk result */ | ||
| 256 | disk_result = STATE_UNKNOWN; | ||
| 257 | |||
| 258 | me = path->best_match; | ||
| 259 | |||
| 260 | if (!me) { | ||
| 261 | continue; | 181 | continue; |
| 262 | } | 182 | } |
| 263 | 183 | ||
| 184 | struct mount_entry *mount_entry = path->best_match; | ||
| 185 | |||
| 264 | #ifdef __CYGWIN__ | 186 | #ifdef __CYGWIN__ |
| 265 | if (strncmp(path->name, "/cygdrive/", 10) != 0 || strlen(path->name) > 11) | 187 | if (strncmp(path->name, "/cygdrive/", 10) != 0 || strlen(path->name) > 11) { |
| 188 | path = mp_int_fs_list_del(&config.path_select_list, path); | ||
| 266 | continue; | 189 | continue; |
| 190 | } | ||
| 191 | |||
| 192 | char *mountdir = NULL; | ||
| 267 | snprintf(mountdir, sizeof(mountdir), "%s:\\", me->me_mountdir + 10); | 193 | snprintf(mountdir, sizeof(mountdir), "%s:\\", me->me_mountdir + 10); |
| 268 | if (GetDriveType(mountdir) != DRIVE_FIXED) | 194 | if (GetDriveType(mountdir) != DRIVE_FIXED) { |
| 269 | me->me_remote = 1; | 195 | mount_entry->me_remote = 1; |
| 196 | } | ||
| 270 | #endif | 197 | #endif |
| 271 | /* Filters */ | ||
| 272 | 198 | ||
| 273 | /* Remove filesystems already seen */ | 199 | /* Remove filesystems already seen */ |
| 274 | if (np_seen_name(seen, me->me_mountdir)) { | 200 | if (np_seen_name(config.seen, mount_entry->me_mountdir)) { |
| 201 | path = mp_int_fs_list_del(&config.path_select_list, path); | ||
| 275 | continue; | 202 | continue; |
| 276 | } | 203 | } |
| 277 | np_add_name(&seen, me->me_mountdir); | ||
| 278 | 204 | ||
| 279 | if (path->group == NULL) { | 205 | if (path->group == NULL) { |
| 280 | /* Skip remote filesystems if we're not interested in them */ | 206 | if (config.fs_exclude_list && np_find_regmatch(config.fs_exclude_list, mount_entry->me_type)) { |
| 281 | if (me->me_remote && show_local_fs) { | 207 | // Skip excluded fs's |
| 282 | if (stat_remote_fs) { | 208 | path = mp_int_fs_list_del(&config.path_select_list, path); |
| 283 | if (!stat_path(path) && ignore_missing == true) { | ||
| 284 | result = STATE_OK; | ||
| 285 | xasprintf(&ignored, "%s %s;", ignored, path->name); | ||
| 286 | } | ||
| 287 | } | ||
| 288 | continue; | ||
| 289 | /* Skip pseudo fs's if we haven't asked for all fs's */ | ||
| 290 | } | ||
| 291 | if (me->me_dummy && !show_all_fs) { | ||
| 292 | continue; | 209 | continue; |
| 293 | /* Skip excluded fstypes */ | ||
| 294 | } | 210 | } |
| 295 | if (fs_exclude_list && np_find_regmatch(fs_exclude_list, me->me_type)) { | 211 | |
| 212 | if (config.device_path_exclude_list && (np_find_name(config.device_path_exclude_list, mount_entry->me_devname) || | ||
| 213 | np_find_name(config.device_path_exclude_list, mount_entry->me_mountdir))) { | ||
| 214 | // Skip excluded device or mount paths | ||
| 215 | path = mp_int_fs_list_del(&config.path_select_list, path); | ||
| 296 | continue; | 216 | continue; |
| 297 | /* Skip excluded fs's */ | ||
| 298 | } | 217 | } |
| 299 | if (dp_exclude_list && (np_find_name(dp_exclude_list, me->me_devname) || np_find_name(dp_exclude_list, me->me_mountdir))) { | 218 | |
| 219 | if (config.fs_include_list && !np_find_regmatch(config.fs_include_list, mount_entry->me_type)) { | ||
| 220 | // Skip not included fstypes | ||
| 221 | path = mp_int_fs_list_del(&config.path_select_list, path); | ||
| 300 | continue; | 222 | continue; |
| 301 | /* Skip not included fstypes */ | ||
| 302 | } | 223 | } |
| 303 | if (fs_include_list && !np_find_regmatch(fs_include_list, me->me_type)) { | 224 | |
| 225 | /* Skip remote filesystems if we're not interested in them */ | ||
| 226 | if (mount_entry->me_remote && config.show_local_fs) { | ||
| 227 | if (config.stat_remote_fs) { | ||
| 228 | // TODO Stat here | ||
| 229 | if (!stat_path(path, config.ignore_missing) && config.ignore_missing) { | ||
| 230 | } | ||
| 231 | } | ||
| 304 | continue; | 232 | continue; |
| 305 | } | 233 | } |
| 306 | } | ||
| 307 | 234 | ||
| 308 | if (!stat_path(path)) { | 235 | // TODO why stat here? remove unstatable fs? |
| 309 | if (ignore_missing == true) { | 236 | if (!stat_path(path, config.ignore_missing)) { |
| 310 | result = STATE_OK; | 237 | // if (config.ignore_missing) { |
| 311 | xasprintf(&ignored, "%s %s;", ignored, path->name); | 238 | // xasprintf(&ignored, "%s %s;", ignored, path->name); |
| 239 | // } | ||
| 240 | // not accessible, remove from list | ||
| 241 | path = mp_int_fs_list_del(&config.path_select_list, path); | ||
| 242 | continue; | ||
| 312 | } | 243 | } |
| 313 | continue; | ||
| 314 | } | 244 | } |
| 315 | get_fs_usage(me->me_mountdir, me->me_devname, &fsp); | ||
| 316 | 245 | ||
| 317 | if (fsp.fsu_blocks && strcmp("none", me->me_mountdir)) { | 246 | path = mp_int_fs_list_get_next(path); |
| 318 | get_stats(path, &fsp); | 247 | } |
| 319 | |||
| 320 | if (verbose >= 3) { | ||
| 321 | printf("For %s, used_pct=%f free_pct=%f used_units=%lu free_units=%lu total_units=%lu used_inodes_pct=%f " | ||
| 322 | "free_inodes_pct=%f fsp.fsu_blocksize=%lu mult=%lu\n", | ||
| 323 | me->me_mountdir, path->dused_pct, path->dfree_pct, path->dused_units, path->dfree_units, path->dtotal_units, | ||
| 324 | path->dused_inodes_percent, path->dfree_inodes_percent, fsp.fsu_blocksize, mult); | ||
| 325 | } | ||
| 326 | |||
| 327 | /* Threshold comparisons */ | ||
| 328 | |||
| 329 | temp_result = get_status(path->dfree_units, path->freespace_units); | ||
| 330 | if (verbose >= 3) | ||
| 331 | printf("Freespace_units result=%d\n", temp_result); | ||
| 332 | disk_result = max_state(disk_result, temp_result); | ||
| 333 | |||
| 334 | temp_result = get_status(path->dfree_pct, path->freespace_percent); | ||
| 335 | if (verbose >= 3) | ||
| 336 | printf("Freespace%% result=%d\n", temp_result); | ||
| 337 | disk_result = max_state(disk_result, temp_result); | ||
| 338 | |||
| 339 | temp_result = get_status(path->dused_units, path->usedspace_units); | ||
| 340 | if (verbose >= 3) | ||
| 341 | printf("Usedspace_units result=%d\n", temp_result); | ||
| 342 | disk_result = max_state(disk_result, temp_result); | ||
| 343 | |||
| 344 | temp_result = get_status(path->dused_pct, path->usedspace_percent); | ||
| 345 | if (verbose >= 3) | ||
| 346 | printf("Usedspace_percent result=%d\n", temp_result); | ||
| 347 | disk_result = max_state(disk_result, temp_result); | ||
| 348 | |||
| 349 | temp_result = get_status(path->dused_inodes_percent, path->usedinodes_percent); | ||
| 350 | if (verbose >= 3) | ||
| 351 | printf("Usedinodes_percent result=%d\n", temp_result); | ||
| 352 | disk_result = max_state(disk_result, temp_result); | ||
| 353 | |||
| 354 | temp_result = get_status(path->dfree_inodes_percent, path->freeinodes_percent); | ||
| 355 | if (verbose >= 3) | ||
| 356 | printf("Freeinodes_percent result=%d\n", temp_result); | ||
| 357 | disk_result = max_state(disk_result, temp_result); | ||
| 358 | |||
| 359 | result = max_state(result, disk_result); | ||
| 360 | 248 | ||
| 361 | /* What a mess of units. The output shows free space, the perf data shows used space. Yikes! | 249 | // now get the actual measurements |
| 362 | Hack here. Trying to get warn/crit levels from freespace_(units|percent) for perf | 250 | for (parameter_list_elem *filesystem = config.path_select_list.first; filesystem;) { |
| 363 | data. Assumption that start=0. Roll on new syntax... | 251 | // Get actual metrics here |
| 364 | */ | 252 | struct mount_entry *mount_entry = filesystem->best_match; |
| 253 | struct fs_usage fsp = {0}; | ||
| 254 | get_fs_usage(mount_entry->me_mountdir, mount_entry->me_devname, &fsp); | ||
| 365 | 255 | ||
| 366 | /* *_high_tide must be reinitialized at each run */ | 256 | if (fsp.fsu_blocks != 0 && strcmp("none", mount_entry->me_mountdir) != 0) { |
| 367 | uint64_t warning_high_tide = UINT64_MAX; | 257 | *filesystem = get_path_stats(*filesystem, fsp, config.freespace_ignore_reserved); |
| 368 | 258 | ||
| 369 | if (path->freespace_units->warning != NULL) { | 259 | if (verbose >= 3) { |
| 370 | warning_high_tide = (path->dtotal_units - path->freespace_units->warning->end) * mult; | 260 | printf("For %s, used_units=%lu free_units=%lu total_units=%lu " |
| 371 | } | 261 | "fsp.fsu_blocksize=%lu\n", |
| 372 | if (path->freespace_percent->warning != NULL) { | 262 | mount_entry->me_mountdir, filesystem->used_bytes, filesystem->free_bytes, filesystem->total_bytes, |
| 373 | warning_high_tide = | 263 | fsp.fsu_blocksize); |
| 374 | min(warning_high_tide, (uint64_t)((1.0 - path->freespace_percent->warning->end / 100) * (path->dtotal_units * mult))); | ||
| 375 | } | ||
| 376 | |||
| 377 | uint64_t critical_high_tide = UINT64_MAX; | ||
| 378 | |||
| 379 | if (path->freespace_units->critical != NULL) { | ||
| 380 | critical_high_tide = (path->dtotal_units - path->freespace_units->critical->end) * mult; | ||
| 381 | } | ||
| 382 | if (path->freespace_percent->critical != NULL) { | ||
| 383 | critical_high_tide = | ||
| 384 | min(critical_high_tide, (uint64_t)((1.0 - path->freespace_percent->critical->end / 100) * (path->dtotal_units * mult))); | ||
| 385 | } | 264 | } |
| 265 | } else { | ||
| 266 | // failed to retrieve file system data or not mounted? | ||
| 267 | filesystem = mp_int_fs_list_del(&config.path_select_list, filesystem); | ||
| 268 | continue; | ||
| 269 | } | ||
| 270 | filesystem = mp_int_fs_list_get_next(filesystem); | ||
| 271 | } | ||
| 386 | 272 | ||
| 387 | /* Nb: *_high_tide are unset when == UINT64_MAX */ | 273 | if (verbose > 2) { |
| 388 | xasprintf(&perf, "%s %s", perf, | 274 | for (parameter_list_elem *filesystem = config.path_select_list.first; filesystem; |
| 389 | perfdata_uint64((!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir, | 275 | filesystem = mp_int_fs_list_get_next(filesystem)) { |
| 390 | path->dused_units * mult, "B", (warning_high_tide == UINT64_MAX ? false : true), warning_high_tide, | 276 | assert(filesystem->best_match != NULL); |
| 391 | (critical_high_tide == UINT64_MAX ? false : true), critical_high_tide, true, 0, true, | 277 | if (filesystem->best_match == NULL) { |
| 392 | path->dtotal_units * mult)); | 278 | printf("Filesystem path %s has no mount_entry!\n", filesystem->name); |
| 393 | 279 | } else { | |
| 394 | if (display_inodes_perfdata) { | 280 | // printf("Filesystem path %s has a mount_entry!\n", filesystem->name); |
| 395 | /* *_high_tide must be reinitialized at each run */ | ||
| 396 | warning_high_tide = UINT64_MAX; | ||
| 397 | critical_high_tide = UINT64_MAX; | ||
| 398 | |||
| 399 | if (path->freeinodes_percent->warning != NULL) { | ||
| 400 | warning_high_tide = (uint64_t)fabs( | ||
| 401 | min((double)warning_high_tide, (double)(1.0 - path->freeinodes_percent->warning->end / 100) * path->inodes_total)); | ||
| 402 | } | ||
| 403 | if (path->freeinodes_percent->critical != NULL) { | ||
| 404 | critical_high_tide = (uint64_t)fabs(min( | ||
| 405 | (double)critical_high_tide, (double)(1.0 - path->freeinodes_percent->critical->end / 100) * path->inodes_total)); | ||
| 406 | } | ||
| 407 | |||
| 408 | xasprintf(&perf_ilabel, "%s (inodes)", | ||
| 409 | (!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir); | ||
| 410 | /* Nb: *_high_tide are unset when == UINT64_MAX */ | ||
| 411 | xasprintf(&perf, "%s %s", perf, | ||
| 412 | perfdata_uint64(perf_ilabel, path->inodes_used, "", (warning_high_tide != UINT64_MAX ? true : false), | ||
| 413 | warning_high_tide, (critical_high_tide != UINT64_MAX ? true : false), critical_high_tide, true, 0, | ||
| 414 | true, path->inodes_total)); | ||
| 415 | } | 281 | } |
| 282 | } | ||
| 283 | } | ||
| 416 | 284 | ||
| 417 | if (disk_result == STATE_OK && erronly && !verbose) | 285 | measurement_unit_list *measurements = NULL; |
| 418 | continue; | 286 | measurement_unit_list *current = NULL; |
| 419 | 287 | // create measuring units, because of groups | |
| 420 | if (disk_result && verbose >= 1) { | 288 | for (parameter_list_elem *filesystem = config.path_select_list.first; filesystem; filesystem = mp_int_fs_list_get_next(filesystem)) { |
| 421 | xasprintf(&flag_header, " %s [", state_text(disk_result)); | 289 | assert(filesystem->best_match != NULL); |
| 290 | |||
| 291 | if (filesystem->group == NULL) { | ||
| 292 | // create a measurement unit for the fs | ||
| 293 | measurement_unit unit = create_measurement_unit_from_filesystem(*filesystem, config.display_mntp); | ||
| 294 | if (measurements == NULL) { | ||
| 295 | measurements = current = add_measurement_list(NULL, unit); | ||
| 422 | } else { | 296 | } else { |
| 423 | xasprintf(&flag_header, ""); | 297 | current = add_measurement_list(measurements, unit); |
| 424 | } | 298 | } |
| 425 | xasprintf(&output, "%s%s %s %llu%s (%.1f%%", output, flag_header, | 299 | } else { |
| 426 | (!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir, path->dfree_units, units, | 300 | // Grouped elements are consecutive |
| 427 | path->dfree_pct); | 301 | if (measurements == NULL) { |
| 428 | if (path->dused_inodes_percent < 0) { | 302 | // first entry |
| 429 | xasprintf(&output, "%s inode=-)%s;", output, (disk_result ? "]" : "")); | 303 | measurement_unit unit = create_measurement_unit_from_filesystem(*filesystem, config.display_mntp); |
| 304 | unit.name = strdup(filesystem->group); | ||
| 305 | measurements = current = add_measurement_list(NULL, unit); | ||
| 430 | } else { | 306 | } else { |
| 431 | xasprintf(&output, "%s inode=%.0f%%)%s;", output, path->dfree_inodes_percent, ((disk_result && verbose >= 1) ? "]" : "")); | 307 | // if this is the first element of a group, the name of the previous entry is different |
| 308 | if (strcmp(filesystem->group, current->unit.name) != 0) { | ||
| 309 | // so, this must be the first element of a group | ||
| 310 | measurement_unit unit = create_measurement_unit_from_filesystem(*filesystem, config.display_mntp); | ||
| 311 | unit.name = filesystem->group; | ||
| 312 | current = add_measurement_list(measurements, unit); | ||
| 313 | |||
| 314 | } else { | ||
| 315 | // NOT the first entry of a group, add info to the other one | ||
| 316 | current->unit = add_filesystem_to_measurement_unit(current->unit, *filesystem); | ||
| 317 | } | ||
| 432 | } | 318 | } |
| 433 | free(flag_header); | ||
| 434 | } | 319 | } |
| 435 | } | 320 | } |
| 436 | 321 | ||
| 437 | if (verbose >= 2) | 322 | /* Process for every path in list */ |
| 438 | xasprintf(&output, "%s%s", output, details); | 323 | if (measurements != NULL) { |
| 324 | for (measurement_unit_list *unit = measurements; unit; unit = unit->next) { | ||
| 325 | mp_subcheck unit_sc = evaluate_filesystem(unit->unit, config.display_inodes_perfdata, config.display_unit); | ||
| 326 | mp_add_subcheck_to_check(&overall, unit_sc); | ||
| 327 | } | ||
| 328 | } else { | ||
| 329 | // Apparently no machting fs found | ||
| 330 | mp_subcheck none_sc = mp_subcheck_init(); | ||
| 331 | xasprintf(&none_sc.output, "No filesystems were found for the provided parameters"); | ||
| 439 | 332 | ||
| 440 | if (strcmp(output, "") == 0 && !erronly) { | 333 | if (config.ignore_missing) { |
| 441 | preamble = ""; | 334 | none_sc = mp_set_subcheck_state(none_sc, STATE_OK); |
| 442 | xasprintf(&output, " - No disks were found for provided parameters"); | 335 | } else { |
| 336 | none_sc = mp_set_subcheck_state(none_sc, STATE_UNKNOWN); | ||
| 337 | } | ||
| 338 | mp_add_subcheck_to_check(&overall, none_sc); | ||
| 443 | } | 339 | } |
| 444 | 340 | ||
| 445 | printf("DISK %s%s%s%s%s|%s\n", state_text(result), ((erronly && result == STATE_OK)) ? "" : preamble, output, | 341 | mp_exit(overall); |
| 446 | (strcmp(ignored, "") == 0) ? "" : ignored_preamble, ignored, perf); | ||
| 447 | return result; | ||
| 448 | } | 342 | } |
| 449 | 343 | ||
| 450 | double calculate_percent(uintmax_t value, uintmax_t total) { | 344 | double calculate_percent(uintmax_t value, uintmax_t total) { |
| 451 | double pct = -1; | 345 | double pct = -1; |
| 452 | if (value <= DBL_MAX && total != 0) { | 346 | if (value <= DBL_MAX && total != 0) { |
| 453 | pct = (double)value / total * 100.0; | 347 | pct = (double)value / (double)total * 100.0; |
| 454 | } | 348 | } |
| 349 | |||
| 455 | return pct; | 350 | return pct; |
| 456 | } | 351 | } |
| 457 | 352 | ||
| 458 | /* process command-line arguments */ | 353 | /* process command-line arguments */ |
| 459 | int process_arguments(int argc, char **argv) { | 354 | check_disk_config_wrapper process_arguments(int argc, char **argv) { |
| 460 | int c; | 355 | |
| 461 | int err; | 356 | check_disk_config_wrapper result = { |
| 462 | struct parameter_list *se; | 357 | .errorcode = OK, |
| 463 | struct parameter_list *temp_list = NULL; | 358 | .config = check_disk_config_init(), |
| 464 | struct parameter_list *previous = NULL; | 359 | }; |
| 465 | struct mount_entry *me; | 360 | |
| 466 | regex_t re; | 361 | if (argc < 2) { |
| 467 | int cflags = REG_NOSUB | REG_EXTENDED; | 362 | result.errorcode = ERROR; |
| 468 | int default_cflags = cflags; | 363 | return result; |
| 469 | char errbuf[MAX_INPUT_BUFFER]; | 364 | } |
| 470 | int fnd = 0; | 365 | |
| 366 | enum { | ||
| 367 | output_format_index = CHAR_MAX + 1, | ||
| 368 | display_unit_index, | ||
| 369 | }; | ||
| 471 | 370 | ||
| 472 | int option = 0; | ||
| 473 | static struct option longopts[] = {{"timeout", required_argument, 0, 't'}, | 371 | static struct option longopts[] = {{"timeout", required_argument, 0, 't'}, |
| 474 | {"warning", required_argument, 0, 'w'}, | 372 | {"warning", required_argument, 0, 'w'}, |
| 475 | {"critical", required_argument, 0, 'c'}, | 373 | {"critical", required_argument, 0, 'c'}, |
| 476 | {"iwarning", required_argument, 0, 'W'}, | 374 | {"iwarning", required_argument, 0, 'W'}, |
| 477 | /* Dang, -C is taken. We might want to reshuffle this. */ | ||
| 478 | {"icritical", required_argument, 0, 'K'}, | 375 | {"icritical", required_argument, 0, 'K'}, |
| 479 | {"kilobytes", no_argument, 0, 'k'}, | 376 | {"kilobytes", no_argument, 0, 'k'}, |
| 480 | {"megabytes", no_argument, 0, 'm'}, | 377 | {"megabytes", no_argument, 0, 'm'}, |
| @@ -507,24 +404,42 @@ int process_arguments(int argc, char **argv) { | |||
| 507 | {"clear", no_argument, 0, 'C'}, | 404 | {"clear", no_argument, 0, 'C'}, |
| 508 | {"version", no_argument, 0, 'V'}, | 405 | {"version", no_argument, 0, 'V'}, |
| 509 | {"help", no_argument, 0, 'h'}, | 406 | {"help", no_argument, 0, 'h'}, |
| 407 | {"output-format", required_argument, 0, output_format_index}, | ||
| 408 | {"display-unit", required_argument, 0, display_unit_index}, | ||
| 510 | {0, 0, 0, 0}}; | 409 | {0, 0, 0, 0}}; |
| 511 | 410 | ||
| 512 | if (argc < 2) | 411 | for (int index = 1; index < argc; index++) { |
| 513 | return ERROR; | 412 | if (strcmp("-to", argv[index]) == 0) { |
| 413 | strcpy(argv[index], "-t"); | ||
| 414 | } | ||
| 415 | } | ||
| 416 | |||
| 417 | int cflags = REG_NOSUB | REG_EXTENDED; | ||
| 418 | int default_cflags = cflags; | ||
| 419 | char *warn_freespace_units = NULL; | ||
| 420 | char *crit_freespace_units = NULL; | ||
| 421 | char *warn_freespace_percent = NULL; | ||
| 422 | char *crit_freespace_percent = NULL; | ||
| 423 | char *warn_freeinodes_percent = NULL; | ||
| 424 | char *crit_freeinodes_percent = NULL; | ||
| 425 | |||
| 426 | bool path_selected = false; | ||
| 427 | char *group = NULL; | ||
| 428 | byte_unit unit = MebiBytes_factor; | ||
| 514 | 429 | ||
| 515 | np_add_regex(&fs_exclude_list, "iso9660", REG_EXTENDED); | 430 | result.config.mount_list = read_file_system_list(false); |
| 516 | 431 | ||
| 517 | for (c = 1; c < argc; c++) | 432 | np_add_regex(&result.config.fs_exclude_list, "iso9660", REG_EXTENDED); |
| 518 | if (strcmp("-to", argv[c]) == 0) | ||
| 519 | strcpy(argv[c], "-t"); | ||
| 520 | 433 | ||
| 521 | while (1) { | 434 | while (true) { |
| 522 | c = getopt_long(argc, argv, "+?VqhvefCt:c:w:K:W:u:p:x:X:N:mklLPg:R:r:i:I:MEAn", longopts, &option); | 435 | int option = 0; |
| 436 | int option_index = getopt_long(argc, argv, "+?VqhvefCt:c:w:K:W:u:p:x:X:N:mklLPg:R:r:i:I:MEAn", longopts, &option); | ||
| 523 | 437 | ||
| 524 | if (c == -1 || c == EOF) | 438 | if (option_index == -1 || option_index == EOF) { |
| 525 | break; | 439 | break; |
| 440 | } | ||
| 526 | 441 | ||
| 527 | switch (c) { | 442 | switch (option_index) { |
| 528 | case 't': /* timeout period */ | 443 | case 't': /* timeout period */ |
| 529 | if (is_integer(optarg)) { | 444 | if (is_integer(optarg)) { |
| 530 | timeout_interval = atoi(optarg); | 445 | timeout_interval = atoi(optarg); |
| @@ -555,10 +470,10 @@ int process_arguments(int argc, char **argv) { | |||
| 555 | break; | 470 | break; |
| 556 | 471 | ||
| 557 | /* Awful mistake where the range values do not make sense. Normally, | 472 | /* Awful mistake where the range values do not make sense. Normally, |
| 558 | you alert if the value is within the range, but since we are using | 473 | * you alert if the value is within the range, but since we are using |
| 559 | freespace, we have to alert if outside the range. Thus we artificially | 474 | * freespace, we have to alert if outside the range. Thus we artificially |
| 560 | force @ at the beginning of the range, so that it is backwards compatible | 475 | * force @ at the beginning of the range, so that it is backwards compatible |
| 561 | */ | 476 | */ |
| 562 | case 'c': /* critical threshold */ | 477 | case 'c': /* critical threshold */ |
| 563 | if (!is_percentage_expression(optarg) && !is_numeric(optarg)) { | 478 | if (!is_percentage_expression(optarg) && !is_numeric(optarg)) { |
| 564 | die(STATE_UNKNOWN, "Argument for --critical invalid or missing: %s\n", optarg); | 479 | die(STATE_UNKNOWN, "Argument for --critical invalid or missing: %s\n", optarg); |
| @@ -594,181 +509,184 @@ int process_arguments(int argc, char **argv) { | |||
| 594 | } | 509 | } |
| 595 | break; | 510 | break; |
| 596 | case 'u': | 511 | case 'u': |
| 597 | if (units) | ||
| 598 | free(units); | ||
| 599 | if (!strcasecmp(optarg, "bytes")) { | 512 | if (!strcasecmp(optarg, "bytes")) { |
| 600 | mult = (uintmax_t)1; | 513 | unit = Bytes_Factor; |
| 601 | units = strdup("B"); | ||
| 602 | } else if (!strcmp(optarg, "KiB")) { | 514 | } else if (!strcmp(optarg, "KiB")) { |
| 603 | mult = (uintmax_t)1024; | 515 | unit = KibiBytes_factor; |
| 604 | units = strdup("KiB"); | ||
| 605 | } else if (!strcmp(optarg, "kB")) { | 516 | } else if (!strcmp(optarg, "kB")) { |
| 606 | mult = (uintmax_t)1000; | 517 | unit = KiloBytes_factor; |
| 607 | units = strdup("kB"); | ||
| 608 | } else if (!strcmp(optarg, "MiB")) { | 518 | } else if (!strcmp(optarg, "MiB")) { |
| 609 | mult = (uintmax_t)1024 * 1024; | 519 | unit = MebiBytes_factor; |
| 610 | units = strdup("MiB"); | ||
| 611 | } else if (!strcmp(optarg, "MB")) { | 520 | } else if (!strcmp(optarg, "MB")) { |
| 612 | mult = (uintmax_t)1000 * 1000; | 521 | unit = MegaBytes_factor; |
| 613 | units = strdup("MB"); | ||
| 614 | } else if (!strcmp(optarg, "GiB")) { | 522 | } else if (!strcmp(optarg, "GiB")) { |
| 615 | mult = (uintmax_t)1024 * 1024 * 1024; | 523 | unit = GibiBytes_factor; |
| 616 | units = strdup("GiB"); | ||
| 617 | } else if (!strcmp(optarg, "GB")) { | 524 | } else if (!strcmp(optarg, "GB")) { |
| 618 | mult = (uintmax_t)1000 * 1000 * 1000; | 525 | unit = GigaBytes_factor; |
| 619 | units = strdup("GB"); | ||
| 620 | } else if (!strcmp(optarg, "TiB")) { | 526 | } else if (!strcmp(optarg, "TiB")) { |
| 621 | mult = (uintmax_t)1024 * 1024 * 1024 * 1024; | 527 | unit = TebiBytes_factor; |
| 622 | units = strdup("TiB"); | ||
| 623 | } else if (!strcmp(optarg, "TB")) { | 528 | } else if (!strcmp(optarg, "TB")) { |
| 624 | mult = (uintmax_t)1000 * 1000 * 1000 * 1000; | 529 | unit = TeraBytes_factor; |
| 625 | units = strdup("TB"); | ||
| 626 | } else if (!strcmp(optarg, "PiB")) { | 530 | } else if (!strcmp(optarg, "PiB")) { |
| 627 | mult = (uintmax_t)1024 * 1024 * 1024 * 1024 * 1024; | 531 | unit = PebiBytes_factor; |
| 628 | units = strdup("PiB"); | ||
| 629 | } else if (!strcmp(optarg, "PB")) { | 532 | } else if (!strcmp(optarg, "PB")) { |
| 630 | mult = (uintmax_t)1000 * 1000 * 1000 * 1000 * 1000; | 533 | unit = PetaBytes_factor; |
| 631 | units = strdup("PB"); | ||
| 632 | } else { | 534 | } else { |
| 633 | die(STATE_UNKNOWN, _("unit type %s not known\n"), optarg); | 535 | die(STATE_UNKNOWN, _("unit type %s not known\n"), optarg); |
| 634 | } | 536 | } |
| 635 | if (units == NULL) | ||
| 636 | die(STATE_UNKNOWN, _("failed allocating storage for '%s'\n"), "units"); | ||
| 637 | break; | 537 | break; |
| 638 | case 'k': /* display mountpoint */ | 538 | case 'k': |
| 639 | mult = 1024; | 539 | unit = KibiBytes_factor; |
| 640 | if (units) | 540 | break; |
| 641 | free(units); | 541 | case 'm': |
| 642 | units = strdup("kiB"); | 542 | unit = MebiBytes_factor; |
| 643 | break; | 543 | break; |
| 644 | case 'm': /* display mountpoint */ | 544 | case display_unit_index: |
| 645 | mult = 1024 * 1024; | 545 | if (!strcasecmp(optarg, "bytes")) { |
| 646 | if (units) | 546 | result.config.display_unit = Bytes; |
| 647 | free(units); | 547 | } else if (!strcmp(optarg, "KiB")) { |
| 648 | units = strdup("MiB"); | 548 | result.config.display_unit = KibiBytes; |
| 549 | } else if (!strcmp(optarg, "kB")) { | ||
| 550 | result.config.display_unit = KiloBytes; | ||
| 551 | } else if (!strcmp(optarg, "MiB")) { | ||
| 552 | result.config.display_unit = MebiBytes; | ||
| 553 | } else if (!strcmp(optarg, "MB")) { | ||
| 554 | result.config.display_unit = MegaBytes; | ||
| 555 | } else if (!strcmp(optarg, "GiB")) { | ||
| 556 | result.config.display_unit = GibiBytes; | ||
| 557 | } else if (!strcmp(optarg, "GB")) { | ||
| 558 | result.config.display_unit = GigaBytes; | ||
| 559 | } else if (!strcmp(optarg, "TiB")) { | ||
| 560 | result.config.display_unit = TebiBytes; | ||
| 561 | } else if (!strcmp(optarg, "TB")) { | ||
| 562 | result.config.display_unit = TeraBytes; | ||
| 563 | } else if (!strcmp(optarg, "PiB")) { | ||
| 564 | result.config.display_unit = PebiBytes; | ||
| 565 | } else if (!strcmp(optarg, "PB")) { | ||
| 566 | result.config.display_unit = PetaBytes; | ||
| 567 | } else { | ||
| 568 | die(STATE_UNKNOWN, _("unit type %s not known\n"), optarg); | ||
| 569 | } | ||
| 649 | break; | 570 | break; |
| 650 | case 'L': | 571 | case 'L': |
| 651 | stat_remote_fs = 1; | 572 | result.config.stat_remote_fs = true; |
| 652 | /* fallthrough */ | 573 | /* fallthrough */ |
| 653 | case 'l': | 574 | case 'l': |
| 654 | show_local_fs = 1; | 575 | result.config.show_local_fs = true; |
| 655 | break; | 576 | break; |
| 656 | case 'P': | 577 | case 'P': |
| 657 | display_inodes_perfdata = 1; | 578 | result.config.display_inodes_perfdata = true; |
| 658 | break; | 579 | break; |
| 659 | case 'p': /* select path */ | 580 | case 'p': /* select path */ { |
| 660 | if (!(warn_freespace_units || crit_freespace_units || warn_freespace_percent || crit_freespace_percent || | 581 | if (!(warn_freespace_units || crit_freespace_units || warn_freespace_percent || crit_freespace_percent || |
| 661 | warn_usedspace_units || crit_usedspace_units || warn_usedspace_percent || crit_usedspace_percent || | 582 | warn_freeinodes_percent || crit_freeinodes_percent)) { |
| 662 | warn_usedinodes_percent || crit_usedinodes_percent || warn_freeinodes_percent || crit_freeinodes_percent)) { | ||
| 663 | die(STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set a threshold value before using -p\n")); | 583 | die(STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set a threshold value before using -p\n")); |
| 664 | } | 584 | } |
| 665 | 585 | ||
| 666 | /* add parameter if not found. overwrite thresholds if path has already been added */ | 586 | /* add parameter if not found. overwrite thresholds if path has already been added */ |
| 667 | if (!(se = np_find_parameter(path_select_list, optarg))) { | 587 | parameter_list_elem *search_entry; |
| 668 | se = np_add_parameter(&path_select_list, optarg); | 588 | if (!(search_entry = mp_int_fs_list_find(result.config.path_select_list, optarg))) { |
| 669 | 589 | search_entry = mp_int_fs_list_append(&result.config.path_select_list, optarg); | |
| 670 | if (stat(optarg, &stat_buf[0]) && ignore_missing == true) { | 590 | |
| 671 | path_ignored = true; | 591 | // struct stat stat_buf = {}; |
| 672 | break; | 592 | // if (stat(optarg, &stat_buf) && result.config.ignore_missing) { |
| 673 | } | 593 | // result.config.path_ignored = true; |
| 594 | // break; | ||
| 595 | // } | ||
| 674 | } | 596 | } |
| 675 | se->group = group; | 597 | search_entry->group = group; |
| 676 | set_all_thresholds(se); | 598 | set_all_thresholds(search_entry, warn_freespace_units, crit_freespace_units, warn_freespace_percent, crit_freespace_percent, |
| 599 | |||
| 600 | warn_freeinodes_percent, crit_freeinodes_percent); | ||
| 677 | 601 | ||
| 678 | /* With autofs, it is required to stat() the path before re-populating the mount_list */ | 602 | /* With autofs, it is required to stat() the path before re-populating the mount_list */ |
| 679 | if (!stat_path(se)) { | 603 | // if (!stat_path(se, result.config.ignore_missing)) { |
| 680 | break; | 604 | // break; |
| 681 | } | 605 | // } |
| 682 | /* NB: We can't free the old mount_list "just like that": both list pointers and struct | 606 | mp_int_fs_list_set_best_match(result.config.path_select_list, result.config.mount_list, result.config.exact_match); |
| 683 | * pointers are copied around. One of the reason it wasn't done yet is that other parts | ||
| 684 | * of check_disk need the same kind of cleanup so it'd better be done as a whole */ | ||
| 685 | mount_list = read_file_system_list(0); | ||
| 686 | np_set_best_match(se, mount_list, exact_match); | ||
| 687 | 607 | ||
| 688 | path_selected = true; | 608 | path_selected = true; |
| 689 | break; | 609 | } break; |
| 690 | case 'x': /* exclude path or partition */ | 610 | case 'x': /* exclude path or partition */ |
| 691 | np_add_name(&dp_exclude_list, optarg); | 611 | np_add_name(&result.config.device_path_exclude_list, optarg); |
| 692 | break; | 612 | break; |
| 693 | case 'X': /* exclude file system type */ | 613 | case 'X': /* exclude file system type */ { |
| 694 | err = np_add_regex(&fs_exclude_list, optarg, REG_EXTENDED); | 614 | int err = np_add_regex(&result.config.fs_exclude_list, optarg, REG_EXTENDED); |
| 695 | if (err != 0) { | 615 | if (err != 0) { |
| 696 | regerror(err, &fs_exclude_list->regex, errbuf, MAX_INPUT_BUFFER); | 616 | char errbuf[MAX_INPUT_BUFFER]; |
| 617 | regerror(err, &result.config.fs_exclude_list->regex, errbuf, MAX_INPUT_BUFFER); | ||
| 697 | die(STATE_UNKNOWN, "DISK %s: %s - %s\n", _("UNKNOWN"), _("Could not compile regular expression"), errbuf); | 618 | die(STATE_UNKNOWN, "DISK %s: %s - %s\n", _("UNKNOWN"), _("Could not compile regular expression"), errbuf); |
| 698 | } | 619 | } |
| 699 | break; | 620 | break; |
| 700 | case 'N': /* include file system type */ | 621 | case 'N': /* include file system type */ |
| 701 | err = np_add_regex(&fs_include_list, optarg, REG_EXTENDED); | 622 | err = np_add_regex(&result.config.fs_include_list, optarg, REG_EXTENDED); |
| 702 | if (err != 0) { | 623 | if (err != 0) { |
| 703 | regerror(err, &fs_exclude_list->regex, errbuf, MAX_INPUT_BUFFER); | 624 | char errbuf[MAX_INPUT_BUFFER]; |
| 625 | regerror(err, &result.config.fs_exclude_list->regex, errbuf, MAX_INPUT_BUFFER); | ||
| 704 | die(STATE_UNKNOWN, "DISK %s: %s - %s\n", _("UNKNOWN"), _("Could not compile regular expression"), errbuf); | 626 | die(STATE_UNKNOWN, "DISK %s: %s - %s\n", _("UNKNOWN"), _("Could not compile regular expression"), errbuf); |
| 705 | } | 627 | } |
| 706 | break; | 628 | } break; |
| 707 | case 'v': /* verbose */ | 629 | case 'v': /* verbose */ |
| 708 | verbose++; | 630 | verbose++; |
| 709 | break; | 631 | break; |
| 710 | case 'q': /* TODO: this function should eventually go away (removed 2007-09-20) */ | 632 | case 'q': /* TODO: this function should eventually go away (removed 2007-09-20) */ |
| 711 | /* verbose--; **replaced by line below**. -q was only a broken way of implementing -e */ | 633 | /* verbose--; **replaced by line below**. -q was only a broken way of implementing -e */ |
| 712 | erronly = true; | 634 | result.config.erronly = true; |
| 713 | break; | 635 | break; |
| 714 | case 'e': | 636 | case 'e': |
| 715 | erronly = true; | 637 | result.config.erronly = true; |
| 716 | break; | 638 | break; |
| 717 | case 'E': | 639 | case 'E': |
| 718 | if (path_selected) | 640 | if (path_selected) { |
| 719 | die(STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set -E before selecting paths\n")); | 641 | die(STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set -E before selecting paths\n")); |
| 720 | exact_match = true; | 642 | } |
| 643 | result.config.exact_match = true; | ||
| 721 | break; | 644 | break; |
| 722 | case 'f': | 645 | case 'f': |
| 723 | freespace_ignore_reserved = true; | 646 | result.config.freespace_ignore_reserved = true; |
| 724 | break; | 647 | break; |
| 725 | case 'g': | 648 | case 'g': |
| 726 | if (path_selected) | 649 | if (path_selected) { |
| 727 | die(STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set group value before selecting paths\n")); | 650 | die(STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set group value before selecting paths\n")); |
| 651 | } | ||
| 728 | group = optarg; | 652 | group = optarg; |
| 729 | break; | 653 | break; |
| 730 | case 'I': | 654 | case 'I': |
| 731 | cflags |= REG_ICASE; | 655 | cflags |= REG_ICASE; |
| 732 | // Intentional fallthrough | 656 | // Intentional fallthrough |
| 733 | case 'i': | 657 | case 'i': { |
| 734 | if (!path_selected) | 658 | if (!path_selected) { |
| 735 | die(STATE_UNKNOWN, "DISK %s: %s\n", _("UNKNOWN"), | 659 | die(STATE_UNKNOWN, "DISK %s: %s\n", _("UNKNOWN"), |
| 736 | _("Paths need to be selected before using -i/-I. Use -A to select all paths explicitly")); | 660 | _("Paths need to be selected before using -i/-I. Use -A to select all paths explicitly")); |
| 737 | err = regcomp(&re, optarg, cflags); | 661 | } |
| 662 | regex_t regex; | ||
| 663 | int err = regcomp(®ex, optarg, cflags); | ||
| 738 | if (err != 0) { | 664 | if (err != 0) { |
| 739 | regerror(err, &re, errbuf, MAX_INPUT_BUFFER); | 665 | char errbuf[MAX_INPUT_BUFFER]; |
| 666 | regerror(err, ®ex, errbuf, MAX_INPUT_BUFFER); | ||
| 740 | die(STATE_UNKNOWN, "DISK %s: %s - %s\n", _("UNKNOWN"), _("Could not compile regular expression"), errbuf); | 667 | die(STATE_UNKNOWN, "DISK %s: %s - %s\n", _("UNKNOWN"), _("Could not compile regular expression"), errbuf); |
| 741 | } | 668 | } |
| 742 | 669 | ||
| 743 | temp_list = path_select_list; | 670 | for (parameter_list_elem *elem = result.config.path_select_list.first; elem;) { |
| 744 | 671 | if (elem->best_match) { | |
| 745 | previous = NULL; | 672 | if (np_regex_match_mount_entry(elem->best_match, ®ex)) { |
| 746 | while (temp_list) { | ||
| 747 | if (temp_list->best_match) { | ||
| 748 | if (np_regex_match_mount_entry(temp_list->best_match, &re)) { | ||
| 749 | 673 | ||
| 750 | if (verbose >= 3) | 674 | if (verbose >= 3) { |
| 751 | printf("ignoring %s matching regex\n", temp_list->name); | 675 | printf("ignoring %s matching regex\n", elem->name); |
| 676 | } | ||
| 752 | 677 | ||
| 753 | temp_list = np_del_parameter(temp_list, previous); | 678 | elem = mp_int_fs_list_del(&result.config.path_select_list, elem); |
| 754 | /* pointer to first element needs to be updated if first item gets deleted */ | 679 | continue; |
| 755 | if (previous == NULL) | ||
| 756 | path_select_list = temp_list; | ||
| 757 | } else { | ||
| 758 | previous = temp_list; | ||
| 759 | temp_list = temp_list->name_next; | ||
| 760 | } | 680 | } |
| 761 | } else { | ||
| 762 | previous = temp_list; | ||
| 763 | temp_list = temp_list->name_next; | ||
| 764 | } | 681 | } |
| 682 | |||
| 683 | elem = mp_int_fs_list_get_next(elem); | ||
| 765 | } | 684 | } |
| 766 | 685 | ||
| 767 | cflags = default_cflags; | 686 | cflags = default_cflags; |
| 768 | break; | 687 | } break; |
| 769 | |||
| 770 | case 'n': | 688 | case 'n': |
| 771 | ignore_missing = true; | 689 | result.config.ignore_missing = true; |
| 772 | break; | 690 | break; |
| 773 | case 'A': | 691 | case 'A': |
| 774 | optarg = strdup(".*"); | 692 | optarg = strdup(".*"); |
| @@ -776,80 +694,83 @@ int process_arguments(int argc, char **argv) { | |||
| 776 | case 'R': | 694 | case 'R': |
| 777 | cflags |= REG_ICASE; | 695 | cflags |= REG_ICASE; |
| 778 | // Intentional fallthrough | 696 | // Intentional fallthrough |
| 779 | case 'r': | 697 | case 'r': { |
| 780 | if (!(warn_freespace_units || crit_freespace_units || warn_freespace_percent || crit_freespace_percent || | 698 | if (!(warn_freespace_units || crit_freespace_units || warn_freespace_percent || crit_freespace_percent || |
| 781 | warn_usedspace_units || crit_usedspace_units || warn_usedspace_percent || crit_usedspace_percent || | 699 | warn_freeinodes_percent || crit_freeinodes_percent)) { |
| 782 | warn_usedinodes_percent || crit_usedinodes_percent || warn_freeinodes_percent || crit_freeinodes_percent)) { | ||
| 783 | die(STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), | 700 | die(STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), |
| 784 | _("Must set a threshold value before using -r/-R/-A (--ereg-path/--eregi-path/--all)\n")); | 701 | _("Must set a threshold value before using -r/-R/-A (--ereg-path/--eregi-path/--all)\n")); |
| 785 | } | 702 | } |
| 786 | 703 | ||
| 787 | err = regcomp(&re, optarg, cflags); | 704 | regex_t regex; |
| 705 | int err = regcomp(®ex, optarg, cflags); | ||
| 788 | if (err != 0) { | 706 | if (err != 0) { |
| 789 | regerror(err, &re, errbuf, MAX_INPUT_BUFFER); | 707 | char errbuf[MAX_INPUT_BUFFER]; |
| 708 | regerror(err, ®ex, errbuf, MAX_INPUT_BUFFER); | ||
| 790 | die(STATE_UNKNOWN, "DISK %s: %s - %s\n", _("UNKNOWN"), _("Could not compile regular expression"), errbuf); | 709 | die(STATE_UNKNOWN, "DISK %s: %s - %s\n", _("UNKNOWN"), _("Could not compile regular expression"), errbuf); |
| 791 | } | 710 | } |
| 792 | 711 | ||
| 793 | for (me = mount_list; me; me = me->me_next) { | 712 | bool found = false; |
| 794 | if (np_regex_match_mount_entry(me, &re)) { | 713 | for (struct mount_entry *me = result.config.mount_list; me; me = me->me_next) { |
| 795 | fnd = true; | 714 | if (np_regex_match_mount_entry(me, ®ex)) { |
| 796 | if (verbose >= 3) | 715 | found = true; |
| 716 | if (verbose >= 3) { | ||
| 797 | printf("%s %s matching expression %s\n", me->me_devname, me->me_mountdir, optarg); | 717 | printf("%s %s matching expression %s\n", me->me_devname, me->me_mountdir, optarg); |
| 718 | } | ||
| 798 | 719 | ||
| 799 | /* add parameter if not found. overwrite thresholds if path has already been added */ | 720 | /* add parameter if not found. overwrite thresholds if path has already been added */ |
| 800 | if (!(se = np_find_parameter(path_select_list, me->me_mountdir))) { | 721 | parameter_list_elem *se = NULL; |
| 801 | se = np_add_parameter(&path_select_list, me->me_mountdir); | 722 | if (!(se = mp_int_fs_list_find(result.config.path_select_list, me->me_mountdir))) { |
| 723 | se = mp_int_fs_list_append(&result.config.path_select_list, me->me_mountdir); | ||
| 802 | } | 724 | } |
| 803 | se->group = group; | 725 | se->group = group; |
| 804 | set_all_thresholds(se); | 726 | set_all_thresholds(se, warn_freespace_units, crit_freespace_units, warn_freespace_percent, crit_freespace_percent, |
| 727 | warn_freeinodes_percent, crit_freeinodes_percent); | ||
| 805 | } | 728 | } |
| 806 | } | 729 | } |
| 807 | 730 | ||
| 808 | if (!fnd && ignore_missing == true) { | 731 | if (!found) { |
| 809 | path_ignored = true; | 732 | if (result.config.ignore_missing) { |
| 810 | path_selected = true; | 733 | result.config.path_ignored = true; |
| 811 | break; | 734 | path_selected = true; |
| 812 | } | 735 | break; |
| 813 | if (!fnd) | 736 | } |
| 737 | |||
| 814 | die(STATE_UNKNOWN, "DISK %s: %s - %s\n", _("UNKNOWN"), _("Regular expression did not match any path or disk"), optarg); | 738 | die(STATE_UNKNOWN, "DISK %s: %s - %s\n", _("UNKNOWN"), _("Regular expression did not match any path or disk"), optarg); |
| 739 | } | ||
| 815 | 740 | ||
| 816 | fnd = false; | ||
| 817 | path_selected = true; | 741 | path_selected = true; |
| 818 | np_set_best_match(path_select_list, mount_list, exact_match); | 742 | mp_int_fs_list_set_best_match(result.config.path_select_list, result.config.mount_list, result.config.exact_match); |
| 819 | cflags = default_cflags; | 743 | cflags = default_cflags; |
| 820 | 744 | ||
| 821 | break; | 745 | } break; |
| 822 | case 'M': /* display mountpoint */ | 746 | case 'M': /* display mountpoint */ |
| 823 | display_mntp = true; | 747 | result.config.display_mntp = true; |
| 824 | break; | 748 | break; |
| 825 | case 'C': | 749 | case 'C': { |
| 826 | /* add all mount entries to path_select list if no partitions have been explicitly defined using -p */ | 750 | /* add all mount entries to path_select list if no partitions have been explicitly defined using -p */ |
| 827 | if (path_selected == false) { | 751 | if (!path_selected) { |
| 828 | struct parameter_list *path; | 752 | parameter_list_elem *path; |
| 829 | for (me = mount_list; me; me = me->me_next) { | 753 | for (struct mount_entry *me = result.config.mount_list; me; me = me->me_next) { |
| 830 | if (!(path = np_find_parameter(path_select_list, me->me_mountdir))) | 754 | if (!(path = mp_int_fs_list_find(result.config.path_select_list, me->me_mountdir))) { |
| 831 | path = np_add_parameter(&path_select_list, me->me_mountdir); | 755 | path = mp_int_fs_list_append(&result.config.path_select_list, me->me_mountdir); |
| 756 | } | ||
| 832 | path->best_match = me; | 757 | path->best_match = me; |
| 833 | path->group = group; | 758 | path->group = group; |
| 834 | set_all_thresholds(path); | 759 | set_all_thresholds(path, warn_freespace_units, crit_freespace_units, warn_freespace_percent, crit_freespace_percent, |
| 760 | warn_freeinodes_percent, crit_freeinodes_percent); | ||
| 835 | } | 761 | } |
| 836 | } | 762 | } |
| 763 | |||
| 837 | warn_freespace_units = NULL; | 764 | warn_freespace_units = NULL; |
| 838 | crit_freespace_units = NULL; | 765 | crit_freespace_units = NULL; |
| 839 | warn_usedspace_units = NULL; | ||
| 840 | crit_usedspace_units = NULL; | ||
| 841 | warn_freespace_percent = NULL; | 766 | warn_freespace_percent = NULL; |
| 842 | crit_freespace_percent = NULL; | 767 | crit_freespace_percent = NULL; |
| 843 | warn_usedspace_percent = NULL; | ||
| 844 | crit_usedspace_percent = NULL; | ||
| 845 | warn_usedinodes_percent = NULL; | ||
| 846 | crit_usedinodes_percent = NULL; | ||
| 847 | warn_freeinodes_percent = NULL; | 768 | warn_freeinodes_percent = NULL; |
| 848 | crit_freeinodes_percent = NULL; | 769 | crit_freeinodes_percent = NULL; |
| 849 | 770 | ||
| 850 | path_selected = false; | 771 | path_selected = false; |
| 851 | group = NULL; | 772 | group = NULL; |
| 852 | break; | 773 | } break; |
| 853 | case 'V': /* version */ | 774 | case 'V': /* version */ |
| 854 | print_revision(progname, NP_VERSION); | 775 | print_revision(progname, NP_VERSION); |
| 855 | exit(STATE_UNKNOWN); | 776 | exit(STATE_UNKNOWN); |
| @@ -858,50 +779,145 @@ int process_arguments(int argc, char **argv) { | |||
| 858 | exit(STATE_UNKNOWN); | 779 | exit(STATE_UNKNOWN); |
| 859 | case '?': /* help */ | 780 | case '?': /* help */ |
| 860 | usage(_("Unknown argument")); | 781 | usage(_("Unknown argument")); |
| 782 | case output_format_index: { | ||
| 783 | parsed_output_format parser = mp_parse_output_format(optarg); | ||
| 784 | if (!parser.parsing_success) { | ||
| 785 | // TODO List all available formats here, maybe add anothoer usage function | ||
| 786 | printf("Invalid output format: %s\n", optarg); | ||
| 787 | exit(STATE_UNKNOWN); | ||
| 788 | } | ||
| 789 | |||
| 790 | result.config.output_format_is_set = true; | ||
| 791 | result.config.output_format = parser.output_format; | ||
| 792 | break; | ||
| 793 | } | ||
| 861 | } | 794 | } |
| 862 | } | 795 | } |
| 863 | 796 | ||
| 864 | /* Support for "check_disk warn crit [fs]" with thresholds at used% level */ | 797 | /* Support for "check_disk warn crit [fs]" with thresholds at used% level */ |
| 865 | c = optind; | 798 | int index = optind; |
| 866 | if (warn_usedspace_percent == NULL && argc > c && is_intnonneg(argv[c])) | 799 | |
| 867 | warn_usedspace_percent = argv[c++]; | 800 | if (argc > index && is_intnonneg(argv[index])) { |
| 801 | if (verbose > 0) { | ||
| 802 | printf("Got an positional warn threshold: %s\n", argv[index]); | ||
| 803 | } | ||
| 804 | char *range = argv[index++]; | ||
| 805 | mp_range_parsed tmp = mp_parse_range_string(range); | ||
| 806 | if (tmp.error != MP_PARSING_SUCCES) { | ||
| 807 | die(STATE_UNKNOWN, "failed to parse warning threshold"); | ||
| 808 | } | ||
| 809 | |||
| 810 | mp_range tmp_range = tmp.range; | ||
| 811 | // Invert range to use it for free instead of used | ||
| 812 | // tmp_range.alert_on_inside_range = !tmp_range.alert_on_inside_range; | ||
| 868 | 813 | ||
| 869 | if (crit_usedspace_percent == NULL && argc > c && is_intnonneg(argv[c])) | 814 | warn_freespace_percent = mp_range_to_string(tmp_range); |
| 870 | crit_usedspace_percent = argv[c++]; | ||
| 871 | 815 | ||
| 872 | if (argc > c) { | 816 | if (verbose > 0) { |
| 873 | se = np_add_parameter(&path_select_list, strdup(argv[c++])); | 817 | printf("Positional warning threshold transformed to: %s\n", warn_freespace_percent); |
| 818 | } | ||
| 819 | } | ||
| 820 | |||
| 821 | if (argc > index && is_intnonneg(argv[index])) { | ||
| 822 | if (verbose > 0) { | ||
| 823 | printf("Got an positional crit threshold: %s\n", argv[index]); | ||
| 824 | } | ||
| 825 | char *range = argv[index++]; | ||
| 826 | mp_range_parsed tmp = mp_parse_range_string(range); | ||
| 827 | if (tmp.error != MP_PARSING_SUCCES) { | ||
| 828 | die(STATE_UNKNOWN, "failed to parse warning threshold"); | ||
| 829 | } | ||
| 830 | |||
| 831 | mp_range tmp_range = tmp.range; | ||
| 832 | // Invert range to use it for free instead of used | ||
| 833 | // tmp_range.alert_on_inside_range = !tmp_range.alert_on_inside_range; | ||
| 834 | |||
| 835 | crit_freespace_percent = mp_range_to_string(tmp_range); | ||
| 836 | |||
| 837 | if (verbose > 0) { | ||
| 838 | printf("Positional critical threshold transformed to: %s\n", crit_freespace_percent); | ||
| 839 | } | ||
| 840 | } | ||
| 841 | |||
| 842 | if (argc > index) { | ||
| 843 | if (verbose > 0) { | ||
| 844 | printf("Got an positional filesystem: %s\n", argv[index]); | ||
| 845 | } | ||
| 846 | struct parameter_list *se = mp_int_fs_list_append(&result.config.path_select_list, strdup(argv[index++])); | ||
| 874 | path_selected = true; | 847 | path_selected = true; |
| 875 | set_all_thresholds(se); | 848 | set_all_thresholds(se, warn_freespace_units, crit_freespace_units, warn_freespace_percent, crit_freespace_percent, |
| 849 | warn_freeinodes_percent, crit_freeinodes_percent); | ||
| 850 | } | ||
| 851 | |||
| 852 | // If a list of paths has not been explicitly selected, find entire | ||
| 853 | // mount list and create list of paths | ||
| 854 | if (!path_selected && !result.config.path_ignored) { | ||
| 855 | for (struct mount_entry *me = result.config.mount_list; me; me = me->me_next) { | ||
| 856 | if (me->me_dummy != 0) { | ||
| 857 | // just do not add dummy filesystems | ||
| 858 | continue; | ||
| 859 | } | ||
| 860 | |||
| 861 | parameter_list_elem *path = NULL; | ||
| 862 | if (!(path = mp_int_fs_list_find(result.config.path_select_list, me->me_mountdir))) { | ||
| 863 | path = mp_int_fs_list_append(&result.config.path_select_list, me->me_mountdir); | ||
| 864 | } | ||
| 865 | path->best_match = me; | ||
| 866 | path->group = group; | ||
| 867 | set_all_thresholds(path, warn_freespace_units, crit_freespace_units, warn_freespace_percent, crit_freespace_percent, | ||
| 868 | warn_freeinodes_percent, crit_freeinodes_percent); | ||
| 869 | } | ||
| 876 | } | 870 | } |
| 877 | 871 | ||
| 878 | if (units == NULL) { | 872 | // Set thresholds to the appropriate unit |
| 879 | units = strdup("MiB"); | 873 | for (parameter_list_elem *tmp = result.config.path_select_list.first; tmp; tmp = mp_int_fs_list_get_next(tmp)) { |
| 880 | mult = (uintmax_t)1024 * 1024; | 874 | |
| 875 | mp_perfdata_value factor = mp_create_pd_value(unit); | ||
| 876 | |||
| 877 | if (tmp->freespace_units.critical_is_set) { | ||
| 878 | tmp->freespace_units.critical = mp_range_multiply(tmp->freespace_units.critical, factor); | ||
| 879 | } | ||
| 880 | if (tmp->freespace_units.warning_is_set) { | ||
| 881 | tmp->freespace_units.warning = mp_range_multiply(tmp->freespace_units.warning, factor); | ||
| 882 | } | ||
| 881 | } | 883 | } |
| 882 | 884 | ||
| 883 | return true; | 885 | return result; |
| 884 | } | 886 | } |
| 885 | 887 | ||
| 886 | void set_all_thresholds(struct parameter_list *path) { | 888 | void set_all_thresholds(parameter_list_elem *path, char *warn_freespace_units, char *crit_freespace_units, char *warn_freespace_percent, |
| 887 | if (path->freespace_units != NULL) | 889 | char *crit_freespace_percent, char *warn_freeinodes_percent, char *crit_freeinodes_percent) { |
| 888 | free(path->freespace_units); | 890 | mp_range_parsed tmp; |
| 889 | set_thresholds(&path->freespace_units, warn_freespace_units, crit_freespace_units); | 891 | |
| 890 | if (path->freespace_percent != NULL) | 892 | if (warn_freespace_units) { |
| 891 | free(path->freespace_percent); | 893 | tmp = mp_parse_range_string(warn_freespace_units); |
| 892 | set_thresholds(&path->freespace_percent, warn_freespace_percent, crit_freespace_percent); | 894 | path->freespace_units = mp_thresholds_set_warn(path->freespace_units, tmp.range); |
| 893 | if (path->usedspace_units != NULL) | 895 | } |
| 894 | free(path->usedspace_units); | 896 | |
| 895 | set_thresholds(&path->usedspace_units, warn_usedspace_units, crit_usedspace_units); | 897 | if (crit_freespace_units) { |
| 896 | if (path->usedspace_percent != NULL) | 898 | tmp = mp_parse_range_string(crit_freespace_units); |
| 897 | free(path->usedspace_percent); | 899 | path->freespace_units = mp_thresholds_set_crit(path->freespace_units, tmp.range); |
| 898 | set_thresholds(&path->usedspace_percent, warn_usedspace_percent, crit_usedspace_percent); | 900 | } |
| 899 | if (path->usedinodes_percent != NULL) | 901 | |
| 900 | free(path->usedinodes_percent); | 902 | if (warn_freespace_percent) { |
| 901 | set_thresholds(&path->usedinodes_percent, warn_usedinodes_percent, crit_usedinodes_percent); | 903 | tmp = mp_parse_range_string(warn_freespace_percent); |
| 902 | if (path->freeinodes_percent != NULL) | 904 | path->freespace_percent = mp_thresholds_set_warn(path->freespace_percent, tmp.range); |
| 903 | free(path->freeinodes_percent); | 905 | } |
| 904 | set_thresholds(&path->freeinodes_percent, warn_freeinodes_percent, crit_freeinodes_percent); | 906 | |
| 907 | if (crit_freespace_percent) { | ||
| 908 | tmp = mp_parse_range_string(crit_freespace_percent); | ||
| 909 | path->freespace_percent = mp_thresholds_set_crit(path->freespace_percent, tmp.range); | ||
| 910 | } | ||
| 911 | |||
| 912 | if (warn_freeinodes_percent) { | ||
| 913 | tmp = mp_parse_range_string(warn_freeinodes_percent); | ||
| 914 | path->freeinodes_percent = mp_thresholds_set_warn(path->freeinodes_percent, tmp.range); | ||
| 915 | } | ||
| 916 | |||
| 917 | if (crit_freeinodes_percent) { | ||
| 918 | tmp = mp_parse_range_string(crit_freeinodes_percent); | ||
| 919 | path->freeinodes_percent = mp_thresholds_set_crit(path->freeinodes_percent, tmp.range); | ||
| 920 | } | ||
| 905 | } | 921 | } |
| 906 | 922 | ||
| 907 | void print_help(void) { | 923 | void print_help(void) { |
| @@ -948,8 +964,6 @@ void print_help(void) { | |||
| 948 | printf(" %s\n", _("Display inode usage in perfdata")); | 964 | printf(" %s\n", _("Display inode usage in perfdata")); |
| 949 | printf(" %s\n", "-g, --group=NAME"); | 965 | printf(" %s\n", "-g, --group=NAME"); |
| 950 | printf(" %s\n", _("Group paths. Thresholds apply to (free-)space of all partitions together")); | 966 | printf(" %s\n", _("Group paths. Thresholds apply to (free-)space of all partitions together")); |
| 951 | printf(" %s\n", "-k, --kilobytes"); | ||
| 952 | printf(" %s\n", _("Same as '--units kB'")); | ||
| 953 | printf(" %s\n", "-l, --local"); | 967 | printf(" %s\n", "-l, --local"); |
| 954 | printf(" %s\n", _("Only check local filesystems")); | 968 | printf(" %s\n", _("Only check local filesystems")); |
| 955 | printf(" %s\n", "-L, --stat-remote-fs"); | 969 | printf(" %s\n", "-L, --stat-remote-fs"); |
| @@ -957,8 +971,6 @@ void print_help(void) { | |||
| 957 | printf(" %s\n", _("to test if they are accessible (e.g. to detect Stale NFS Handles)")); | 971 | printf(" %s\n", _("to test if they are accessible (e.g. to detect Stale NFS Handles)")); |
| 958 | printf(" %s\n", "-M, --mountpoint"); | 972 | printf(" %s\n", "-M, --mountpoint"); |
| 959 | printf(" %s\n", _("Display the (block) device instead of the mount point")); | 973 | printf(" %s\n", _("Display the (block) device instead of the mount point")); |
| 960 | printf(" %s\n", "-m, --megabytes"); | ||
| 961 | printf(" %s\n", _("Same as '--units MB'")); | ||
| 962 | printf(" %s\n", "-A, --all"); | 974 | printf(" %s\n", "-A, --all"); |
| 963 | printf(" %s\n", _("Explicitly select all paths. This is equivalent to -R '.*'")); | 975 | printf(" %s\n", _("Explicitly select all paths. This is equivalent to -R '.*'")); |
| 964 | printf(" %s\n", "-R, --eregi-path=PATH, --eregi-partition=PARTITION"); | 976 | printf(" %s\n", "-R, --eregi-path=PATH, --eregi-partition=PARTITION"); |
| @@ -974,12 +986,25 @@ void print_help(void) { | |||
| 974 | printf(" %s\n", _("(Provide this option before -p / -r / --ereg-path if used)")); | 986 | printf(" %s\n", _("(Provide this option before -p / -r / --ereg-path if used)")); |
| 975 | printf(UT_PLUG_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 987 | printf(UT_PLUG_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
| 976 | printf(" %s\n", "-u, --units=STRING"); | 988 | printf(" %s\n", "-u, --units=STRING"); |
| 977 | printf(" %s\n", _("Choose bytes, kB, MB, GB, TB (default: MB)")); | 989 | printf(" %s\n", _("Select the unit used for the absolute value thresholds")); |
| 990 | printf( | ||
| 991 | " %s\n", | ||
| 992 | _("Choose one of \"bytes\", \"KiB\", \"kB\", \"MiB\", \"MB\", \"GiB\", \"GB\", \"TiB\", \"TB\", \"PiB\", \"PB\" (default: MiB)")); | ||
| 993 | printf(" %s\n", "-k, --kilobytes"); | ||
| 994 | printf(" %s\n", _("Same as '--units kB'")); | ||
| 995 | printf(" %s\n", "--display-unit"); | ||
| 996 | printf(" %s\n", _("Select the unit used for in the output")); | ||
| 997 | printf( | ||
| 998 | " %s\n", | ||
| 999 | _("Choose one of \"bytes\", \"KiB\", \"kB\", \"MiB\", \"MB\", \"GiB\", \"GB\", \"TiB\", \"TB\", \"PiB\", \"PB\" (default: MiB)")); | ||
| 1000 | printf(" %s\n", "-m, --megabytes"); | ||
| 1001 | printf(" %s\n", _("Same as '--units MB'")); | ||
| 978 | printf(UT_VERBOSE); | 1002 | printf(UT_VERBOSE); |
| 979 | printf(" %s\n", "-X, --exclude-type=TYPE_REGEX"); | 1003 | printf(" %s\n", "-X, --exclude-type=TYPE_REGEX"); |
| 980 | printf(" %s\n", _("Ignore all filesystems of types matching given regex(7) (may be repeated)")); | 1004 | printf(" %s\n", _("Ignore all filesystems of types matching given regex(7) (may be repeated)")); |
| 981 | printf(" %s\n", "-N, --include-type=TYPE_REGEX"); | 1005 | printf(" %s\n", "-N, --include-type=TYPE_REGEX"); |
| 982 | printf(" %s\n", _("Check only filesystems where the type matches this given regex(7) (may be repeated)")); | 1006 | printf(" %s\n", _("Check only filesystems where the type matches this given regex(7) (may be repeated)")); |
| 1007 | printf(UT_OUTPUT_FORMAT); | ||
| 983 | 1008 | ||
| 984 | printf("\n"); | 1009 | printf("\n"); |
| 985 | printf("%s\n", _("General usage hints:")); | 1010 | printf("%s\n", _("General usage hints:")); |
| @@ -1009,105 +1034,187 @@ void print_usage(void) { | |||
| 1009 | printf("[-t timeout] [-u unit] [-v] [-X type_regex] [-N type]\n"); | 1034 | printf("[-t timeout] [-u unit] [-v] [-X type_regex] [-N type]\n"); |
| 1010 | } | 1035 | } |
| 1011 | 1036 | ||
| 1012 | bool stat_path(struct parameter_list *p) { | 1037 | bool stat_path(parameter_list_elem *parameters, bool ignore_missing) { |
| 1013 | /* Stat entry to check that dir exists and is accessible */ | 1038 | /* Stat entry to check that dir exists and is accessible */ |
| 1014 | if (verbose >= 3) | 1039 | if (verbose >= 3) { |
| 1015 | printf("calling stat on %s\n", p->name); | 1040 | printf("calling stat on %s\n", parameters->name); |
| 1016 | if (stat(p->name, &stat_buf[0])) { | 1041 | } |
| 1017 | if (verbose >= 3) | 1042 | |
| 1018 | printf("stat failed on %s\n", p->name); | 1043 | struct stat stat_buf = {0}; |
| 1019 | if (ignore_missing == true) { | 1044 | if (stat(parameters->name, &stat_buf)) { |
| 1045 | if (verbose >= 3) { | ||
| 1046 | printf("stat failed on %s\n", parameters->name); | ||
| 1047 | } | ||
| 1048 | if (ignore_missing) { | ||
| 1020 | return false; | 1049 | return false; |
| 1021 | } | 1050 | } |
| 1022 | printf("DISK %s - ", _("CRITICAL")); | 1051 | printf("DISK %s - ", _("CRITICAL")); |
| 1023 | die(STATE_CRITICAL, _("%s %s: %s\n"), p->name, _("is not accessible"), strerror(errno)); | 1052 | die(STATE_CRITICAL, _("%s %s: %s\n"), parameters->name, _("is not accessible"), strerror(errno)); |
| 1024 | } | 1053 | } |
| 1054 | |||
| 1025 | return true; | 1055 | return true; |
| 1026 | } | 1056 | } |
| 1027 | 1057 | ||
| 1028 | void get_stats(struct parameter_list *p, struct fs_usage *fsp) { | 1058 | static parameter_list_elem get_path_stats(parameter_list_elem parameters, const struct fs_usage fsp, bool freespace_ignore_reserved) { |
| 1029 | struct parameter_list *p_list; | 1059 | uintmax_t available = fsp.fsu_bavail; |
| 1030 | struct fs_usage tmpfsp; | 1060 | uintmax_t available_to_root = fsp.fsu_bfree; |
| 1031 | int first = 1; | 1061 | uintmax_t used = fsp.fsu_blocks - fsp.fsu_bfree; |
| 1062 | uintmax_t total; | ||
| 1032 | 1063 | ||
| 1033 | if (p->group == NULL) { | ||
| 1034 | get_path_stats(p, fsp); | ||
| 1035 | } else { | ||
| 1036 | /* find all group members */ | ||
| 1037 | for (p_list = path_select_list; p_list; p_list = p_list->name_next) { | ||
| 1038 | #ifdef __CYGWIN__ | ||
| 1039 | if (strncmp(p_list->name, "/cygdrive/", 10) != 0) | ||
| 1040 | continue; | ||
| 1041 | #endif | ||
| 1042 | if (p_list->group && !(strcmp(p_list->group, p->group))) { | ||
| 1043 | if (!stat_path(p_list)) | ||
| 1044 | continue; | ||
| 1045 | get_fs_usage(p_list->best_match->me_mountdir, p_list->best_match->me_devname, &tmpfsp); | ||
| 1046 | get_path_stats(p_list, &tmpfsp); | ||
| 1047 | if (verbose >= 3) | ||
| 1048 | printf("Group %s: adding %lu blocks sized %lu, (%s) used_units=%lu free_units=%lu total_units=%lu mult=%lu\n", | ||
| 1049 | p_list->group, tmpfsp.fsu_blocks, tmpfsp.fsu_blocksize, p_list->best_match->me_mountdir, p_list->dused_units, | ||
| 1050 | p_list->dfree_units, p_list->dtotal_units, mult); | ||
| 1051 | |||
| 1052 | /* prevent counting the first FS of a group twice since its parameter_list entry | ||
| 1053 | * is used to carry the information of all file systems of the entire group */ | ||
| 1054 | if (!first) { | ||
| 1055 | p->total += p_list->total; | ||
| 1056 | p->available += p_list->available; | ||
| 1057 | p->available_to_root += p_list->available_to_root; | ||
| 1058 | p->used += p_list->used; | ||
| 1059 | |||
| 1060 | p->dused_units += p_list->dused_units; | ||
| 1061 | p->dfree_units += p_list->dfree_units; | ||
| 1062 | p->dtotal_units += p_list->dtotal_units; | ||
| 1063 | p->inodes_total += p_list->inodes_total; | ||
| 1064 | p->inodes_free += p_list->inodes_free; | ||
| 1065 | p->inodes_free_to_root += p_list->inodes_free_to_root; | ||
| 1066 | p->inodes_used += p_list->inodes_used; | ||
| 1067 | } | ||
| 1068 | first = 0; | ||
| 1069 | } | ||
| 1070 | if (verbose >= 3) | ||
| 1071 | printf("Group %s now has: used_units=%lu free_units=%lu total_units=%lu fsu_blocksize=%lu mult=%lu\n", p->group, | ||
| 1072 | p->dused_units, p->dfree_units, p->dtotal_units, tmpfsp.fsu_blocksize, mult); | ||
| 1073 | } | ||
| 1074 | /* modify devname and mountdir for output */ | ||
| 1075 | p->best_match->me_mountdir = p->best_match->me_devname = p->group; | ||
| 1076 | } | ||
| 1077 | /* finally calculate percentages for either plain FS or summed up group */ | ||
| 1078 | p->dused_pct = calculate_percent(p->used, p->used + p->available); /* used + available can never be > uintmax */ | ||
| 1079 | p->dfree_pct = 100.0 - p->dused_pct; | ||
| 1080 | p->dused_inodes_percent = calculate_percent(p->inodes_total - p->inodes_free, p->inodes_total); | ||
| 1081 | p->dfree_inodes_percent = 100 - p->dused_inodes_percent; | ||
| 1082 | } | ||
| 1083 | |||
| 1084 | void get_path_stats(struct parameter_list *p, struct fs_usage *fsp) { | ||
| 1085 | p->available = fsp->fsu_bavail; | ||
| 1086 | p->available_to_root = fsp->fsu_bfree; | ||
| 1087 | p->used = fsp->fsu_blocks - fsp->fsu_bfree; | ||
| 1088 | if (freespace_ignore_reserved) { | 1064 | if (freespace_ignore_reserved) { |
| 1089 | /* option activated : we subtract the root-reserved space from the total */ | 1065 | /* option activated : we subtract the root-reserved space from the total */ |
| 1090 | p->total = fsp->fsu_blocks - p->available_to_root + p->available; | 1066 | total = fsp.fsu_blocks - available_to_root + available; |
| 1091 | } else { | 1067 | } else { |
| 1092 | /* default behaviour : take all the blocks into account */ | 1068 | /* default behaviour : take all the blocks into account */ |
| 1093 | p->total = fsp->fsu_blocks; | 1069 | total = fsp.fsu_blocks; |
| 1094 | } | 1070 | } |
| 1095 | 1071 | ||
| 1096 | p->dused_units = p->used * fsp->fsu_blocksize / mult; | 1072 | parameters.used_bytes = used * fsp.fsu_blocksize; |
| 1097 | p->dfree_units = p->available * fsp->fsu_blocksize / mult; | 1073 | parameters.free_bytes = available * fsp.fsu_blocksize; |
| 1098 | p->dtotal_units = p->total * fsp->fsu_blocksize / mult; | 1074 | parameters.total_bytes = total * fsp.fsu_blocksize; |
| 1075 | |||
| 1099 | /* Free file nodes. Not sure the workaround is required, but in case...*/ | 1076 | /* Free file nodes. Not sure the workaround is required, but in case...*/ |
| 1100 | p->inodes_free = fsp->fsu_ffree; | 1077 | parameters.inodes_free = fsp.fsu_ffree; |
| 1101 | p->inodes_free_to_root = fsp->fsu_ffree; /* Free file nodes for root. */ | 1078 | parameters.inodes_free_to_root = fsp.fsu_ffree; /* Free file nodes for root. */ |
| 1102 | p->inodes_used = fsp->fsu_files - fsp->fsu_ffree; | 1079 | parameters.inodes_used = fsp.fsu_files - fsp.fsu_ffree; |
| 1080 | |||
| 1103 | if (freespace_ignore_reserved) { | 1081 | if (freespace_ignore_reserved) { |
| 1104 | /* option activated : we subtract the root-reserved inodes from the total */ | 1082 | /* option activated : we subtract the root-reserved inodes from the total */ |
| 1105 | /* not all OS report fsp->fsu_favail, only the ones with statvfs syscall */ | 1083 | /* not all OS report fsp->fsu_favail, only the ones with statvfs syscall */ |
| 1106 | /* for others, fsp->fsu_ffree == fsp->fsu_favail */ | 1084 | /* for others, fsp->fsu_ffree == fsp->fsu_favail */ |
| 1107 | p->inodes_total = fsp->fsu_files - p->inodes_free_to_root + p->inodes_free; | 1085 | parameters.inodes_total = fsp.fsu_files - parameters.inodes_free_to_root + parameters.inodes_free; |
| 1108 | } else { | 1086 | } else { |
| 1109 | /* default behaviour : take all the inodes into account */ | 1087 | /* default behaviour : take all the inodes into account */ |
| 1110 | p->inodes_total = fsp->fsu_files; | 1088 | parameters.inodes_total = fsp.fsu_files; |
| 1089 | } | ||
| 1090 | |||
| 1091 | return parameters; | ||
| 1092 | } | ||
| 1093 | |||
| 1094 | mp_subcheck evaluate_filesystem(measurement_unit measurement_unit, bool display_inodes_perfdata, byte_unit unit) { | ||
| 1095 | mp_subcheck result = mp_subcheck_init(); | ||
| 1096 | result = mp_set_subcheck_default_state(result, STATE_UNKNOWN); | ||
| 1097 | xasprintf(&result.output, "%s", measurement_unit.name); | ||
| 1098 | |||
| 1099 | if (!measurement_unit.is_group && measurement_unit.filesystem_type) { | ||
| 1100 | xasprintf(&result.output, "%s (%s)", result.output, measurement_unit.filesystem_type); | ||
| 1111 | } | 1101 | } |
| 1112 | np_add_name(&seen, p->best_match->me_mountdir); | 1102 | |
| 1103 | /* Threshold comparisons */ | ||
| 1104 | |||
| 1105 | // =============================== | ||
| 1106 | // Free space absolute values test | ||
| 1107 | mp_subcheck freespace_bytes_sc = mp_subcheck_init(); | ||
| 1108 | freespace_bytes_sc = mp_set_subcheck_default_state(freespace_bytes_sc, STATE_OK); | ||
| 1109 | |||
| 1110 | if (unit != Humanized) { | ||
| 1111 | xasprintf(&freespace_bytes_sc.output, "Free space absolute: %ju%s (of %ju%s)", (uintmax_t)(measurement_unit.free_bytes / unit), | ||
| 1112 | get_unit_string(unit), (uintmax_t)(measurement_unit.total_bytes / unit), get_unit_string(unit)); | ||
| 1113 | } else { | ||
| 1114 | xasprintf(&freespace_bytes_sc.output, "Free space absolute: %s (of %s)", humanize_byte_value(measurement_unit.free_bytes, false), | ||
| 1115 | humanize_byte_value((unsigned long long)measurement_unit.total_bytes, false)); | ||
| 1116 | } | ||
| 1117 | |||
| 1118 | mp_perfdata used_space = perfdata_init(); | ||
| 1119 | used_space.label = measurement_unit.name; | ||
| 1120 | used_space.value = mp_create_pd_value(measurement_unit.free_bytes); | ||
| 1121 | used_space = mp_set_pd_max_value(used_space, mp_create_pd_value(measurement_unit.total_bytes)); | ||
| 1122 | used_space = mp_set_pd_min_value(used_space, mp_create_pd_value(0)); | ||
| 1123 | used_space.uom = "B"; | ||
| 1124 | used_space = mp_pd_set_thresholds(used_space, measurement_unit.freespace_bytes_thresholds); | ||
| 1125 | freespace_bytes_sc = mp_set_subcheck_state(freespace_bytes_sc, mp_get_pd_status(used_space)); | ||
| 1126 | |||
| 1127 | // special case for absolute space thresholds here: | ||
| 1128 | // if absolute values are not set, compute the thresholds from percentage thresholds | ||
| 1129 | mp_thresholds temp_thlds = measurement_unit.freespace_bytes_thresholds; | ||
| 1130 | if (!temp_thlds.critical_is_set && measurement_unit.freespace_percent_thresholds.critical_is_set) { | ||
| 1131 | mp_range tmp_range = measurement_unit.freespace_percent_thresholds.critical; | ||
| 1132 | |||
| 1133 | if (!tmp_range.end_infinity) { | ||
| 1134 | tmp_range.end = mp_create_pd_value(mp_get_pd_value(tmp_range.end) / 100 * measurement_unit.total_bytes); | ||
| 1135 | } | ||
| 1136 | |||
| 1137 | if (!tmp_range.start_infinity) { | ||
| 1138 | tmp_range.start = mp_create_pd_value(mp_get_pd_value(tmp_range.start) / 100 * measurement_unit.total_bytes); | ||
| 1139 | } | ||
| 1140 | measurement_unit.freespace_bytes_thresholds = mp_thresholds_set_crit(measurement_unit.freespace_bytes_thresholds, tmp_range); | ||
| 1141 | used_space = mp_pd_set_thresholds(used_space, measurement_unit.freespace_bytes_thresholds); | ||
| 1142 | } | ||
| 1143 | |||
| 1144 | if (!temp_thlds.warning_is_set && measurement_unit.freespace_percent_thresholds.warning_is_set) { | ||
| 1145 | mp_range tmp_range = measurement_unit.freespace_percent_thresholds.warning; | ||
| 1146 | if (!tmp_range.end_infinity) { | ||
| 1147 | tmp_range.end = mp_create_pd_value(mp_get_pd_value(tmp_range.end) / 100 * measurement_unit.total_bytes); | ||
| 1148 | } | ||
| 1149 | if (!tmp_range.start_infinity) { | ||
| 1150 | tmp_range.start = mp_create_pd_value(mp_get_pd_value(tmp_range.start) / 100 * measurement_unit.total_bytes); | ||
| 1151 | } | ||
| 1152 | measurement_unit.freespace_bytes_thresholds = mp_thresholds_set_warn(measurement_unit.freespace_bytes_thresholds, tmp_range); | ||
| 1153 | used_space = mp_pd_set_thresholds(used_space, measurement_unit.freespace_bytes_thresholds); | ||
| 1154 | } | ||
| 1155 | |||
| 1156 | mp_add_perfdata_to_subcheck(&freespace_bytes_sc, used_space); | ||
| 1157 | mp_add_subcheck_to_subcheck(&result, freespace_bytes_sc); | ||
| 1158 | |||
| 1159 | // ========================== | ||
| 1160 | // Free space percentage test | ||
| 1161 | mp_subcheck freespace_percent_sc = mp_subcheck_init(); | ||
| 1162 | freespace_percent_sc = mp_set_subcheck_default_state(freespace_percent_sc, STATE_OK); | ||
| 1163 | |||
| 1164 | double free_percentage = calculate_percent(measurement_unit.free_bytes, measurement_unit.total_bytes); | ||
| 1165 | xasprintf(&freespace_percent_sc.output, "Free space percentage: %g%%", free_percentage); | ||
| 1166 | |||
| 1167 | // Using perfdata here just to get to the test result | ||
| 1168 | mp_perfdata free_space_percent_pd = perfdata_init(); | ||
| 1169 | free_space_percent_pd.value = mp_create_pd_value(free_percentage); | ||
| 1170 | free_space_percent_pd = mp_pd_set_thresholds(free_space_percent_pd, measurement_unit.freespace_percent_thresholds); | ||
| 1171 | |||
| 1172 | freespace_percent_sc = mp_set_subcheck_state(freespace_percent_sc, mp_get_pd_status(free_space_percent_pd)); | ||
| 1173 | mp_add_subcheck_to_subcheck(&result, freespace_percent_sc); | ||
| 1174 | |||
| 1175 | // ================ | ||
| 1176 | // Free inodes test | ||
| 1177 | // Only ever useful if the number of inodes is static (e.g. ext4), | ||
| 1178 | // not when it is dynamic (e.g btrfs) | ||
| 1179 | // Assumption: if the total number of inodes == 0, we have such a case and just skip the test | ||
| 1180 | if (measurement_unit.inodes_total > 0) { | ||
| 1181 | mp_subcheck freeindodes_percent_sc = mp_subcheck_init(); | ||
| 1182 | freeindodes_percent_sc = mp_set_subcheck_default_state(freeindodes_percent_sc, STATE_OK); | ||
| 1183 | |||
| 1184 | double free_inode_percentage = calculate_percent(measurement_unit.inodes_free, measurement_unit.inodes_total); | ||
| 1185 | |||
| 1186 | if (verbose > 0) { | ||
| 1187 | printf("free inode percentage computed: %g\n", free_inode_percentage); | ||
| 1188 | } | ||
| 1189 | |||
| 1190 | xasprintf(&freeindodes_percent_sc.output, "Inodes free: %g%% (%ju of %ju)", free_inode_percentage, measurement_unit.inodes_free, | ||
| 1191 | measurement_unit.inodes_total); | ||
| 1192 | |||
| 1193 | mp_perfdata inodes_pd = perfdata_init(); | ||
| 1194 | xasprintf(&inodes_pd.label, "%s (inodes)", measurement_unit.name); | ||
| 1195 | inodes_pd = mp_set_pd_value(inodes_pd, measurement_unit.inodes_used); | ||
| 1196 | inodes_pd = mp_set_pd_max_value(inodes_pd, mp_create_pd_value(measurement_unit.inodes_total)); | ||
| 1197 | inodes_pd = mp_set_pd_min_value(inodes_pd, mp_create_pd_value(0)); | ||
| 1198 | |||
| 1199 | mp_thresholds absolut_inode_thresholds = measurement_unit.freeinodes_percent_thresholds; | ||
| 1200 | |||
| 1201 | if (absolut_inode_thresholds.critical_is_set) { | ||
| 1202 | absolut_inode_thresholds.critical = | ||
| 1203 | mp_range_multiply(absolut_inode_thresholds.critical, mp_create_pd_value(measurement_unit.inodes_total / 100)); | ||
| 1204 | } | ||
| 1205 | if (absolut_inode_thresholds.warning_is_set) { | ||
| 1206 | absolut_inode_thresholds.warning = | ||
| 1207 | mp_range_multiply(absolut_inode_thresholds.warning, mp_create_pd_value(measurement_unit.inodes_total / 100)); | ||
| 1208 | } | ||
| 1209 | |||
| 1210 | inodes_pd = mp_pd_set_thresholds(inodes_pd, absolut_inode_thresholds); | ||
| 1211 | |||
| 1212 | freeindodes_percent_sc = mp_set_subcheck_state(freeindodes_percent_sc, mp_get_pd_status(inodes_pd)); | ||
| 1213 | if (display_inodes_perfdata) { | ||
| 1214 | mp_add_perfdata_to_subcheck(&freeindodes_percent_sc, inodes_pd); | ||
| 1215 | } | ||
| 1216 | mp_add_subcheck_to_subcheck(&result, freeindodes_percent_sc); | ||
| 1217 | } | ||
| 1218 | |||
| 1219 | return result; | ||
| 1113 | } | 1220 | } |
