1 /*****************************************************************************
3 * Library for check_disk
6 * Copyright (c) 1999-2007 Monitoring Plugins Development Team
10 * This file contains utilities for check_disk. These are tested by libtap
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.
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.
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/>.
27 *****************************************************************************/
30 #include "utils_disk.h"
31 #include "gl/fsusage.h"
35 np_add_name (struct name_list
**list
, const char *name
)
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
;
44 /* @brief Initialises a new regex at the begin of list via regcomp(3)
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
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)
54 np_add_regex (struct regex_list
**list
, const char *regex
, int cflags
)
56 struct regex_list
*new_entry
= (struct regex_list
*) malloc (sizeof *new_entry
);
58 if (new_entry
== NULL
) {
59 die(STATE_UNKNOWN
, _("Cannot allocate memory: %s"),
63 int regcomp_result
= regcomp(&new_entry
->regex
, regex
, cflags
);
65 if (!regcomp_result
) {
67 new_entry
->next
= *list
;
75 return regcomp_result
;
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
)
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;
103 new_path
->available
= 0;
104 new_path
->available_to_root
= 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;
116 strcpy(new_path
->name
, name
);
118 if (current
== NULL
) {
120 new_path
->name_prev
= NULL
;
122 while (current
->name_next
) {
123 current
= current
->name_next
;
125 current
->name_next
= new_path
;
126 new_path
->name_prev
= current
;
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
)
138 struct parameter_list
*next
;
141 next
= item
->name_next
;
146 next
->name_prev
= prev
;
149 prev
->name_next
= next
;
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
)
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
))
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
;
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) {
192 /* set best match by directory name if no match was found by devname */
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))
200 if (get_fs_usage(me
->me_mountdir
, me
->me_devname
, &fsp
) >= 0) {
202 best_match_len
= len
;
209 d
->best_match
= best_match
;
211 d
->best_match
= NULL
; /* Not sure why this is needed as it should be null on initialisation */
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
;
221 if (list
== NULL
|| name
== NULL
) {
224 for (n
= list
; n
; n
= n
->next
) {
225 if (!strcmp(name
, n
->name
)) {
232 /* Returns true if name is in list */
233 bool np_find_regmatch (struct regex_list
*list
, const char *name
) {
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
) {
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
)) {
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 ) {