df plugin: Converted to the new plugin interface.
[collectd.git] / src / df.c
1 /**
2  * collectd - src/df.c
3  * Copyright (C) 2005-2007  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26 #include "utils_mount.h"
27 #include "utils_ignorelist.h"
28
29 #if HAVE_STATFS || HAVE_STATVFS
30 # define DF_HAVE_READ 1
31 #else
32 # define DF_HAVE_READ 0
33 #endif
34
35 #if HAVE_STATVFS
36 # if HAVE_SYS_STATVFS_H
37 #  include <sys/statvfs.h>
38 # endif
39 # define STATANYFS statvfs
40 # define BLOCKSIZE(s) ((s).f_frsize ? (s).f_frsize : (s).f_bsize)
41 #elif HAVE_STATFS
42 # if HAVE_SYS_STATFS_H
43 #  include <sys/statfs.h>
44 # endif
45 # define STATANYFS statfs
46 # define BLOCKSIZE(s) (s).f_bsize
47 #endif
48
49 /* 2^50 - 1 == 1125899906842623 = 1 Petabyte */
50 static data_source_t dsrc[2] =
51 {
52         {"free", DS_TYPE_GAUGE, 0, 1125899906842623.0},
53         {"used", DS_TYPE_GAUGE, 0, 1125899906842623.0}
54 };
55
56 static data_set_t ds =
57 {
58         "df", 2, dsrc
59 };
60
61 #if DF_HAVE_READ
62 static const char *config_keys[] =
63 {
64         "Device",
65         "MountPoint",
66         "FSType",
67         "IgnoreSelected",
68         NULL
69 };
70 static int config_keys_num = 4;
71
72 static ignorelist_t *il_device = NULL;
73 static ignorelist_t *il_mountpoint = NULL;
74 static ignorelist_t *il_fstype = NULL;
75
76 static int df_init (void)
77 {
78         if (il_device == NULL)
79                 il_device = ignorelist_create (1);
80         if (il_mountpoint == NULL)
81                 il_mountpoint = ignorelist_create (1);
82         if (il_fstype == NULL)
83                 il_fstype = ignorelist_create (1);
84
85         return (0);
86 }
87
88 static int df_config (const char *key, const char *value)
89 {
90         df_init ();
91
92         if (strcasecmp (key, "Device") == 0)
93         {
94                 if (ignorelist_add (il_device, value))
95                         return (1);
96                 return (0);
97         }
98         else if (strcasecmp (key, "MountPoint") == 0)
99         {
100                 if (ignorelist_add (il_mountpoint, value))
101                         return (1);
102                 return (0);
103         }
104         else if (strcasecmp (key, "FSType") == 0)
105         {
106                 if (ignorelist_add (il_fstype, value))
107                         return (1);
108                 return (0);
109         }
110         else if (strcasecmp (key, "IgnoreSelected") == 0)
111         {
112                 if ((strcasecmp (value, "True") == 0)
113                                 || (strcasecmp (value, "Yes") == 0)
114                                 || (strcasecmp (value, "On") == 0))
115                 {
116                         ignorelist_set_invert (il_device, 0);
117                         ignorelist_set_invert (il_mountpoint, 0);
118                         ignorelist_set_invert (il_fstype, 0);
119                 }
120                 else
121                 {
122                         ignorelist_set_invert (il_device, 1);
123                         ignorelist_set_invert (il_mountpoint, 1);
124                         ignorelist_set_invert (il_fstype, 1);
125                 }
126                 return (0);
127         }
128
129         return (-1);
130 }
131
132 static void df_submit (char *df_name,
133                 gauge_t df_used,
134                 gauge_t df_free)
135 {
136         value_t values[2];
137         value_list_t vl = VALUE_LIST_INIT;
138
139         values[0].gauge = df_used;
140         values[1].gauge = df_free;
141
142         vl.values = values;
143         vl.values_len = 2;
144         vl.time = time (NULL);
145         strcpy (vl.host, hostname);
146         strcpy (vl.plugin, "df");
147         strcpy (vl.plugin_instance, "");
148         strncpy (vl.type_instance, df_name, sizeof (vl.type_instance));
149
150         plugin_dispatch_values ("df", &vl);
151 } /* void df_submit */
152
153 static int df_read (void)
154 {
155 #if HAVE_STATVFS
156         struct statvfs statbuf;
157 #elif HAVE_STATFS
158         struct statfs statbuf;
159 #endif
160         /* struct STATANYFS statbuf; */
161         cu_mount_t *mnt_list;
162         cu_mount_t *mnt_ptr;
163
164         unsigned long long blocksize;
165         gauge_t df_free;
166         gauge_t df_used;
167         char mnt_name[256];
168
169         mnt_list = NULL;
170         if (cu_mount_getlist (&mnt_list) == NULL)
171                 return (-1);
172
173         for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
174         {
175                 if (STATANYFS (mnt_ptr->dir, &statbuf) < 0)
176                 {
177                         syslog (LOG_ERR, "statv?fs failed: %s", strerror (errno));
178                         continue;
179                 }
180
181                 if (!statbuf.f_blocks)
182                         continue;
183
184                 blocksize = BLOCKSIZE(statbuf);
185                 df_free = statbuf.f_bfree * blocksize;
186                 df_used = (statbuf.f_blocks - statbuf.f_bfree) * blocksize;
187
188                 if (strcmp (mnt_ptr->dir, "/") == 0)
189                 {
190                         strncpy (mnt_name, "root", sizeof (mnt_name));
191                 }
192                 else
193                 {
194                         int i, len;
195
196                         strncpy (mnt_name, mnt_ptr->dir + 1, sizeof (mnt_name));
197                         len = strlen (mnt_name);
198
199                         for (i = 0; i < len; i++)
200                                 if (mnt_name[i] == '/')
201                                         mnt_name[i] = '-';
202                 }
203
204                 if (ignorelist_match (il_device,
205                                         (mnt_ptr->spec_device != NULL)
206                                         ? mnt_ptr->spec_device
207                                         : mnt_ptr->device))
208                         continue;
209                 if (ignorelist_match (il_mountpoint, mnt_ptr->dir))
210                         continue;
211                 if (ignorelist_match (il_fstype, mnt_ptr->type))
212                         continue;
213
214                 df_submit (mnt_name, df_used, df_free);
215         }
216
217         cu_mount_freelist (mnt_list);
218
219         return (0);
220 } /* int df_read */
221 #endif /* DF_HAVE_READ */
222
223 void module_register (void)
224 {
225         plugin_register_data_set (&ds);
226
227 #if DF_HAVE_READ
228         plugin_register_config ("df", df_config, config_keys, config_keys_num);
229         plugin_register_init ("df", df_init);
230         plugin_register_read ("df", df_read);
231 #endif
232 } /* void module_register */