diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2024-10-31 15:27:01 +0100 |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2024-10-31 15:27:01 +0100 |
commit | fa15fdcf5dc2d40aba2f8520108e552b73b1df2b (patch) | |
tree | d51d62db024b317091f42d9ee540da7371e9d170 /lib/utils_disk.c | |
parent | 0fd0421052fed1972ecbdfdabecba5a616eaa109 (diff) | |
parent | 87eb2bef1ee2a6a42793437b2f5d63f41b1e1806 (diff) | |
download | monitoring-plugins-fa15fdc.tar.gz |
Merge branch 'master' into fix/check_ssh-variable-stuff
Diffstat (limited to 'lib/utils_disk.c')
-rw-r--r-- | lib/utils_disk.c | 397 |
1 files changed, 191 insertions, 206 deletions
diff --git a/lib/utils_disk.c b/lib/utils_disk.c index 483be06d..2b761f5e 100644 --- a/lib/utils_disk.c +++ b/lib/utils_disk.c | |||
@@ -1,44 +1,42 @@ | |||
1 | /***************************************************************************** | 1 | /***************************************************************************** |
2 | * | 2 | * |
3 | * Library for check_disk | 3 | * Library for check_disk |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 1999-2007 Monitoring Plugins Development Team | 6 | * Copyright (c) 1999-2024 Monitoring Plugins Development Team |
7 | * | 7 | * |
8 | * Description: | 8 | * Description: |
9 | * | 9 | * |
10 | * This file contains utilities for check_disk. These are tested by libtap | 10 | * This file contains utilities for check_disk. These are tested by libtap |
11 | * | 11 | * |
12 | * | 12 | * |
13 | * This program is free software: you can redistribute it and/or modify | 13 | * This program is free software: you can redistribute it and/or modify |
14 | * it under the terms of the GNU General Public License as published by | 14 | * it under the terms of the GNU General Public License as published by |
15 | * the Free Software Foundation, either version 3 of the License, or | 15 | * the Free Software Foundation, either version 3 of the License, or |
16 | * (at your option) any later version. | 16 | * (at your option) any later version. |
17 | * | 17 | * |
18 | * This program is distributed in the hope that it will be useful, | 18 | * This program is distributed in the hope that it will be useful, |
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
21 | * GNU General Public License for more details. | 21 | * GNU General Public License for more details. |
22 | * | 22 | * |
23 | * You should have received a copy of the GNU General Public License | 23 | * You should have received a copy of the GNU General Public License |
24 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 24 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
25 | * | 25 | * |
26 | * | 26 | * |
27 | *****************************************************************************/ | 27 | *****************************************************************************/ |
28 | 28 | ||
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 <string.h> | 32 | #include <string.h> |
33 | 33 | ||
34 | void | 34 | void np_add_name(struct name_list **list, const char *name) { |
35 | np_add_name (struct name_list **list, const char *name) | 35 | struct name_list *new_entry; |
36 | { | 36 | new_entry = (struct name_list *)malloc(sizeof *new_entry); |
37 | struct name_list *new_entry; | 37 | new_entry->name = (char *)name; |
38 | new_entry = (struct name_list *) malloc (sizeof *new_entry); | 38 | new_entry->next = *list; |
39 | new_entry->name = (char *) name; | 39 | *list = new_entry; |
40 | new_entry->next = *list; | ||
41 | *list = new_entry; | ||
42 | } | 40 | } |
43 | 41 | ||
44 | /* @brief Initialises a new regex at the begin of list via regcomp(3) | 42 | /* @brief Initialises a new regex at the begin of list via regcomp(3) |
@@ -50,17 +48,14 @@ np_add_name (struct name_list **list, const char *name) | |||
50 | * @param regex the string containing the regex which should be inserted into the list | 48 | * @param regex the string containing the regex which should be inserted into the list |
51 | * @param clags the cflags parameter for regcomp(3) | 49 | * @param clags the cflags parameter for regcomp(3) |
52 | */ | 50 | */ |
53 | int | 51 | int np_add_regex(struct regex_list **list, const char *regex, int cflags) { |
54 | np_add_regex (struct regex_list **list, const char *regex, int cflags) | 52 | struct regex_list *new_entry = (struct regex_list *)malloc(sizeof *new_entry); |
55 | { | ||
56 | struct regex_list *new_entry = (struct regex_list *) malloc (sizeof *new_entry); | ||
57 | 53 | ||
58 | if (new_entry == NULL) { | 54 | if (new_entry == NULL) { |
59 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), | 55 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); |
60 | strerror(errno)); | ||
61 | } | 56 | } |
62 | 57 | ||
63 | int regcomp_result = regcomp(&new_entry->regex, regex, cflags); | 58 | int regcomp_result = regcomp(&new_entry->regex, regex, cflags); |
64 | 59 | ||
65 | if (!regcomp_result) { | 60 | if (!regcomp_result) { |
66 | // regcomp succeeded | 61 | // regcomp succeeded |
@@ -74,197 +69,187 @@ np_add_regex (struct regex_list **list, const char *regex, int cflags) | |||
74 | 69 | ||
75 | return regcomp_result; | 70 | return regcomp_result; |
76 | } | 71 | } |
77 | |||
78 | } | 72 | } |
79 | 73 | ||
80 | /* Initialises a new parameter at the end of list */ | 74 | /* Initialises a new parameter at the end of list */ |
81 | struct parameter_list * | 75 | struct parameter_list *np_add_parameter(struct parameter_list **list, const char *name) { |
82 | np_add_parameter(struct parameter_list **list, const char *name) | 76 | struct parameter_list *current = *list; |
83 | { | 77 | struct parameter_list *new_path; |
84 | struct parameter_list *current = *list; | 78 | new_path = (struct parameter_list *)malloc(sizeof *new_path); |
85 | struct parameter_list *new_path; | 79 | new_path->name = (char *)malloc(strlen(name) + 1); |
86 | new_path = (struct parameter_list *) malloc (sizeof *new_path); | 80 | new_path->best_match = NULL; |
87 | new_path->name = (char *) malloc(strlen(name) + 1); | 81 | new_path->name_next = NULL; |
88 | new_path->best_match = NULL; | 82 | new_path->name_prev = NULL; |
89 | new_path->name_next = NULL; | 83 | new_path->freespace_bytes = NULL; |
90 | new_path->name_prev = NULL; | 84 | new_path->freespace_units = NULL; |
91 | new_path->freespace_bytes = NULL; | 85 | new_path->freespace_percent = NULL; |
92 | new_path->freespace_units = NULL; | 86 | new_path->usedspace_bytes = NULL; |
93 | new_path->freespace_percent = NULL; | 87 | new_path->usedspace_units = NULL; |
94 | new_path->usedspace_bytes = NULL; | 88 | new_path->usedspace_percent = NULL; |
95 | new_path->usedspace_units = NULL; | 89 | new_path->usedinodes_percent = NULL; |
96 | new_path->usedspace_percent = NULL; | 90 | new_path->freeinodes_percent = NULL; |
97 | new_path->usedinodes_percent = NULL; | 91 | new_path->group = NULL; |
98 | new_path->freeinodes_percent = NULL; | 92 | new_path->dfree_pct = -1; |
99 | new_path->group = NULL; | 93 | new_path->dused_pct = -1; |
100 | new_path->dfree_pct = -1; | 94 | new_path->total = 0; |
101 | new_path->dused_pct = -1; | 95 | new_path->available = 0; |
102 | new_path->total = 0; | 96 | new_path->available_to_root = 0; |
103 | new_path->available = 0; | 97 | new_path->used = 0; |
104 | new_path->available_to_root = 0; | 98 | new_path->dused_units = 0; |
105 | new_path->used = 0; | 99 | new_path->dfree_units = 0; |
106 | new_path->dused_units = 0; | 100 | new_path->dtotal_units = 0; |
107 | new_path->dfree_units = 0; | 101 | new_path->inodes_total = 0; |
108 | new_path->dtotal_units = 0; | 102 | new_path->inodes_free = 0; |
109 | new_path->inodes_total = 0; | 103 | new_path->inodes_free_to_root = 0; |
110 | new_path->inodes_free = 0; | 104 | new_path->inodes_used = 0; |
111 | new_path->inodes_free_to_root = 0; | 105 | new_path->dused_inodes_percent = 0; |
112 | new_path->inodes_used = 0; | 106 | new_path->dfree_inodes_percent = 0; |
113 | new_path->dused_inodes_percent = 0; | 107 | |
114 | new_path->dfree_inodes_percent = 0; | 108 | strcpy(new_path->name, name); |
115 | 109 | ||
116 | strcpy(new_path->name, name); | 110 | if (current == NULL) { |
117 | 111 | *list = new_path; | |
118 | if (current == NULL) { | 112 | new_path->name_prev = NULL; |
119 | *list = new_path; | 113 | } else { |
120 | new_path->name_prev = NULL; | 114 | while (current->name_next) { |
121 | } else { | 115 | current = current->name_next; |
122 | while (current->name_next) { | 116 | } |
123 | current = current->name_next; | 117 | current->name_next = new_path; |
124 | } | 118 | new_path->name_prev = current; |
125 | current->name_next = new_path; | 119 | } |
126 | new_path->name_prev = current; | 120 | return new_path; |
127 | } | ||
128 | return new_path; | ||
129 | } | 121 | } |
130 | 122 | ||
131 | /* Delete a given parameter from list and return pointer to next element*/ | 123 | /* Delete a given parameter from list and return pointer to next element*/ |
132 | struct parameter_list * | 124 | struct parameter_list *np_del_parameter(struct parameter_list *item, struct parameter_list *prev) { |
133 | np_del_parameter(struct parameter_list *item, struct parameter_list *prev) | 125 | if (item == NULL) { |
134 | { | 126 | return NULL; |
135 | if (item == NULL) { | 127 | } |
136 | return NULL; | 128 | struct parameter_list *next; |
137 | } | 129 | |
138 | struct parameter_list *next; | 130 | if (item->name_next) |
139 | 131 | next = item->name_next; | |
140 | if (item->name_next) | 132 | else |
141 | next = item->name_next; | 133 | next = NULL; |
142 | else | 134 | |
143 | next = NULL; | 135 | if (next) |
144 | 136 | next->name_prev = prev; | |
145 | if (next) | ||
146 | next->name_prev = prev; | ||
147 | |||
148 | if (prev) | ||
149 | prev->name_next = next; | ||
150 | |||
151 | if (item->name) { | ||
152 | free(item->name); | ||
153 | } | ||
154 | free(item); | ||
155 | |||
156 | return next; | ||
157 | } | ||
158 | 137 | ||
138 | if (prev) | ||
139 | prev->name_next = next; | ||
140 | |||
141 | if (item->name) { | ||
142 | free(item->name); | ||
143 | } | ||
144 | free(item); | ||
145 | |||
146 | return next; | ||
147 | } | ||
159 | 148 | ||
160 | /* returns a pointer to the struct found in the list */ | 149 | /* returns a pointer to the struct found in the list */ |
161 | struct parameter_list * | 150 | struct parameter_list *np_find_parameter(struct parameter_list *list, const char *name) { |
162 | np_find_parameter(struct parameter_list *list, const char *name) | 151 | struct parameter_list *temp_list; |
163 | { | 152 | for (temp_list = list; temp_list; temp_list = temp_list->name_next) { |
164 | struct parameter_list *temp_list; | 153 | if (!strcmp(temp_list->name, name)) |
165 | for (temp_list = list; temp_list; temp_list = temp_list->name_next) { | 154 | return temp_list; |
166 | if (! strcmp(temp_list->name, name)) | 155 | } |
167 | return temp_list; | 156 | |
168 | } | 157 | return NULL; |
169 | |||
170 | return NULL; | ||
171 | } | 158 | } |
172 | 159 | ||
173 | void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, bool exact) { | 160 | void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, bool exact) { |
174 | struct parameter_list *d; | 161 | struct parameter_list *d; |
175 | for (d = desired; d; d= d->name_next) { | 162 | for (d = desired; d; d = d->name_next) { |
176 | if (! d->best_match) { | 163 | if (!d->best_match) { |
177 | struct mount_entry *me; | 164 | struct mount_entry *me; |
178 | size_t name_len = strlen(d->name); | 165 | size_t name_len = strlen(d->name); |
179 | size_t best_match_len = 0; | 166 | size_t best_match_len = 0; |
180 | struct mount_entry *best_match = NULL; | 167 | struct mount_entry *best_match = NULL; |
181 | struct fs_usage fsp; | 168 | struct fs_usage fsp; |
182 | 169 | ||
183 | /* set best match if path name exactly matches a mounted device name */ | 170 | /* set best match if path name exactly matches a mounted device name */ |
184 | for (me = mount_list; me; me = me->me_next) { | 171 | for (me = mount_list; me; me = me->me_next) { |
185 | if (strcmp(me->me_devname, d->name)==0) { | 172 | if (strcmp(me->me_devname, d->name) == 0) { |
186 | if (get_fs_usage(me->me_mountdir, me->me_devname, &fsp) >= 0) { | 173 | if (get_fs_usage(me->me_mountdir, me->me_devname, &fsp) >= 0) { |
187 | best_match = me; | 174 | best_match = me; |
188 | } | 175 | } |
189 | } | 176 | } |
190 | } | 177 | } |
191 | 178 | ||
192 | /* set best match by directory name if no match was found by devname */ | 179 | /* set best match by directory name if no match was found by devname */ |
193 | if (! best_match) { | 180 | if (!best_match) { |
194 | for (me = mount_list; me; me = me->me_next) { | 181 | for (me = mount_list; me; me = me->me_next) { |
195 | size_t len = strlen (me->me_mountdir); | 182 | size_t len = strlen(me->me_mountdir); |
196 | if ((!exact && (best_match_len <= len && len <= name_len && | 183 | if ((!exact && |
197 | (len == 1 || strncmp (me->me_mountdir, d->name, len) == 0))) | 184 | (best_match_len <= len && len <= name_len && (len == 1 || strncmp(me->me_mountdir, d->name, len) == 0))) || |
198 | || (exact && strcmp(me->me_mountdir, d->name)==0)) | 185 | (exact && strcmp(me->me_mountdir, d->name) == 0)) { |
199 | { | 186 | if (get_fs_usage(me->me_mountdir, me->me_devname, &fsp) >= 0) { |
200 | if (get_fs_usage(me->me_mountdir, me->me_devname, &fsp) >= 0) { | 187 | best_match = me; |
201 | best_match = me; | 188 | best_match_len = len; |
202 | best_match_len = len; | 189 | } |
203 | } | 190 | } |
204 | } | 191 | } |
205 | } | 192 | } |
206 | } | 193 | |
207 | 194 | if (best_match) { | |
208 | if (best_match) { | 195 | d->best_match = best_match; |
209 | d->best_match = best_match; | 196 | } else { |
210 | } else { | 197 | d->best_match = NULL; /* Not sure why this is needed as it should be null on initialisation */ |
211 | d->best_match = NULL; /* Not sure why this is needed as it should be null on initialisation */ | 198 | } |
212 | } | 199 | } |
213 | } | 200 | } |
214 | } | ||
215 | } | 201 | } |
216 | 202 | ||
217 | /* Returns true if name is in list */ | 203 | /* Returns true if name is in list */ |
218 | bool np_find_name (struct name_list *list, const char *name) { | 204 | bool np_find_name(struct name_list *list, const char *name) { |
219 | const struct name_list *n; | 205 | const struct name_list *n; |
220 | 206 | ||
221 | if (list == NULL || name == NULL) { | 207 | if (list == NULL || name == NULL) { |
222 | return false; | 208 | return false; |
223 | } | 209 | } |
224 | for (n = list; n; n = n->next) { | 210 | for (n = list; n; n = n->next) { |
225 | if (!strcmp(name, n->name)) { | 211 | if (!strcmp(name, n->name)) { |
226 | return true; | 212 | return true; |
227 | } | 213 | } |
228 | } | 214 | } |
229 | return false; | 215 | return false; |
230 | } | 216 | } |
231 | 217 | ||
232 | /* Returns true if name is in list */ | 218 | /* Returns true if name is in list */ |
233 | bool np_find_regmatch (struct regex_list *list, const char *name) { | 219 | bool np_find_regmatch(struct regex_list *list, const char *name) { |
234 | int len; | 220 | int len; |
235 | regmatch_t m; | 221 | regmatch_t m; |
236 | 222 | ||
237 | if (name == NULL) { | 223 | if (name == NULL) { |
238 | return false; | 224 | return false; |
239 | } | 225 | } |
240 | 226 | ||
241 | len = strlen(name); | 227 | len = strlen(name); |
242 | 228 | ||
243 | for (; list; list = list->next) { | 229 | for (; list; list = list->next) { |
244 | /* Emulate a full match as if surrounded with ^( )$ | 230 | /* Emulate a full match as if surrounded with ^( )$ |
245 | by checking whether the match spans the whole name */ | 231 | by checking whether the match spans the whole name */ |
246 | if (!regexec(&list->regex, name, 1, &m, 0) && m.rm_so == 0 && m.rm_eo == len) { | 232 | if (!regexec(&list->regex, name, 1, &m, 0) && m.rm_so == 0 && m.rm_eo == len) { |
247 | return true; | 233 | return true; |
248 | } | 234 | } |
249 | } | 235 | } |
250 | 236 | ||
251 | return false; | 237 | return false; |
252 | } | 238 | } |
253 | 239 | ||
254 | bool np_seen_name(struct name_list *list, const char *name) { | 240 | bool np_seen_name(struct name_list *list, const char *name) { |
255 | const struct name_list *s; | 241 | const struct name_list *s; |
256 | for (s = list; s; s=s->next) { | 242 | for (s = list; s; s = s->next) { |
257 | if (!strcmp(s->name, name)) { | 243 | if (!strcmp(s->name, name)) { |
258 | return true; | 244 | return true; |
259 | } | 245 | } |
260 | } | 246 | } |
261 | return false; | 247 | return false; |
262 | } | 248 | } |
263 | 249 | ||
264 | bool np_regex_match_mount_entry (struct mount_entry* me, regex_t* re) { | 250 | bool np_regex_match_mount_entry(struct mount_entry *me, regex_t *re) { |
265 | if (regexec(re, me->me_devname, (size_t) 0, NULL, 0) == 0 || | 251 | if (regexec(re, me->me_devname, (size_t)0, NULL, 0) == 0 || regexec(re, me->me_mountdir, (size_t)0, NULL, 0) == 0) { |
266 | regexec(re, me->me_mountdir, (size_t) 0, NULL, 0) == 0 ) { | 252 | return true; |
267 | return true; | 253 | } |
268 | } | ||
269 | return false; | 254 | return false; |
270 | } | 255 | } |