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 /plugins/check_disk.d | |
| parent | 0bca1d1aa36b13723e77672eae162352e9be99c9 (diff) | |
| download | monitoring-plugins-908aed4e6f9072e601a189d4ceff3152bdecc49d.tar.gz | |
Refactor check_disk and library functions
Diffstat (limited to 'plugins/check_disk.d')
| -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 |
3 files changed, 471 insertions, 232 deletions
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); | ||
