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