diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-18 14:37:02 +0100 |
|---|---|---|
| committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-18 14:37:02 +0100 |
| commit | 8ccff07bed03046a97637a54d45a9ffe77edc235 (patch) | |
| tree | 6be1c9d8d6b6833c1416e3ad2e960a1e75cbcd3a | |
| parent | 285db2a9fa25519cacd48a76347ae2dee0c06605 (diff) | |
| download | monitoring-plugins-8ccff07bed03046a97637a54d45a9ffe77edc235.tar.gz | |
refactor check_disk.d code a bit
| -rw-r--r-- | plugins/check_disk.d/utils_disk.c | 139 | ||||
| -rw-r--r-- | plugins/check_disk.d/utils_disk.h | 39 |
2 files changed, 100 insertions, 78 deletions
diff --git a/plugins/check_disk.d/utils_disk.c b/plugins/check_disk.d/utils_disk.c index 2b761f5e..1d806715 100644 --- a/plugins/check_disk.d/utils_disk.c +++ b/plugins/check_disk.d/utils_disk.c | |||
| @@ -63,12 +63,46 @@ int np_add_regex(struct regex_list **list, const char *regex, int cflags) { | |||
| 63 | *list = new_entry; | 63 | *list = new_entry; |
| 64 | 64 | ||
| 65 | return 0; | 65 | return 0; |
| 66 | } else { | ||
| 67 | // regcomp failed | ||
| 68 | free(new_entry); | ||
| 69 | |||
| 70 | return regcomp_result; | ||
| 71 | } | 66 | } |
| 67 | // regcomp failed | ||
| 68 | free(new_entry); | ||
| 69 | |||
| 70 | return regcomp_result; | ||
| 71 | } | ||
| 72 | |||
| 73 | struct parameter_list parameter_list_init(const char *name) { | ||
| 74 | struct parameter_list result = { | ||
| 75 | .name = strdup(name), | ||
| 76 | .best_match = NULL, | ||
| 77 | |||
| 78 | .name_next = NULL, | ||
| 79 | .name_prev = NULL, | ||
| 80 | |||
| 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 | |||
| 88 | .group = NULL, | ||
| 89 | .dfree_pct = -1, | ||
| 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, | ||
| 99 | .inodes_free = 0, | ||
| 100 | .inodes_free_to_root = 0, | ||
| 101 | .inodes_used = 0, | ||
| 102 | .dused_inodes_percent = 0, | ||
| 103 | .dfree_inodes_percent = 0, | ||
| 104 | }; | ||
| 105 | return result; | ||
| 72 | } | 106 | } |
| 73 | 107 | ||
| 74 | /* Initialises a new parameter at the end of list */ | 108 | /* Initialises a new parameter at the end of list */ |
| @@ -76,36 +110,8 @@ struct parameter_list *np_add_parameter(struct parameter_list **list, const char | |||
| 76 | struct parameter_list *current = *list; | 110 | struct parameter_list *current = *list; |
| 77 | struct parameter_list *new_path; | 111 | struct parameter_list *new_path; |
| 78 | new_path = (struct parameter_list *)malloc(sizeof *new_path); | 112 | new_path = (struct parameter_list *)malloc(sizeof *new_path); |
| 79 | new_path->name = (char *)malloc(strlen(name) + 1); | 113 | |
| 80 | new_path->best_match = NULL; | 114 | *new_path = parameter_list_init(name); |
| 81 | new_path->name_next = NULL; | ||
| 82 | new_path->name_prev = NULL; | ||
| 83 | new_path->freespace_bytes = NULL; | ||
| 84 | new_path->freespace_units = NULL; | ||
| 85 | new_path->freespace_percent = NULL; | ||
| 86 | new_path->usedspace_bytes = NULL; | ||
| 87 | new_path->usedspace_units = NULL; | ||
| 88 | new_path->usedspace_percent = NULL; | ||
| 89 | new_path->usedinodes_percent = NULL; | ||
| 90 | new_path->freeinodes_percent = NULL; | ||
| 91 | new_path->group = NULL; | ||
| 92 | new_path->dfree_pct = -1; | ||
| 93 | new_path->dused_pct = -1; | ||
| 94 | new_path->total = 0; | ||
| 95 | new_path->available = 0; | ||
| 96 | new_path->available_to_root = 0; | ||
| 97 | new_path->used = 0; | ||
| 98 | new_path->dused_units = 0; | ||
| 99 | new_path->dfree_units = 0; | ||
| 100 | new_path->dtotal_units = 0; | ||
| 101 | new_path->inodes_total = 0; | ||
| 102 | new_path->inodes_free = 0; | ||
| 103 | new_path->inodes_free_to_root = 0; | ||
| 104 | new_path->inodes_used = 0; | ||
| 105 | new_path->dused_inodes_percent = 0; | ||
| 106 | new_path->dfree_inodes_percent = 0; | ||
| 107 | |||
| 108 | strcpy(new_path->name, name); | ||
| 109 | 115 | ||
| 110 | if (current == NULL) { | 116 | if (current == NULL) { |
| 111 | *list = new_path; | 117 | *list = new_path; |
| @@ -125,18 +131,22 @@ struct parameter_list *np_del_parameter(struct parameter_list *item, struct para | |||
| 125 | if (item == NULL) { | 131 | if (item == NULL) { |
| 126 | return NULL; | 132 | return NULL; |
| 127 | } | 133 | } |
| 134 | |||
| 128 | struct parameter_list *next; | 135 | struct parameter_list *next; |
| 129 | 136 | ||
| 130 | if (item->name_next) | 137 | if (item->name_next) { |
| 131 | next = item->name_next; | 138 | next = item->name_next; |
| 132 | else | 139 | } else { |
| 133 | next = NULL; | 140 | next = NULL; |
| 141 | } | ||
| 134 | 142 | ||
| 135 | if (next) | 143 | if (next) { |
| 136 | next->name_prev = prev; | 144 | next->name_prev = prev; |
| 145 | } | ||
| 137 | 146 | ||
| 138 | if (prev) | 147 | if (prev) { |
| 139 | prev->name_next = next; | 148 | prev->name_next = next; |
| 149 | } | ||
| 140 | 150 | ||
| 141 | if (item->name) { | 151 | if (item->name) { |
| 142 | free(item->name); | 152 | free(item->name); |
| @@ -148,43 +158,42 @@ struct parameter_list *np_del_parameter(struct parameter_list *item, struct para | |||
| 148 | 158 | ||
| 149 | /* returns a pointer to the struct found in the list */ | 159 | /* returns a pointer to the struct found in the list */ |
| 150 | struct parameter_list *np_find_parameter(struct parameter_list *list, const char *name) { | 160 | struct parameter_list *np_find_parameter(struct parameter_list *list, const char *name) { |
| 151 | struct parameter_list *temp_list; | 161 | for (struct parameter_list *temp_list = list; temp_list; temp_list = temp_list->name_next) { |
| 152 | for (temp_list = list; temp_list; temp_list = temp_list->name_next) { | 162 | if (!strcmp(temp_list->name, name)) { |
| 153 | if (!strcmp(temp_list->name, name)) | ||
| 154 | return temp_list; | 163 | return temp_list; |
| 164 | } | ||
| 155 | } | 165 | } |
| 156 | 166 | ||
| 157 | return NULL; | 167 | return NULL; |
| 158 | } | 168 | } |
| 159 | 169 | ||
| 160 | void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, bool exact) { | 170 | void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, bool exact) { |
| 161 | struct parameter_list *d; | 171 | for (struct parameter_list *d = desired; d; d = d->name_next) { |
| 162 | for (d = desired; d; d = d->name_next) { | ||
| 163 | if (!d->best_match) { | 172 | if (!d->best_match) { |
| 164 | struct mount_entry *me; | 173 | struct mount_entry *mount_entry; |
| 165 | size_t name_len = strlen(d->name); | 174 | size_t name_len = strlen(d->name); |
| 166 | size_t best_match_len = 0; | 175 | size_t best_match_len = 0; |
| 167 | struct mount_entry *best_match = NULL; | 176 | struct mount_entry *best_match = NULL; |
| 168 | struct fs_usage fsp; | 177 | struct fs_usage fsp; |
| 169 | 178 | ||
| 170 | /* set best match if path name exactly matches a mounted device name */ | 179 | /* set best match if path name exactly matches a mounted device name */ |
| 171 | for (me = mount_list; me; me = me->me_next) { | 180 | for (mount_entry = mount_list; mount_entry; mount_entry = mount_entry->me_next) { |
| 172 | if (strcmp(me->me_devname, d->name) == 0) { | 181 | if (strcmp(mount_entry->me_devname, d->name) == 0) { |
| 173 | if (get_fs_usage(me->me_mountdir, me->me_devname, &fsp) >= 0) { | 182 | if (get_fs_usage(mount_entry->me_mountdir, mount_entry->me_devname, &fsp) >= 0) { |
| 174 | best_match = me; | 183 | best_match = mount_entry; |
| 175 | } | 184 | } |
| 176 | } | 185 | } |
| 177 | } | 186 | } |
| 178 | 187 | ||
| 179 | /* set best match by directory name if no match was found by devname */ | 188 | /* set best match by directory name if no match was found by devname */ |
| 180 | if (!best_match) { | 189 | if (!best_match) { |
| 181 | for (me = mount_list; me; me = me->me_next) { | 190 | for (mount_entry = mount_list; mount_entry; mount_entry = mount_entry->me_next) { |
| 182 | size_t len = strlen(me->me_mountdir); | 191 | size_t len = strlen(mount_entry->me_mountdir); |
| 183 | if ((!exact && | 192 | if ((!exact && (best_match_len <= len && len <= name_len && |
| 184 | (best_match_len <= len && len <= name_len && (len == 1 || strncmp(me->me_mountdir, d->name, len) == 0))) || | 193 | (len == 1 || strncmp(mount_entry->me_mountdir, d->name, len) == 0))) || |
| 185 | (exact && strcmp(me->me_mountdir, d->name) == 0)) { | 194 | (exact && strcmp(mount_entry->me_mountdir, d->name) == 0)) { |
| 186 | if (get_fs_usage(me->me_mountdir, me->me_devname, &fsp) >= 0) { | 195 | if (get_fs_usage(mount_entry->me_mountdir, mount_entry->me_devname, &fsp) >= 0) { |
| 187 | best_match = me; | 196 | best_match = mount_entry; |
| 188 | best_match_len = len; | 197 | best_match_len = len; |
| 189 | } | 198 | } |
| 190 | } | 199 | } |
| @@ -202,12 +211,10 @@ void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount | |||
| 202 | 211 | ||
| 203 | /* Returns true if name is in list */ | 212 | /* Returns true if name is in list */ |
| 204 | bool np_find_name(struct name_list *list, const char *name) { | 213 | bool np_find_name(struct name_list *list, const char *name) { |
| 205 | const struct name_list *n; | ||
| 206 | |||
| 207 | if (list == NULL || name == NULL) { | 214 | if (list == NULL || name == NULL) { |
| 208 | return false; | 215 | return false; |
| 209 | } | 216 | } |
| 210 | for (n = list; n; n = n->next) { | 217 | for (struct name_list *n = list; n; n = n->next) { |
| 211 | if (!strcmp(name, n->name)) { | 218 | if (!strcmp(name, n->name)) { |
| 212 | return true; | 219 | return true; |
| 213 | } | 220 | } |
| @@ -217,18 +224,16 @@ bool np_find_name(struct name_list *list, const char *name) { | |||
| 217 | 224 | ||
| 218 | /* Returns true if name is in list */ | 225 | /* Returns true if name is in list */ |
| 219 | bool np_find_regmatch(struct regex_list *list, const char *name) { | 226 | bool np_find_regmatch(struct regex_list *list, const char *name) { |
| 220 | int len; | ||
| 221 | regmatch_t m; | ||
| 222 | |||
| 223 | if (name == NULL) { | 227 | if (name == NULL) { |
| 224 | return false; | 228 | return false; |
| 225 | } | 229 | } |
| 226 | 230 | ||
| 227 | len = strlen(name); | 231 | int len = strlen(name); |
| 228 | 232 | ||
| 229 | for (; list; list = list->next) { | 233 | for (; list; list = list->next) { |
| 230 | /* Emulate a full match as if surrounded with ^( )$ | 234 | /* Emulate a full match as if surrounded with ^( )$ |
| 231 | by checking whether the match spans the whole name */ | 235 | by checking whether the match spans the whole name */ |
| 236 | regmatch_t m; | ||
| 232 | if (!regexec(&list->regex, name, 1, &m, 0) && m.rm_so == 0 && m.rm_eo == len) { | 237 | if (!regexec(&list->regex, name, 1, &m, 0) && m.rm_so == 0 && m.rm_eo == len) { |
| 233 | return true; | 238 | return true; |
| 234 | } | 239 | } |
| @@ -238,8 +243,7 @@ bool np_find_regmatch(struct regex_list *list, const char *name) { | |||
| 238 | } | 243 | } |
| 239 | 244 | ||
| 240 | bool np_seen_name(struct name_list *list, const char *name) { | 245 | bool np_seen_name(struct name_list *list, const char *name) { |
| 241 | const struct name_list *s; | 246 | for (struct name_list *s = list; s; s = s->next) { |
| 242 | for (s = list; s; s = s->next) { | ||
| 243 | if (!strcmp(s->name, name)) { | 247 | if (!strcmp(s->name, name)) { |
| 244 | return true; | 248 | return true; |
| 245 | } | 249 | } |
| @@ -248,8 +252,5 @@ bool np_seen_name(struct name_list *list, const char *name) { | |||
| 248 | } | 252 | } |
| 249 | 253 | ||
| 250 | bool np_regex_match_mount_entry(struct mount_entry *me, regex_t *re) { | 254 | bool np_regex_match_mount_entry(struct mount_entry *me, regex_t *re) { |
| 251 | if (regexec(re, me->me_devname, (size_t)0, NULL, 0) == 0 || regexec(re, me->me_mountdir, (size_t)0, NULL, 0) == 0) { | 255 | return ((regexec(re, me->me_devname, (size_t)0, NULL, 0) == 0) || (regexec(re, me->me_mountdir, (size_t)0, NULL, 0) == 0)); |
| 252 | return true; | ||
| 253 | } | ||
| 254 | return false; | ||
| 255 | } | 256 | } |
diff --git a/plugins/check_disk.d/utils_disk.h b/plugins/check_disk.d/utils_disk.h index c5e81dc1..1c68fed9 100644 --- a/plugins/check_disk.d/utils_disk.h +++ b/plugins/check_disk.d/utils_disk.h | |||
| @@ -1,8 +1,10 @@ | |||
| 1 | /* Header file for utils_disk */ | 1 | /* Header file for utils_disk */ |
| 2 | 2 | ||
| 3 | #include "mountlist.h" | 3 | #include "../../config.h" |
| 4 | #include "../../gl/mountlist.h" | ||
| 4 | #include "utils_base.h" | 5 | #include "utils_base.h" |
| 5 | #include "regex.h" | 6 | #include "regex.h" |
| 7 | #include <stdint.h> | ||
| 6 | 8 | ||
| 7 | struct name_list { | 9 | struct name_list { |
| 8 | char *name; | 10 | char *name; |
| @@ -16,22 +18,39 @@ struct regex_list { | |||
| 16 | 18 | ||
| 17 | struct parameter_list { | 19 | struct parameter_list { |
| 18 | char *name; | 20 | char *name; |
| 19 | thresholds *freespace_bytes; | 21 | char *group; |
| 22 | |||
| 20 | thresholds *freespace_units; | 23 | thresholds *freespace_units; |
| 21 | thresholds *freespace_percent; | 24 | thresholds *freespace_percent; |
| 22 | thresholds *usedspace_bytes; | ||
| 23 | thresholds *usedspace_units; | 25 | thresholds *usedspace_units; |
| 24 | thresholds *usedspace_percent; | 26 | thresholds *usedspace_percent; |
| 27 | |||
| 25 | thresholds *usedinodes_percent; | 28 | thresholds *usedinodes_percent; |
| 26 | thresholds *freeinodes_percent; | 29 | thresholds *freeinodes_percent; |
| 27 | char *group; | 30 | |
| 28 | struct mount_entry *best_match; | 31 | struct mount_entry *best_match; |
| 32 | |||
| 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; | ||
| 39 | uintmax_t inodes_used; | ||
| 40 | uintmax_t inodes_total; | ||
| 41 | |||
| 42 | double dfree_pct; | ||
| 43 | double dused_pct; | ||
| 44 | |||
| 45 | uint64_t dused_units; | ||
| 46 | uint64_t dfree_units; | ||
| 47 | uint64_t dtotal_units; | ||
| 48 | |||
| 49 | double dused_inodes_percent; | ||
| 50 | double dfree_inodes_percent; | ||
| 51 | |||
| 29 | struct parameter_list *name_next; | 52 | struct parameter_list *name_next; |
| 30 | struct parameter_list *name_prev; | 53 | struct parameter_list *name_prev; |
| 31 | uintmax_t total, available, available_to_root, used, inodes_free, inodes_free_to_root, inodes_used, inodes_total; | ||
| 32 | double dfree_pct, dused_pct; | ||
| 33 | uint64_t dused_units, dfree_units, dtotal_units; | ||
| 34 | double dused_inodes_percent, dfree_inodes_percent; | ||
| 35 | }; | 54 | }; |
| 36 | 55 | ||
| 37 | void np_add_name(struct name_list **list, const char *name); | 56 | void np_add_name(struct name_list **list, const char *name); |
| @@ -39,10 +58,12 @@ bool np_find_name(struct name_list *list, const char *name); | |||
| 39 | bool np_seen_name(struct name_list *list, const char *name); | 58 | bool np_seen_name(struct name_list *list, const char *name); |
| 40 | int np_add_regex(struct regex_list **list, const char *regex, int cflags); | 59 | int np_add_regex(struct regex_list **list, const char *regex, int cflags); |
| 41 | bool np_find_regmatch(struct regex_list *list, const char *name); | 60 | bool np_find_regmatch(struct regex_list *list, const char *name); |
| 61 | |||
| 42 | struct parameter_list *np_add_parameter(struct parameter_list **list, const char *name); | 62 | struct parameter_list *np_add_parameter(struct parameter_list **list, const char *name); |
| 43 | struct parameter_list *np_find_parameter(struct parameter_list *list, const char *name); | 63 | struct parameter_list *np_find_parameter(struct parameter_list *list, const char *name); |
| 44 | struct parameter_list *np_del_parameter(struct parameter_list *item, struct parameter_list *prev); | 64 | struct parameter_list *np_del_parameter(struct parameter_list *item, struct parameter_list *prev); |
| 65 | struct parameter_list parameter_list_init(const char *); | ||
| 45 | 66 | ||
| 46 | int search_parameter_list(struct parameter_list *list, const char *name); | 67 | int search_parameter_list(struct parameter_list *list, const char *name); |
| 47 | void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, bool exact); | 68 | void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, bool exact); |
| 48 | bool np_regex_match_mount_entry(struct mount_entry *me, regex_t *re); | 69 | bool np_regex_match_mount_entry(struct mount_entry *, regex_t *); |
