Merge branch 'collectd-4.10'
[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 BLOCKSIZE(s) ((s).f_frsize ? (s).f_frsize : (s).f_bsize)
37 #elif HAVE_STATFS
38 # if HAVE_SYS_STATFS_H
39 #  include <sys/statfs.h>
40 # endif
41 # define STATANYFS statfs
42 # define BLOCKSIZE(s) (s).f_bsize
43 #else
44 # error "No applicable input method."
45 #endif
46
47 static const char *config_keys[] =
48 {
49         "Device",
50         "MountPoint",
51         "FSType",
52         "IgnoreSelected",
53         "ReportByDevice",
54         "ReportReserved",
55         "ReportInodes"
56 };
57 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
58
59 static ignorelist_t *il_device = NULL;
60 static ignorelist_t *il_mountpoint = NULL;
61 static ignorelist_t *il_fstype = NULL;
62
63 static _Bool by_device = false;
64 static _Bool report_inodes = false;
65
66 static int df_init (void)
67 {
68         if (il_device == NULL)
69                 il_device = ignorelist_create (1);
70         if (il_mountpoint == NULL)
71                 il_mountpoint = ignorelist_create (1);
72         if (il_fstype == NULL)
73                 il_fstype = ignorelist_create (1);
74
75         return (0);
76 }
77
78 static int df_config (const char *key, const char *value)
79 {
80         df_init ();
81
82         if (strcasecmp (key, "Device") == 0)
83         {
84                 if (ignorelist_add (il_device, value))
85                         return (1);
86                 return (0);
87         }
88         else if (strcasecmp (key, "MountPoint") == 0)
89         {
90                 if (ignorelist_add (il_mountpoint, value))
91                         return (1);
92                 return (0);
93         }
94         else if (strcasecmp (key, "FSType") == 0)
95         {
96                 if (ignorelist_add (il_fstype, value))
97                         return (1);
98                 return (0);
99         }
100         else if (strcasecmp (key, "IgnoreSelected") == 0)
101         {
102                 if (IS_TRUE (value))
103                 {
104                         ignorelist_set_invert (il_device, 0);
105                         ignorelist_set_invert (il_mountpoint, 0);
106                         ignorelist_set_invert (il_fstype, 0);
107                 }
108                 else
109                 {
110                         ignorelist_set_invert (il_device, 1);
111                         ignorelist_set_invert (il_mountpoint, 1);
112                         ignorelist_set_invert (il_fstype, 1);
113                 }
114                 return (0);
115         }
116         else if (strcasecmp (key, "ReportByDevice") == 0)
117         {
118                 if (IS_TRUE (value))
119                         by_device = true;
120
121                 return (0);
122         }
123         else if (strcasecmp (key, "ReportReserved") == 0)
124         {
125                 /* Nop for backwards compatibility. */
126                 return (0);
127         }
128         else if (strcasecmp (key, "ReportInodes") == 0)
129         {
130                 if (IS_TRUE (value))
131                         report_inodes = true;
132                 else
133                         report_inodes = false;
134
135                 return (0);
136         }
137
138
139         return (-1);
140 }
141
142 __attribute__ ((nonnull(2)))
143 static void df_submit_one (char *plugin_instance,
144                 const char *type, const char *type_instance,
145                 gauge_t value)
146 {
147         value_t values[1];
148         value_list_t vl = VALUE_LIST_INIT;
149
150         values[0].gauge = value;
151
152         vl.values = values;
153         vl.values_len = 1;
154         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
155         sstrncpy (vl.plugin, "df", sizeof (vl.plugin));
156         if (plugin_instance != NULL)
157                 sstrncpy (vl.plugin_instance, plugin_instance,
158                                 sizeof (vl.plugin_instance));
159         sstrncpy (vl.type, type, sizeof (vl.type));
160         if (type_instance != NULL)
161                 sstrncpy (vl.type_instance, type_instance,
162                                 sizeof (vl.type_instance));
163
164         plugin_dispatch_values (&vl);
165 } /* void df_submit_one */
166
167 static int df_read (void)
168 {
169 #if HAVE_STATVFS
170         struct statvfs statbuf;
171 #elif HAVE_STATFS
172         struct statfs statbuf;
173 #endif
174         /* struct STATANYFS statbuf; */
175         cu_mount_t *mnt_list;
176         cu_mount_t *mnt_ptr;
177
178         mnt_list = NULL;
179         if (cu_mount_getlist (&mnt_list) == NULL)
180                 return (-1);
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 ("statv?fs failed: %s",
204                                         sstrerror (errno, errbuf,
205                                                 sizeof (errbuf)));
206                         continue;
207                 }
208
209                 if (!statbuf.f_blocks)
210                         continue;
211
212                 if (by_device) 
213                 {
214                         /* eg, /dev/hda1  -- strip off the "/dev/" */
215                         if (strncmp (mnt_ptr->spec_device, "/dev/", strlen ("/dev/")) == 0)
216                                 sstrncpy (disk_name, mnt_ptr->spec_device + strlen ("/dev/"), sizeof (disk_name));
217                         else
218                                 sstrncpy (disk_name, mnt_ptr->spec_device, sizeof (disk_name));
219
220                         if (strlen(disk_name) < 1) 
221                         {
222                                 DEBUG("df: no device name name for mountpoint %s, skipping", mnt_ptr->dir);
223                                 continue;
224                         }
225                 } 
226                 else 
227                 {
228                         if (strcmp (mnt_ptr->dir, "/") == 0)
229                         {
230                                 sstrncpy (disk_name, "root", sizeof (disk_name));
231                         }
232                         else
233                         {
234                                 int i, len;
235
236                                 sstrncpy (disk_name, mnt_ptr->dir + 1, sizeof (disk_name));
237                                 len = strlen (disk_name);
238
239                                 for (i = 0; i < len; i++)
240                                         if (disk_name[i] == '/')
241                                                 disk_name[i] = '-';
242                         }
243                 }
244
245                 blocksize = BLOCKSIZE(statbuf);
246
247                 /* Sanity-check for the values in the struct */
248                 if (statbuf.f_bfree < statbuf.f_bavail)
249                         statbuf.f_bfree = statbuf.f_bavail;
250                 if (statbuf.f_blocks < statbuf.f_bfree)
251                         statbuf.f_blocks = statbuf.f_bfree;
252
253                 blk_free     = (uint64_t) statbuf.f_bavail;
254                 blk_reserved = (uint64_t) (statbuf.f_bfree - statbuf.f_bavail);
255                 blk_used     = (uint64_t) (statbuf.f_blocks - statbuf.f_bfree);
256
257                 df_submit_one (disk_name, "df_complex", "free",
258                                 (gauge_t) (blk_free * blocksize));
259                 df_submit_one (disk_name, "df_complex", "reserved",
260                                 (gauge_t) (blk_reserved * blocksize));
261                 df_submit_one (disk_name, "df_complex", "used",
262                                 (gauge_t) (blk_used * blocksize));
263
264                 /* inode handling */
265                 if (report_inodes)
266                 {
267                         uint64_t inode_free;
268                         uint64_t inode_reserved;
269                         uint64_t inode_used;
270
271                         /* Sanity-check for the values in the struct */
272                         if (statbuf.f_ffree < statbuf.f_favail)
273                                 statbuf.f_ffree = statbuf.f_favail;
274                         if (statbuf.f_files < statbuf.f_ffree)
275                                 statbuf.f_files = statbuf.f_ffree;
276
277                         inode_free = (uint64_t) statbuf.f_favail;
278                         inode_reserved = (uint64_t) (statbuf.f_ffree - statbuf.f_favail);
279                         inode_used = (uint64_t) (statbuf.f_files - statbuf.f_ffree);
280                         
281                         df_submit_one (disk_name, "df_inodes", "free",
282                                         (gauge_t) inode_free);
283                         df_submit_one (disk_name, "df_inodes", "reserved",
284                                         (gauge_t) inode_reserved);
285                         df_submit_one (disk_name, "df_inodes", "used",
286                                         (gauge_t) inode_used);
287                 }
288         }
289
290         cu_mount_freelist (mnt_list);
291
292         return (0);
293 } /* int df_read */
294
295 void module_register (void)
296 {
297         plugin_register_config ("df", df_config,
298                         config_keys, config_keys_num);
299         plugin_register_init ("df", df_init);
300         plugin_register_read ("df", df_read);
301 } /* void module_register */