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