df plugin: Add option to report by mountpoint or devicename
[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_STATVFS
30 # if HAVE_SYS_STATVFS_H
31 #  include <sys/statvfs.h>
32 # endif
33 # define STATANYFS statvfs
34 # define BLOCKSIZE(s) ((s).f_frsize ? (s).f_frsize : (s).f_bsize)
35 #elif HAVE_STATFS
36 # if HAVE_SYS_STATFS_H
37 #  include <sys/statfs.h>
38 # endif
39 # define STATANYFS statfs
40 # define BLOCKSIZE(s) (s).f_bsize
41 #else
42 # error "No applicable input method."
43 #endif
44
45 static const char *config_keys[] =
46 {
47         "Device",
48         "MountPoint",
49         "FSType",
50         "IgnoreSelected",
51         "ReportByDevice",
52         NULL
53 };
54 static int config_keys_num = 5;
55
56 static ignorelist_t *il_device = NULL;
57 static ignorelist_t *il_mountpoint = NULL;
58 static ignorelist_t *il_fstype = NULL;
59
60 static bool by_device = false;
61
62 static int df_init (void)
63 {
64         if (il_device == NULL)
65                 il_device = ignorelist_create (1);
66         if (il_mountpoint == NULL)
67                 il_mountpoint = ignorelist_create (1);
68         if (il_fstype == NULL)
69                 il_fstype = ignorelist_create (1);
70
71         return (0);
72 }
73
74 static int df_config (const char *key, const char *value)
75 {
76         df_init ();
77
78         if (strcasecmp (key, "Device") == 0)
79         {
80                 if (ignorelist_add (il_device, value))
81                         return (1);
82                 return (0);
83         }
84         else if (strcasecmp (key, "MountPoint") == 0)
85         {
86                 if (ignorelist_add (il_mountpoint, value))
87                         return (1);
88                 return (0);
89         }
90         else if (strcasecmp (key, "FSType") == 0)
91         {
92                 if (ignorelist_add (il_fstype, value))
93                         return (1);
94                 return (0);
95         }
96         else if (strcasecmp (key, "IgnoreSelected") == 0)
97         {
98                 if ((strcasecmp (value, "True") == 0)
99                                 || (strcasecmp (value, "Yes") == 0)
100                                 || (strcasecmp (value, "On") == 0))
101                 {
102                         ignorelist_set_invert (il_device, 0);
103                         ignorelist_set_invert (il_mountpoint, 0);
104                         ignorelist_set_invert (il_fstype, 0);
105                 }
106                 else
107                 {
108                         ignorelist_set_invert (il_device, 1);
109                         ignorelist_set_invert (il_mountpoint, 1);
110                         ignorelist_set_invert (il_fstype, 1);
111                 }
112                 return (0);
113         }
114         else if (strcasecmp (key, "ReportByDevice") == 0)
115         {
116                 if ((strcasecmp (value, "True") == 0)
117                                 || (strcasecmp (value, "Yes") == 0)
118                                 || (strcasecmp (value, "On") == 0))
119                 {
120                         by_device = false;
121                 }
122                 return (0);
123         }
124
125         return (-1);
126 }
127
128 static void df_submit (char *df_name,
129                 gauge_t df_used,
130                 gauge_t df_free)
131 {
132         value_t values[2];
133         value_list_t vl = VALUE_LIST_INIT;
134
135         values[0].gauge = df_used;
136         values[1].gauge = df_free;
137
138         vl.values = values;
139         vl.values_len = 2;
140         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
141         sstrncpy (vl.plugin, "df", sizeof (vl.plugin));
142         sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
143         sstrncpy (vl.type, "df", sizeof (vl.host));
144         sstrncpy (vl.type_instance, df_name, sizeof (vl.type_instance));
145
146         plugin_dispatch_values (&vl);
147 } /* void df_submit */
148
149 static int df_read (void)
150 {
151 #if HAVE_STATVFS
152         struct statvfs statbuf;
153 #elif HAVE_STATFS
154         struct statfs statbuf;
155 #endif
156         /* struct STATANYFS statbuf; */
157         cu_mount_t *mnt_list;
158         cu_mount_t *mnt_ptr;
159
160         unsigned long long blocksize;
161         gauge_t df_free;
162         gauge_t df_used;
163         char disk_name[256];
164
165         mnt_list = NULL;
166         if (cu_mount_getlist (&mnt_list) == NULL)
167                 return (-1);
168
169         for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
170         {
171                 if (STATANYFS (mnt_ptr->dir, &statbuf) < 0)
172                 {
173                         char errbuf[1024];
174                         ERROR ("statv?fs failed: %s",
175                                         sstrerror (errno, errbuf,
176                                                 sizeof (errbuf)));
177                         continue;
178                 }
179
180                 if (!statbuf.f_blocks)
181                         continue;
182
183                 blocksize = BLOCKSIZE(statbuf);
184                 df_free = statbuf.f_bfree * blocksize;
185                 df_used = (statbuf.f_blocks - statbuf.f_bfree) * blocksize;
186
187                 if (by_device) 
188                 {
189                         // eg, /dev/hda1  -- strip off the "/dev/"
190                         strncpy (disk_name, mnt_ptr->device + 5, sizeof (disk_name));
191                 } 
192                 else 
193                 {
194                         if (strcmp (mnt_ptr->dir, "/") == 0)
195                         {
196                                 sstrncpy (disk_name, "root", sizeof (disk_name));
197                         }
198                         else
199                         {
200                                 int i, len;
201
202                                 sstrncpy (disk_name, mnt_ptr->dir + 1, sizeof (disk_name));
203                                 len = strlen (disk_name);
204
205                                 for (i = 0; i < len; i++)
206                                         if (disk_name[i] == '/')
207                                                 disk_name[i] = '-';
208                         }
209                 }
210
211                 if (ignorelist_match (il_device,
212                                         (mnt_ptr->spec_device != NULL)
213                                         ? mnt_ptr->spec_device
214                                         : mnt_ptr->device))
215                         continue;
216                 if (ignorelist_match (il_mountpoint, mnt_ptr->dir))
217                         continue;
218                 if (ignorelist_match (il_fstype, mnt_ptr->type))
219                         continue;
220
221                 df_submit (disk_name, df_used, df_free);
222         }
223
224         cu_mount_freelist (mnt_list);
225
226         return (0);
227 } /* int df_read */
228
229 void module_register (void)
230 {
231         plugin_register_config ("df", df_config,
232                         config_keys, config_keys_num);
233         plugin_register_init ("df", df_init);
234         plugin_register_read ("df", df_read);
235 } /* void module_register */