3 * Copyright (C) 2005-2009 Florian octo Forster
4 * Copyright (C) 2009 Paul Sadauskas
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.
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.
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
20 * Florian octo Forster <octo at collectd.org>
21 * Paul Sadauskas <psadauskas at gmail.com>
28 #include "utils_ignorelist.h"
29 #include "utils_mount.h"
32 #if HAVE_SYS_STATVFS_H
33 #include <sys/statvfs.h>
35 #define STATANYFS statvfs
36 #define STATANYFS_STR "statvfs"
37 #define BLOCKSIZE(s) ((s).f_frsize ? (s).f_frsize : (s).f_bsize)
40 #include <sys/statfs.h>
42 #define STATANYFS statfs
43 #define STATANYFS_STR "statfs"
44 #define BLOCKSIZE(s) (s).f_bsize
46 #error "No applicable input method."
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;
55 static ignorelist_t *il_mountpoint;
56 static ignorelist_t *il_fstype;
58 static bool by_device;
59 static bool report_inodes;
60 static bool values_absolute = true;
61 static bool values_percentage;
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);
74 static int df_config(const char *key, const char *value) {
77 if (strcasecmp(key, "Device") == 0) {
78 if (ignorelist_add(il_device, value))
81 } else if (strcasecmp(key, "MountPoint") == 0) {
82 if (ignorelist_add(il_mountpoint, value))
85 } else if (strcasecmp(key, "FSType") == 0) {
86 if (ignorelist_add(il_fstype, value))
89 } else if (strcasecmp(key, "IgnoreSelected") == 0) {
91 ignorelist_set_invert(il_device, 0);
92 ignorelist_set_invert(il_mountpoint, 0);
93 ignorelist_set_invert(il_fstype, 0);
95 ignorelist_set_invert(il_device, 1);
96 ignorelist_set_invert(il_mountpoint, 1);
97 ignorelist_set_invert(il_fstype, 1);
100 } else if (strcasecmp(key, "ReportByDevice") == 0) {
105 } else if (strcasecmp(key, "ReportInodes") == 0) {
107 report_inodes = true;
109 report_inodes = false;
112 } else if (strcasecmp(key, "ValuesAbsolute") == 0) {
114 values_absolute = true;
116 values_absolute = false;
119 } else if (strcasecmp(key, "ValuesPercentage") == 0) {
121 values_percentage = true;
123 values_percentage = false;
131 __attribute__((nonnull(2))) static void df_submit_one(char *plugin_instance,
133 const char *type_instance,
135 value_list_t vl = VALUE_LIST_INIT;
137 vl.values = &(value_t){.gauge = value};
139 sstrncpy(vl.plugin, "df", sizeof(vl.plugin));
140 if (plugin_instance != NULL)
141 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
142 sstrncpy(vl.type, type, sizeof(vl.type));
143 if (type_instance != NULL)
144 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
146 plugin_dispatch_values(&vl);
147 } /* void df_submit_one */
149 static int df_read(void) {
151 struct statvfs statbuf;
153 struct statfs statbuf;
156 /* struct STATANYFS statbuf; */
157 cu_mount_t *mnt_list;
160 if (cu_mount_getlist(&mnt_list) == NULL) {
161 ERROR("df plugin: cu_mount_getlist failed.");
165 for (cu_mount_t *mnt_ptr = mnt_list; mnt_ptr != NULL;
166 mnt_ptr = mnt_ptr->next) {
167 unsigned long long blocksize;
171 uint64_t blk_reserved;
175 (mnt_ptr->spec_device != NULL) ? mnt_ptr->spec_device : mnt_ptr->device;
177 if (ignorelist_match(il_device, dev))
179 if (ignorelist_match(il_mountpoint, mnt_ptr->dir))
181 if (ignorelist_match(il_fstype, mnt_ptr->type))
184 /* search for duplicates *in front of* the current mnt_ptr. */
185 for (dup_ptr = mnt_list; dup_ptr != NULL; dup_ptr = dup_ptr->next) {
186 /* No duplicate found: mnt_ptr is the first of its kind. */
187 if (dup_ptr == mnt_ptr) {
192 /* Duplicate found: leave non-NULL dup_ptr. */
193 if (by_device && (mnt_ptr->spec_device != NULL) &&
194 (dup_ptr->spec_device != NULL) &&
195 (strcmp(mnt_ptr->spec_device, dup_ptr->spec_device) == 0))
197 else if (!by_device && (strcmp(mnt_ptr->dir, dup_ptr->dir) == 0))
201 /* ignore duplicates */
205 if (STATANYFS(mnt_ptr->dir, &statbuf) < 0) {
206 ERROR(STATANYFS_STR "(%s) failed: %s", mnt_ptr->dir, STRERRNO);
210 if (!statbuf.f_blocks)
214 /* eg, /dev/hda1 -- strip off the "/dev/" */
215 if (strncmp(dev, "/dev/", strlen("/dev/")) == 0)
216 sstrncpy(disk_name, dev + strlen("/dev/"), sizeof(disk_name));
218 sstrncpy(disk_name, dev, sizeof(disk_name));
220 if (strlen(disk_name) < 1) {
221 DEBUG("df: no device name for mountpoint %s, skipping", mnt_ptr->dir);
225 if (strcmp(mnt_ptr->dir, "/") == 0)
226 sstrncpy(disk_name, "root", sizeof(disk_name));
228 sstrncpy(disk_name, mnt_ptr->dir + 1, sizeof(disk_name));
229 size_t len = strlen(disk_name);
231 for (size_t i = 0; i < len; i++)
232 if (disk_name[i] == '/')
237 blocksize = BLOCKSIZE(statbuf);
240 * Sanity-check for the values in the struct
242 /* Check for negative "available" byes. For example UFS can
243 * report negative free space for user. Notice. blk_reserved
244 * will start to diminish after this. */
246 /* Cast and temporary variable are needed to avoid
248 * ((struct statvfs).f_bavail is unsigned (POSIX)) */
249 int64_t signed_bavail = (int64_t)statbuf.f_bavail;
250 if (signed_bavail < 0)
251 statbuf.f_bavail = 0;
253 if (statbuf.f_bavail < 0)
254 statbuf.f_bavail = 0;
256 /* Make sure that f_blocks >= f_bfree >= f_bavail */
257 if (statbuf.f_bfree < statbuf.f_bavail)
258 statbuf.f_bfree = statbuf.f_bavail;
259 if (statbuf.f_blocks < statbuf.f_bfree)
260 statbuf.f_blocks = statbuf.f_bfree;
262 blk_free = (uint64_t)statbuf.f_bavail;
263 blk_reserved = (uint64_t)(statbuf.f_bfree - statbuf.f_bavail);
264 blk_used = (uint64_t)(statbuf.f_blocks - statbuf.f_bfree);
266 if (values_absolute) {
267 df_submit_one(disk_name, "df_complex", "free",
268 (gauge_t)(blk_free * blocksize));
269 df_submit_one(disk_name, "df_complex", "reserved",
270 (gauge_t)(blk_reserved * blocksize));
271 df_submit_one(disk_name, "df_complex", "used",
272 (gauge_t)(blk_used * blocksize));
275 if (values_percentage) {
276 if (statbuf.f_blocks > 0) {
277 df_submit_one(disk_name, "percent_bytes", "free",
278 (gauge_t)((float_t)(blk_free) / statbuf.f_blocks * 100));
280 disk_name, "percent_bytes", "reserved",
281 (gauge_t)((float_t)(blk_reserved) / statbuf.f_blocks * 100));
282 df_submit_one(disk_name, "percent_bytes", "used",
283 (gauge_t)((float_t)(blk_used) / statbuf.f_blocks * 100));
291 if (report_inodes && statbuf.f_files != 0 && statbuf.f_ffree != 0) {
293 uint64_t inode_reserved;
296 /* Sanity-check for the values in the struct */
297 if (statbuf.f_ffree < statbuf.f_favail)
298 statbuf.f_ffree = statbuf.f_favail;
299 if (statbuf.f_files < statbuf.f_ffree)
300 statbuf.f_files = statbuf.f_ffree;
302 inode_free = (uint64_t)statbuf.f_favail;
303 inode_reserved = (uint64_t)(statbuf.f_ffree - statbuf.f_favail);
304 inode_used = (uint64_t)(statbuf.f_files - statbuf.f_ffree);
306 if (values_percentage) {
307 if (statbuf.f_files > 0) {
309 disk_name, "percent_inodes", "free",
310 (gauge_t)((float_t)(inode_free) / statbuf.f_files * 100));
312 disk_name, "percent_inodes", "reserved",
313 (gauge_t)((float_t)(inode_reserved) / statbuf.f_files * 100));
315 disk_name, "percent_inodes", "used",
316 (gauge_t)((float_t)(inode_used) / statbuf.f_files * 100));
322 if (values_absolute) {
323 df_submit_one(disk_name, "df_inodes", "free", (gauge_t)inode_free);
324 df_submit_one(disk_name, "df_inodes", "reserved",
325 (gauge_t)inode_reserved);
326 df_submit_one(disk_name, "df_inodes", "used", (gauge_t)inode_used);
331 cu_mount_freelist(mnt_list);
336 void module_register(void) {
337 plugin_register_config("df", df_config, config_keys, config_keys_num);
338 plugin_register_init("df", df_init);
339 plugin_register_read("df", df_read);
340 } /* void module_register */