Debian Codegraph @

src/df.c

raw
1 /**
2 * collectd - src/df.c
3 * Copyright (C) 2005-2009 Florian octo Forster
4 * Copyright (C) 2009 Paul Sadauskas
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Florian octo Forster <octo at collectd.org>
21 * Paul Sadauskas <psadauskas at gmail.com>
22 **/
23
24 #include "collectd.h"
25
26 #include "plugin.h"
27 #include "utils/common/common.h"
28 #include "utils/ignorelist/ignorelist.h"
29 #include "utils/mount/mount.h"
30
31 #if HAVE_STATVFS
32 #if HAVE_SYS_STATVFS_H
33 #include <sys/statvfs.h>
34 #endif
35 #define STATANYFS statvfs
36 #define STATANYFS_STR "statvfs"
37 #define BLOCKSIZE(s) ((s).f_frsize ? (s).f_frsize : (s).f_bsize)
38 #elif HAVE_STATFS
39 #if HAVE_SYS_STATFS_H
40 #include <sys/statfs.h>
41 #endif
42 #define STATANYFS statfs
43 #define STATANYFS_STR "statfs"
44 #define BLOCKSIZE(s) (s).f_bsize
45 #else
46 #error "No applicable input method."
47 #endif
48
49 static const char *config_keys[] = {
50 "Device", "MountPoint", "FSType",
51 "IgnoreSelected", "ReportByDevice", "ReportInodes",
52 "ValuesAbsolute", "ValuesPercentage", "LogOnce"};
53 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
54
55 static ignorelist_t *il_device;
56 static ignorelist_t *il_mountpoint;
57 static ignorelist_t *il_fstype;
58 static ignorelist_t *il_errors;
59
60 static bool by_device;
61 static bool report_inodes;
62 static bool values_absolute = true;
63 static bool values_percentage;
64 static bool log_once;
65
66 static int df_init(void) {
67 if (il_device == NULL)
68 il_device = ignorelist_create(1);
69 if (il_mountpoint == NULL)
70 il_mountpoint = ignorelist_create(1);
71 if (il_fstype == NULL)
72 il_fstype = ignorelist_create(1);
73 if (il_errors == NULL)
74 il_errors = ignorelist_create(1);
75
76 return 0;
77 }
78
79 static int df_config(const char *key, const char *value) {
80 df_init();
81
82 if (strcasecmp(key, "Device") == 0) {
83 if (ignorelist_add(il_device, value))
84 return 1;
85 return 0;
86 } else if (strcasecmp(key, "MountPoint") == 0) {
87 if (ignorelist_add(il_mountpoint, value))
88 return 1;
89 return 0;
90 } else if (strcasecmp(key, "FSType") == 0) {
91 if (ignorelist_add(il_fstype, value))
92 return 1;
93 return 0;
94 } else if (strcasecmp(key, "IgnoreSelected") == 0) {
95 if (IS_TRUE(value)) {
96 ignorelist_set_invert(il_device, 0);
97 ignorelist_set_invert(il_mountpoint, 0);
98 ignorelist_set_invert(il_fstype, 0);
99 } else {
100 ignorelist_set_invert(il_device, 1);
101 ignorelist_set_invert(il_mountpoint, 1);
102 ignorelist_set_invert(il_fstype, 1);
103 }
104 return 0;
105 } else if (strcasecmp(key, "ReportByDevice") == 0) {
106 if (IS_TRUE(value))
107 by_device = true;
108
109 return 0;
110 } else if (strcasecmp(key, "ReportInodes") == 0) {
111 if (IS_TRUE(value))
112 report_inodes = true;
113 else
114 report_inodes = false;
115
116 return 0;
117 } else if (strcasecmp(key, "ValuesAbsolute") == 0) {
118 if (IS_TRUE(value))
119 values_absolute = true;
120 else
121 values_absolute = false;
122
123 return 0;
124 } else if (strcasecmp(key, "ValuesPercentage") == 0) {
125 if (IS_TRUE(value))
126 values_percentage = true;
127 else
128 values_percentage = false;
129
130 return 0;
131 } else if (strcasecmp(key, "LogOnce") == 0) {
132 if (IS_TRUE(value))
133 log_once = true;
134 else
135 log_once = false;
136
137 return 0;
138 }
139
140 return -1;
141 }
142
143 __attribute__((nonnull(2))) static void df_submit_one(char *plugin_instance,
144 const char *type,
145 const char *type_instance,
146 gauge_t value) {
147 value_list_t vl = VALUE_LIST_INIT;
148
149 vl.values = &(value_t){.gauge = value};
150 vl.values_len = 1;
151 sstrncpy(vl.plugin, "df", sizeof(vl.plugin));
152 if (plugin_instance != NULL)
153 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
154 sstrncpy(vl.type, type, sizeof(vl.type));
155 if (type_instance != NULL)
156 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
157
158 plugin_dispatch_values(&vl);
159 } /* void df_submit_one */
160
161 static int df_read(void) {
162 #if HAVE_STATVFS
163 struct statvfs statbuf;
164 #elif HAVE_STATFS
165 struct statfs statbuf;
166 #endif
167 int retval = 0;
168 /* struct STATANYFS statbuf; */
169 cu_mount_t *mnt_list;
170
171 mnt_list = NULL;
172 if (cu_mount_getlist(&mnt_list) == NULL) {
173 ERROR("df plugin: cu_mount_getlist failed.");
174 return -1;
175 }
176
177 for (cu_mount_t *mnt_ptr = mnt_list; mnt_ptr != NULL;
178 mnt_ptr = mnt_ptr->next) {
179 unsigned long long blocksize;
180 char disk_name[256];
181 cu_mount_t *dup_ptr;
182 uint64_t blk_free;
183 uint64_t blk_reserved;
184 uint64_t blk_used;
185
186 char const *dev =
187 (mnt_ptr->spec_device != NULL) ? mnt_ptr->spec_device : mnt_ptr->device;
188
189 if (ignorelist_match(il_device, dev))
190 continue;
191 if (ignorelist_match(il_mountpoint, mnt_ptr->dir))
192 continue;
193 if (ignorelist_match(il_fstype, mnt_ptr->type))
194 continue;
195
196 /* search for duplicates *in front of* the current mnt_ptr. */
197 for (dup_ptr = mnt_list; dup_ptr != NULL; dup_ptr = dup_ptr->next) {
198 /* No duplicate found: mnt_ptr is the first of its kind. */
199 if (dup_ptr == mnt_ptr) {
200 dup_ptr = NULL;
201 break;
202 }
203
204 /* Duplicate found: leave non-NULL dup_ptr. */
205 if (by_device && (mnt_ptr->spec_device != NULL) &&
206 (dup_ptr->spec_device != NULL) &&
207 (strcmp(mnt_ptr->spec_device, dup_ptr->spec_device) == 0))
208 break;
209 else if (!by_device && (strcmp(mnt_ptr->dir, dup_ptr->dir) == 0))
210 break;
211 }
212
213 /* ignore duplicates */
214 if (dup_ptr != NULL)
215 continue;
216
217 if (STATANYFS(mnt_ptr->dir, &statbuf) < 0) {
218 if (log_once == false || ignorelist_match(il_errors, mnt_ptr->dir) == 0) {
219 if (log_once == true) {
220 ignorelist_add(il_errors, mnt_ptr->dir);
221 }
222 ERROR(STATANYFS_STR "(%s) failed: %s", mnt_ptr->dir, STRERRNO);
223 }
224 continue;
225 } else {
226 if (log_once == true) {
227 ignorelist_remove(il_errors, mnt_ptr->dir);
228 }
229 }
230
231 if (!statbuf.f_blocks)
232 continue;
233
234 if (by_device) {
235 /* eg, /dev/hda1 -- strip off the "/dev/" */
236 if (strncmp(dev, "/dev/", strlen("/dev/")) == 0)
237 sstrncpy(disk_name, dev + strlen("/dev/"), sizeof(disk_name));
238 else
239 sstrncpy(disk_name, dev, sizeof(disk_name));
240
241 if (strlen(disk_name) < 1) {
242 DEBUG("df: no device name for mountpoint %s, skipping", mnt_ptr->dir);
243 continue;
244 }
245 } else {
246 if (strcmp(mnt_ptr->dir, "/") == 0)
247 sstrncpy(disk_name, "root", sizeof(disk_name));
248 else {
249 sstrncpy(disk_name, mnt_ptr->dir + 1, sizeof(disk_name));
250 size_t len = strlen(disk_name);
251
252 for (size_t i = 0; i < len; i++)
253 if (disk_name[i] == '/')
254 disk_name[i] = '-';
255 }
256 }
257
258 blocksize = BLOCKSIZE(statbuf);
259
260 /*
261 * Sanity-check for the values in the struct
262 */
263 /* Check for negative "available" byes. For example UFS can
264 * report negative free space for user. Notice. blk_reserved
265 * will start to diminish after this. */
266 #if HAVE_STATVFS
267 /* Cast and temporary variable are needed to avoid
268 * compiler warnings.
269 * ((struct statvfs).f_bavail is unsigned (POSIX)) */
270 int64_t signed_bavail = (int64_t)statbuf.f_bavail;
271 if (signed_bavail < 0)
272 statbuf.f_bavail = 0;
273 #elif HAVE_STATFS
274 if (statbuf.f_bavail < 0)
275 statbuf.f_bavail = 0;
276 #endif
277 /* Make sure that f_blocks >= f_bfree >= f_bavail */
278 if (statbuf.f_bfree < statbuf.f_bavail)
279 statbuf.f_bfree = statbuf.f_bavail;
280 if (statbuf.f_blocks < statbuf.f_bfree)
281 statbuf.f_blocks = statbuf.f_bfree;
282
283 blk_free = (uint64_t)statbuf.f_bavail;
284 blk_reserved = (uint64_t)(statbuf.f_bfree - statbuf.f_bavail);
285 blk_used = (uint64_t)(statbuf.f_blocks - statbuf.f_bfree);
286
287 if (values_absolute) {
288 df_submit_one(disk_name, "df_complex", "free",
289 (gauge_t)(blk_free * blocksize));
290 df_submit_one(disk_name, "df_complex", "reserved",
291 (gauge_t)(blk_reserved * blocksize));
292 df_submit_one(disk_name, "df_complex", "used",
293 (gauge_t)(blk_used * blocksize));
294 }
295
296 if (values_percentage) {
297 if (statbuf.f_blocks > 0) {
298 df_submit_one(disk_name, "percent_bytes", "free",
299 (gauge_t)((float_t)(blk_free) / statbuf.f_blocks * 100));
300 df_submit_one(
301 disk_name, "percent_bytes", "reserved",
302 (gauge_t)((float_t)(blk_reserved) / statbuf.f_blocks * 100));
303 df_submit_one(disk_name, "percent_bytes", "used",
304 (gauge_t)((float_t)(blk_used) / statbuf.f_blocks * 100));
305 } else {
306 retval = -1;
307 break;
308 }
309 }
310
311 /* inode handling */
312 if (report_inodes && statbuf.f_files != 0 && statbuf.f_ffree != 0) {
313 uint64_t inode_free;
314 uint64_t inode_reserved;
315 uint64_t inode_used;
316
317 /* Sanity-check for the values in the struct */
318 if (statbuf.f_ffree < statbuf.f_favail)
319 statbuf.f_ffree = statbuf.f_favail;
320 if (statbuf.f_files < statbuf.f_ffree)
321 statbuf.f_files = statbuf.f_ffree;
322
323 inode_free = (uint64_t)statbuf.f_favail;
324 inode_reserved = (uint64_t)(statbuf.f_ffree - statbuf.f_favail);
325 inode_used = (uint64_t)(statbuf.f_files - statbuf.f_ffree);
326
327 if (values_percentage) {
328 if (statbuf.f_files > 0) {
329 df_submit_one(
330 disk_name, "percent_inodes", "free",
331 (gauge_t)((float_t)(inode_free) / statbuf.f_files * 100));
332 df_submit_one(
333 disk_name, "percent_inodes", "reserved",
334 (gauge_t)((float_t)(inode_reserved) / statbuf.f_files * 100));
335 df_submit_one(
336 disk_name, "percent_inodes", "used",
337 (gauge_t)((float_t)(inode_used) / statbuf.f_files * 100));
338 } else {
339 retval = -1;
340 break;
341 }
342 }
343 if (values_absolute) {
344 df_submit_one(disk_name, "df_inodes", "free", (gauge_t)inode_free);
345 df_submit_one(disk_name, "df_inodes", "reserved",
346 (gauge_t)inode_reserved);
347 df_submit_one(disk_name, "df_inodes", "used", (gauge_t)inode_used);
348 }
349 }
350 }
351
352 cu_mount_freelist(mnt_list);
353
354 return retval;
355 } /* int df_read */
356
357 void module_register(void) {
358 plugin_register_config("df", df_config, config_keys, config_keys_num);
359 plugin_register_init("df", df_init);
360 plugin_register_read("df", df_read);
361 } /* void module_register */