ubnt-freifunk-map-api/staticDevices.go

95 lines
2.6 KiB
Go
Raw Normal View History

2023-05-11 19:38:10 +00:00
package main
import (
"log"
"strings"
"time"
2023-05-12 13:14:06 +00:00
client "github.com/influxdata/influxdb1-client/v2"
2023-05-11 19:38:10 +00:00
)
func processGateways() []node {
d := getDevices(conf.Gateways.GatewaysURL)
var nodes []node
2023-05-12 13:14:06 +00:00
2023-05-11 19:38:10 +00:00
for i := range d.Devices {
log.Println("Processing Static Device: ", d.Devices[i].Name)
2023-05-12 13:14:06 +00:00
currentDevice := d.Devices[i]
2023-05-12 16:00:25 +00:00
//Collect data
//Calulate Memory (%)
2023-05-12 13:14:06 +00:00
mem := getInfluxDataPoint("mem", currentDevice.FQDN, conf.General.ProxmoxInfluxPort)
maxmem := getInfluxDataPoint("maxmem", currentDevice.FQDN, conf.General.ProxmoxInfluxPort)
memory := mem / maxmem * 100
rx := getInfluxDataPoint("netin", currentDevice.FQDN, conf.General.ProxmoxInfluxPort)
tx := getInfluxDataPoint("netout", currentDevice.FQDN, conf.General.ProxmoxInfluxPort)
2023-05-12 16:00:25 +00:00
// Get CPU (%)
cpu := getInfluxDataPoint("cpu", currentDevice.FQDN, conf.General.ProxmoxInfluxPort) * 100
2023-05-12 13:14:06 +00:00
//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)
}
sendInfluxBatchDataPoint(point, conf.General.FreifunkInfluxPort)
2023-05-12 16:00:25 +00:00
2023-05-12 13:14:06 +00:00
//Build Nodes
2023-05-11 19:38:10 +00:00
nodes = append(nodes, node{
2023-05-12 13:14:06 +00:00
Firstseen: up.Format(iso8601),
2023-05-11 19:38:10 +00:00
Lastseen: time.Now().Format(iso8601),
IsOnline: true,
IsGateway: true,
Clients: 0,
ClientsWifi24: 0,
ClientsWifi5: 0,
ClientsOther: 0,
RootFSUsage: 0,
2023-05-12 13:14:06 +00:00
LoadAVG: cpu,
MemoryUsage: memory,
Uptime: up.Format(iso8601),
2023-05-12 05:55:58 +00:00
GatewayNexthop: "",
Gateway: "",
2023-05-11 19:38:10 +00:00
NodeID: strings.ReplaceAll(d.Devices[i].MAC, ":", ""),
MAC: d.Devices[i].MAC,
2023-05-12 06:17:31 +00:00
Adresses: d.Devices[i].Adresses,
2023-05-11 19:38:10 +00:00
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
}