ubnt-freifunk-map-api/staticDevices.go
Stefan 73166fcedc
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
send influx datapoints only when enabled
2024-03-18 19:34:06 +01:00

100 lines
2.7 KiB
Go

package main
import (
"log"
"strings"
"time"
client "github.com/influxdata/influxdb1-client/v2"
)
func processGateways() []node {
d := getDevices(conf.Gateways.GatewaysURL)
var nodes []node
for i := range d.Devices {
log.Println("Processing Static Device: ", d.Devices[i].Name)
currentDevice := d.Devices[i]
//Collect data
//Calulate Memory (%)
mem := getInfluxDataPoint("mem", currentDevice.FQDN, conf.General.ProxmoxInfluxPort)
maxmem := getInfluxDataPoint("maxmem", currentDevice.FQDN, conf.General.ProxmoxInfluxPort)
memoryMap := mem / maxmem
memory := memoryMap * 100
// Get Network
rx := getInfluxDataPoint("netin", currentDevice.FQDN, conf.General.ProxmoxInfluxPort)
tx := getInfluxDataPoint("netout", currentDevice.FQDN, conf.General.ProxmoxInfluxPort)
// Get CPU (%)
cpuMap := getInfluxDataPoint("cpu", currentDevice.FQDN, conf.General.ProxmoxInfluxPort)
cpu := cpuMap * 100
//Uptime (seconds)
uptime := getInfluxDataPoint("uptime", currentDevice.FQDN, conf.General.ProxmoxInfluxPort)
t := time.Duration(uptime * float64(time.Second))
up := time.Now().Add(-t)
// fields := map[string]interface{}{}
fields := make(map[string]any)
tags := map[string]string{
"hostname": strings.ReplaceAll(d.Devices[i].Name, " ", "-"),
"nodeid": strings.ReplaceAll(d.Devices[i].MAC, ":", ""),
}
//Build fields for InfluxDB
fields["load"] = cpu
fields["ram"] = int(memory)
fields["time.up"] = int(uptime)
//Network
fields["traffic.rx.bytes"] = int(rx)
fields["traffic.tx.bytes"] = int(tx)
point, err := client.NewPoint(
"node",
tags,
fields,
time.Now(),
)
if err != nil {
log.Fatalln("Error: ", err)
}
if conf.General.InfluxEnabled {
sendInfluxBatchDataPoint(point, conf.General.FreifunkInfluxPort)
}
//Build Nodes
nodes = append(nodes, node{
Firstseen: up.Format(iso8601),
Lastseen: time.Now().Format(iso8601),
IsOnline: true,
IsGateway: true,
Clients: 0,
ClientsWifi24: 0,
ClientsWifi5: 0,
ClientsOther: 0,
RootFSUsage: 0,
LoadAVG: cpuMap,
MemoryUsage: memoryMap,
Uptime: up.Format(iso8601),
GatewayNexthop: "",
Gateway: "",
NodeID: strings.ReplaceAll(d.Devices[i].MAC, ":", ""),
MAC: d.Devices[i].MAC,
Adresses: d.Devices[i].Adresses,
Domain: d.Devices[i].Domain,
Hostname: "[Gateway] " + d.Devices[i].Name,
Owner: "Freifunk Troisdorf",
Firmware: firmware{
Base: "KVM",
Release: "Ubuntu 22.04",
},
Autoupdater: autoupdater{
Enabled: false,
Branch: "stable",
},
NProc: 1,
Model: "KVM",
})
}
return nodes
}