df plugin: Implement the "ReportReserved" option.
[collectd.git] / src / df.c
1 /**
2  * collectd - src/df.c
3  * Copyright (C) 2005-2007  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 };
56 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
57
58 static ignorelist_t *il_device = NULL;
59 static ignorelist_t *il_mountpoint = NULL;
60 static ignorelist_t *il_fstype = NULL;
61
62 static _Bool by_device = false;
63 static _Bool report_reserved = false;
64
65 static int df_init (void)
66 {
67         if (il_device == NULL)
68                 il_device = ignorelist_create (1);
69         if (il_mountpoint == NULL)
70                 il_mountpoint = ignorelist_create (1);
71         if (il_fstype == NULL)
72                 il_fstype = ignorelist_create (1);
73
74         return (0);
75 }
76
77 static int df_config (const char *key, const char *value)
78 {
79         df_init ();
80
81         if (strcasecmp (key, "Device") == 0)
82         {
83                 if (ignorelist_add (il_device, value))
84                         return (1);
85                 return (0);
86         }
87         else if (strcasecmp (key, "MountPoint") == 0)
88         {
89                 if (ignorelist_add (il_mountpoint, value))
90                         return (1);
91                 return (0);
92         }
93         else if (strcasecmp (key, "FSType") == 0)
94         {
95                 if (ignorelist_add (il_fstype, value))
96                         return (1);
97                 return (0);
98         }
99         else if (strcasecmp (key, "IgnoreSelected") == 0)
100         {
101                 if (IS_TRUE (value))
102                 {
103                         ignorelist_set_invert (il_device, 0);
104                         ignorelist_set_invert (il_mountpoint, 0);
105                         ignorelist_set_invert (il_fstype, 0);
106                 }
107                 else
108                 {
109                         ignorelist_set_invert (il_device, 1);
110                         ignorelist_set_invert (il_mountpoint, 1);
111                         ignorelist_set_invert (il_fstype, 1);
112                 }
113                 return (0);
114         }
115         else if (strcasecmp (key, "ReportByDevice") == 0)
116         {
117                 if (IS_TRUE (value))
118                         by_device = true;
119
120                 return (0);
121         }
122         else if (strcasecmp (key, "ReportReserved") == 0)
123         {
124                 if (IS_TRUE (value))
125                         report_reserved = true;
126                 else
127                         report_reserved = false;
128
129                 return (0);
130         }
131
132
133         return (-1);
134 }
135
136 static void df_submit_two (char *df_name,
137                 const char *type,
138                 gauge_t df_used,
139                 gauge_t df_free)
140 {
141         value_t values[2];
142         value_list_t vl = VALUE_LIST_INIT;
143
144         values[0].gauge = df_used;
145         values[1].gauge = df_free;
146
147         vl.values = values;
148         vl.values_len = 2;
149         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
150         sstrncpy (vl.plugin, "df", sizeof (vl.plugin));
151         sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
152         sstrncpy (vl.type, type, sizeof (vl.type));
153         sstrncpy (vl.type_instance, df_name, sizeof (vl.type_instance));
154
155         plugin_dispatch_values (&vl);
156 } /* void df_submit_two */
157
158 __attribute__ ((nonnull(2)))
159 static void df_submit_one (char *plugin_instance,
160                 const char *type, const char *type_instance,
161                 gauge_t value)
162 {
163         value_t values[1];
164         value_list_t vl = VALUE_LIST_INIT;
165
166         values[0].gauge = value;
167
168         vl.values = values;
169         vl.values_len = 1;
170         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
171         sstrncpy (vl.plugin, "df", sizeof (vl.plugin));
172         if (plugin_instance != NULL)
173                 sstrncpy (vl.plugin_instance, plugin_instance,
174                                 sizeof (vl.plugin_instance));
175         sstrncpy (vl.type, type, sizeof (vl.type));
176         if (type_instance != NULL)
177                 sstrncpy (vl.type_instance, type_instance,
178                                 sizeof (vl.type_instance));
179
180         plugin_dispatch_values (&vl);
181 } /* void df_submit_one */
182
183 static int df_read (void)
184 {
185 #if HAVE_STATVFS
186         struct statvfs statbuf;
187 #elif HAVE_STATFS
188         struct statfs statbuf;
189 #endif
190         /* struct STATANYFS statbuf; */
191         cu_mount_t *mnt_list;
192         cu_mount_t *mnt_ptr;
193
194         mnt_list = NULL;
195         if (cu_mount_getlist (&mnt_list) == NULL)
196                 return (-1);
197
198         for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
199         {
200                 unsigned long long blocksize;
201                 char disk_name[256];
202
203                 if (ignorelist_match (il_device,
204                                         (mnt_ptr->spec_device != NULL)
205                                         ? mnt_ptr->spec_device
206                                         : mnt_ptr->device))
207                         continue;
208                 if (ignorelist_match (il_mountpoint, mnt_ptr->dir))
209                         continue;
210                 if (ignorelist_match (il_fstype, mnt_ptr->type))
211                         continue;
212
213                 if (STATANYFS (mnt_ptr->dir, &statbuf) < 0)
214                 {
215                         char errbuf[1024];
216                         ERROR ("statv?fs failed: %s",
217                                         sstrerror (errno, errbuf,
218                                                 sizeof (errbuf)));
219                         continue;
220                 }
221
222                 if (!statbuf.f_blocks)
223                         continue;
224
225                 if (by_device) 
226                 {
227                         /* eg, /dev/hda1  -- strip off the "/dev/" */
228                         if (strncmp (mnt_ptr->spec_device, "/dev/", strlen ("/dev/")) == 0)
229                                 sstrncpy (disk_name, mnt_ptr->spec_device + strlen ("/dev/"), sizeof (disk_name));
230                         else
231                                 sstrncpy (disk_name, mnt_ptr->spec_device, sizeof (disk_name));
232
233                         if (strlen(disk_name) < 1) 
234                         {
235                                 DEBUG("df: no device name name for mountpoint %s, skipping", mnt_ptr->dir);
236                                 continue;
237                         }
238                 } 
239                 else 
240                 {
241                         if (strcmp (mnt_ptr->dir, "/") == 0)
242                         {
243                                 sstrncpy (disk_name, "root", sizeof (disk_name));
244                         }
245                         else
246                         {
247                                 int i, len;
248
249                                 sstrncpy (disk_name, mnt_ptr->dir + 1, sizeof (disk_name));
250                                 len = strlen (disk_name);
251
252                                 for (i = 0; i < len; i++)
253                                         if (disk_name[i] == '/')
254                                                 disk_name[i] = '-';
255                         }
256                 }
257
258                 blocksize = BLOCKSIZE(statbuf);
259
260                 if (report_reserved)
261                 {
262                         uint64_t blk_free;
263                         uint64_t blk_reserved;
264                         uint64_t blk_used;
265
266                         /* Sanity-check for the values in the struct */
267                         if (statbuf.f_bfree < statbuf.f_bavail)
268                                 statbuf.f_bfree = statbuf.f_bavail;
269                         if (statbuf.f_blocks < statbuf.f_bfree)
270                                 statbuf.f_blocks = statbuf.f_bfree;
271
272                         blk_free = (uint64_t) statbuf.f_bavail;
273                         blk_reserved = (uint64_t) (statbuf.f_bfree - statbuf.f_bavail);
274                         blk_used = (uint64_t) (statbuf.f_blocks - statbuf.f_bfree);
275                         
276                         df_submit_one (disk_name, "df_complex", "free",
277                                         (gauge_t) (blk_free * blocksize));
278                         df_submit_one (disk_name, "df_complex", "reserved",
279                                         (gauge_t) (blk_reserved * blocksize));
280                         df_submit_one (disk_name, "df_complex", "used",
281                                         (gauge_t) (blk_used * blocksize));
282                 }
283                 else /* compatibility code */
284                 {
285                         gauge_t df_free;
286                         gauge_t df_used;
287
288                         df_free = statbuf.f_bfree * blocksize;
289                         df_used = (statbuf.f_blocks - statbuf.f_bfree) * blocksize;
290
291                         df_submit_two (disk_name, "df", df_used, df_free);
292                 }
293
294                 /* inode handling */
295                 {
296                         uint64_t inode_free;
297                         uint64_t inode_reserved;
298                         uint64_t inode_used;
299
300                         /* Sanity-check for the values in the struct */
301                         if (statbuf.f_ffree < statbuf.f_favail)
302                                 statbuf.f_ffree = statbuf.f_favail;
303                         if (statbuf.f_files < statbuf.f_ffree)
304                                 statbuf.f_files = statbuf.f_ffree;
305
306                         inode_free = (uint64_t) statbuf.f_favail;
307                         inode_reserved = (uint64_t) (statbuf.f_ffree - statbuf.f_favail);
308                         inode_used = (uint64_t) (statbuf.f_files - statbuf.f_ffree);
309                         
310                         df_submit_one (disk_name, "df_inodes", "free",
311                                         (gauge_t) inode_free);
312                         df_submit_one (disk_name, "df_inodes", "reserved",
313                                         (gauge_t) inode_reserved);
314                         df_submit_one (disk_name, "df_inodes", "used",
315                                         (gauge_t) inode_used);
316                 }
317         }
318
319         cu_mount_freelist (mnt_list);
320
321         return (0);
322 } /* int df_read */
323
324 void module_register (void)
325 {
326         plugin_register_config ("df", df_config,
327                         config_keys, config_keys_num);
328         plugin_register_init ("df", df_init);
329         plugin_register_read ("df", df_read);
330 } /* void module_register */