2017-04-10 18:54:12 +02:00
|
|
|
package influxdb
|
2016-10-03 19:55:37 +02:00
|
|
|
|
|
|
|
import (
|
2017-04-18 01:48:38 +02:00
|
|
|
"fmt"
|
2022-03-28 03:56:00 +02:00
|
|
|
"log"
|
2017-01-29 22:14:40 +01:00
|
|
|
"strconv"
|
2023-09-19 09:11:35 +02:00
|
|
|
"strings"
|
2017-04-18 01:48:38 +02:00
|
|
|
"time"
|
2017-01-29 22:14:40 +01:00
|
|
|
|
2019-01-15 20:19:55 +01:00
|
|
|
models "github.com/influxdata/influxdb1-client/models"
|
|
|
|
client "github.com/influxdata/influxdb1-client/v2"
|
2017-03-03 16:19:35 +01:00
|
|
|
|
2017-04-10 18:54:12 +02:00
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
2016-10-03 19:55:37 +02:00
|
|
|
)
|
|
|
|
|
2017-09-27 13:55:02 +02:00
|
|
|
// PruneNodes prunes historical per-node data
|
2017-04-18 01:48:38 +02:00
|
|
|
func (conn *Connection) PruneNodes(deleteAfter time.Duration) {
|
2017-09-27 13:55:02 +02:00
|
|
|
for _, measurement := range []string{MeasurementNode, MeasurementLink} {
|
|
|
|
query := fmt.Sprintf("delete from %s where time < now() - %ds", measurement, deleteAfter/time.Second)
|
2022-03-28 03:56:00 +02:00
|
|
|
if _, err := conn.client.Query(client.NewQuery(query, conn.config.Database(), "m")); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2017-09-27 13:55:02 +02:00
|
|
|
}
|
|
|
|
|
2017-04-18 01:48:38 +02:00
|
|
|
}
|
|
|
|
|
2017-09-27 13:55:02 +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
|
2017-09-27 13:55:02 +02:00
|
|
|
time := node.Lastseen.GetTime()
|
|
|
|
|
|
|
|
if stats == nil || stats.NodeID == "" {
|
|
|
|
return
|
|
|
|
}
|
2016-10-03 19:55:37 +02:00
|
|
|
|
2017-09-27 13:55:02 +02:00
|
|
|
tags := models.Tags{}
|
2017-01-20 22:27:44 +01:00
|
|
|
tags.SetString("nodeid", stats.NodeID)
|
2016-10-03 19:55:37 +02:00
|
|
|
|
2017-09-27 13:55:02 +02:00
|
|
|
fields := models.Fields{
|
2018-08-25 01:17:52 +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,
|
2023-09-18 19:56:18 +02:00
|
|
|
"clients.owe": stats.Clients.OWE,
|
|
|
|
"clients.owe24": stats.Clients.OWE24,
|
|
|
|
"clients.owe5": stats.Clients.OWE5,
|
2018-08-25 01:17:52 +02:00
|
|
|
"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,
|
|
|
|
"memory.available": stats.Memory.Available,
|
2016-10-03 19:55:37 +02:00
|
|
|
}
|
|
|
|
|
2019-05-28 19:27:48 +02:00
|
|
|
vpnInterfaces := make(map[string]bool)
|
|
|
|
|
2016-10-03 19:55:37 +02:00
|
|
|
if nodeinfo := node.Nodeinfo; nodeinfo != nil {
|
2019-05-28 19:27:48 +02:00
|
|
|
for _, mIface := range nodeinfo.Network.Mesh {
|
|
|
|
for _, tunnel := range mIface.Interfaces.Tunnel {
|
|
|
|
vpnInterfaces[tunnel] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-15 23:26:34 +01:00
|
|
|
tags.SetString("hostname", nodeinfo.Hostname)
|
2017-11-20 12:22:52 +01:00
|
|
|
if nodeinfo.System.SiteCode != "" {
|
|
|
|
tags.SetString("site", nodeinfo.System.SiteCode)
|
|
|
|
}
|
2018-01-17 20:20:35 +01:00
|
|
|
if nodeinfo.System.DomainCode != "" {
|
|
|
|
tags.SetString("domain", nodeinfo.System.DomainCode)
|
|
|
|
}
|
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
|
|
|
|
}
|
2017-03-15 23:26:34 +01:00
|
|
|
// Hardware
|
|
|
|
tags.SetString("model", nodeinfo.Hardware.Model)
|
2018-02-25 17:59:28 +01:00
|
|
|
fields["nproc"] = nodeinfo.Hardware.Nproc
|
2021-02-17 23:35:12 +01:00
|
|
|
if nodeinfo.Software.Firmware != nil {
|
|
|
|
tags.SetString("firmware_base", nodeinfo.Software.Firmware.Base)
|
|
|
|
tags.SetString("firmware_release", nodeinfo.Software.Firmware.Release)
|
|
|
|
}
|
|
|
|
if nodeinfo.Software.Autoupdater != nil && nodeinfo.Software.Autoupdater.Enabled {
|
2017-11-14 19:33:50 +01:00
|
|
|
tags.SetString("autoupdater", nodeinfo.Software.Autoupdater.Branch)
|
|
|
|
} else {
|
|
|
|
tags.SetString("autoupdater", runtime.DISABLED_AUTOUPDATER)
|
|
|
|
}
|
2017-03-15 23:26:34 +01:00
|
|
|
|
2016-10-03 19:55:37 +02:00
|
|
|
}
|
2017-01-20 23:23:31 +01:00
|
|
|
if neighbours := node.Neighbours; neighbours != nil {
|
|
|
|
// VPN Neighbours are Neighbours but includet in one protocol
|
|
|
|
vpn := 0
|
|
|
|
|
|
|
|
// protocol: Batman Advance
|
|
|
|
batadv := 0
|
2019-05-28 19:27:48 +02:00
|
|
|
for mac, batadvNeighbours := range neighbours.Batadv {
|
2017-01-20 23:23:31 +01:00
|
|
|
batadv += len(batadvNeighbours.Neighbours)
|
2019-05-28 19:27:48 +02:00
|
|
|
if _, ok := vpnInterfaces[mac]; ok {
|
|
|
|
vpn += len(batadvNeighbours.Neighbours)
|
|
|
|
}
|
2017-01-20 23:23:31 +01:00
|
|
|
}
|
|
|
|
fields["neighbours.batadv"] = batadv
|
|
|
|
|
2019-05-28 19:27:48 +02:00
|
|
|
// protocol: Babel
|
|
|
|
babel := 0
|
|
|
|
for _, babelNeighbours := range neighbours.Babel {
|
|
|
|
babel += len(babelNeighbours.Neighbours)
|
|
|
|
if _, ok := vpnInterfaces[babelNeighbours.LinkLocalAddress]; ok {
|
|
|
|
vpn += len(babelNeighbours.Neighbours)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fields["neighbours.babel"] = babel
|
|
|
|
|
2017-01-20 23:23:31 +01:00
|
|
|
// protocol: LLDP
|
2017-01-21 04:24:04 +01:00
|
|
|
lldp := 0
|
|
|
|
for _, lldpNeighbours := range neighbours.LLDP {
|
|
|
|
lldp += len(lldpNeighbours)
|
|
|
|
}
|
2017-01-20 23:23:31 +01:00
|
|
|
fields["neighbours.lldp"] = lldp
|
|
|
|
|
2019-05-28 19:27:48 +02:00
|
|
|
// vpn wait for babel
|
|
|
|
fields["neighbours.vpn"] = vpn
|
|
|
|
|
2017-01-20 23:23:31 +01:00
|
|
|
// total is the sum of all protocols
|
2019-05-28 19:27:48 +02:00
|
|
|
fields["neighbours.total"] = batadv + babel + lldp
|
2017-01-20 23:23:31 +01:00
|
|
|
}
|
2018-05-06 00:11:01 +02:00
|
|
|
if procstat := stats.ProcStats; procstat != nil {
|
|
|
|
fields["stat.cpu.user"] = procstat.CPU.User
|
|
|
|
fields["stat.cpu.nice"] = procstat.CPU.Nice
|
|
|
|
fields["stat.cpu.system"] = procstat.CPU.System
|
|
|
|
fields["stat.cpu.idle"] = procstat.CPU.Idle
|
|
|
|
fields["stat.cpu.iowait"] = procstat.CPU.IOWait
|
|
|
|
fields["stat.cpu.irq"] = procstat.CPU.IRQ
|
|
|
|
fields["stat.cpu.softirq"] = procstat.CPU.SoftIRQ
|
|
|
|
fields["stat.intr"] = procstat.Intr
|
|
|
|
fields["stat.ctxt"] = procstat.ContextSwitches
|
|
|
|
fields["stat.softirq"] = procstat.SoftIRQ
|
|
|
|
fields["stat.processes"] = procstat.Processes
|
|
|
|
}
|
2017-01-20 23:23:31 +01:00
|
|
|
|
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
|
|
|
}
|
2023-09-18 00:42:50 +02:00
|
|
|
// Add selected gateway mac as tag
|
2023-09-19 09:11:35 +02:00
|
|
|
tags.SetString("gateway", strings.ReplaceAll(stats.GatewayIPv4, ":", ""))
|
|
|
|
tags.SetString("gateway6", strings.ReplaceAll(stats.GatewayIPv6, ":", ""))
|
2017-09-27 13:55:02 +02:00
|
|
|
conn.addPoint(MeasurementNode, tags, fields, time)
|
|
|
|
|
2018-04-19 11:26:08 +02:00
|
|
|
// Add DHCP statistics
|
|
|
|
if dhcp := stats.DHCP; dhcp != nil {
|
|
|
|
fields := models.Fields{
|
|
|
|
"decline": dhcp.Decline,
|
|
|
|
"offer": dhcp.Offer,
|
|
|
|
"ack": dhcp.Ack,
|
|
|
|
"nak": dhcp.Nak,
|
|
|
|
"request": dhcp.Request,
|
|
|
|
"discover": dhcp.Discover,
|
|
|
|
"inform": dhcp.Inform,
|
|
|
|
"release": dhcp.Release,
|
|
|
|
|
|
|
|
"leases.allocated": dhcp.LeasesAllocated,
|
|
|
|
"leases.pruned": dhcp.LeasesPruned,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tags
|
|
|
|
tags.SetString("nodeid", stats.NodeID)
|
|
|
|
if nodeinfo := node.Nodeinfo; nodeinfo != nil {
|
|
|
|
tags.SetString("hostname", nodeinfo.Hostname)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.addPoint(MeasurementDHCP, tags, fields, time)
|
|
|
|
}
|
2016-10-03 19:55:37 +02:00
|
|
|
}
|