df plugin: Remove the "ReportReserved" config option.
[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, "ReportInodes") == 0)
124         {
125                 if (IS_TRUE (value))
126                         report_inodes = true;
127                 else
128                         report_inodes = false;
129
130                 return (0);
131         }
132
133
134         return (-1);
135 }
136
137 __attribute__ ((nonnull(2)))
138 static void df_submit_one (char *plugin_instance,
139                 const char *type, const char *type_instance,
140                 gauge_t value)
141 {
142         value_t values[1];
143         value_list_t vl = VALUE_LIST_INIT;
144
145         values[0].gauge = value;
146
147         vl.values = values;
148         vl.values_len = 1;
149         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
150         sstrncpy (vl.plugin, "df", sizeof (vl.plugin));
151         if (plugin_instance != NULL)
152                 sstrncpy (vl.plugin_instance, plugin_instance,
153                                 sizeof (vl.plugin_instance));
154         sstrncpy (vl.type, type, sizeof (vl.type));
155         if (type_instance != NULL)
156                 sstrncpy (vl.type_instance, type_instance,
157                                 sizeof (vl.type_instance));
158
159         plugin_dispatch_values (&vl);
160 } /* void df_submit_one */
161
162 static int df_read (void)
163 {
164 #if HAVE_STATVFS
165         struct statvfs statbuf;
166 #elif HAVE_STATFS
167         struct statfs statbuf;
168 #endif
169         /* struct STATANYFS statbuf; */
170         cu_mount_t *mnt_list;
171         cu_mount_t *mnt_ptr;
172
173         mnt_list = NULL;
174         if (cu_mount_getlist (&mnt_list) == NULL)
175                 return (-1);
176
177         for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
178         {
179                 unsigned long long blocksize;
180                 char disk_name[256];
181                 uint64_t blk_free;
182                 uint64_t blk_reserved;
183                 uint64_t blk_used;
184
185                 if (ignorelist_match (il_device,
186                                         (mnt_ptr->spec_device != NULL)
187                                         ? mnt_ptr->spec_device
188                                         : mnt_ptr->device))
189                         continue;
190                 if (ignorelist_match (il_mountpoint, mnt_ptr->dir))
191                         continue;
192                 if (ignorelist_match (il_fstype, mnt_ptr->type))
193                         continue;
194
195                 if (STATANYFS (mnt_ptr->dir, &statbuf) < 0)
196                 {
197                         char errbuf[1024];
198                         ERROR ("statv?fs failed: %s",
199                                         sstrerror (errno, errbuf,
200                                                 sizeof (errbuf)));
201                         continue;
202                 }
203
204                 if (!statbuf.f_blocks)
205                         continue;
206
207                 if (by_device) 
208                 {
209                         /* eg, /dev/hda1  -- strip off the "/dev/" */
210                         if (strncmp (mnt_ptr->spec_device, "/dev/", strlen ("/dev/")) == 0)
211                                 sstrncpy (disk_name, mnt_ptr->spec_device + strlen ("/dev/"), sizeof (disk_name));
212                         else
213                                 sstrncpy (disk_name, mnt_ptr->spec_device, sizeof (disk_name));
214
215                         if (strlen(disk_name) < 1) 
216                         {
217                                 DEBUG("df: no device name name for mountpoint %s, skipping", mnt_ptr->dir);
218                                 continue;
219                         }
220                 } 
221                 else 
222                 {
223                         if (strcmp (mnt_ptr->dir, "/") == 0)
224                         {
225                                 sstrncpy (disk_name, "root", sizeof (disk_name));
226                         }
227                         else
228                         {
229                                 int i, len;
230
231                                 sstrncpy (disk_name, mnt_ptr->dir + 1, sizeof (disk_name));
232                                 len = strlen (disk_name);
233
234                                 for (i = 0; i < len; i++)
235                                         if (disk_name[i] == '/')
236                                                 disk_name[i] = '-';
237                         }
238                 }
239
240                 blocksize = BLOCKSIZE(statbuf);
241
242                 /* Sanity-check for the values in the struct */
243                 if (statbuf.f_bfree < statbuf.f_bavail)
244                         statbuf.f_bfree = statbuf.f_bavail;
245                 if (statbuf.f_blocks < statbuf.f_bfree)
246                         statbuf.f_blocks = statbuf.f_bfree;
247
248                 blk_free     = (uint64_t) statbuf.f_bavail;
249                 blk_reserved = (uint64_t) (statbuf.f_bfree - statbuf.f_bavail);
250                 blk_used     = (uint64_t) (statbuf.f_blocks - statbuf.f_bfree);
251
252                 df_submit_one (disk_name, "df_complex", "free",
253                                 (gauge_t) (blk_free * blocksize));
254                 df_submit_one (disk_name, "df_complex", "reserved",
255                                 (gauge_t) (blk_reserved * blocksize));
256                 df_submit_one (disk_name, "df_complex", "used",
257                                 (gauge_t) (blk_used * blocksize));
258
259                 /* inode handling */
260                 if (report_inodes)
261                 {
262                         uint64_t inode_free;
263                         uint64_t inode_reserved;
264                         uint64_t inode_used;
265
266                         /* Sanity-check for the values in the struct */
267                         if (statbuf.f_ffree < statbuf.f_favail)
268                                 statbuf.f_ffree = statbuf.f_favail;
269                         if (statbuf.f_files < statbuf.f_ffree)
270                                 statbuf.f_files = statbuf.f_ffree;
271
272                         inode_free = (uint64_t) statbuf.f_favail;
273                         inode_reserved = (uint64_t) (statbuf.f_ffree - statbuf.f_favail);
274                         inode_used = (uint64_t) (statbuf.f_files - statbuf.f_ffree);
275                         
276                         df_submit_one (disk_name, "df_inodes", "free",
277                                         (gauge_t) inode_free);
278                         df_submit_one (disk_name, "df_inodes", "reserved",
279                                         (gauge_t) inode_reserved);
280                         df_submit_one (disk_name, "df_inodes", "used",
281                                         (gauge_t) inode_used);
282                 }
283         }
284
285         cu_mount_freelist (mnt_list);
286
287         return (0);
288 } /* int df_read */
289
290 void module_register (void)
291 {
292         plugin_register_config ("df", df_config,
293                         config_keys, config_keys_num);
294         plugin_register_init ("df", df_init);
295         plugin_register_read ("df", df_read);
296 } /* void module_register */