1 /*****************************************************************************
2 *
3 * Library for check_disk
4 *
5 * License: GPL
6 * Copyright (c) 1999-2007 Nagios 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 *****************************************************************************/
29 #include "common.h"
30 #include "utils_disk.h"
32 void
33 np_add_name (struct name_list **list, const char *name)
34 {
35 struct name_list *new_entry;
36 new_entry = (struct name_list *) malloc (sizeof *new_entry);
37 new_entry->name = (char *) name;
38 new_entry->next = *list;
39 *list = new_entry;
40 }
42 /* Initialises a new parameter at the end of list */
43 struct parameter_list *
44 np_add_parameter(struct parameter_list **list, const char *name)
45 {
46 struct parameter_list *current = *list;
47 struct parameter_list *new_path;
48 new_path = (struct parameter_list *) malloc (sizeof *new_path);
49 new_path->name = (char *) name;
50 new_path->best_match = NULL;
51 new_path->name_next = NULL;
52 new_path->freespace_bytes = NULL;
53 new_path->freespace_units = NULL;
54 new_path->freespace_percent = NULL;
55 new_path->usedspace_bytes = NULL;
56 new_path->usedspace_units = NULL;
57 new_path->usedspace_percent = NULL;
58 new_path->usedinodes_percent = NULL;
59 new_path->freeinodes_percent = NULL;
60 new_path->group = NULL;
61 new_path->dfree_pct = -1;
62 new_path->dused_pct = -1;
63 new_path->total = 0;
64 new_path->available = 0;
65 new_path->available_to_root = 0;
66 new_path->used = 0;
67 new_path->dused_units = 0;
68 new_path->dfree_units = 0;
69 new_path->dtotal_units = 0;
70 new_path->inodes_total = 0;
71 new_path->inodes_free = 0;
72 new_path->dused_inodes_percent = 0;
73 new_path->dfree_inodes_percent = 0;
75 if (current == NULL) {
76 *list = new_path;
77 } else {
78 while (current->name_next) {
79 current = current->name_next;
80 }
81 current->name_next = new_path;
82 }
83 return new_path;
84 }
86 /* Delete a given parameter from list and return pointer to next element*/
87 struct parameter_list *
88 np_del_parameter(struct parameter_list *item, struct parameter_list *prev)
89 {
90 struct parameter_list *next;
92 if (item->name_next)
93 next = item->name_next;
94 else
95 next = NULL;
97 free(item);
98 if (prev)
99 prev->name_next = next;
101 return next;
102 }
105 /* returns a pointer to the struct found in the list */
106 struct parameter_list *
107 np_find_parameter(struct parameter_list *list, const char *name)
108 {
109 struct parameter_list *temp_list;
110 for (temp_list = list; temp_list; temp_list = temp_list->name_next) {
111 if (! strcmp(temp_list->name, name))
112 return temp_list;
113 }
115 return NULL;
116 }
118 void
119 np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, int exact)
120 {
121 struct parameter_list *d;
122 for (d = desired; d; d= d->name_next) {
123 if (! d->best_match) {
124 struct mount_entry *me;
125 size_t name_len = strlen(d->name);
126 size_t best_match_len = 0;
127 struct mount_entry *best_match = NULL;
129 /* set best match if path name exactly matches a mounted device name */
130 for (me = mount_list; me; me = me->me_next) {
131 if (strcmp(me->me_devname, d->name)==0)
132 best_match = me;
133 }
135 /* set best match by directory name if no match was found by devname */
136 if (! best_match) {
137 for (me = mount_list; me; me = me->me_next) {
138 size_t len = strlen (me->me_mountdir);
139 if ((exact == FALSE && (best_match_len <= len && len <= name_len &&
140 (len == 1 || strncmp (me->me_mountdir, d->name, len) == 0)))
141 || (exact == TRUE && strcmp(me->me_mountdir, d->name)==0))
142 {
143 best_match = me;
144 best_match_len = len;
145 }
146 }
147 }
149 if (best_match) {
150 d->best_match = best_match;
151 } else {
152 d->best_match = NULL; /* Not sure why this is needed as it should be null on initialisation */
153 }
154 }
155 }
156 }
158 /* Returns TRUE if name is in list */
159 int
160 np_find_name (struct name_list *list, const char *name)
161 {
162 const struct name_list *n;
164 if (list == NULL || name == NULL) {
165 return FALSE;
166 }
167 for (n = list; n; n = n->next) {
168 if (!strcmp(name, n->name)) {
169 return TRUE;
170 }
171 }
172 return FALSE;
173 }
175 int
176 np_seen_name(struct name_list *list, const char *name)
177 {
178 const struct name_list *s;
179 for (s = list; s; s=s->next) {
180 if (!strcmp(s->name, name)) {
181 return TRUE;
182 }
183 }
184 return FALSE;
185 }
187 int
188 np_regex_match_mount_entry (struct mount_entry* me, regex_t* re)
189 {
190 if (regexec(re, me->me_devname, (size_t) 0, NULL, 0) == 0 ||
191 regexec(re, me->me_mountdir, (size_t) 0, NULL, 0) == 0 ) {
192 return TRUE;
193 } else {
194 return FALSE;
195 }
196 }