From 2d03e4e3df8383b6324845cb24bae021ce76115c Mon Sep 17 00:00:00 2001 From: octo Date: Sun, 18 Dec 2005 11:59:35 +0000 Subject: [PATCH] Added first version of a `df' module --- configure.in | 6 +++ src/Makefile.am | 9 ++++ src/df.c | 140 ++++++++++++++++++++++++++++++++++++++++++++++++ src/disk.c | 2 +- 4 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 src/df.c diff --git a/configure.in b/configure.in index dffe2f67..976f9329 100644 --- a/configure.in +++ b/configure.in @@ -74,6 +74,7 @@ AC_CHECK_HEADERS(sys/mntent.h) AC_CHECK_HEADERS(sys/mnttab.h) AC_CHECK_HEADERS(sys/mount.h) AC_CHECK_HEADERS(sys/statfs.h) +AC_CHECK_HEADERS(sys/statvfs.h) AC_CHECK_HEADERS(sys/vfs.h) AC_CHECK_HEADERS(sys/vfstab.h) @@ -106,6 +107,9 @@ AC_CHECK_FUNCS(openlog syslog closelog) # For cpu module AC_CHECK_FUNCS(sysctlbyname, [have_sysctlbyname="yes"], [have_sysctlbyname="no"]) +# For df module +AC_CHECK_FUNCS(statfs statvfs) + # For load module AC_CHECK_FUNCS(getloadavg, [have_getloadavg="yes"], [have_getloadavg="no"]) @@ -451,6 +455,7 @@ collectd modules:]) AC_COLLECTD([cpu], [disable], [module], [cpu usage statistics]) AC_COLLECTD([cpufreq], [disable], [module], [system cpu frequency statistics]) AC_COLLECTD([disk], [disable], [module], [disk/partition statistics]) +AC_COLLECTD([df], [disable], [module], [df statistics]) AC_COLLECTD([quota], [enable], [module], [quota statistics (experimental)]) AC_COLLECTD([hddtemp], [disable], [module], [hdd temperature statistics]) AC_COLLECTD([load], [disable], [module], [system load statistics]) @@ -701,6 +706,7 @@ Configuration: Modules: cpu . . . . . . . . $enable_cpu cpufreq . . . . . . $enable_cpufreq + df . . . . . . . . $enable_df disk . . . . . . . $enable_disk hddtemp . . . . . . $enable_hddtemp load . . . . . . . $enable_load diff --git a/src/Makefile.am b/src/Makefile.am index 1f309101..67105950 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -48,6 +48,15 @@ collectd_LDADD += "-dlopen" cpufreq.la collectd_DEPENDENCIES += cpufreq.la endif +if BUILD_MODULE_DF +pkglib_LTLIBRARIES += df.la +df_la_SOURCES = df.c +df_la_LDFLAGS = -module -avoid-version +df_la_CFLAGS = -Wall -Werror +collectd_LDADD += "-dlopen" df.la +collectd_DEPENDENCIES += df.la +endif + if BUILD_MODULE_DISK pkglib_LTLIBRARIES += disk.la disk_la_SOURCES = disk.c disk.h diff --git a/src/df.c b/src/df.c new file mode 100644 index 00000000..780197f6 --- /dev/null +++ b/src/df.c @@ -0,0 +1,140 @@ +/** + * collectd - src/df.c + * Copyright (C) 2005 Florian octo Forster + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * Florian octo Forster + **/ + +#include "collectd.h" +#include "common.h" +#include "plugin.h" +#include "utils_mount.h" + +#define MODULE_NAME "df" + +#if HAVE_STATFS || HAVE_STATVFS +# define DF_HAVE_READ 1 +#else +# define DF_HAVE_READ 0 +#endif + +#if HAVE_STATFS +#define STATANYFS statfs +#define BLOCKSIZE(s) (s).f_bsize + +#elif HAVE_STATVFS +#define STATANYFS statvfs +#define BLOCKSIZE(s) ((s).f_frsize ? (s).f_frsize : (s).f_bsize) +#endif + +static char *filename_template = "df-%s.rrd"; + +/* 104857600 == 100 MB */ +static char *ds_def[] = +{ + "DS:used:GAUGE:25:0:U", + "DS:free:GAUGE:25:0:U", + NULL +}; +static int ds_num = 2; + +#define BUFSIZE 512 + +static void df_init (void) +{ + return; +} + +static void df_write (char *host, char *inst, char *val) +{ + char file[BUFSIZE]; + int status; + + status = snprintf (file, BUFSIZE, filename_template, inst); + if (status < 1) + return; + else if (status >= BUFSIZE) + return; + + rrd_update_file (host, file, val, ds_def, ds_num); +} + +#if DF_HAVE_READ +static void df_submit (char *df_name, + unsigned long long df_used, + unsigned long long df_free) +{ + char buf[BUFSIZE]; + + if (snprintf (buf, BUFSIZE, "%u:%llu:%llu", (unsigned int) curtime, + df_used, df_free) >= BUFSIZE) + return; + + plugin_submit (MODULE_NAME, df_name, buf); +} + +static void df_read (void) +{ + struct STATANYFS statbuf; + cu_mount_t *mnt_list; + cu_mount_t *mnt_ptr; + + unsigned long long blocksize; + unsigned long long df_free; + unsigned long long df_used; + + mnt_list = NULL; + if (cu_mount_getlist (&mnt_list) == NULL) + { + syslog (LOG_WARNING, "cu_mount_getlist returned `NULL'"); + return; + } + + for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next) + { + + if (STATANYFS (mnt_ptr->dir, &statbuf) < 0) + { + syslog (LOG_ERR, "statv?fs failed: %s", strerror (errno)); + continue; + } + + if (!statbuf.f_blocks) + continue; + + blocksize = BLOCKSIZE(statbuf); + df_free = statbuf.f_bfree * blocksize; + df_used = (statbuf.f_blocks - statbuf.f_bfree) * blocksize; + + syslog (LOG_INFO, "blocksize = %llu, free = %llu, used = %llu, dir = %s", blocksize, df_free, df_used, mnt_ptr->dir); + df_submit ("blahfoo", df_used, df_free); + } + + cu_mount_freelist (mnt_list); +} /* static void df_read (void) */ +#else +# define df_read NULL +#endif /* DF_HAVE_READ */ + +void module_register (void) +{ + plugin_register (MODULE_NAME, df_init, df_read, df_write); +} + +#undef BUFSIZE +#undef MODULE_NAME diff --git a/src/disk.c b/src/disk.c index e3638f22..4e53df0d 100644 --- a/src/disk.c +++ b/src/disk.c @@ -138,6 +138,7 @@ static void partition_write (char *host, char *inst, char *val) rrd_update_file (host, file, val, part_ds_def, part_ds_num); } +#if DISK_HAVE_READ #define BUFSIZE 512 static void disk_submit (char *disk_name, unsigned long long read_count, @@ -161,7 +162,6 @@ static void disk_submit (char *disk_name, plugin_submit (MODULE_NAME, disk_name, buf); } -#if DISK_HAVE_READ static void partition_submit (char *part_name, unsigned long long read_count, unsigned long long read_bytes, -- 2.30.2