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