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>
27 #include "configfile.h"
28 #include "utils_mount.h"
29 #include "utils_ignorelist.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)
39 # if HAVE_SYS_STATFS_H
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[] =
61 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
63 static ignorelist_t *il_device = NULL;
64 static ignorelist_t *il_mountpoint = NULL;
65 static ignorelist_t *il_fstype = NULL;
67 static _Bool by_device = 0;
68 static _Bool report_inodes = 0;
69 static _Bool values_absolute = 1;
70 static _Bool values_percentage = 0;
72 static int df_init (void)
74 if (il_device == NULL)
75 il_device = ignorelist_create (1);
76 if (il_mountpoint == NULL)
77 il_mountpoint = ignorelist_create (1);
78 if (il_fstype == NULL)
79 il_fstype = ignorelist_create (1);
84 static int df_config (const char *key, const char *value)
88 if (strcasecmp (key, "Device") == 0)
90 if (ignorelist_add (il_device, value))
94 else if (strcasecmp (key, "MountPoint") == 0)
96 if (ignorelist_add (il_mountpoint, value))
100 else if (strcasecmp (key, "FSType") == 0)
102 if (ignorelist_add (il_fstype, value))
106 else if (strcasecmp (key, "IgnoreSelected") == 0)
110 ignorelist_set_invert (il_device, 0);
111 ignorelist_set_invert (il_mountpoint, 0);
112 ignorelist_set_invert (il_fstype, 0);
116 ignorelist_set_invert (il_device, 1);
117 ignorelist_set_invert (il_mountpoint, 1);
118 ignorelist_set_invert (il_fstype, 1);
122 else if (strcasecmp (key, "ReportByDevice") == 0)
129 else if (strcasecmp (key, "ReportInodes") == 0)
138 else if (strcasecmp (key, "ValuesAbsolute") == 0)
147 else if (strcasecmp (key, "ValuesPercentage") == 0)
150 values_percentage = 1;
152 values_percentage = 0;
160 __attribute__ ((nonnull(2)))
161 static void df_submit_one (char *plugin_instance,
162 const char *type, const char *type_instance,
166 value_list_t vl = VALUE_LIST_INIT;
168 values[0].gauge = value;
172 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
173 sstrncpy (vl.plugin, "df", sizeof (vl.plugin));
174 if (plugin_instance != NULL)
175 sstrncpy (vl.plugin_instance, plugin_instance,
176 sizeof (vl.plugin_instance));
177 sstrncpy (vl.type, type, sizeof (vl.type));
178 if (type_instance != NULL)
179 sstrncpy (vl.type_instance, type_instance,
180 sizeof (vl.type_instance));
182 plugin_dispatch_values (&vl);
183 } /* void df_submit_one */
185 static int df_read (void)
188 struct statvfs statbuf;
190 struct statfs statbuf;
192 /* struct STATANYFS statbuf; */
193 cu_mount_t *mnt_list;
197 if (cu_mount_getlist (&mnt_list) == NULL)
199 ERROR ("df plugin: cu_mount_getlist failed.");
203 for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
205 unsigned long long blocksize;
208 uint64_t blk_reserved;
211 if (ignorelist_match (il_device,
212 (mnt_ptr->spec_device != NULL)
213 ? mnt_ptr->spec_device
216 if (ignorelist_match (il_mountpoint, mnt_ptr->dir))
218 if (ignorelist_match (il_fstype, mnt_ptr->type))
221 if (STATANYFS (mnt_ptr->dir, &statbuf) < 0)
224 ERROR (STATANYFS_STR"(%s) failed: %s",
226 sstrerror (errno, errbuf,
231 if (!statbuf.f_blocks)
236 /* eg, /dev/hda1 -- strip off the "/dev/" */
237 if (strncmp (mnt_ptr->spec_device, "/dev/", strlen ("/dev/")) == 0)
238 sstrncpy (disk_name, mnt_ptr->spec_device + strlen ("/dev/"), sizeof (disk_name));
240 sstrncpy (disk_name, mnt_ptr->spec_device, sizeof (disk_name));
242 if (strlen(disk_name) < 1)
244 DEBUG("df: no device name for mountpoint %s, skipping", mnt_ptr->dir);
250 if (strcmp (mnt_ptr->dir, "/") == 0)
252 if (strcmp (mnt_ptr->type, "rootfs") == 0)
254 sstrncpy (disk_name, "root", sizeof (disk_name));
260 sstrncpy (disk_name, mnt_ptr->dir + 1, sizeof (disk_name));
261 len = strlen (disk_name);
263 for (i = 0; i < len; i++)
264 if (disk_name[i] == '/')
269 blocksize = BLOCKSIZE(statbuf);
272 * Sanity-check for the values in the struct
274 /* Check for negative "available" byes. For example UFS can
275 * report negative free space for user. Notice. blk_reserved
276 * will start to diminish after this. */
278 /* Cast and temporary variable are needed to avoid
280 * ((struct statvfs).f_bavail is unsigned (POSIX)) */
281 int64_t signed_bavail = (int64_t) statbuf.f_bavail;
282 if (signed_bavail < 0)
283 statbuf.f_bavail = 0;
285 if (statbuf.f_bavail < 0)
286 statbuf.f_bavail = 0;
288 /* Make sure that f_blocks >= f_bfree >= f_bavail */
289 if (statbuf.f_bfree < statbuf.f_bavail)
290 statbuf.f_bfree = statbuf.f_bavail;
291 if (statbuf.f_blocks < statbuf.f_bfree)
292 statbuf.f_blocks = statbuf.f_bfree;
294 blk_free = (uint64_t) statbuf.f_bavail;
295 blk_reserved = (uint64_t) (statbuf.f_bfree - statbuf.f_bavail);
296 blk_used = (uint64_t) (statbuf.f_blocks - statbuf.f_bfree);
300 df_submit_one (disk_name, "df_complex", "free",
301 (gauge_t) (blk_free * blocksize));
302 df_submit_one (disk_name, "df_complex", "reserved",
303 (gauge_t) (blk_reserved * blocksize));
304 df_submit_one (disk_name, "df_complex", "used",
305 (gauge_t) (blk_used * blocksize));
308 if (values_percentage)
310 if (statbuf.f_blocks > 0)
312 df_submit_one (disk_name, "percent_bytes", "free",
313 (gauge_t) ((float_t)(blk_free) / statbuf.f_blocks * 100));
314 df_submit_one (disk_name, "percent_bytes", "reserved",
315 (gauge_t) ((float_t)(blk_reserved) / statbuf.f_blocks * 100));
316 df_submit_one (disk_name, "percent_bytes", "used",
317 (gauge_t) ((float_t)(blk_used) / statbuf.f_blocks * 100));
326 uint64_t inode_reserved;
329 /* Sanity-check for the values in the struct */
330 if (statbuf.f_ffree < statbuf.f_favail)
331 statbuf.f_ffree = statbuf.f_favail;
332 if (statbuf.f_files < statbuf.f_ffree)
333 statbuf.f_files = statbuf.f_ffree;
335 inode_free = (uint64_t) statbuf.f_favail;
336 inode_reserved = (uint64_t) (statbuf.f_ffree - statbuf.f_favail);
337 inode_used = (uint64_t) (statbuf.f_files - statbuf.f_ffree);
339 if (values_percentage)
341 if (statbuf.f_files > 0)
343 df_submit_one (disk_name, "percent_inodes", "free",
344 (gauge_t) ((float_t)(inode_free) / statbuf.f_files * 100));
345 df_submit_one (disk_name, "percent_inodes", "reserved",
346 (gauge_t) ((float_t)(inode_reserved) / statbuf.f_files * 100));
347 df_submit_one (disk_name, "percent_inodes", "used",
348 (gauge_t) ((float_t)(inode_used) / statbuf.f_files * 100));
354 df_submit_one (disk_name, "df_inodes", "free",
355 (gauge_t) inode_free);
356 df_submit_one (disk_name, "df_inodes", "reserved",
357 (gauge_t) inode_reserved);
358 df_submit_one (disk_name, "df_inodes", "used",
359 (gauge_t) inode_used);
364 cu_mount_freelist (mnt_list);
369 void module_register (void)
371 plugin_register_config ("df", df_config,
372 config_keys, config_keys_num);
373 plugin_register_init ("df", df_init);
374 plugin_register_read ("df", df_read);
375 } /* void module_register */