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