From 2148a7de281fc523413e6439c35bbb99f17a2343 Mon Sep 17 00:00:00 2001 From: Julian Kornberger Date: Thu, 6 Jul 2017 11:33:20 +0200 Subject: [PATCH] Fix division by zero to calculate memory usage Closes #41 --- meshviewer/node.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/meshviewer/node.go b/meshviewer/node.go index 266968f..8abc49f 100644 --- a/meshviewer/node.go +++ b/meshviewer/node.go @@ -23,15 +23,15 @@ type Flags struct { // Statistics a meshviewer spezifisch struct, diffrent from respondd type Statistics struct { - NodeID string `json:"node_id"` - Clients uint32 `json:"clients"` - RootFsUsage float64 `json:"rootfs_usage,omitempty"` - LoadAverage float64 `json:"loadavg,omitempty"` - MemoryUsage float64 `json:"memory_usage,omitempty"` - Uptime float64 `json:"uptime,omitempty"` - Idletime float64 `json:"idletime,omitempty"` - GatewayIPv4 string `json:"gateway,omitempty"` - GatewayIPv6 string `json:"gateway6,omitempty"` + NodeID string `json:"node_id"` + Clients uint32 `json:"clients"` + RootFsUsage float64 `json:"rootfs_usage,omitempty"` + LoadAverage float64 `json:"loadavg,omitempty"` + MemoryUsage *float64 `json:"memory_usage,omitempty"` + Uptime float64 `json:"uptime,omitempty"` + Idletime float64 `json:"idletime,omitempty"` + GatewayIPv4 string `json:"gateway,omitempty"` + GatewayIPv6 string `json:"gateway6,omitempty"` Processes struct { Total uint32 `json:"total"` Running uint32 `json:"running"` @@ -57,7 +57,11 @@ func NewStatistics(stats *data.Statistics) *Statistics { * calc is coppied from node statuspage (look discussion: * https://github.com/FreifunkBremen/yanic/issues/35) */ - memoryUsage := 1 - (float64(stats.Memory.Free)+float64(stats.Memory.Buffers)+float64(stats.Memory.Cached))/float64(stats.Memory.Total) + var memoryUsage *float64 + if stats.Memory.Total > 0 { + usage := 1 - (float64(stats.Memory.Free)+float64(stats.Memory.Buffers)+float64(stats.Memory.Cached))/float64(stats.Memory.Total) + memoryUsage = &usage + } return &Statistics{ NodeID: stats.NodeID,