diff options
Diffstat (limited to 'lib/utils_disk.c')
-rw-r--r-- | lib/utils_disk.c | 270 |
1 files changed, 0 insertions, 270 deletions
diff --git a/lib/utils_disk.c b/lib/utils_disk.c deleted file mode 100644 index 483be06d..00000000 --- a/lib/utils_disk.c +++ /dev/null | |||
@@ -1,270 +0,0 @@ | |||
1 | /***************************************************************************** | ||
2 | * | ||
3 | * Library for check_disk | ||
4 | * | ||
5 | * License: GPL | ||
6 | * Copyright (c) 1999-2007 Monitoring Plugins Development Team | ||
7 | * | ||
8 | * Description: | ||
9 | * | ||
10 | * This file contains utilities for check_disk. These are tested by libtap | ||
11 | * | ||
12 | * | ||
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 | ||
15 | * the Free Software Foundation, either version 3 of the License, or | ||
16 | * (at your option) any later version. | ||
17 | * | ||
18 | * This program is distributed in the hope that it will be useful, | ||
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
21 | * GNU General Public License for more details. | ||
22 | * | ||
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/>. | ||
25 | * | ||
26 | * | ||
27 | *****************************************************************************/ | ||
28 | |||
29 | #include "common.h" | ||
30 | #include "utils_disk.h" | ||
31 | #include "gl/fsusage.h" | ||
32 | #include <string.h> | ||
33 | |||
34 | void | ||
35 | np_add_name (struct name_list **list, const char *name) | ||
36 | { | ||
37 | struct name_list *new_entry; | ||
38 | new_entry = (struct name_list *) malloc (sizeof *new_entry); | ||
39 | new_entry->name = (char *) name; | ||
40 | new_entry->next = *list; | ||
41 | *list = new_entry; | ||
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 | |||
80 | /* Initialises a new parameter at the end of list */ | ||
81 | struct parameter_list * | ||
82 | np_add_parameter(struct parameter_list **list, const char *name) | ||
83 | { | ||
84 | struct parameter_list *current = *list; | ||
85 | struct parameter_list *new_path; | ||
86 | new_path = (struct parameter_list *) malloc (sizeof *new_path); | ||
87 | new_path->name = (char *) malloc(strlen(name) + 1); | ||
88 | new_path->best_match = NULL; | ||
89 | new_path->name_next = NULL; | ||
90 | new_path->name_prev = NULL; | ||
91 | new_path->freespace_bytes = NULL; | ||
92 | new_path->freespace_units = NULL; | ||
93 | new_path->freespace_percent = NULL; | ||
94 | new_path->usedspace_bytes = NULL; | ||
95 | new_path->usedspace_units = NULL; | ||
96 | new_path->usedspace_percent = NULL; | ||
97 | new_path->usedinodes_percent = NULL; | ||
98 | new_path->freeinodes_percent = NULL; | ||
99 | new_path->group = NULL; | ||
100 | new_path->dfree_pct = -1; | ||
101 | new_path->dused_pct = -1; | ||
102 | new_path->total = 0; | ||
103 | new_path->available = 0; | ||
104 | new_path->available_to_root = 0; | ||
105 | new_path->used = 0; | ||
106 | new_path->dused_units = 0; | ||
107 | new_path->dfree_units = 0; | ||
108 | new_path->dtotal_units = 0; | ||
109 | new_path->inodes_total = 0; | ||
110 | new_path->inodes_free = 0; | ||
111 | new_path->inodes_free_to_root = 0; | ||
112 | new_path->inodes_used = 0; | ||
113 | new_path->dused_inodes_percent = 0; | ||
114 | new_path->dfree_inodes_percent = 0; | ||
115 | |||
116 | strcpy(new_path->name, name); | ||
117 | |||
118 | if (current == NULL) { | ||
119 | *list = new_path; | ||
120 | new_path->name_prev = NULL; | ||
121 | } else { | ||
122 | while (current->name_next) { | ||
123 | current = current->name_next; | ||
124 | } | ||
125 | current->name_next = new_path; | ||
126 | new_path->name_prev = current; | ||
127 | } | ||
128 | return new_path; | ||
129 | } | ||
130 | |||
131 | /* Delete a given parameter from list and return pointer to next element*/ | ||
132 | struct parameter_list * | ||
133 | np_del_parameter(struct parameter_list *item, struct parameter_list *prev) | ||
134 | { | ||
135 | if (item == NULL) { | ||
136 | return NULL; | ||
137 | } | ||
138 | struct parameter_list *next; | ||
139 | |||
140 | if (item->name_next) | ||
141 | next = item->name_next; | ||
142 | else | ||
143 | next = NULL; | ||
144 | |||
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 | |||
159 | |||
160 | /* returns a pointer to the struct found in the list */ | ||
161 | struct parameter_list * | ||
162 | np_find_parameter(struct parameter_list *list, const char *name) | ||
163 | { | ||
164 | struct parameter_list *temp_list; | ||
165 | for (temp_list = list; temp_list; temp_list = temp_list->name_next) { | ||
166 | if (! strcmp(temp_list->name, name)) | ||
167 | return temp_list; | ||
168 | } | ||
169 | |||
170 | return NULL; | ||
171 | } | ||
172 | |||
173 | void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, bool exact) { | ||
174 | struct parameter_list *d; | ||
175 | for (d = desired; d; d= d->name_next) { | ||
176 | if (! d->best_match) { | ||
177 | struct mount_entry *me; | ||
178 | size_t name_len = strlen(d->name); | ||
179 | size_t best_match_len = 0; | ||
180 | struct mount_entry *best_match = NULL; | ||
181 | struct fs_usage fsp; | ||
182 | |||
183 | /* set best match if path name exactly matches a mounted device name */ | ||
184 | for (me = mount_list; me; me = me->me_next) { | ||
185 | if (strcmp(me->me_devname, d->name)==0) { | ||
186 | if (get_fs_usage(me->me_mountdir, me->me_devname, &fsp) >= 0) { | ||
187 | best_match = me; | ||
188 | } | ||
189 | } | ||
190 | } | ||
191 | |||
192 | /* set best match by directory name if no match was found by devname */ | ||
193 | if (! best_match) { | ||
194 | for (me = mount_list; me; me = me->me_next) { | ||
195 | size_t len = strlen (me->me_mountdir); | ||
196 | if ((!exact && (best_match_len <= len && len <= name_len && | ||
197 | (len == 1 || strncmp (me->me_mountdir, d->name, len) == 0))) | ||
198 | || (exact && strcmp(me->me_mountdir, d->name)==0)) | ||
199 | { | ||
200 | if (get_fs_usage(me->me_mountdir, me->me_devname, &fsp) >= 0) { | ||
201 | best_match = me; | ||
202 | best_match_len = len; | ||
203 | } | ||
204 | } | ||
205 | } | ||
206 | } | ||
207 | |||
208 | if (best_match) { | ||
209 | d->best_match = best_match; | ||
210 | } else { | ||
211 | d->best_match = NULL; /* Not sure why this is needed as it should be null on initialisation */ | ||
212 | } | ||
213 | } | ||
214 | } | ||
215 | } | ||
216 | |||
217 | /* Returns true if name is in list */ | ||
218 | bool np_find_name (struct name_list *list, const char *name) { | ||
219 | const struct name_list *n; | ||
220 | |||
221 | if (list == NULL || name == NULL) { | ||
222 | return false; | ||
223 | } | ||
224 | for (n = list; n; n = n->next) { | ||
225 | if (!strcmp(name, n->name)) { | ||
226 | return true; | ||
227 | } | ||
228 | } | ||
229 | return false; | ||
230 | } | ||
231 | |||
232 | /* Returns true if name is in list */ | ||
233 | bool np_find_regmatch (struct regex_list *list, const char *name) { | ||
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) { | ||
255 | const struct name_list *s; | ||
256 | for (s = list; s; s=s->next) { | ||
257 | if (!strcmp(s->name, name)) { | ||
258 | return true; | ||
259 | } | ||
260 | } | ||
261 | return false; | ||
262 | } | ||
263 | |||
264 | 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 || | ||
266 | regexec(re, me->me_mountdir, (size_t) 0, NULL, 0) == 0 ) { | ||
267 | return true; | ||
268 | } | ||
269 | return false; | ||
270 | } | ||