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