From e9c996898031309e0c33afbadc4fa99db9ed2e56 Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Tue, 14 Mar 2017 08:54:46 +0100 Subject: [PATCH] [BUGFIX] meshviewer divide by zero --- data/airtime.go | 19 +++++++++---------- data/statistics.go | 22 +++++++++++----------- database/influxdb/node.go | 33 +++++++++++++++++++++------------ meshviewer/node.go | 37 +++++++++++++++++++++---------------- runtime/stats.go | 2 +- runtime/stats_test.go | 4 ++-- 6 files changed, 65 insertions(+), 52 deletions(-) diff --git a/data/airtime.go b/data/airtime.go index 884e791..f4d4660 100644 --- a/data/airtime.go +++ b/data/airtime.go @@ -15,16 +15,15 @@ type WirelessStatistics []*WirelessAirtime // WirelessAirtime struct type WirelessAirtime struct { - ChanUtil float32 // Channel utilization - RxUtil float32 // Receive utilization - TxUtil float32 // Transmit utilization - - ActiveTime uint64 `json:"active"` - BusyTime uint64 `json:"busy"` - RxTime uint64 `json:"rx"` - TxTime uint64 `json:"tx"` - Noise uint32 `json:"noise"` - Frequency uint32 `json:"frequency"` + ChanUtil float32 `json:"chan_util"` // Channel utilization + RxUtil float32 `json:"rx_util"` // Receive utilization + TxUtil float32 `json:"tx_util"` // Transmit utilization + ActiveTime uint64 `json:"active"` + BusyTime uint64 `json:"busy"` + RxTime uint64 `json:"rx"` + TxTime uint64 `json:"tx"` + Noise uint32 `json:"noise"` + Frequency uint32 `json:"frequency"` } // FrequencyName returns 11g or 11a diff --git a/data/statistics.go b/data/statistics.go index 942c651..916e23b 100644 --- a/data/statistics.go +++ b/data/statistics.go @@ -7,17 +7,17 @@ package data //Statistics struct type Statistics struct { - NodeID string `json:"node_id"` - Clients Clients `json:"clients"` - RootFsUsage float64 `json:"rootfs_usage,omitempty"` - LoadAverage float64 `json:"loadavg,omitempty"` - Memory Memory `json:"memory,omitempty"` - Uptime float64 `json:"uptime,omitempty"` - Idletime float64 `json:"idletime,omitempty"` - GatewayIPv4 string `json:"gateway,omitempty"` - GatewayIPv6 string `json:"gateway6,omitempty"` - GatewayNexthop string `json:"gateway_nexthop,omitempty"` - Processes struct { + NodeID string `json:"node_id"` + Clients *Clients `json:"clients"` + RootFsUsage float64 `json:"rootfs_usage,omitempty"` + LoadAverage float64 `json:"loadavg,omitempty"` + Memory *Memory `json:"memory,omitempty"` + Uptime float64 `json:"uptime,omitempty"` + Idletime float64 `json:"idletime,omitempty"` + GatewayIPv4 string `json:"gateway,omitempty"` + GatewayIPv6 string `json:"gateway6,omitempty"` + GatewayNexthop string `json:"gateway_nexthop,omitempty"` + Processes *struct { Total uint32 `json:"total"` Running uint32 `json:"running"` } `json:"processes,omitempty"` diff --git a/database/influxdb/node.go b/database/influxdb/node.go index 5f23368..098b080 100644 --- a/database/influxdb/node.go +++ b/database/influxdb/node.go @@ -29,18 +29,27 @@ func buildNodeStats(node *runtime.Node) (tags models.Tags, fields models.Fields) tags.SetString("nodeid", stats.NodeID) fields = map[string]interface{}{ - "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), + } + + if clients := stats.Clients; clients != nil { + fields["clients.wifi"] = clients.Wifi + fields["clients.wifi24"] = clients.Wifi24 + fields["clients.wifi5"] = clients.Wifi5 + fields["clients.total"] = clients.Total + } + + if proc := stats.Processes; proc != nil { + fields["proc.running"] = stats.Processes.Running + } + + if mem := stats.Memory; mem != nil { + fields["memory.buffers"] = mem.Buffers + fields["memory.cached"] = mem.Cached + fields["memory.free"] = mem.Free + fields["memory.total"] = mem.Total } if nodeinfo := node.Nodeinfo; nodeinfo != nil { diff --git a/meshviewer/node.go b/meshviewer/node.go index 266968f..1dade7d 100644 --- a/meshviewer/node.go +++ b/meshviewer/node.go @@ -23,16 +23,16 @@ 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"` - Processes 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"` + Processes *struct { Total uint32 `json:"total"` Running uint32 `json:"running"` } `json:"processes,omitempty"` @@ -48,24 +48,25 @@ type Statistics struct { // NewStatistics transform respond Statistics to meshviewer Statistics func NewStatistics(stats *data.Statistics) *Statistics { - total := stats.Clients.Total - if total == 0 { - total = stats.Clients.Wifi24 + stats.Clients.Wifi5 + var total uint32 + if clients := stats.Clients; clients != nil { + total = clients.Total + if total <= 0 { + total = clients.Wifi24 + clients.Wifi5 + } } /* 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) */ - memoryUsage := 1 - (float64(stats.Memory.Free)+float64(stats.Memory.Buffers)+float64(stats.Memory.Cached))/float64(stats.Memory.Total) - return &Statistics{ + meshviewerStats := &Statistics{ NodeID: stats.NodeID, GatewayIPv4: stats.GatewayIPv4, GatewayIPv6: stats.GatewayIPv6, RootFsUsage: stats.RootFsUsage, LoadAverage: stats.LoadAverage, - MemoryUsage: memoryUsage, Uptime: stats.Uptime, Idletime: stats.Idletime, Processes: stats.Processes, @@ -73,4 +74,8 @@ func NewStatistics(stats *data.Statistics) *Statistics { Traffic: stats.Traffic, Clients: total, } + if memory := stats.Memory; memory != nil && memory.Total > 0 { + *meshviewerStats.MemoryUsage = 1 - (float64(memory.Free)+float64(memory.Buffers)+float64(memory.Cached))/float64(memory.Total) + } + return meshviewerStats } diff --git a/runtime/stats.go b/runtime/stats.go index 423a5f9..c50df78 100644 --- a/runtime/stats.go +++ b/runtime/stats.go @@ -27,7 +27,7 @@ func NewGlobalStats(nodes *Nodes) (result *GlobalStats) { for _, node := range nodes.List { if node.Online { result.Nodes++ - if stats := node.Statistics; stats != nil { + if stats := node.Statistics; stats != nil && stats.Clients != nil { result.Clients += stats.Clients.Total result.ClientsWifi24 += stats.Clients.Wifi24 result.ClientsWifi5 += stats.Clients.Wifi5 diff --git a/runtime/stats_test.go b/runtime/stats_test.go index fd0bfcb..b742b66 100644 --- a/runtime/stats_test.go +++ b/runtime/stats_test.go @@ -31,7 +31,7 @@ func createTestNodes() *Nodes { nodeData := &data.ResponseData{ Statistics: &data.Statistics{ - Clients: data.Clients{ + Clients: &data.Clients{ Total: 23, }, }, @@ -46,7 +46,7 @@ func createTestNodes() *Nodes { nodes.Update("112233445566", &data.ResponseData{ Statistics: &data.Statistics{ - Clients: data.Clients{ + Clients: &data.Clients{ Total: 2, }, },