[BUGFIX] meshviewer divide by zero

This commit is contained in:
Martin Geno 2017-03-14 08:54:46 +01:00
parent dc24c8b250
commit e9c9968980
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
6 changed files with 65 additions and 52 deletions

View File

@ -15,10 +15,9 @@ type WirelessStatistics []*WirelessAirtime
// WirelessAirtime struct
type WirelessAirtime struct {
ChanUtil float32 // Channel utilization
RxUtil float32 // Receive utilization
TxUtil float32 // Transmit utilization
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"`

View File

@ -8,16 +8,16 @@ package data
//Statistics struct
type Statistics struct {
NodeID string `json:"node_id"`
Clients Clients `json:"clients"`
Clients *Clients `json:"clients"`
RootFsUsage float64 `json:"rootfs_usage,omitempty"`
LoadAverage float64 `json:"loadavg,omitempty"`
Memory Memory `json:"memory,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 {
Processes *struct {
Total uint32 `json:"total"`
Running uint32 `json:"running"`
} `json:"processes,omitempty"`

View File

@ -32,15 +32,24 @@ func buildNodeStats(node *runtime.Node) (tags models.Tags, 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,
}
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 {

View File

@ -27,12 +27,12 @@ type Statistics struct {
Clients uint32 `json:"clients"`
RootFsUsage float64 `json:"rootfs_usage,omitempty"`
LoadAverage float64 `json:"loadavg,omitempty"`
MemoryUsage float64 `json:"memory_usage,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 {
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
}

View File

@ -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

View File

@ -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,
},
},