diff options
Diffstat (limited to 'lib/utils_disk.c')
-rw-r--r-- | lib/utils_disk.c | 119 |
1 files changed, 84 insertions, 35 deletions
diff --git a/lib/utils_disk.c b/lib/utils_disk.c index 582d3ea..483be06 100644 --- a/lib/utils_disk.c +++ b/lib/utils_disk.c | |||
@@ -1,34 +1,35 @@ | |||
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-2007 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 | 33 | ||
33 | void | 34 | void |
34 | np_add_name (struct name_list **list, const char *name) | 35 | np_add_name (struct name_list **list, const char *name) |
@@ -40,6 +41,42 @@ np_add_name (struct name_list **list, const char *name) | |||
40 | *list = new_entry; | 41 | *list = new_entry; |
41 | } | 42 | } |
42 | 43 | ||
44 | /* @brief Initialises a new regex at the begin of list via regcomp(3) | ||
45 | * | ||
46 | * @details if the regex fails to compile the error code of regcomp(3) is returned | ||
47 | * and list is not modified, otherwise list is modified to point to the new | ||
48 | * element | ||
49 | * @param list Pointer to a linked list of regex_list elements | ||
50 | * @param regex the string containing the regex which should be inserted into the list | ||
51 | * @param clags the cflags parameter for regcomp(3) | ||
52 | */ | ||
53 | int | ||
54 | np_add_regex (struct regex_list **list, const char *regex, int cflags) | ||
55 | { | ||
56 | struct regex_list *new_entry = (struct regex_list *) malloc (sizeof *new_entry); | ||
57 | |||
58 | if (new_entry == NULL) { | ||
59 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), | ||
60 | strerror(errno)); | ||
61 | } | ||
62 | |||
63 | int regcomp_result = regcomp(&new_entry->regex, regex, cflags); | ||
64 | |||
65 | if (!regcomp_result) { | ||
66 | // regcomp succeeded | ||
67 | new_entry->next = *list; | ||
68 | *list = new_entry; | ||
69 | |||
70 | return 0; | ||
71 | } else { | ||
72 | // regcomp failed | ||
73 | free(new_entry); | ||
74 | |||
75 | return regcomp_result; | ||
76 | } | ||
77 | |||
78 | } | ||
79 | |||
43 | /* Initialises a new parameter at the end of list */ | 80 | /* Initialises a new parameter at the end of list */ |
44 | struct parameter_list * | 81 | struct parameter_list * |
45 | np_add_parameter(struct parameter_list **list, const char *name) | 82 | np_add_parameter(struct parameter_list **list, const char *name) |
@@ -61,7 +98,7 @@ np_add_parameter(struct parameter_list **list, const char *name) | |||
61 | new_path->freeinodes_percent = NULL; | 98 | new_path->freeinodes_percent = NULL; |
62 | new_path->group = NULL; | 99 | new_path->group = NULL; |
63 | new_path->dfree_pct = -1; | 100 | new_path->dfree_pct = -1; |
64 | new_path->dused_pct = -1; | 101 | new_path->dused_pct = -1; |
65 | new_path->total = 0; | 102 | new_path->total = 0; |
66 | new_path->available = 0; | 103 | new_path->available = 0; |
67 | new_path->available_to_root = 0; | 104 | new_path->available_to_root = 0; |
@@ -133,9 +170,7 @@ np_find_parameter(struct parameter_list *list, const char *name) | |||
133 | return NULL; | 170 | return NULL; |
134 | } | 171 | } |
135 | 172 | ||
136 | void | 173 | void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, bool exact) { |
137 | np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, int exact) | ||
138 | { | ||
139 | struct parameter_list *d; | 174 | struct parameter_list *d; |
140 | for (d = desired; d; d= d->name_next) { | 175 | for (d = desired; d; d= d->name_next) { |
141 | if (! d->best_match) { | 176 | if (! d->best_match) { |
@@ -158,9 +193,9 @@ np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list | |||
158 | if (! best_match) { | 193 | if (! best_match) { |
159 | for (me = mount_list; me; me = me->me_next) { | 194 | for (me = mount_list; me; me = me->me_next) { |
160 | size_t len = strlen (me->me_mountdir); | 195 | size_t len = strlen (me->me_mountdir); |
161 | if ((exact == FALSE && (best_match_len <= len && len <= name_len && | 196 | if ((!exact && (best_match_len <= len && len <= name_len && |
162 | (len == 1 || strncmp (me->me_mountdir, d->name, len) == 0))) | 197 | (len == 1 || strncmp (me->me_mountdir, d->name, len) == 0))) |
163 | || (exact == TRUE && strcmp(me->me_mountdir, d->name)==0)) | 198 | || (exact && strcmp(me->me_mountdir, d->name)==0)) |
164 | { | 199 | { |
165 | 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) { |
166 | best_match = me; | 201 | best_match = me; |
@@ -179,43 +214,57 @@ np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list | |||
179 | } | 214 | } |
180 | } | 215 | } |
181 | 216 | ||
182 | /* Returns TRUE if name is in list */ | 217 | /* Returns true if name is in list */ |
183 | int | 218 | bool np_find_name (struct name_list *list, const char *name) { |
184 | np_find_name (struct name_list *list, const char *name) | ||
185 | { | ||
186 | const struct name_list *n; | 219 | const struct name_list *n; |
187 | 220 | ||
188 | if (list == NULL || name == NULL) { | 221 | if (list == NULL || name == NULL) { |
189 | return FALSE; | 222 | return false; |
190 | } | 223 | } |
191 | for (n = list; n; n = n->next) { | 224 | for (n = list; n; n = n->next) { |
192 | if (!strcmp(name, n->name)) { | 225 | if (!strcmp(name, n->name)) { |
193 | return TRUE; | 226 | return true; |
194 | } | 227 | } |
195 | } | 228 | } |
196 | return FALSE; | 229 | return false; |
197 | } | 230 | } |
198 | 231 | ||
199 | int | 232 | /* Returns true if name is in list */ |
200 | np_seen_name(struct name_list *list, const char *name) | 233 | bool np_find_regmatch (struct regex_list *list, const char *name) { |
201 | { | 234 | int len; |
235 | regmatch_t m; | ||
236 | |||
237 | if (name == NULL) { | ||
238 | return false; | ||
239 | } | ||
240 | |||
241 | len = strlen(name); | ||
242 | |||
243 | for (; list; list = list->next) { | ||
244 | /* Emulate a full match as if surrounded with ^( )$ | ||
245 | 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) { | ||
247 | return true; | ||
248 | } | ||
249 | } | ||
250 | |||
251 | return false; | ||
252 | } | ||
253 | |||
254 | bool np_seen_name(struct name_list *list, const char *name) { | ||
202 | const struct name_list *s; | 255 | const struct name_list *s; |
203 | for (s = list; s; s=s->next) { | 256 | for (s = list; s; s=s->next) { |
204 | if (!strcmp(s->name, name)) { | 257 | if (!strcmp(s->name, name)) { |
205 | return TRUE; | 258 | return true; |
206 | } | 259 | } |
207 | } | 260 | } |
208 | return FALSE; | 261 | return false; |
209 | } | 262 | } |
210 | 263 | ||
211 | int | 264 | bool np_regex_match_mount_entry (struct mount_entry* me, regex_t* re) { |
212 | np_regex_match_mount_entry (struct mount_entry* me, regex_t* re) | ||
213 | { | ||
214 | if (regexec(re, me->me_devname, (size_t) 0, NULL, 0) == 0 || | 265 | if (regexec(re, me->me_devname, (size_t) 0, NULL, 0) == 0 || |
215 | regexec(re, me->me_mountdir, (size_t) 0, NULL, 0) == 0 ) { | 266 | regexec(re, me->me_mountdir, (size_t) 0, NULL, 0) == 0 ) { |
216 | return TRUE; | 267 | return true; |
217 | } else { | ||
218 | return FALSE; | ||
219 | } | 268 | } |
269 | return false; | ||
220 | } | 270 | } |
221 | |||