Merge branches 'ff/dns' and 'sh/email' into next
[collectd.git] / src / df.c
1 /**
2  * collectd - src/df.c
3  * Copyright (C) 2005,2006  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; either version 2 of the License, or (at your
8  * option) any later version.
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  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27 #include "utils_mount.h"
28 #include "utils_ignorelist.h"
29
30 #define MODULE_NAME "df"
31
32 #if HAVE_STATFS || HAVE_STATVFS
33 # define DF_HAVE_READ 1
34 #else
35 # define DF_HAVE_READ 0
36 #endif
37
38 #if HAVE_STATVFS
39 # if HAVE_SYS_STATVFS_H
40 #  include <sys/statvfs.h>
41 # endif
42 # define STATANYFS statvfs
43 # define BLOCKSIZE(s) ((s).f_frsize ? (s).f_frsize : (s).f_bsize)
44 #elif HAVE_STATFS
45 # if HAVE_SYS_STATFS_H
46 #  include <sys/statfs.h>
47 # endif
48 # define STATANYFS statfs
49 # define BLOCKSIZE(s) (s).f_bsize
50 #endif
51
52 static char *filename_template = "df-%s.rrd";
53
54 static char *ds_def[] =
55 {
56         "DS:used:GAUGE:"COLLECTD_HEARTBEAT":0:U",
57         "DS:free:GAUGE:"COLLECTD_HEARTBEAT":0:U",
58         NULL
59 };
60 static int ds_num = 2;
61
62 static 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 #define BUFSIZE 512
77
78 static void df_init (void)
79 {
80         if (il_device == NULL)
81                 il_device = ignorelist_create (1);
82         if (il_mountpoint == NULL)
83                 il_mountpoint = ignorelist_create (1);
84         if (il_fstype == NULL)
85                 il_fstype = ignorelist_create (1);
86
87         return;
88 }
89
90 static int df_config (char *key, char *value)
91 {
92         df_init ();
93
94         if (strcasecmp (key, "Device") == 0)
95         {
96                 if (ignorelist_add (il_device, value))
97                         return (1);
98                 return (0);
99         }
100         else if (strcasecmp (key, "MountPoint") == 0)
101         {
102                 if (ignorelist_add (il_mountpoint, value))
103                         return (1);
104                 return (0);
105         }
106         else if (strcasecmp (key, "FSType") == 0)
107         {
108                 if (ignorelist_add (il_fstype, value))
109                         return (1);
110                 return (0);
111         }
112         else if (strcasecmp (key, "IgnoreSelected") == 0)
113         {
114                 if ((strcasecmp (value, "True") == 0)
115                                 || (strcasecmp (value, "Yes") == 0)
116                                 || (strcasecmp (value, "On") == 0))
117                 {
118                         ignorelist_set_invert (il_device, 0);
119                         ignorelist_set_invert (il_mountpoint, 0);
120                         ignorelist_set_invert (il_fstype, 0);
121                 }
122                 else
123                 {
124                         ignorelist_set_invert (il_device, 1);
125                         ignorelist_set_invert (il_mountpoint, 1);
126                         ignorelist_set_invert (il_fstype, 1);
127                 }
128                 return (0);
129         }
130
131         return (-1);
132 }
133
134 static void df_write (char *host, char *inst, char *val)
135 {
136         char file[BUFSIZE];
137         int status;
138
139         status = snprintf (file, BUFSIZE, filename_template, inst);
140         if (status < 1)
141                 return;
142         else if (status >= BUFSIZE)
143                 return;
144
145         rrd_update_file (host, file, val, ds_def, ds_num);
146 }
147
148 #if DF_HAVE_READ
149 static void df_submit (char *df_name,
150                 unsigned long long df_used,
151                 unsigned long long df_free)
152 {
153         char buf[BUFSIZE];
154
155         if (snprintf (buf, BUFSIZE, "%u:%llu:%llu", (unsigned int) curtime,
156                                 df_used, df_free) >= BUFSIZE)
157                 return;
158
159         plugin_submit (MODULE_NAME, df_name, buf);
160 }
161
162 static void df_read (void)
163 {
164 #if HAVE_STATVFS
165         struct statvfs statbuf;
166 #elif HAVE_STATFS
167         struct statfs statbuf;
168 #endif
169         /* struct STATANYFS statbuf; */
170         cu_mount_t *mnt_list;
171         cu_mount_t *mnt_ptr;
172
173         unsigned long long blocksize;
174         unsigned long long df_free;
175         unsigned long long df_used;
176         char mnt_name[BUFSIZE];
177
178         mnt_list = NULL;
179         if (cu_mount_getlist (&mnt_list) == NULL)
180                 return;
181
182         for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
183         {
184                 if (STATANYFS (mnt_ptr->dir, &statbuf) < 0)
185                 {
186                         syslog (LOG_ERR, "statv?fs failed: %s", strerror (errno));
187                         continue;
188                 }
189
190                 if (!statbuf.f_blocks)
191                         continue;
192
193                 blocksize = BLOCKSIZE(statbuf);
194                 df_free = statbuf.f_bfree * blocksize;
195                 df_used = (statbuf.f_blocks - statbuf.f_bfree) * blocksize;
196
197                 if (strcmp (mnt_ptr->dir, "/") == 0)
198                 {
199                         strncpy (mnt_name, "root", BUFSIZE);
200                 }
201                 else
202                 {
203                         int i, len;
204
205                         strncpy (mnt_name, mnt_ptr->dir + 1, BUFSIZE);
206                         len = strlen (mnt_name);
207
208                         for (i = 0; i < len; i++)
209                                 if (mnt_name[i] == '/')
210                                         mnt_name[i] = '-';
211                 }
212
213                 if (ignorelist_match (il_device,
214                                         (mnt_ptr->spec_device != NULL)
215                                         ? mnt_ptr->spec_device
216                                         : mnt_ptr->device))
217                         continue;
218                 if (ignorelist_match (il_mountpoint, mnt_ptr->dir))
219                         continue;
220                 if (ignorelist_match (il_fstype, mnt_ptr->type))
221                         continue;
222
223                 df_submit (mnt_name, df_used, df_free);
224         }
225
226         cu_mount_freelist (mnt_list);
227 } /* static void df_read (void) */
228 #else
229 # define df_read NULL
230 #endif /* DF_HAVE_READ */
231
232 void module_register (void)
233 {
234         plugin_register (MODULE_NAME, df_init, df_read, df_write);
235         cf_register (MODULE_NAME, df_config, config_keys, config_keys_num);
236 }
237
238 #undef BUFSIZE
239 #undef MODULE_NAME