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 **/
24 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
28 #include "utils_ignorelist.h"
29 #include "utils_mount.h"
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
49 static const char *config_keys[] = {
50 "Device", "MountPoint", "FSType", "IgnoreSelected",
51 "ReportByDevice", "ReportInodes", "ValuesAbsolute", "ValuesPercentage"};
52 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
54 static ignorelist_t *il_device = NULL;
55 static ignorelist_t *il_mountpoint = NULL;
56 static ignorelist_t *il_fstype = NULL;
58 static _Bool by_device = 0;
59 static _Bool report_inodes = 0;
60 static _Bool values_absolute = 1;
61 static _Bool values_percentage = 0;
63 static int df_init(void) {
64 if (il_device == NULL)
65 il_device = ignorelist_create(1);
66 if (il_mountpoint == NULL)
67 il_mountpoint = ignorelist_create(1);
68 if (il_fstype == NULL)
69 il_fstype = ignorelist_create(1);
71 return (0);
72 }
74 static int df_config(const char *key, const char *value) {
75 df_init();
77 if (strcasecmp(key, "Device") == 0) {
78 if (ignorelist_add(il_device, value))
79 return (1);
80 return (0);
81 } else if (strcasecmp(key, "MountPoint") == 0) {
82 if (ignorelist_add(il_mountpoint, value))
83 return (1);
84 return (0);
85 } else if (strcasecmp(key, "FSType") == 0) {
86 if (ignorelist_add(il_fstype, value))
87 return (1);
88 return (0);
89 } else if (strcasecmp(key, "IgnoreSelected") == 0) {
90 if (IS_TRUE(value)) {
91 ignorelist_set_invert(il_device, 0);
92 ignorelist_set_invert(il_mountpoint, 0);
93 ignorelist_set_invert(il_fstype, 0);
94 } else {
95 ignorelist_set_invert(il_device, 1);
96 ignorelist_set_invert(il_mountpoint, 1);
97 ignorelist_set_invert(il_fstype, 1);
98 }
99 return (0);
100 } else if (strcasecmp(key, "ReportByDevice") == 0) {
101 if (IS_TRUE(value))
102 by_device = 1;
104 return (0);
105 } else if (strcasecmp(key, "ReportInodes") == 0) {
106 if (IS_TRUE(value))
107 report_inodes = 1;
108 else
109 report_inodes = 0;
111 return (0);
112 } else if (strcasecmp(key, "ValuesAbsolute") == 0) {
113 if (IS_TRUE(value))
114 values_absolute = 1;
115 else
116 values_absolute = 0;
118 return (0);
119 } else if (strcasecmp(key, "ValuesPercentage") == 0) {
120 if (IS_TRUE(value))
121 values_percentage = 1;
122 else
123 values_percentage = 0;
125 return (0);
126 }
128 return (-1);
129 }
131 __attribute__((nonnull(2))) static void df_submit_one(char *plugin_instance,
132 const char *type,
133 const char *type_instance,
134 gauge_t value) {
135 value_t values[1];
136 value_list_t vl = VALUE_LIST_INIT;
138 values[0].gauge = value;
140 vl.values = values;
141 vl.values_len = 1;
142 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
143 sstrncpy(vl.plugin, "df", sizeof(vl.plugin));
144 if (plugin_instance != NULL)
145 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
146 sstrncpy(vl.type, type, sizeof(vl.type));
147 if (type_instance != NULL)
148 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
150 plugin_dispatch_values(&vl);
151 } /* void df_submit_one */
153 static int df_read(void) {
154 #if HAVE_STATVFS
155 struct statvfs statbuf;
156 #elif HAVE_STATFS
157 struct statfs statbuf;
158 #endif
159 /* struct STATANYFS statbuf; */
160 cu_mount_t *mnt_list;
162 mnt_list = NULL;
163 if (cu_mount_getlist(&mnt_list) == NULL) {
164 ERROR("df plugin: cu_mount_getlist failed.");
165 return (-1);
166 }
168 for (cu_mount_t *mnt_ptr = mnt_list; mnt_ptr != NULL;
169 mnt_ptr = mnt_ptr->next) {
170 unsigned long long blocksize;
171 char disk_name[256];
172 cu_mount_t *dup_ptr;
173 uint64_t blk_free;
174 uint64_t blk_reserved;
175 uint64_t blk_used;
177 char const *dev =
178 (mnt_ptr->spec_device != NULL) ? mnt_ptr->spec_device : mnt_ptr->device;
180 if (ignorelist_match(il_device, dev))
181 continue;
182 if (ignorelist_match(il_mountpoint, mnt_ptr->dir))
183 continue;
184 if (ignorelist_match(il_fstype, mnt_ptr->type))
185 continue;
187 /* search for duplicates *in front of* the current mnt_ptr. */
188 for (dup_ptr = mnt_list; dup_ptr != NULL; dup_ptr = dup_ptr->next) {
189 /* No duplicate found: mnt_ptr is the first of its kind. */
190 if (dup_ptr == mnt_ptr) {
191 dup_ptr = NULL;
192 break;
193 }
195 /* Duplicate found: leave non-NULL dup_ptr. */
196 if (by_device &&
197 (strcmp(mnt_ptr->spec_device, dup_ptr->spec_device) == 0))
198 break;
199 else if (!by_device && (strcmp(mnt_ptr->dir, dup_ptr->dir) == 0))
200 break;
201 }
203 /* ignore duplicates */
204 if (dup_ptr != NULL)
205 continue;
207 if (STATANYFS(mnt_ptr->dir, &statbuf) < 0) {
208 char errbuf[1024];
209 ERROR(STATANYFS_STR "(%s) failed: %s", mnt_ptr->dir,
210 sstrerror(errno, errbuf, sizeof(errbuf)));
211 continue;
212 }
214 if (!statbuf.f_blocks)
215 continue;
217 if (by_device) {
218 /* eg, /dev/hda1 -- strip off the "/dev/" */
219 if (strncmp(dev, "/dev/", strlen("/dev/")) == 0)
220 sstrncpy(disk_name, dev + strlen("/dev/"), sizeof(disk_name));
221 else
222 sstrncpy(disk_name, dev, sizeof(disk_name));
224 if (strlen(disk_name) < 1) {
225 DEBUG("df: no device name for mountpoint %s, skipping", mnt_ptr->dir);
226 continue;
227 }
228 } else {
229 if (strcmp(mnt_ptr->dir, "/") == 0)
230 sstrncpy(disk_name, "root", sizeof(disk_name));
231 else {
232 int len;
234 sstrncpy(disk_name, mnt_ptr->dir + 1, sizeof(disk_name));
235 len = strlen(disk_name);
237 for (int i = 0; i < len; i++)
238 if (disk_name[i] == '/')
239 disk_name[i] = '-';
240 }
241 }
243 blocksize = BLOCKSIZE(statbuf);
245 /*
246 * Sanity-check for the values in the struct
247 */
248 /* Check for negative "available" byes. For example UFS can
249 * report negative free space for user. Notice. blk_reserved
250 * will start to diminish after this. */
251 #if HAVE_STATVFS
252 /* Cast and temporary variable are needed to avoid
253 * compiler warnings.
254 * ((struct statvfs).f_bavail is unsigned (POSIX)) */
255 int64_t signed_bavail = (int64_t)statbuf.f_bavail;
256 if (signed_bavail < 0)
257 statbuf.f_bavail = 0;
258 #elif HAVE_STATFS
259 if (statbuf.f_bavail < 0)
260 statbuf.f_bavail = 0;
261 #endif
262 /* Make sure that f_blocks >= f_bfree >= f_bavail */
263 if (statbuf.f_bfree < statbuf.f_bavail)
264 statbuf.f_bfree = statbuf.f_bavail;
265 if (statbuf.f_blocks < statbuf.f_bfree)
266 statbuf.f_blocks = statbuf.f_bfree;
268 blk_free = (uint64_t)statbuf.f_bavail;
269 blk_reserved = (uint64_t)(statbuf.f_bfree - statbuf.f_bavail);
270 blk_used = (uint64_t)(statbuf.f_blocks - statbuf.f_bfree);
272 if (values_absolute) {
273 df_submit_one(disk_name, "df_complex", "free",
274 (gauge_t)(blk_free * blocksize));
275 df_submit_one(disk_name, "df_complex", "reserved",
276 (gauge_t)(blk_reserved * blocksize));
277 df_submit_one(disk_name, "df_complex", "used",
278 (gauge_t)(blk_used * blocksize));
279 }
281 if (values_percentage) {
282 if (statbuf.f_blocks > 0) {
283 df_submit_one(disk_name, "percent_bytes", "free",
284 (gauge_t)((float_t)(blk_free) / statbuf.f_blocks * 100));
285 df_submit_one(
286 disk_name, "percent_bytes", "reserved",
287 (gauge_t)((float_t)(blk_reserved) / statbuf.f_blocks * 100));
288 df_submit_one(disk_name, "percent_bytes", "used",
289 (gauge_t)((float_t)(blk_used) / statbuf.f_blocks * 100));
290 } else
291 return (-1);
292 }
294 /* inode handling */
295 if (report_inodes && statbuf.f_files != 0 && statbuf.f_ffree != 0) {
296 uint64_t inode_free;
297 uint64_t inode_reserved;
298 uint64_t inode_used;
300 /* Sanity-check for the values in the struct */
301 if (statbuf.f_ffree < statbuf.f_favail)
302 statbuf.f_ffree = statbuf.f_favail;
303 if (statbuf.f_files < statbuf.f_ffree)
304 statbuf.f_files = statbuf.f_ffree;
306 inode_free = (uint64_t)statbuf.f_favail;
307 inode_reserved = (uint64_t)(statbuf.f_ffree - statbuf.f_favail);
308 inode_used = (uint64_t)(statbuf.f_files - statbuf.f_ffree);
310 if (values_percentage) {
311 if (statbuf.f_files > 0) {
312 df_submit_one(
313 disk_name, "percent_inodes", "free",
314 (gauge_t)((float_t)(inode_free) / statbuf.f_files * 100));
315 df_submit_one(
316 disk_name, "percent_inodes", "reserved",
317 (gauge_t)((float_t)(inode_reserved) / statbuf.f_files * 100));
318 df_submit_one(
319 disk_name, "percent_inodes", "used",
320 (gauge_t)((float_t)(inode_used) / statbuf.f_files * 100));
321 } else
322 return (-1);
323 }
324 if (values_absolute) {
325 df_submit_one(disk_name, "df_inodes", "free", (gauge_t)inode_free);
326 df_submit_one(disk_name, "df_inodes", "reserved",
327 (gauge_t)inode_reserved);
328 df_submit_one(disk_name, "df_inodes", "used", (gauge_t)inode_used);
329 }
330 }
331 }
333 cu_mount_freelist(mnt_list);
335 return (0);
336 } /* int df_read */
338 void module_register(void) {
339 plugin_register_config("df", df_config, config_keys, config_keys_num);
340 plugin_register_init("df", df_init);
341 plugin_register_read("df", df_read);
342 } /* void module_register */