983d0c667249c12c74d7a32b885c2cd356120562
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"
30 #include "utils_complain.h"
32 #if HAVE_STATVFS
33 # if HAVE_SYS_STATVFS_H
34 # include <sys/statvfs.h>
35 # endif
36 # define STATANYFS 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 BLOCKSIZE(s) (s).f_bsize
44 #else
45 # error "No applicable input method."
46 #endif
48 static const char *config_keys[] =
49 {
50 "Device",
51 "MountPoint",
52 "FSType",
53 "IgnoreSelected",
54 "ReportByDevice",
55 "ReportReserved",
56 "ReportInodes"
57 };
58 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
60 static ignorelist_t *il_device = NULL;
61 static ignorelist_t *il_mountpoint = NULL;
62 static ignorelist_t *il_fstype = NULL;
64 static _Bool by_device = false;
65 static _Bool report_reserved = false;
66 static _Bool report_inodes = false;
68 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
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 = true;
125 return (0);
126 }
127 else if (strcasecmp (key, "ReportReserved") == 0)
128 {
129 if (IS_TRUE (value))
130 report_reserved = true;
131 else
132 report_reserved = false;
134 return (0);
135 }
136 else if (strcasecmp (key, "ReportInodes") == 0)
137 {
138 if (IS_TRUE (value))
139 report_inodes = true;
140 else
141 report_inodes = false;
143 return (0);
144 }
147 return (-1);
148 }
150 static void df_submit_two (char *df_name,
151 const char *type,
152 gauge_t df_used,
153 gauge_t df_free)
154 {
155 value_t values[2];
156 value_list_t vl = VALUE_LIST_INIT;
158 values[0].gauge = df_used;
159 values[1].gauge = df_free;
161 vl.values = values;
162 vl.values_len = 2;
163 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
164 sstrncpy (vl.plugin, "df", sizeof (vl.plugin));
165 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
166 sstrncpy (vl.type, type, sizeof (vl.type));
167 sstrncpy (vl.type_instance, df_name, sizeof (vl.type_instance));
169 plugin_dispatch_values (&vl);
170 } /* void df_submit_two */
172 __attribute__ ((nonnull(2)))
173 static void df_submit_one (char *plugin_instance,
174 const char *type, const char *type_instance,
175 gauge_t value)
176 {
177 value_t values[1];
178 value_list_t vl = VALUE_LIST_INIT;
180 values[0].gauge = value;
182 vl.values = values;
183 vl.values_len = 1;
184 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
185 sstrncpy (vl.plugin, "df", sizeof (vl.plugin));
186 if (plugin_instance != NULL)
187 sstrncpy (vl.plugin_instance, plugin_instance,
188 sizeof (vl.plugin_instance));
189 sstrncpy (vl.type, type, sizeof (vl.type));
190 if (type_instance != NULL)
191 sstrncpy (vl.type_instance, type_instance,
192 sizeof (vl.type_instance));
194 plugin_dispatch_values (&vl);
195 } /* void df_submit_one */
197 static int df_read (void)
198 {
199 #if HAVE_STATVFS
200 struct statvfs statbuf;
201 #elif HAVE_STATFS
202 struct statfs statbuf;
203 #endif
204 /* struct STATANYFS statbuf; */
205 cu_mount_t *mnt_list;
206 cu_mount_t *mnt_ptr;
208 mnt_list = NULL;
209 if (cu_mount_getlist (&mnt_list) == NULL)
210 {
211 c_complain (LOG_ERR, &complaint,
212 "df plugin: cu_mount_getlist failed.");
213 return (-1);
214 }
215 c_release (LOG_INFO, &complaint,
216 "df plugin: cu_mount_getlist returned successfully.");
218 for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
219 {
220 unsigned long long blocksize;
221 char disk_name[256];
223 if (ignorelist_match (il_device,
224 (mnt_ptr->spec_device != NULL)
225 ? mnt_ptr->spec_device
226 : mnt_ptr->device))
227 continue;
228 if (ignorelist_match (il_mountpoint, mnt_ptr->dir))
229 continue;
230 if (ignorelist_match (il_fstype, mnt_ptr->type))
231 continue;
233 if (STATANYFS (mnt_ptr->dir, &statbuf) < 0)
234 {
235 char errbuf[1024];
236 ERROR ("statv?fs failed: %s",
237 sstrerror (errno, errbuf,
238 sizeof (errbuf)));
239 continue;
240 }
242 if (!statbuf.f_blocks)
243 continue;
245 if (by_device)
246 {
247 /* eg, /dev/hda1 -- strip off the "/dev/" */
248 if (strncmp (mnt_ptr->spec_device, "/dev/", strlen ("/dev/")) == 0)
249 sstrncpy (disk_name, mnt_ptr->spec_device + strlen ("/dev/"), sizeof (disk_name));
250 else
251 sstrncpy (disk_name, mnt_ptr->spec_device, sizeof (disk_name));
253 if (strlen(disk_name) < 1)
254 {
255 DEBUG("df: no device name name for mountpoint %s, skipping", mnt_ptr->dir);
256 continue;
257 }
258 }
259 else
260 {
261 if (strcmp (mnt_ptr->dir, "/") == 0)
262 {
263 sstrncpy (disk_name, "root", sizeof (disk_name));
264 }
265 else
266 {
267 int i, len;
269 sstrncpy (disk_name, mnt_ptr->dir + 1, sizeof (disk_name));
270 len = strlen (disk_name);
272 for (i = 0; i < len; i++)
273 if (disk_name[i] == '/')
274 disk_name[i] = '-';
275 }
276 }
278 blocksize = BLOCKSIZE(statbuf);
280 if (report_reserved)
281 {
282 uint64_t blk_free;
283 uint64_t blk_reserved;
284 uint64_t blk_used;
286 /* Sanity-check for the values in the struct */
287 if (statbuf.f_bfree < statbuf.f_bavail)
288 statbuf.f_bfree = statbuf.f_bavail;
289 if (statbuf.f_blocks < statbuf.f_bfree)
290 statbuf.f_blocks = statbuf.f_bfree;
292 blk_free = (uint64_t) statbuf.f_bavail;
293 blk_reserved = (uint64_t) (statbuf.f_bfree - statbuf.f_bavail);
294 blk_used = (uint64_t) (statbuf.f_blocks - statbuf.f_bfree);
296 df_submit_one (disk_name, "df_complex", "free",
297 (gauge_t) (blk_free * blocksize));
298 df_submit_one (disk_name, "df_complex", "reserved",
299 (gauge_t) (blk_reserved * blocksize));
300 df_submit_one (disk_name, "df_complex", "used",
301 (gauge_t) (blk_used * blocksize));
302 }
303 else /* compatibility code */
304 {
305 gauge_t df_free;
306 gauge_t df_used;
308 df_free = statbuf.f_bfree * blocksize;
309 df_used = (statbuf.f_blocks - statbuf.f_bfree) * blocksize;
311 df_submit_two (disk_name, "df", df_used, df_free);
312 }
314 /* inode handling */
315 if (report_inodes)
316 {
317 uint64_t inode_free;
318 uint64_t inode_reserved;
319 uint64_t inode_used;
321 /* Sanity-check for the values in the struct */
322 if (statbuf.f_ffree < statbuf.f_favail)
323 statbuf.f_ffree = statbuf.f_favail;
324 if (statbuf.f_files < statbuf.f_ffree)
325 statbuf.f_files = statbuf.f_ffree;
327 inode_free = (uint64_t) statbuf.f_favail;
328 inode_reserved = (uint64_t) (statbuf.f_ffree - statbuf.f_favail);
329 inode_used = (uint64_t) (statbuf.f_files - statbuf.f_ffree);
331 df_submit_one (disk_name, "df_inodes", "free",
332 (gauge_t) inode_free);
333 df_submit_one (disk_name, "df_inodes", "reserved",
334 (gauge_t) inode_reserved);
335 df_submit_one (disk_name, "df_inodes", "used",
336 (gauge_t) inode_used);
337 }
338 }
340 cu_mount_freelist (mnt_list);
342 return (0);
343 } /* int df_read */
345 void module_register (void)
346 {
347 plugin_register_config ("df", df_config,
348 config_keys, config_keys_num);
349 plugin_register_init ("df", df_init);
350 plugin_register_read ("df", df_read);
351 } /* void module_register */