Commit 5ddbc1cb authored by Marcin Olszewski's avatar Marcin Olszewski

Fix NaN values in metrics

- use safe division wrapper function to compute percentage
- remove unused fields in internal struct
parent 7a792ebb
......@@ -45,7 +45,7 @@ const (
// PluginName df collector plugin name
PluginName = "df"
// Version of plugin
Version = 5
Version = 6
nsVendor = "intel"
nsClass = "procfs"
......@@ -278,11 +278,11 @@ func fillMetric(kind string, dfm dfMetric, metric *plugin.MetricType) {
case "space_used":
metric.Data_ = dfm.Used
case "space_percent_free":
metric.Data_ = 100 * float64(dfm.Available) / float64(dfm.Blocks)
metric.Data_ = ceilPercent(dfm.Available, dfm.Blocks)
case "space_percent_reserved":
metric.Data_ = 100 * float64(dfm.Blocks-(dfm.Used+dfm.Available)) / float64(dfm.Blocks)
metric.Data_ = ceilPercent(dfm.Blocks-(dfm.Used+dfm.Available), dfm.Blocks)
case "space_percent_used":
metric.Data_ = 100 * float64(dfm.Used) / float64(dfm.Blocks)
metric.Data_ = ceilPercent(dfm.Used, dfm.Blocks)
case "device_name":
metric.Data_ = dfm.Filesystem
case "device_type":
......@@ -294,11 +294,11 @@ func fillMetric(kind string, dfm dfMetric, metric *plugin.MetricType) {
case "inodes_used":
metric.Data_ = dfm.IUsed
case "inodes_percent_free":
metric.Data_ = 100 * float64(dfm.IFree) / float64(dfm.Inodes)
metric.Data_ = ceilPercent(dfm.IFree, dfm.Inodes)
case "inodes_percent_reserved":
metric.Data_ = 100 * float64(dfm.Inodes-(dfm.IUsed+dfm.IFree)) / float64(dfm.Inodes)
metric.Data_ = ceilPercent(dfm.Inodes-(dfm.IUsed+dfm.IFree), dfm.Inodes)
case "inodes_percent_used":
metric.Data_ = 100 * float64(dfm.IUsed) / float64(dfm.Inodes)
metric.Data_ = ceilPercent(dfm.IUsed, dfm.Inodes)
}
}
......@@ -367,12 +367,10 @@ type dfCollector struct {
type dfMetric struct {
Filesystem string
Used, Available, Blocks uint64
Capacity float64
FsType string
MountPoint string
UnchangedMountPoint string
Inodes, IUsed, IFree uint64
IUse float64
}
type collector interface {
......@@ -446,14 +444,10 @@ func (dfs *dfStats) collect(procPath string, excluded_fs_names []string, exclude
dfm.Available = (stat.Bavail * uint64(stat.Bsize)) / 1024
xFree := (stat.Bfree * uint64(stat.Bsize)) / 1024
dfm.Used = dfm.Blocks - xFree
percentAvailable := ceilPercent(dfm.Used, dfm.Used+dfm.Available)
dfm.Capacity = percentAvailable / 100.0
// Inodes
dfm.Inodes = stat.Files
dfm.IFree = stat.Ffree
dfm.IUsed = dfm.Inodes - dfm.IFree
percentIUsed := ceilPercent(dfm.IUsed, dfm.Inodes)
dfm.IUse = percentIUsed / 100.0
dfms = append(dfms, dfm)
}
return dfms, nil
......
......@@ -46,27 +46,23 @@ func (dfp *DfPluginSuite) SetupSuite() {
Blocks: 100,
Used: 50,
Available: 40,
Capacity: 0.5,
FsType: "ext4",
Filesystem: "/dev/sda1",
MountPoint: "rootfs",
Inodes: 1000,
IUsed: 500,
IFree: 400,
IUse: 0.5,
},
dfMetric{
Blocks: 200,
Used: 110,
Available: 80,
Capacity: 0.3,
FsType: "ext4",
Filesystem: "/dev/sda2",
MountPoint: "big",
Inodes: 2000,
IUsed: 1000,
IFree: 800,
IUse: 0.5,
},
}
dfms_unchanged := []dfMetric{
......@@ -74,27 +70,23 @@ func (dfp *DfPluginSuite) SetupSuite() {
Blocks: 100,
Used: 50,
Available: 40,
Capacity: 0.5,
FsType: "ext4",
Filesystem: "/dev/sda1",
MountPoint: "/",
Inodes: 1000,
IUsed: 500,
IFree: 400,
IUse: 0.5,
},
dfMetric{
Blocks: 200,
Used: 110,
Available: 80,
Capacity: 0.3,
FsType: "ext4",
Filesystem: "/dev/sda2",
MountPoint: "/big",
Inodes: 2000,
IUsed: 1000,
IFree: 800,
IUse: 0.5,
},
}
mc := &MockCollector{}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment