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 verplant.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[] =
59 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
61 static ignorelist_t *il_device = NULL;
62 static ignorelist_t *il_mountpoint = NULL;
63 static ignorelist_t *il_fstype = NULL;
65 static _Bool by_device = 0;
66 static _Bool report_inodes = 0;
68 static int df_init (void)
70 if (il_device == NULL)
71 il_device = ignorelist_create (1);
72 if (il_mountpoint == NULL)
73 il_mountpoint = ignorelist_create (1);
74 if (il_fstype == NULL)
75 il_fstype = ignorelist_create (1);
80 static int df_config (const char *key, const char *value)
84 if (strcasecmp (key, "Device") == 0)
86 if (ignorelist_add (il_device, value))
90 else if (strcasecmp (key, "MountPoint") == 0)
92 if (ignorelist_add (il_mountpoint, value))
96 else if (strcasecmp (key, "FSType") == 0)
98 if (ignorelist_add (il_fstype, value))
102 else if (strcasecmp (key, "IgnoreSelected") == 0)
106 ignorelist_set_invert (il_device, 0);
107 ignorelist_set_invert (il_mountpoint, 0);
108 ignorelist_set_invert (il_fstype, 0);
112 ignorelist_set_invert (il_device, 1);
113 ignorelist_set_invert (il_mountpoint, 1);
114 ignorelist_set_invert (il_fstype, 1);
118 else if (strcasecmp (key, "ReportByDevice") == 0)
125 else if (strcasecmp (key, "ReportInodes") == 0)
139 __attribute__ ((nonnull(2)))
140 static void df_submit_one (char *plugin_instance,
141 const char *type, const char *type_instance,
145 value_list_t vl = VALUE_LIST_INIT;
147 values[0].gauge = value;
151 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
152 sstrncpy (vl.plugin, "df", sizeof (vl.plugin));
153 if (plugin_instance != NULL)
154 sstrncpy (vl.plugin_instance, plugin_instance,
155 sizeof (vl.plugin_instance));
156 sstrncpy (vl.type, type, sizeof (vl.type));
157 if (type_instance != NULL)
158 sstrncpy (vl.type_instance, type_instance,
159 sizeof (vl.type_instance));
161 plugin_dispatch_values (&vl);
162 } /* void df_submit_one */
164 static int df_read (void)
167 struct statvfs statbuf;
169 struct statfs statbuf;
171 /* struct STATANYFS statbuf; */
172 cu_mount_t *mnt_list;
176 if (cu_mount_getlist (&mnt_list) == NULL)
178 ERROR ("df plugin: cu_mount_getlist failed.");
182 for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
184 unsigned long long blocksize;
187 uint64_t blk_reserved;
190 if (ignorelist_match (il_device,
191 (mnt_ptr->spec_device != NULL)
192 ? mnt_ptr->spec_device
195 if (ignorelist_match (il_mountpoint, mnt_ptr->dir))
197 if (ignorelist_match (il_fstype, mnt_ptr->type))
200 if (STATANYFS (mnt_ptr->dir, &statbuf) < 0)
203 ERROR (STATANYFS_STR"(%s) failed: %s",
205 sstrerror (errno, errbuf,
210 if (!statbuf.f_blocks)
215 /* eg, /dev/hda1 -- strip off the "/dev/" */
216 if (strncmp (mnt_ptr->spec_device, "/dev/", strlen ("/dev/")) == 0)
217 sstrncpy (disk_name, mnt_ptr->spec_device + strlen ("/dev/"), sizeof (disk_name));
219 sstrncpy (disk_name, mnt_ptr->spec_device, sizeof (disk_name));
221 if (strlen(disk_name) < 1)
223 DEBUG("df: no device name name for mountpoint %s, skipping", mnt_ptr->dir);
229 if (strcmp (mnt_ptr->dir, "/") == 0)
231 if (strcmp (mnt_ptr->type, "rootfs") == 0)
233 sstrncpy (disk_name, "root", sizeof (disk_name));
239 sstrncpy (disk_name, mnt_ptr->dir + 1, sizeof (disk_name));
240 len = strlen (disk_name);
242 for (i = 0; i < len; i++)
243 if (disk_name[i] == '/')
248 blocksize = BLOCKSIZE(statbuf);
251 * Sanity-check for the values in the struct
253 /* Check for negative "available" byes. For example UFS can
254 * report negative free space for user. Notice. blk_reserved
255 * will start to diminish after this. */
257 /* Cast and temporary variable are needed to avoid
259 * ((struct statvfs).f_bavail is unsigned (POSIX)) */
260 int64_t signed_bavail = (int64_t) statbuf.f_bavail;
261 if (signed_bavail < 0)
262 statbuf.f_bavail = 0;
264 if (statbuf.f_bavail < 0)
265 statbuf.f_bavail = 0;
267 /* Make sure that f_blocks >= f_bfree >= f_bavail */
268 if (statbuf.f_bfree < statbuf.f_bavail)
269 statbuf.f_bfree = statbuf.f_bavail;
270 if (statbuf.f_blocks < statbuf.f_bfree)
271 statbuf.f_blocks = statbuf.f_bfree;
273 blk_free = (uint64_t) statbuf.f_bavail;
274 blk_reserved = (uint64_t) (statbuf.f_bfree - statbuf.f_bavail);
275 blk_used = (uint64_t) (statbuf.f_blocks - statbuf.f_bfree);
277 df_submit_one (disk_name, "df_complex", "free",
278 (gauge_t) (blk_free * blocksize));
279 df_submit_one (disk_name, "df_complex", "reserved",
280 (gauge_t) (blk_reserved * blocksize));
281 df_submit_one (disk_name, "df_complex", "used",
282 (gauge_t) (blk_used * blocksize));
288 uint64_t inode_reserved;
291 /* Sanity-check for the values in the struct */
292 if (statbuf.f_ffree < statbuf.f_favail)
293 statbuf.f_ffree = statbuf.f_favail;
294 if (statbuf.f_files < statbuf.f_ffree)
295 statbuf.f_files = statbuf.f_ffree;
297 inode_free = (uint64_t) statbuf.f_favail;
298 inode_reserved = (uint64_t) (statbuf.f_ffree - statbuf.f_favail);
299 inode_used = (uint64_t) (statbuf.f_files - statbuf.f_ffree);
301 df_submit_one (disk_name, "df_inodes", "free",
302 (gauge_t) inode_free);
303 df_submit_one (disk_name, "df_inodes", "reserved",
304 (gauge_t) inode_reserved);
305 df_submit_one (disk_name, "df_inodes", "used",
306 (gauge_t) inode_used);
310 cu_mount_freelist (mnt_list);
315 void module_register (void)
317 plugin_register_config ("df", df_config,
318 config_keys, config_keys_num);
319 plugin_register_init ("df", df_init);
320 plugin_register_read ("df", df_read);
321 } /* void module_register */