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