3 * Copyright (C) 2005,2006 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; either version 2 of the License, or (at your
8 * option) any later version.
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.
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
20 * Florian octo Forster <octo at verplant.org>
26 #include "configfile.h"
27 #include "utils_mount.h"
28 #include "utils_ignorelist.h"
30 #define MODULE_NAME "df"
32 #if HAVE_STATFS || HAVE_STATVFS
33 # define DF_HAVE_READ 1
35 # define DF_HAVE_READ 0
39 # if HAVE_SYS_STATVFS_H
40 # include <sys/statvfs.h>
42 # define STATANYFS statvfs
43 # define BLOCKSIZE(s) ((s).f_frsize ? (s).f_frsize : (s).f_bsize)
45 # if HAVE_SYS_STATFS_H
46 # include <sys/statfs.h>
48 # define STATANYFS statfs
49 # define BLOCKSIZE(s) (s).f_bsize
52 static char *filename_template = "df-%s.rrd";
54 static char *ds_def[] =
56 "DS:used:GAUGE:"COLLECTD_HEARTBEAT":0:U",
57 "DS:free:GAUGE:"COLLECTD_HEARTBEAT":0:U",
60 static int ds_num = 2;
62 static 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;
78 static void df_init (void)
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);
90 static int df_config (char *key, char *value)
94 if (strcasecmp (key, "Device") == 0)
96 if (ignorelist_add (il_device, value))
100 else if (strcasecmp (key, "MountPoint") == 0)
102 if (ignorelist_add (il_mountpoint, value))
106 else if (strcasecmp (key, "FSType") == 0)
108 if (ignorelist_add (il_fstype, value))
112 else if (strcasecmp (key, "IgnoreSelected") == 0)
114 if ((strcasecmp (value, "True") == 0)
115 || (strcasecmp (value, "Yes") == 0)
116 || (strcasecmp (value, "On") == 0))
118 ignorelist_set_invert (il_device, 0);
119 ignorelist_set_invert (il_mountpoint, 0);
120 ignorelist_set_invert (il_fstype, 0);
124 ignorelist_set_invert (il_device, 1);
125 ignorelist_set_invert (il_mountpoint, 1);
126 ignorelist_set_invert (il_fstype, 1);
134 static void df_write (char *host, char *inst, char *val)
139 status = snprintf (file, BUFSIZE, filename_template, inst);
142 else if (status >= BUFSIZE)
145 rrd_update_file (host, file, val, ds_def, ds_num);
149 static void df_submit (char *df_name,
150 unsigned long long df_used,
151 unsigned long long df_free)
155 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu", (unsigned int) curtime,
156 df_used, df_free) >= BUFSIZE)
159 plugin_submit (MODULE_NAME, df_name, buf);
162 static void df_read (void)
165 struct statvfs statbuf;
167 struct statfs statbuf;
169 /* struct STATANYFS statbuf; */
170 cu_mount_t *mnt_list;
173 unsigned long long blocksize;
174 unsigned long long df_free;
175 unsigned long long df_used;
176 char mnt_name[BUFSIZE];
179 if (cu_mount_getlist (&mnt_list) == NULL)
182 for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
184 if (STATANYFS (mnt_ptr->dir, &statbuf) < 0)
186 syslog (LOG_ERR, "statv?fs failed: %s", strerror (errno));
190 if (!statbuf.f_blocks)
193 blocksize = BLOCKSIZE(statbuf);
194 df_free = statbuf.f_bfree * blocksize;
195 df_used = (statbuf.f_blocks - statbuf.f_bfree) * blocksize;
197 if (strcmp (mnt_ptr->dir, "/") == 0)
199 strncpy (mnt_name, "root", BUFSIZE);
205 strncpy (mnt_name, mnt_ptr->dir + 1, BUFSIZE);
206 len = strlen (mnt_name);
208 for (i = 0; i < len; i++)
209 if (mnt_name[i] == '/')
213 if (ignorelist_match (il_device,
214 (mnt_ptr->spec_device != NULL)
215 ? mnt_ptr->spec_device
218 if (ignorelist_match (il_mountpoint, mnt_ptr->dir))
220 if (ignorelist_match (il_fstype, mnt_ptr->type))
223 df_submit (mnt_name, df_used, df_free);
226 cu_mount_freelist (mnt_list);
227 } /* static void df_read (void) */
229 # define df_read NULL
230 #endif /* DF_HAVE_READ */
232 void module_register (void)
234 plugin_register (MODULE_NAME, df_init, df_read, df_write);
235 cf_register (MODULE_NAME, df_config, config_keys, config_keys_num);