X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fdf.c;h=af54c92f9f7da4ce3c919deb11d93c12478e8d85;hb=4d370741101aeb037ae52f3529a4a0869e0dc08a;hp=540985d798ea46da3d12ad24aec2e638a0075643;hpb=10b92353ea2ccf66eabad273799c36225d2efc3f;p=collectd.git diff --git a/src/df.c b/src/df.c index 540985d7..af54c92f 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,7 +53,6 @@ static const char *config_keys[] = "FSType", "IgnoreSelected", "ReportByDevice", - "ReportReserved", "ReportInodes", "ValuesAbsolute", "ValuesPercentage" @@ -191,7 +190,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) @@ -200,24 +198,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]; @@ -234,10 +258,10 @@ static int df_read (void) 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) { @@ -248,19 +272,15 @@ static int df_read (void) 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] = '-'; } @@ -304,23 +324,23 @@ static int df_read (void) 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, "df_complex_pct", "free", + df_submit_one (disk_name, "percent_bytes", "free", (gauge_t) ((float_t)(blk_free) / statbuf.f_blocks * 100)); - df_submit_one (disk_name, "df_complex_pct", "reserved", + df_submit_one (disk_name, "percent_bytes", "reserved", (gauge_t) ((float_t)(blk_reserved) / statbuf.f_blocks * 100)); - df_submit_one (disk_name, "df_complex_pct", "used", + 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; @@ -331,7 +351,7 @@ static int df_read (void) statbuf.f_ffree = statbuf.f_favail; if (statbuf.f_files < statbuf.f_ffree) statbuf.f_files = statbuf.f_ffree; - + 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); @@ -340,11 +360,11 @@ static int df_read (void) { if (statbuf.f_files > 0) { - df_submit_one (disk_name, "df_inodes_pct", "free", + df_submit_one (disk_name, "percent_inodes", "free", (gauge_t) ((float_t)(inode_free) / statbuf.f_files * 100)); - df_submit_one (disk_name, "df_inodes_pct", "reserved", + df_submit_one (disk_name, "percent_inodes", "reserved", (gauge_t) ((float_t)(inode_reserved) / statbuf.f_files * 100)); - df_submit_one (disk_name, "df_inodes_pct", "used", + df_submit_one (disk_name, "percent_inodes", "used", (gauge_t) ((float_t)(inode_used) / statbuf.f_files * 100)); } else return (-1);