Merge branch 'collectd-4.10' into collectd-5.0
[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 };
59 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
60
61 static ignorelist_t *il_device = NULL;
62 static ignorelist_t *il_mountpoint = NULL;
63 static ignorelist_t *il_fstype = NULL;
64
65 static _Bool by_device = 0;
66 static _Bool report_inodes = 0;
67
68 static int df_init (void)
69 {
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);
76
77         return (0);
78 }
79
80 static int df_config (const char *key, const char *value)
81 {
82         df_init ();
83
84         if (strcasecmp (key, "Device") == 0)
85         {
86                 if (ignorelist_add (il_device, value))
87                         return (1);
88                 return (0);
89         }
90         else if (strcasecmp (key, "MountPoint") == 0)
91         {
92                 if (ignorelist_add (il_mountpoint, value))
93                         return (1);
94                 return (0);
95         }
96         else if (strcasecmp (key, "FSType") == 0)
97         {
98                 if (ignorelist_add (il_fstype, value))
99                         return (1);
100                 return (0);
101         }
102         else if (strcasecmp (key, "IgnoreSelected") == 0)
103         {
104                 if (IS_TRUE (value))
105                 {
106                         ignorelist_set_invert (il_device, 0);
107                         ignorelist_set_invert (il_mountpoint, 0);
108                         ignorelist_set_invert (il_fstype, 0);
109                 }
110                 else
111                 {
112                         ignorelist_set_invert (il_device, 1);
113                         ignorelist_set_invert (il_mountpoint, 1);
114                         ignorelist_set_invert (il_fstype, 1);
115                 }
116                 return (0);
117         }
118         else if (strcasecmp (key, "ReportByDevice") == 0)
119         {
120                 if (IS_TRUE (value))
121                         by_device = 1;
122
123                 return (0);
124         }
125         else if (strcasecmp (key, "ReportInodes") == 0)
126         {
127                 if (IS_TRUE (value))
128                         report_inodes = 1;
129                 else
130                         report_inodes = 0;
131
132                 return (0);
133         }
134
135
136         return (-1);
137 }
138
139 __attribute__ ((nonnull(2)))
140 static void df_submit_one (char *plugin_instance,
141                 const char *type, const char *type_instance,
142                 gauge_t value)
143 {
144         value_t values[1];
145         value_list_t vl = VALUE_LIST_INIT;
146
147         values[0].gauge = value;
148
149         vl.values = values;
150         vl.values_len = 1;
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));
160
161         plugin_dispatch_values (&vl);
162 } /* void df_submit_one */
163
164 static int df_read (void)
165 {
166 #if HAVE_STATVFS
167         struct statvfs statbuf;
168 #elif HAVE_STATFS
169         struct statfs statbuf;
170 #endif
171         /* struct STATANYFS statbuf; */
172         cu_mount_t *mnt_list;
173         cu_mount_t *mnt_ptr;
174
175         mnt_list = NULL;
176         if (cu_mount_getlist (&mnt_list) == NULL)
177         {
178                 ERROR ("df plugin: cu_mount_getlist failed.");
179                 return (-1);
180         }
181
182         for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
183         {
184                 unsigned long long blocksize;
185                 char disk_name[256];
186                 uint64_t blk_free;
187                 uint64_t blk_reserved;
188                 uint64_t blk_used;
189
190                 if (ignorelist_match (il_device,
191                                         (mnt_ptr->spec_device != NULL)
192                                         ? mnt_ptr->spec_device
193                                         : mnt_ptr->device))
194                         continue;
195                 if (ignorelist_match (il_mountpoint, mnt_ptr->dir))
196                         continue;
197                 if (ignorelist_match (il_fstype, mnt_ptr->type))
198                         continue;
199
200                 if (STATANYFS (mnt_ptr->dir, &statbuf) < 0)
201                 {
202                         char errbuf[1024];
203                         ERROR (STATANYFS_STR"(%s) failed: %s",
204                                         mnt_ptr->dir,
205                                         sstrerror (errno, errbuf,
206                                                 sizeof (errbuf)));
207                         continue;
208                 }
209
210                 if (!statbuf.f_blocks)
211                         continue;
212
213                 if (by_device) 
214                 {
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));
218                         else
219                                 sstrncpy (disk_name, mnt_ptr->spec_device, sizeof (disk_name));
220
221                         if (strlen(disk_name) < 1) 
222                         {
223                                 DEBUG("df: no device name name for mountpoint %s, skipping", mnt_ptr->dir);
224                                 continue;
225                         }
226                 } 
227                 else 
228                 {
229                         if (strcmp (mnt_ptr->dir, "/") == 0)
230                         {
231                                 sstrncpy (disk_name, "root", sizeof (disk_name));
232                         }
233                         else
234                         {
235                                 int i, len;
236
237                                 sstrncpy (disk_name, mnt_ptr->dir + 1, sizeof (disk_name));
238                                 len = strlen (disk_name);
239
240                                 for (i = 0; i < len; i++)
241                                         if (disk_name[i] == '/')
242                                                 disk_name[i] = '-';
243                         }
244                 }
245
246                 blocksize = BLOCKSIZE(statbuf);
247
248                 /*
249                  * Sanity-check for the values in the struct
250                  */
251                 /* Check for negative "available" byes. For example UFS can
252                  * report negative free space for user. Notice. blk_reserved
253                  * will start to diminish after this. */
254 #if HAVE_STATVFS
255                 /* Cast and temporary variable are needed to avoid
256                  * compiler warnings.
257                  * ((struct statvfs).f_bavail is unsigned (POSIX)) */
258                 int64_t signed_bavail = (int64_t) statbuf.f_bavail;
259                 if (signed_bavail < 0)
260                         statbuf.f_bavail = 0;
261 #elif HAVE_STATFS
262                 if (statbuf.f_bavail < 0)
263                         statbuf.f_bavail = 0;
264 #endif
265                 /* Make sure that f_blocks >= f_bfree >= f_bavail */
266                 if (statbuf.f_bfree < statbuf.f_bavail)
267                         statbuf.f_bfree = statbuf.f_bavail;
268                 if (statbuf.f_blocks < statbuf.f_bfree)
269                         statbuf.f_blocks = statbuf.f_bfree;
270
271                 blk_free     = (uint64_t) statbuf.f_bavail;
272                 blk_reserved = (uint64_t) (statbuf.f_bfree - statbuf.f_bavail);
273                 blk_used     = (uint64_t) (statbuf.f_blocks - statbuf.f_bfree);
274
275                 df_submit_one (disk_name, "df_complex", "free",
276                                 (gauge_t) (blk_free * blocksize));
277                 df_submit_one (disk_name, "df_complex", "reserved",
278                                 (gauge_t) (blk_reserved * blocksize));
279                 df_submit_one (disk_name, "df_complex", "used",
280                                 (gauge_t) (blk_used * blocksize));
281
282                 /* inode handling */
283                 if (report_inodes)
284                 {
285                         uint64_t inode_free;
286                         uint64_t inode_reserved;
287                         uint64_t inode_used;
288
289                         /* Sanity-check for the values in the struct */
290                         if (statbuf.f_ffree < statbuf.f_favail)
291                                 statbuf.f_ffree = statbuf.f_favail;
292                         if (statbuf.f_files < statbuf.f_ffree)
293                                 statbuf.f_files = statbuf.f_ffree;
294
295                         inode_free = (uint64_t) statbuf.f_favail;
296                         inode_reserved = (uint64_t) (statbuf.f_ffree - statbuf.f_favail);
297                         inode_used = (uint64_t) (statbuf.f_files - statbuf.f_ffree);
298                         
299                         df_submit_one (disk_name, "df_inodes", "free",
300                                         (gauge_t) inode_free);
301                         df_submit_one (disk_name, "df_inodes", "reserved",
302                                         (gauge_t) inode_reserved);
303                         df_submit_one (disk_name, "df_inodes", "used",
304                                         (gauge_t) inode_used);
305                 }
306         }
307
308         cu_mount_freelist (mnt_list);
309
310         return (0);
311 } /* int df_read */
312
313 void module_register (void)
314 {
315         plugin_register_config ("df", df_config,
316                         config_keys, config_keys_num);
317         plugin_register_init ("df", df_init);
318         plugin_register_read ("df", df_read);
319 } /* void module_register */