3 * Copyright (C) 2005-2007 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
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
32 # define DF_HAVE_READ 0
36 # if HAVE_SYS_STATVFS_H
37 # include <sys/statvfs.h>
39 # define STATANYFS statvfs
40 # define BLOCKSIZE(s) ((s).f_frsize ? (s).f_frsize : (s).f_bsize)
42 # if HAVE_SYS_STATFS_H
43 # include <sys/statfs.h>
45 # define STATANYFS statfs
46 # define BLOCKSIZE(s) (s).f_bsize
49 /* 2^50 - 1 == 1125899906842623 = 1 Petabyte */
50 static data_source_t dsrc[2] =
52 {"free", DS_TYPE_GAUGE, 0, 1125899906842623.0},
53 {"used", DS_TYPE_GAUGE, 0, 1125899906842623.0}
56 static data_set_t ds =
62 static const char *config_keys[] =
70 static int config_keys_num = 4;
72 static ignorelist_t *il_device = NULL;
73 static ignorelist_t *il_mountpoint = NULL;
74 static ignorelist_t *il_fstype = NULL;
76 static int df_init (void)
78 if (il_device == NULL)
79 il_device = ignorelist_create (1);
80 if (il_mountpoint == NULL)
81 il_mountpoint = ignorelist_create (1);
82 if (il_fstype == NULL)
83 il_fstype = ignorelist_create (1);
88 static int df_config (const char *key, const char *value)
92 if (strcasecmp (key, "Device") == 0)
94 if (ignorelist_add (il_device, value))
98 else if (strcasecmp (key, "MountPoint") == 0)
100 if (ignorelist_add (il_mountpoint, value))
104 else if (strcasecmp (key, "FSType") == 0)
106 if (ignorelist_add (il_fstype, value))
110 else if (strcasecmp (key, "IgnoreSelected") == 0)
112 if ((strcasecmp (value, "True") == 0)
113 || (strcasecmp (value, "Yes") == 0)
114 || (strcasecmp (value, "On") == 0))
116 ignorelist_set_invert (il_device, 0);
117 ignorelist_set_invert (il_mountpoint, 0);
118 ignorelist_set_invert (il_fstype, 0);
122 ignorelist_set_invert (il_device, 1);
123 ignorelist_set_invert (il_mountpoint, 1);
124 ignorelist_set_invert (il_fstype, 1);
132 static void df_submit (char *df_name,
137 value_list_t vl = VALUE_LIST_INIT;
139 values[0].gauge = df_used;
140 values[1].gauge = df_free;
144 vl.time = time (NULL);
145 strcpy (vl.host, hostname_g);
146 strcpy (vl.plugin, "df");
147 strcpy (vl.plugin_instance, "");
148 strncpy (vl.type_instance, df_name, sizeof (vl.type_instance));
150 plugin_dispatch_values ("df", &vl);
151 } /* void df_submit */
153 static int df_read (void)
156 struct statvfs statbuf;
158 struct statfs statbuf;
160 /* struct STATANYFS statbuf; */
161 cu_mount_t *mnt_list;
164 unsigned long long blocksize;
170 if (cu_mount_getlist (&mnt_list) == NULL)
173 for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
175 if (STATANYFS (mnt_ptr->dir, &statbuf) < 0)
178 ERROR ("statv?fs failed: %s",
179 sstrerror (errno, errbuf,
184 if (!statbuf.f_blocks)
187 blocksize = BLOCKSIZE(statbuf);
188 df_free = statbuf.f_bfree * blocksize;
189 df_used = (statbuf.f_blocks - statbuf.f_bfree) * blocksize;
191 if (strcmp (mnt_ptr->dir, "/") == 0)
193 strncpy (mnt_name, "root", sizeof (mnt_name));
199 strncpy (mnt_name, mnt_ptr->dir + 1, sizeof (mnt_name));
200 len = strlen (mnt_name);
202 for (i = 0; i < len; i++)
203 if (mnt_name[i] == '/')
207 if (ignorelist_match (il_device,
208 (mnt_ptr->spec_device != NULL)
209 ? mnt_ptr->spec_device
212 if (ignorelist_match (il_mountpoint, mnt_ptr->dir))
214 if (ignorelist_match (il_fstype, mnt_ptr->type))
217 df_submit (mnt_name, df_used, df_free);
220 cu_mount_freelist (mnt_list);
224 #endif /* DF_HAVE_READ */
226 void module_register (void)
228 plugin_register_data_set (&ds);
231 plugin_register_config ("df", df_config, config_keys, config_keys_num);
232 plugin_register_init ("df", df_init);
233 plugin_register_read ("df", df_read);
235 } /* void module_register */