X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fdf.c;h=632787b22be41b678f68487ac6347a6411c7715e;hb=e1bfa71aca1f37c2f293dc9adb44065c6e7a9ad9;hp=ded374b942e5d08d804dd027292eef2ef9f50fcb;hpb=82e27678f8972d2625b466e7e7ba9daaa6395c98;p=collectd.git diff --git a/src/df.c b/src/df.c index ded374b9..632787b2 100644 --- a/src/df.c +++ b/src/df.c @@ -17,14 +17,14 @@ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * Authors: - * Florian octo Forster + * Florian octo Forster * Paul Sadauskas **/ #include "collectd.h" + #include "common.h" #include "plugin.h" -#include "configfile.h" #include "utils_mount.h" #include "utils_ignorelist.h" @@ -53,8 +53,9 @@ static const char *config_keys[] = "FSType", "IgnoreSelected", "ReportByDevice", - "ReportReserved", - "ReportInodes" + "ReportInodes", + "ValuesAbsolute", + "ValuesPercentage" }; static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); @@ -64,6 +65,8 @@ static ignorelist_t *il_fstype = NULL; static _Bool by_device = 0; static _Bool report_inodes = 0; +static _Bool values_absolute = 1; +static _Bool values_percentage = 0; static int df_init (void) { @@ -131,7 +134,24 @@ static int df_config (const char *key, const char *value) return (0); } + else if (strcasecmp (key, "ValuesAbsolute") == 0) + { + if (IS_TRUE (value)) + values_absolute = 1; + else + values_absolute = 0; + + return (0); + } + else if (strcasecmp (key, "ValuesPercentage") == 0) + { + if (IS_TRUE (value)) + values_percentage = 1; + else + values_percentage = 0; + return (0); + } return (-1); } @@ -141,12 +161,9 @@ static void df_submit_one (char *plugin_instance, const char *type, const char *type_instance, gauge_t value) { - value_t values[1]; value_list_t vl = VALUE_LIST_INIT; - values[0].gauge = value; - - vl.values = values; + vl.values = &(value_t) { .gauge = value }; vl.values_len = 1; sstrncpy (vl.host, hostname_g, sizeof (vl.host)); sstrncpy (vl.plugin, "df", sizeof (vl.plugin)); @@ -170,7 +187,6 @@ static int df_read (void) #endif /* struct STATANYFS statbuf; */ cu_mount_t *mnt_list; - cu_mount_t *mnt_ptr; mnt_list = NULL; if (cu_mount_getlist (&mnt_list) == NULL) @@ -179,24 +195,50 @@ static int df_read (void) return (-1); } - for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next) + for (cu_mount_t *mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next) { unsigned long long blocksize; char disk_name[256]; + cu_mount_t *dup_ptr; uint64_t blk_free; uint64_t blk_reserved; uint64_t blk_used; - if (ignorelist_match (il_device, - (mnt_ptr->spec_device != NULL) - ? mnt_ptr->spec_device - : mnt_ptr->device)) + char const *dev = (mnt_ptr->spec_device != NULL) + ? mnt_ptr->spec_device + : mnt_ptr->device; + + if (ignorelist_match (il_device, dev)) continue; if (ignorelist_match (il_mountpoint, mnt_ptr->dir)) continue; if (ignorelist_match (il_fstype, mnt_ptr->type)) continue; + /* search for duplicates *in front of* the current mnt_ptr. */ + for (dup_ptr = mnt_list; dup_ptr != NULL; dup_ptr = dup_ptr->next) + { + /* No duplicate found: mnt_ptr is the first of its kind. */ + if (dup_ptr == mnt_ptr) + { + dup_ptr = NULL; + break; + } + + /* Duplicate found: leave non-NULL dup_ptr. */ + if (by_device + && (mnt_ptr->spec_device != NULL) + && (dup_ptr->spec_device != NULL) + && (strcmp (mnt_ptr->spec_device, dup_ptr->spec_device) == 0)) + break; + else if (!by_device && (strcmp (mnt_ptr->dir, dup_ptr->dir) == 0)) + break; + } + + /* ignore duplicates */ + if (dup_ptr != NULL) + continue; + if (STATANYFS (mnt_ptr->dir, &statbuf) < 0) { char errbuf[1024]; @@ -210,36 +252,32 @@ static int df_read (void) if (!statbuf.f_blocks) continue; - if (by_device) + if (by_device) { /* eg, /dev/hda1 -- strip off the "/dev/" */ - if (strncmp (mnt_ptr->spec_device, "/dev/", strlen ("/dev/")) == 0) - sstrncpy (disk_name, mnt_ptr->spec_device + strlen ("/dev/"), sizeof (disk_name)); + if (strncmp (dev, "/dev/", strlen ("/dev/")) == 0) + sstrncpy (disk_name, dev + strlen ("/dev/"), sizeof (disk_name)); else - sstrncpy (disk_name, mnt_ptr->spec_device, sizeof (disk_name)); + sstrncpy (disk_name, dev, sizeof (disk_name)); - if (strlen(disk_name) < 1) + if (strlen(disk_name) < 1) { - DEBUG("df: no device name name for mountpoint %s, skipping", mnt_ptr->dir); + DEBUG("df: no device name for mountpoint %s, skipping", mnt_ptr->dir); continue; } - } - else + } + else { if (strcmp (mnt_ptr->dir, "/") == 0) - { - if (strcmp (mnt_ptr->type, "rootfs") == 0) - continue; sstrncpy (disk_name, "root", sizeof (disk_name)); - } else { - int i, len; + int len; sstrncpy (disk_name, mnt_ptr->dir + 1, sizeof (disk_name)); len = strlen (disk_name); - for (i = 0; i < len; i++) + for (int i = 0; i < len; i++) if (disk_name[i] == '/') disk_name[i] = '-'; } @@ -274,15 +312,32 @@ static int df_read (void) blk_reserved = (uint64_t) (statbuf.f_bfree - statbuf.f_bavail); blk_used = (uint64_t) (statbuf.f_blocks - statbuf.f_bfree); - df_submit_one (disk_name, "df_complex", "free", + if (values_absolute) + { + df_submit_one (disk_name, "df_complex", "free", (gauge_t) (blk_free * blocksize)); - df_submit_one (disk_name, "df_complex", "reserved", + df_submit_one (disk_name, "df_complex", "reserved", (gauge_t) (blk_reserved * blocksize)); - df_submit_one (disk_name, "df_complex", "used", + df_submit_one (disk_name, "df_complex", "used", (gauge_t) (blk_used * blocksize)); + } + + if (values_percentage) + { + if (statbuf.f_blocks > 0) + { + df_submit_one (disk_name, "percent_bytes", "free", + (gauge_t) ((float_t)(blk_free) / statbuf.f_blocks * 100)); + df_submit_one (disk_name, "percent_bytes", "reserved", + (gauge_t) ((float_t)(blk_reserved) / statbuf.f_blocks * 100)); + df_submit_one (disk_name, "percent_bytes", "used", + (gauge_t) ((float_t)(blk_used) / statbuf.f_blocks * 100)); + } + else return (-1); + } /* inode handling */ - if (report_inodes) + if (report_inodes && statbuf.f_files != 0 && statbuf.f_ffree != 0) { uint64_t inode_free; uint64_t inode_reserved; @@ -297,13 +352,29 @@ static int df_read (void) inode_free = (uint64_t) statbuf.f_favail; inode_reserved = (uint64_t) (statbuf.f_ffree - statbuf.f_favail); inode_used = (uint64_t) (statbuf.f_files - statbuf.f_ffree); - - df_submit_one (disk_name, "df_inodes", "free", - (gauge_t) inode_free); - df_submit_one (disk_name, "df_inodes", "reserved", - (gauge_t) inode_reserved); - df_submit_one (disk_name, "df_inodes", "used", - (gauge_t) inode_used); + + if (values_percentage) + { + if (statbuf.f_files > 0) + { + df_submit_one (disk_name, "percent_inodes", "free", + (gauge_t) ((float_t)(inode_free) / statbuf.f_files * 100)); + df_submit_one (disk_name, "percent_inodes", "reserved", + (gauge_t) ((float_t)(inode_reserved) / statbuf.f_files * 100)); + df_submit_one (disk_name, "percent_inodes", "used", + (gauge_t) ((float_t)(inode_used) / statbuf.f_files * 100)); + } + else return (-1); + } + if (values_absolute) + { + df_submit_one (disk_name, "df_inodes", "free", + (gauge_t) inode_free); + df_submit_one (disk_name, "df_inodes", "reserved", + (gauge_t) inode_reserved); + df_submit_one (disk_name, "df_inodes", "used", + (gauge_t) inode_used); + } } }