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 verplant.org>
21 * Paul Sadauskas <psadauskas at gmail.com>
22 **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27 #include "configfile.h"
28 #include "utils_mount.h"
29 #include "utils_ignorelist.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 {
51 "Device",
52 "MountPoint",
53 "FSType",
54 "IgnoreSelected",
55 "ReportByDevice",
56 "ReportReserved",
57 "ReportInodes",
58 "ReportPercentage"
59 };
60 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
62 static ignorelist_t *il_device = NULL;
63 static ignorelist_t *il_mountpoint = NULL;
64 static ignorelist_t *il_fstype = NULL;
66 static _Bool by_device = 0;
67 static _Bool report_inodes = 0;
68 static _Bool report_percentage = 0;
70 static int df_init (void)
71 {
72 if (il_device == NULL)
73 il_device = ignorelist_create (1);
74 if (il_mountpoint == NULL)
75 il_mountpoint = ignorelist_create (1);
76 if (il_fstype == NULL)
77 il_fstype = ignorelist_create (1);
79 return (0);
80 }
82 static int df_config (const char *key, const char *value)
83 {
84 df_init ();
86 if (strcasecmp (key, "Device") == 0)
87 {
88 if (ignorelist_add (il_device, value))
89 return (1);
90 return (0);
91 }
92 else if (strcasecmp (key, "MountPoint") == 0)
93 {
94 if (ignorelist_add (il_mountpoint, value))
95 return (1);
96 return (0);
97 }
98 else if (strcasecmp (key, "FSType") == 0)
99 {
100 if (ignorelist_add (il_fstype, value))
101 return (1);
102 return (0);
103 }
104 else if (strcasecmp (key, "IgnoreSelected") == 0)
105 {
106 if (IS_TRUE (value))
107 {
108 ignorelist_set_invert (il_device, 0);
109 ignorelist_set_invert (il_mountpoint, 0);
110 ignorelist_set_invert (il_fstype, 0);
111 }
112 else
113 {
114 ignorelist_set_invert (il_device, 1);
115 ignorelist_set_invert (il_mountpoint, 1);
116 ignorelist_set_invert (il_fstype, 1);
117 }
118 return (0);
119 }
120 else if (strcasecmp (key, "ReportByDevice") == 0)
121 {
122 if (IS_TRUE (value))
123 by_device = 1;
125 return (0);
126 }
127 else if (strcasecmp (key, "ReportInodes") == 0)
128 {
129 if (IS_TRUE (value))
130 report_inodes = 1;
131 else
132 report_inodes = 0;
134 return (0);
135 }
137 else if (strcasecmp (key, "ReportPercentage") == 0)
138 {
139 if (IS_TRUE (value))
140 report_percentage = 1;
141 else
142 report_percentage = 0;
144 return (0);
145 }
147 return (-1);
148 }
150 __attribute__ ((nonnull(2)))
151 static void df_submit_one (char *plugin_instance,
152 const char *type, const char *type_instance,
153 gauge_t value)
154 {
155 value_t values[1];
156 value_list_t vl = VALUE_LIST_INIT;
158 values[0].gauge = value;
160 vl.values = values;
161 vl.values_len = 1;
162 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
163 sstrncpy (vl.plugin, "df", sizeof (vl.plugin));
164 if (plugin_instance != NULL)
165 sstrncpy (vl.plugin_instance, plugin_instance,
166 sizeof (vl.plugin_instance));
167 sstrncpy (vl.type, type, sizeof (vl.type));
168 if (type_instance != NULL)
169 sstrncpy (vl.type_instance, type_instance,
170 sizeof (vl.type_instance));
172 plugin_dispatch_values (&vl);
173 } /* void df_submit_one */
175 static int df_read (void)
176 {
177 #if HAVE_STATVFS
178 struct statvfs statbuf;
179 #elif HAVE_STATFS
180 struct statfs statbuf;
181 #endif
182 /* struct STATANYFS statbuf; */
183 cu_mount_t *mnt_list;
184 cu_mount_t *mnt_ptr;
186 mnt_list = NULL;
187 if (cu_mount_getlist (&mnt_list) == NULL)
188 {
189 ERROR ("df plugin: cu_mount_getlist failed.");
190 return (-1);
191 }
193 for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
194 {
195 unsigned long long blocksize;
196 char disk_name[256];
197 uint64_t blk_free;
198 uint64_t blk_reserved;
199 uint64_t blk_used;
201 if (ignorelist_match (il_device,
202 (mnt_ptr->spec_device != NULL)
203 ? mnt_ptr->spec_device
204 : mnt_ptr->device))
205 continue;
206 if (ignorelist_match (il_mountpoint, mnt_ptr->dir))
207 continue;
208 if (ignorelist_match (il_fstype, mnt_ptr->type))
209 continue;
211 if (STATANYFS (mnt_ptr->dir, &statbuf) < 0)
212 {
213 char errbuf[1024];
214 ERROR (STATANYFS_STR"(%s) failed: %s",
215 mnt_ptr->dir,
216 sstrerror (errno, errbuf,
217 sizeof (errbuf)));
218 continue;
219 }
221 if (!statbuf.f_blocks)
222 continue;
224 if (by_device)
225 {
226 /* eg, /dev/hda1 -- strip off the "/dev/" */
227 if (strncmp (mnt_ptr->spec_device, "/dev/", strlen ("/dev/")) == 0)
228 sstrncpy (disk_name, mnt_ptr->spec_device + strlen ("/dev/"), sizeof (disk_name));
229 else
230 sstrncpy (disk_name, mnt_ptr->spec_device, sizeof (disk_name));
232 if (strlen(disk_name) < 1)
233 {
234 DEBUG("df: no device name name for mountpoint %s, skipping", mnt_ptr->dir);
235 continue;
236 }
237 }
238 else
239 {
240 if (strcmp (mnt_ptr->dir, "/") == 0)
241 {
242 if (strcmp (mnt_ptr->type, "rootfs") == 0)
243 continue;
244 sstrncpy (disk_name, "root", sizeof (disk_name));
245 }
246 else
247 {
248 int i, len;
250 sstrncpy (disk_name, mnt_ptr->dir + 1, sizeof (disk_name));
251 len = strlen (disk_name);
253 for (i = 0; i < len; i++)
254 if (disk_name[i] == '/')
255 disk_name[i] = '-';
256 }
257 }
259 blocksize = BLOCKSIZE(statbuf);
261 /*
262 * Sanity-check for the values in the struct
263 */
264 /* Check for negative "available" byes. For example UFS can
265 * report negative free space for user. Notice. blk_reserved
266 * will start to diminish after this. */
267 #if HAVE_STATVFS
268 /* Cast and temporary variable are needed to avoid
269 * compiler warnings.
270 * ((struct statvfs).f_bavail is unsigned (POSIX)) */
271 int64_t signed_bavail = (int64_t) statbuf.f_bavail;
272 if (signed_bavail < 0)
273 statbuf.f_bavail = 0;
274 #elif HAVE_STATFS
275 if (statbuf.f_bavail < 0)
276 statbuf.f_bavail = 0;
277 #endif
278 /* Make sure that f_blocks >= f_bfree >= f_bavail */
279 if (statbuf.f_bfree < statbuf.f_bavail)
280 statbuf.f_bfree = statbuf.f_bavail;
281 if (statbuf.f_blocks < statbuf.f_bfree)
282 statbuf.f_blocks = statbuf.f_bfree;
284 blk_free = (uint64_t) statbuf.f_bavail;
285 blk_reserved = (uint64_t) (statbuf.f_bfree - statbuf.f_bavail);
286 blk_used = (uint64_t) (statbuf.f_blocks - statbuf.f_bfree);
288 if (report_percentage && (statbuf.f_blocks > 0))
289 {
290 uint64_t blk_total = (uint64_t) statbuf.f_blocks;
291 char plugin_instance[DATA_MAX_NAME_LEN];
293 ssnprintf (plugin_instance, sizeof (plugin_instance),
294 "%s-bytes", disk_name);
296 df_submit_one (plugin_instance, "percent", "free",
297 100.0 * ((gauge_t) blk_free) / ((gauge_t) blk_total));
298 df_submit_one (plugin_instance, "percent", "reserved",
299 100.0 * ((gauge_t) blk_reserved) / ((gauge_t) blk_total));
300 df_submit_one (plugin_instance, "percent", "used",
301 100.0 * ((gauge_t) blk_used) / ((gauge_t) blk_total));
302 }
303 else if (!report_percentage)
304 {
305 df_submit_one (disk_name, "df_complex", "free",
306 (gauge_t) (blk_free * blocksize));
307 df_submit_one (disk_name, "df_complex", "reserved",
308 (gauge_t) (blk_reserved * blocksize));
309 df_submit_one (disk_name, "df_complex", "used",
310 (gauge_t) (blk_used * blocksize));
311 }
313 /* inode handling */
314 if (report_inodes)
315 {
316 uint64_t inode_free;
317 uint64_t inode_reserved;
318 uint64_t inode_used;
320 /* Sanity-check for the values in the struct */
321 if (statbuf.f_ffree < statbuf.f_favail)
322 statbuf.f_ffree = statbuf.f_favail;
323 if (statbuf.f_files < statbuf.f_ffree)
324 statbuf.f_files = statbuf.f_ffree;
326 inode_free = (uint64_t) statbuf.f_favail;
327 inode_reserved = (uint64_t) (statbuf.f_ffree - statbuf.f_favail);
328 inode_used = (uint64_t) (statbuf.f_files - statbuf.f_ffree);
330 if (report_percentage && (statbuf.f_files > 0))
331 {
332 uint64_t inode_total = (uint64_t) statbuf.f_files;
333 char plugin_instance[DATA_MAX_NAME_LEN];
335 ssnprintf (plugin_instance, sizeof (plugin_instance),
336 "%s-inodes", disk_name);
338 df_submit_one (plugin_instance, "percent", "free",
339 100.0 * ((gauge_t) inode_free) / ((gauge_t) inode_total));
340 df_submit_one (plugin_instance, "percent", "reserved",
341 100.0 * ((gauge_t) inode_reserved) / ((gauge_t) inode_total));
342 df_submit_one (plugin_instance, "percent", "used",
343 100.0 * ((gauge_t) inode_used) / ((gauge_t) inode_total));
344 }
345 else if (!report_percentage)
346 {
347 df_submit_one (disk_name, "df_inodes", "free",
348 (gauge_t) inode_free);
349 df_submit_one (disk_name, "df_inodes", "reserved",
350 (gauge_t) inode_reserved);
351 df_submit_one (disk_name, "df_inodes", "used",
352 (gauge_t) inode_used);
353 }
354 }
355 }
357 cu_mount_freelist (mnt_list);
359 return (0);
360 } /* int df_read */
362 void module_register (void)
363 {
364 plugin_register_config ("df", df_config,
365 config_keys, config_keys_num);
366 plugin_register_init ("df", df_init);
367 plugin_register_read ("df", df_read);
368 } /* void module_register */