df plugin: skip duplicate entries, fixes "uc_update: Value too old" error
[collectd.git] / src / df.c
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  **/
23
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
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
48
49 static const char *config_keys[] =
50 {
51         "Device",
52         "MountPoint",
53         "FSType",
54         "IgnoreSelected",
55         "ReportByDevice",
56         "ReportReserved",
57         "ReportInodes",
58         "ValuesAbsolute",
59         "ValuesPercentage"
60 };
61 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
62
63 static ignorelist_t *il_device = NULL;
64 static ignorelist_t *il_mountpoint = NULL;
65 static ignorelist_t *il_fstype = NULL;
66
67 static _Bool by_device = 0;
68 static _Bool report_inodes = 0;
69 static _Bool values_absolute = 1;
70 static _Bool values_percentage = 0;
71
72 static int df_init (void)
73 {
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);
80
81         return (0);
82 }
83
84 static int df_config (const char *key, const char *value)
85 {
86         df_init ();
87
88         if (strcasecmp (key, "Device") == 0)
89         {
90                 if (ignorelist_add (il_device, value))
91                         return (1);
92                 return (0);
93         }
94         else if (strcasecmp (key, "MountPoint") == 0)
95         {
96                 if (ignorelist_add (il_mountpoint, value))
97                         return (1);
98                 return (0);
99         }
100         else if (strcasecmp (key, "FSType") == 0)
101         {
102                 if (ignorelist_add (il_fstype, value))
103                         return (1);
104                 return (0);
105         }
106         else if (strcasecmp (key, "IgnoreSelected") == 0)
107         {
108                 if (IS_TRUE (value))
109                 {
110                         ignorelist_set_invert (il_device, 0);
111                         ignorelist_set_invert (il_mountpoint, 0);
112                         ignorelist_set_invert (il_fstype, 0);
113                 }
114                 else
115                 {
116                         ignorelist_set_invert (il_device, 1);
117                         ignorelist_set_invert (il_mountpoint, 1);
118                         ignorelist_set_invert (il_fstype, 1);
119                 }
120                 return (0);
121         }
122         else if (strcasecmp (key, "ReportByDevice") == 0)
123         {
124                 if (IS_TRUE (value))
125                         by_device = 1;
126
127                 return (0);
128         }
129         else if (strcasecmp (key, "ReportInodes") == 0)
130         {
131                 if (IS_TRUE (value))
132                         report_inodes = 1;
133                 else
134                         report_inodes = 0;
135
136                 return (0);
137         }
138         else if (strcasecmp (key, "ValuesAbsolute") == 0)
139         {
140                 if (IS_TRUE (value))
141                         values_absolute = 1;
142                 else
143                         values_absolute = 0;
144
145                 return (0);
146         }
147         else if (strcasecmp (key, "ValuesPercentage") == 0)
148         {
149                 if (IS_TRUE (value))
150                         values_percentage = 1;
151                 else
152                         values_percentage = 0;
153
154                 return (0);
155         }
156
157         return (-1);
158 }
159
160 __attribute__ ((nonnull(2)))
161 static void df_submit_one (char *plugin_instance,
162                 const char *type, const char *type_instance,
163                 gauge_t value)
164 {
165         value_t values[1];
166         value_list_t vl = VALUE_LIST_INIT;
167
168         values[0].gauge = value;
169
170         vl.values = values;
171         vl.values_len = 1;
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));
181
182         plugin_dispatch_values (&vl);
183 } /* void df_submit_one */
184
185 static int df_read (void)
186 {
187 #if HAVE_STATVFS
188         struct statvfs statbuf;
189 #elif HAVE_STATFS
190         struct statfs statbuf;
191 #endif
192         /* struct STATANYFS statbuf; */
193         cu_mount_t *mnt_list;
194         cu_mount_t *mnt_ptr;
195
196         mnt_list = NULL;
197         if (cu_mount_getlist (&mnt_list) == NULL)
198         {
199                 ERROR ("df plugin: cu_mount_getlist failed.");
200                 return (-1);
201         }
202
203         for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
204         {
205                 unsigned long long blocksize;
206                 char disk_name[256];
207         cu_mount_t *mnt_dup_ptr;
208                 uint64_t blk_free;
209                 uint64_t blk_reserved;
210                 uint64_t blk_used;
211
212                 char const *dev = (mnt_ptr->spec_device != NULL)
213                         ? mnt_ptr->spec_device
214                         : mnt_ptr->device;
215
216                 if (ignorelist_match (il_device, dev))
217                         continue;
218                 if (ignorelist_match (il_mountpoint, mnt_ptr->dir))
219                         continue;
220                 if (ignorelist_match (il_fstype, mnt_ptr->type))
221                         continue;
222
223                 /* ignore duplicates */
224                 for (mnt_dup_ptr = mnt_ptr; mnt_dup_ptr != NULL; mnt_dup_ptr = mnt_dup_ptr->next)
225                 {
226                         if (by_device) {
227                                 if (strcmp (mnt_ptr->spec_device, mnt_dup_ptr->spec_device) == 0)
228                                         continue;
229                         }
230                         else
231                         {
232                                 if (strcmp (mnt_ptr->dir, mnt_dup_ptr->dir) == 0)
233                                         continue;
234                         }
235                 }
236
237                 if (STATANYFS (mnt_ptr->dir, &statbuf) < 0)
238                 {
239                         char errbuf[1024];
240                         ERROR (STATANYFS_STR"(%s) failed: %s",
241                                         mnt_ptr->dir,
242                                         sstrerror (errno, errbuf,
243                                                 sizeof (errbuf)));
244                         continue;
245                 }
246
247                 if (!statbuf.f_blocks)
248                         continue;
249
250                 if (by_device)
251                 {
252                         /* eg, /dev/hda1  -- strip off the "/dev/" */
253                         if (strncmp (dev, "/dev/", strlen ("/dev/")) == 0)
254                                 sstrncpy (disk_name, dev + strlen ("/dev/"), sizeof (disk_name));
255                         else
256                                 sstrncpy (disk_name, dev, sizeof (disk_name));
257
258                         if (strlen(disk_name) < 1)
259                         {
260                                 DEBUG("df: no device name for mountpoint %s, skipping", mnt_ptr->dir);
261                                 continue;
262                         }
263                 }
264                 else
265                 {
266                         if (strcmp (mnt_ptr->dir, "/") == 0)
267                         {
268                                 if (strcmp (mnt_ptr->type, "rootfs") == 0)
269                                         continue;
270                                 sstrncpy (disk_name, "root", sizeof (disk_name));
271                         }
272                         else
273                         {
274                                 int i, len;
275
276                                 sstrncpy (disk_name, mnt_ptr->dir + 1, sizeof (disk_name));
277                                 len = strlen (disk_name);
278
279                                 for (i = 0; i < len; i++)
280                                         if (disk_name[i] == '/')
281                                                 disk_name[i] = '-';
282                         }
283                 }
284
285                 blocksize = BLOCKSIZE(statbuf);
286
287                 /*
288                  * Sanity-check for the values in the struct
289                  */
290                 /* Check for negative "available" byes. For example UFS can
291                  * report negative free space for user. Notice. blk_reserved
292                  * will start to diminish after this. */
293 #if HAVE_STATVFS
294                 /* Cast and temporary variable are needed to avoid
295                  * compiler warnings.
296                  * ((struct statvfs).f_bavail is unsigned (POSIX)) */
297                 int64_t signed_bavail = (int64_t) statbuf.f_bavail;
298                 if (signed_bavail < 0)
299                         statbuf.f_bavail = 0;
300 #elif HAVE_STATFS
301                 if (statbuf.f_bavail < 0)
302                         statbuf.f_bavail = 0;
303 #endif
304                 /* Make sure that f_blocks >= f_bfree >= f_bavail */
305                 if (statbuf.f_bfree < statbuf.f_bavail)
306                         statbuf.f_bfree = statbuf.f_bavail;
307                 if (statbuf.f_blocks < statbuf.f_bfree)
308                         statbuf.f_blocks = statbuf.f_bfree;
309
310                 blk_free     = (uint64_t) statbuf.f_bavail;
311                 blk_reserved = (uint64_t) (statbuf.f_bfree - statbuf.f_bavail);
312                 blk_used     = (uint64_t) (statbuf.f_blocks - statbuf.f_bfree);
313
314                 if (values_absolute)
315                 {
316                         df_submit_one (disk_name, "df_complex", "free",
317                                 (gauge_t) (blk_free * blocksize));
318                         df_submit_one (disk_name, "df_complex", "reserved",
319                                 (gauge_t) (blk_reserved * blocksize));
320                         df_submit_one (disk_name, "df_complex", "used",
321                                 (gauge_t) (blk_used * blocksize));
322                 }
323
324                 if (values_percentage)
325                 {
326                         if (statbuf.f_blocks > 0)
327                                 {
328                                 df_submit_one (disk_name, "percent_bytes", "free",
329                                         (gauge_t) ((float_t)(blk_free) / statbuf.f_blocks * 100));
330                                 df_submit_one (disk_name, "percent_bytes", "reserved",
331                                         (gauge_t) ((float_t)(blk_reserved) / statbuf.f_blocks * 100));
332                                 df_submit_one (disk_name, "percent_bytes", "used",
333                                         (gauge_t) ((float_t)(blk_used) / statbuf.f_blocks * 100));
334                                 }
335                         else return (-1);
336                 }
337
338                 /* inode handling */
339                 if (report_inodes && statbuf.f_files != 0 && statbuf.f_ffree != 0)
340                 {
341                         uint64_t inode_free;
342                         uint64_t inode_reserved;
343                         uint64_t inode_used;
344
345                         /* Sanity-check for the values in the struct */
346                         if (statbuf.f_ffree < statbuf.f_favail)
347                                 statbuf.f_ffree = statbuf.f_favail;
348                         if (statbuf.f_files < statbuf.f_ffree)
349                                 statbuf.f_files = statbuf.f_ffree;
350
351                         inode_free = (uint64_t) statbuf.f_favail;
352                         inode_reserved = (uint64_t) (statbuf.f_ffree - statbuf.f_favail);
353                         inode_used = (uint64_t) (statbuf.f_files - statbuf.f_ffree);
354
355                         if (values_percentage)
356                         {
357                                 if (statbuf.f_files > 0)
358                                 {
359                                         df_submit_one (disk_name, "percent_inodes", "free",
360                                                 (gauge_t) ((float_t)(inode_free) / statbuf.f_files * 100));
361                                         df_submit_one (disk_name, "percent_inodes", "reserved",
362                                                 (gauge_t) ((float_t)(inode_reserved) / statbuf.f_files * 100));
363                                         df_submit_one (disk_name, "percent_inodes", "used",
364                                                 (gauge_t) ((float_t)(inode_used) / statbuf.f_files * 100));
365                                 }
366                                 else return (-1);
367                         }
368                         if (values_absolute)
369                         {
370                                 df_submit_one (disk_name, "df_inodes", "free",
371                                                 (gauge_t) inode_free);
372                                 df_submit_one (disk_name, "df_inodes", "reserved",
373                                                 (gauge_t) inode_reserved);
374                                 df_submit_one (disk_name, "df_inodes", "used",
375                                                 (gauge_t) inode_used);
376                         }
377                 }
378         }
379
380         cu_mount_freelist (mnt_list);
381
382         return (0);
383 } /* int df_read */
384
385 void module_register (void)
386 {
387         plugin_register_config ("df", df_config,
388                         config_keys, config_keys_num);
389         plugin_register_init ("df", df_init);
390         plugin_register_read ("df", df_read);
391 } /* void module_register */