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