yanic/output/meshviewer/node.go

89 lines
2.7 KiB
Go
Raw Permalink Normal View History

2016-05-29 19:42:11 +00:00
package meshviewer
import (
2017-03-03 15:19:35 +00:00
"github.com/FreifunkBremen/yanic/data"
"github.com/FreifunkBremen/yanic/lib/jsontime"
2016-05-29 19:42:11 +00:00
)
// Node struct
type Node struct {
Firstseen jsontime.Time `json:"firstseen"`
Lastseen jsontime.Time `json:"lastseen"`
Flags Flags `json:"flags"`
2016-05-29 19:42:11 +00:00
Statistics *Statistics `json:"statistics"`
Nodeinfo *data.Nodeinfo `json:"nodeinfo"`
2016-05-29 19:42:11 +00:00
Neighbours *data.Neighbours `json:"-"`
}
// Flags status of node set by collector for the meshviewer
2016-05-29 19:42:11 +00:00
type Flags struct {
Online bool `json:"online"`
Gateway bool `json:"gateway"`
}
2018-01-13 15:54:48 +00:00
// Statistics a meshviewer spezifisch struct, different from respondd
2016-05-29 19:42:11 +00:00
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"`
2016-05-29 19:42:11 +00:00
Processes struct {
Total uint32 `json:"total"`
Running uint32 `json:"running"`
2017-01-20 12:08:58 +00:00
} `json:"processes,omitempty"`
MeshVPN *data.MeshVPN `json:"mesh_vpn,omitempty"`
2016-05-29 19:42:11 +00:00
Traffic struct {
Tx *data.Traffic `json:"tx"`
Rx *data.Traffic `json:"rx"`
Forward *data.Traffic `json:"forward"`
MgmtTx *data.Traffic `json:"mgmt_tx"`
MgmtRx *data.Traffic `json:"mgmt_rx"`
2017-01-20 12:08:58 +00:00
} `json:"traffic,omitempty"`
2016-05-29 19:42:11 +00:00
}
// NewStatistics transform respond Statistics to meshviewer Statistics
func NewStatistics(stats *data.Statistics, isOnline bool) *Statistics {
output := &Statistics{
NodeID: stats.NodeID,
GatewayIPv4: stats.GatewayIPv4,
GatewayIPv6: stats.GatewayIPv6,
RootFsUsage: stats.RootFsUsage,
LoadAverage: stats.LoadAverage,
Uptime: stats.Uptime,
Idletime: stats.Idletime,
Processes: stats.Processes,
MeshVPN: stats.MeshVPN,
Traffic: stats.Traffic,
}
if isOnline {
total := stats.Clients.Total
if total == 0 {
total = stats.Clients.Wifi24 + stats.Clients.Wifi5
}
output.Clients = total
}
/* 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 and
* https://github.com/freifunk-gluon/gluon/pull/1517)
*/
if stats.Memory.Total > 0 {
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
}
return output
}