tidy up unter hempels sofa
All checks were successful
continuous-integration/drone/tag Build is passing

This commit is contained in:
Stefan Hoffmann 2023-03-20 20:49:01 +01:00
parent 12cc8fe7f1
commit 3e0c70bb03

134
unms.go
View File

@ -7,7 +7,6 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"strconv"
"strings" "strings"
"time" "time"
@ -111,14 +110,16 @@ func processUNMSAPIRouter() []node {
d := getDevices(conf.Unms.RouterURL) d := getDevices(conf.Unms.RouterURL)
// API CALL 1 // API CALL 1, get all devices list from UNMS
log.Println("calling API 1") log.Println("Get all devices from UNMS")
var u []unifiAPIResponse var u []unifiAPIResponse
err := UnmsCallAPI("/devices", &u) err := UnmsCallAPI("/devices", &u)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
// END OF API CALL 1
// Get Information for devices device
for i := range d.Devices { for i := range d.Devices {
var dev unifiAPIResponse var dev unifiAPIResponse
var currentDevice device var currentDevice device
@ -133,12 +134,23 @@ func processUNMSAPIRouter() []node {
if dev.Overview.Status == "active" { if dev.Overview.Status == "active" {
isOnline = true isOnline = true
} }
// END OF API CALL 1
// API CALL FOR ROUTER DETAILS // API CALL FOR ROUTER DETAILS (Interface RX/TX)
log.Println("calling API 2 for device", d.Devices[i].Name) log.Println("Getting details of ", d.Devices[i].Name, "from UNMS API")
var details unifiAPIDetails var details unifiAPIDetails
UnmsCallAPI("/devices/erouters/"+dev.Identification.ID, &details) UnmsCallAPI("/devices/erouters/"+dev.Identification.ID, &details)
// API CALL FOR DEVICE STATISTICS (CPU, RAM)
log.Println("Getting statistics of ", d.Devices[i].Name, "from UNMS API")
var statistics UNMSstatistics
UnmsCallAPI("/devices/"+dev.Identification.ID+"/statistics?interval=hour", &statistics)
// API CALL FOR DHCP LEASES
log.Println("Getting DHCP Leases of ", d.Devices[i].Name, "from UNMS API")
var dhcpleases UNMSdhcp
UnmsCallAPI("/devices/erouters/"+dev.Identification.ID+"/dhcp/leases", &dhcpleases)
// Open connection to InfluxDB
bp, err := client.NewBatchPoints(client.BatchPointsConfig{ bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: "freifunk", Database: "freifunk",
Precision: "s", Precision: "s",
@ -146,120 +158,48 @@ func processUNMSAPIRouter() []node {
if err != nil { if err != nil {
log.Fatalln("Error: ", err) log.Fatalln("Error: ", err)
} }
for i := range d.Devices { //
log.Println("calling API 3 for device", d.Devices[i].Name)
var statistics UNMSstatistics fields := map[string]interface{}{}
UnmsCallAPI("/devices/"+dev.Identification.ID+"/statistics?interval=hour", &statistics)
for eth := range details.Interfaces {
tags := map[string]string{ tags := map[string]string{
"hostname": d.Devices[i].Name, "hostname": strings.ReplaceAll(d.Devices[i].Name, " ", "-"),
"nodeid": strings.ReplaceAll(dev.Identification.MAC, ":", ""), "nodeid": strings.ReplaceAll(dev.Identification.MAC, ":", ""),
} }
// Generate fields for all network interfaces
for eth := range details.Interfaces {
interface_name_rx := ("rate.rx" + "_" + details.Interfaces[eth].Identification.Name) interface_name_rx := ("rate.rx" + "_" + details.Interfaces[eth].Identification.Name)
interface_name_tx := ("rate.tx" + "_" + details.Interfaces[eth].Identification.Name) interface_name_tx := ("rate.tx" + "_" + details.Interfaces[eth].Identification.Name)
fields := map[string]interface{}{ fields[interface_name_rx] = details.Interfaces[eth].Statistics.Rxrate
interface_name_rx: details.Interfaces[eth].Statistics.Rxrate, fields[interface_name_tx] = details.Interfaces[eth].Statistics.Txrate
interface_name_tx: details.Interfaces[eth].Statistics.Txrate,
}
tm := time.Now()
point, err := client.NewPoint(
"node",
tags,
fields,
tm,
)
if err != nil {
log.Fatalln("Error: ", err)
} }
bp.AddPoint(point) // Generate fields for all Statistics
c := influxDBClient() load := (float64(statistics.CPU[0].Y) / float64(100))
err = c.Write(bp) fields["cpu"] = statistics.CPU[0].Y
if err != nil { fields["load"] = load
log.Fatal(err) fields["ram"] = statistics.RAM[0].Y
}
}
}
// END OF API CALL
// API CALL 3 STATISTICS
for i := range d.Devices {
log.Println("calling API 3 for device", d.Devices[i].Name)
var statistics UNMSstatistics
UnmsCallAPI("/devices/"+dev.Identification.ID+"/statistics?interval=hour", &statistics)
for t := range statistics.CPU {
if t > 9 {
break
}
tags := map[string]string{
"hostname": strings.ReplaceAll(d.Devices[i].Name, " ", "-"),
"nodeid": strings.ReplaceAll(dev.Identification.MAC, ":", ""),
}
load := (float64(statistics.CPU[t].Y) / float64(100))
fields := map[string]interface{}{
"cpu": statistics.CPU[t].Y,
"load": load,
"ram": statistics.RAM[t].Y,
}
slice := strconv.Itoa(statistics.CPU[t].X)
timeunix, err := strconv.Atoi(slice[:10])
if err != nil {
log.Fatalln("Error: ", err)
}
tm := time.Unix(int64(timeunix), 0)
time_local := tm.Add(1 * time.Hour)
point, err := client.NewPoint(
"node",
tags,
fields,
time_local,
)
if err != nil {
log.Fatalln("Error: ", err)
}
bp.AddPoint(point)
c := influxDBClient()
err = c.Write(bp)
if err != nil {
log.Fatal(err)
}
}
}
// END OF API CALL 3
// Start API Call 4 (DHCP Leases)
for i := range d.Devices {
log.Println("calling API 4 for device", d.Devices[i].Name)
var dhcpleases UNMSdhcp
UnmsCallAPI("/devices/erouters/"+dev.Identification.ID+"/dhcp/leases", &dhcpleases)
// Generate field for DHCP Leases
leases := len(dhcpleases) leases := len(dhcpleases)
tags := map[string]string{ fields["clients.total"] = leases
"hostname": strings.ReplaceAll(d.Devices[i].Name, " ", "-"),
"nodeid": strings.ReplaceAll(dev.Identification.MAC, ":", ""), // Generate Dataponts
}
fields := map[string]interface{}{
"clients.total": leases,
}
tm := time.Now()
point, err := client.NewPoint( point, err := client.NewPoint(
"node", "node",
tags, tags,
fields, fields,
tm, time.Now(),
) )
if err != nil { if err != nil {
log.Fatalln("Error: ", err) log.Fatalln("Error: ", err)
} }
// Add Datapoints in InfluxDB
bp.AddPoint(point) bp.AddPoint(point)
c := influxDBClient() c := influxDBClient()
err = c.Write(bp) err = c.Write(bp)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
}
// Get info from json file (static) // Get info from json file (static)
nodes = append(nodes, node{ nodes = append(nodes, node{