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