From 4551923d4651a0bc59281cf5e7dfe9739f088ca5 Mon Sep 17 00:00:00 2001 From: skorpy Date: Sat, 25 Aug 2018 01:17:52 +0200 Subject: [PATCH] [BUGFIX] use available memory for usage estimation Fix memory usage estimation following this commit: https://github.com/freifunk-gluon/gluon/pull/1517 --- data/statistics.go | 9 +++++---- database/graphite/node.go | 1 + database/influxdb/node.go | 25 +++++++++++++------------ output/meshviewer-ffrgb/struct.go | 10 ++++++++-- output/meshviewer/node.go | 10 ++++++++-- 5 files changed, 35 insertions(+), 20 deletions(-) diff --git a/data/statistics.go b/data/statistics.go index b2946d6..365e3e6 100644 --- a/data/statistics.go +++ b/data/statistics.go @@ -84,10 +84,11 @@ type DHCP struct { // Memory struct type Memory struct { - Cached int64 `json:"cached"` - Total int64 `json:"total"` - Buffers int64 `json:"buffers"` - Free int64 `json:"free"` + Cached int64 `json:"cached"` + Total int64 `json:"total"` + Buffers int64 `json:"buffers"` + Free int64 `json:"free,omitempty"` + Available int64 `json:"available,omitempty"` } // SwitchPort struct diff --git a/database/graphite/node.go b/database/graphite/node.go index 3c460e2..d5db231 100644 --- a/database/graphite/node.go +++ b/database/graphite/node.go @@ -104,6 +104,7 @@ func (c *Connection) InsertNode(node *runtime.Node) { addField("memory.cached", stats.Memory.Cached) addField("memory.free", stats.Memory.Free) addField("memory.total", stats.Memory.Total) + addField("memory.available", stats.Memory.Available) c.addPoint(fields) } diff --git a/database/influxdb/node.go b/database/influxdb/node.go index 89c1d69..0d5e8a6 100644 --- a/database/influxdb/node.go +++ b/database/influxdb/node.go @@ -33,18 +33,19 @@ func (conn *Connection) InsertNode(node *runtime.Node) { tags.SetString("nodeid", stats.NodeID) fields := models.Fields{ - "load": stats.LoadAverage, - "time.up": int64(stats.Uptime), - "time.idle": int64(stats.Idletime), - "proc.running": stats.Processes.Running, - "clients.wifi": stats.Clients.Wifi, - "clients.wifi24": stats.Clients.Wifi24, - "clients.wifi5": stats.Clients.Wifi5, - "clients.total": stats.Clients.Total, - "memory.buffers": stats.Memory.Buffers, - "memory.cached": stats.Memory.Cached, - "memory.free": stats.Memory.Free, - "memory.total": stats.Memory.Total, + "load": stats.LoadAverage, + "time.up": int64(stats.Uptime), + "time.idle": int64(stats.Idletime), + "proc.running": stats.Processes.Running, + "clients.wifi": stats.Clients.Wifi, + "clients.wifi24": stats.Clients.Wifi24, + "clients.wifi5": stats.Clients.Wifi5, + "clients.total": stats.Clients.Total, + "memory.buffers": stats.Memory.Buffers, + "memory.cached": stats.Memory.Cached, + "memory.free": stats.Memory.Free, + "memory.total": stats.Memory.Total, + "memory.available": stats.Memory.Available, } if nodeinfo := node.Nodeinfo; nodeinfo != nil { diff --git a/output/meshviewer-ffrgb/struct.go b/output/meshviewer-ffrgb/struct.go index f8975bc..398401e 100644 --- a/output/meshviewer-ffrgb/struct.go +++ b/output/meshviewer-ffrgb/struct.go @@ -126,10 +126,16 @@ func NewNode(nodes *runtime.Nodes, n *runtime.Node) *Node { /* The Meshviewer could not handle absolute memory output * calc the used memory as a float which 100% equal 1.0 * calc is coppied from node statuspage (look discussion: - * https://github.com/FreifunkBremen/yanic/issues/35) + * https://github.com/FreifunkBremen/yanic/issues/35 and + * https://github.com/freifunk-gluon/gluon/pull/1517) */ if statistic.Memory.Total > 0 { - usage := 1 - (float64(statistic.Memory.Free)+float64(statistic.Memory.Buffers)+float64(statistic.Memory.Cached))/float64(statistic.Memory.Total) + usage := 0.0 + if statistic.Memory.Available > 0 { + usage = 1 - float64(statistic.Memory.Available)/float64(statistic.Memory.Total) + } else { + usage = 1 - (float64(statistic.Memory.Free)+float64(statistic.Memory.Buffers)+float64(statistic.Memory.Cached))/float64(statistic.Memory.Total) + } node.MemoryUsage = &usage } diff --git a/output/meshviewer/node.go b/output/meshviewer/node.go index 718dc00..bf479ef 100644 --- a/output/meshviewer/node.go +++ b/output/meshviewer/node.go @@ -71,10 +71,16 @@ func NewStatistics(stats *data.Statistics, isOnline bool) *Statistics { /* The Meshviewer could not handle absolute memory output * calc the used memory as a float which 100% equal 1.0 * calc is coppied from node statuspage (look discussion: - * https://github.com/FreifunkBremen/yanic/issues/35) + * https://github.com/FreifunkBremen/yanic/issues/35 and + * https://github.com/freifunk-gluon/gluon/pull/1517) */ if stats.Memory.Total > 0 { - usage := 1 - (float64(stats.Memory.Free)+float64(stats.Memory.Buffers)+float64(stats.Memory.Cached))/float64(stats.Memory.Total) + usage := 0.0 + if stats.Memory.Available > 0 { + usage = 1 - float64(stats.Memory.Available)/float64(stats.Memory.Total) + } else { + usage = 1 - (float64(stats.Memory.Free)+float64(stats.Memory.Buffers)+float64(stats.Memory.Cached))/float64(stats.Memory.Total) + } output.MemoryUsage = &usage }