yanic/database/influxdb/node.go

137 lines
4.0 KiB
Go
Raw Normal View History

package influxdb
2016-10-03 19:55:37 +02:00
import (
2017-04-18 01:48:38 +02:00
"fmt"
"strconv"
2017-04-18 01:48:38 +02:00
"time"
2017-04-18 01:48:38 +02:00
client "github.com/influxdata/influxdb/client/v2"
models "github.com/influxdata/influxdb/models"
2017-03-03 16:19:35 +01:00
"github.com/FreifunkBremen/yanic/runtime"
2016-10-03 19:55:37 +02:00
)
// PruneNodes prunes historical per-node data
2017-04-18 01:48:38 +02:00
func (conn *Connection) PruneNodes(deleteAfter time.Duration) {
for _, measurement := range []string{MeasurementNode, MeasurementLink} {
query := fmt.Sprintf("delete from %s where time < now() - %ds", measurement, deleteAfter/time.Second)
conn.client.Query(client.NewQuery(query, conn.config.Database(), "m"))
}
2017-04-18 01:48:38 +02:00
}
// InsertNode stores statistics and neighbours in the database
func (conn *Connection) InsertNode(node *runtime.Node) {
2016-10-03 19:55:37 +02:00
stats := node.Statistics
time := node.Lastseen.GetTime()
if stats == nil || stats.NodeID == "" {
return
}
2016-10-03 19:55:37 +02:00
tags := models.Tags{}
tags.SetString("nodeid", stats.NodeID)
2016-10-03 19:55:37 +02:00
fields := models.Fields{
2016-10-03 19:55:37 +02:00
"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 nodeinfo := node.Nodeinfo; nodeinfo != nil {
tags.SetString("hostname", nodeinfo.Hostname)
2017-11-11 22:21:53 +01:00
if len(nodeinfo.System.SiteCode) > 0 {
tags.SetString("site_code", nodeinfo.System.SiteCode)
}
2016-10-03 19:55:37 +02:00
if owner := nodeinfo.Owner; owner != nil {
tags.SetString("owner", owner.Contact)
}
if wireless := nodeinfo.Wireless; wireless != nil {
fields["wireless.txpower24"] = wireless.TxPower24
fields["wireless.txpower5"] = wireless.TxPower5
}
// Hardware
tags.SetString("model", nodeinfo.Hardware.Model)
tags.SetString("firmware_base", nodeinfo.Software.Firmware.Base)
tags.SetString("firmware_release", nodeinfo.Software.Firmware.Release)
2016-10-03 19:55:37 +02:00
}
if neighbours := node.Neighbours; neighbours != nil {
// VPN Neighbours are Neighbours but includet in one protocol
vpn := 0
if meshvpn := stats.MeshVPN; meshvpn != nil {
for _, group := range meshvpn.Groups {
for _, link := range group.Peers {
2017-01-21 04:24:04 +01:00
if link != nil && link.Established > 1 {
vpn++
}
}
}
}
fields["neighbours.vpn"] = vpn
// protocol: Batman Advance
batadv := 0
for _, batadvNeighbours := range neighbours.Batadv {
batadv += len(batadvNeighbours.Neighbours)
}
fields["neighbours.batadv"] = batadv
// protocol: LLDP
2017-01-21 04:24:04 +01:00
lldp := 0
for _, lldpNeighbours := range neighbours.LLDP {
lldp += len(lldpNeighbours)
}
fields["neighbours.lldp"] = lldp
// total is the sum of all protocols
fields["neighbours.total"] = batadv + lldp
}
2016-10-03 19:55:37 +02:00
if t := stats.Traffic.Rx; t != nil {
fields["traffic.rx.bytes"] = int64(t.Bytes)
fields["traffic.rx.packets"] = t.Packets
}
if t := stats.Traffic.Tx; t != nil {
fields["traffic.tx.bytes"] = int64(t.Bytes)
fields["traffic.tx.packets"] = t.Packets
fields["traffic.tx.dropped"] = t.Dropped
}
if t := stats.Traffic.Forward; t != nil {
fields["traffic.forward.bytes"] = int64(t.Bytes)
fields["traffic.forward.packets"] = t.Packets
}
if t := stats.Traffic.MgmtRx; t != nil {
fields["traffic.mgmt_rx.bytes"] = int64(t.Bytes)
fields["traffic.mgmt_rx.packets"] = t.Packets
}
if t := stats.Traffic.MgmtTx; t != nil {
fields["traffic.mgmt_tx.bytes"] = int64(t.Bytes)
fields["traffic.mgmt_tx.packets"] = t.Packets
}
2016-12-22 03:14:51 +01:00
for _, airtime := range stats.Wireless {
suffix := airtime.FrequencyName()
fields["airtime"+suffix+".chan_util"] = airtime.ChanUtil
fields["airtime"+suffix+".rx_util"] = airtime.RxUtil
fields["airtime"+suffix+".tx_util"] = airtime.TxUtil
fields["airtime"+suffix+".noise"] = airtime.Noise
fields["airtime"+suffix+".frequency"] = airtime.Frequency
tags.SetString("frequency"+suffix, strconv.Itoa(int(airtime.Frequency)))
2016-10-03 19:55:37 +02:00
}
conn.addPoint(MeasurementNode, tags, fields, time)
2016-10-03 19:55:37 +02:00
return
}