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

166
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,119 +158,47 @@ 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
UnmsCallAPI("/devices/"+dev.Identification.ID+"/statistics?interval=hour", &statistics)
for eth := range details.Interfaces {
tags := map[string]string{
"hostname": d.Devices[i].Name,
"nodeid": strings.ReplaceAll(dev.Identification.MAC, ":", ""),
}
interface_name_rx := ("rate.rx" + "_" + details.Interfaces[eth].Identification.Name)
interface_name_tx := ("rate.tx" + "_" + details.Interfaces[eth].Identification.Name)
fields := map[string]interface{}{
interface_name_rx: details.Interfaces[eth].Statistics.Rxrate,
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) fields := map[string]interface{}{}
c := influxDBClient() tags := map[string]string{
err = c.Write(bp) "hostname": strings.ReplaceAll(d.Devices[i].Name, " ", "-"),
if err != nil { "nodeid": strings.ReplaceAll(dev.Identification.MAC, ":", ""),
log.Fatal(err)
}
}
} }
// END OF API CALL // Generate fields for all network interfaces
for eth := range details.Interfaces {
// API CALL 3 STATISTICS interface_name_rx := ("rate.rx" + "_" + details.Interfaces[eth].Identification.Name)
for i := range d.Devices { interface_name_tx := ("rate.tx" + "_" + details.Interfaces[eth].Identification.Name)
log.Println("calling API 3 for device", d.Devices[i].Name) fields[interface_name_rx] = details.Interfaces[eth].Statistics.Rxrate
var statistics UNMSstatistics fields[interface_name_tx] = details.Interfaces[eth].Statistics.Txrate
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) // Generate fields for all Statistics
for i := range d.Devices { load := (float64(statistics.CPU[0].Y) / float64(100))
log.Println("calling API 4 for device", d.Devices[i].Name) fields["cpu"] = statistics.CPU[0].Y
var dhcpleases UNMSdhcp fields["load"] = load
UnmsCallAPI("/devices/erouters/"+dev.Identification.ID+"/dhcp/leases", &dhcpleases) fields["ram"] = statistics.RAM[0].Y
leases := len(dhcpleases) // Generate field for DHCP Leases
tags := map[string]string{ leases := len(dhcpleases)
"hostname": strings.ReplaceAll(d.Devices[i].Name, " ", "-"), fields["clients.total"] = leases
"nodeid": strings.ReplaceAll(dev.Identification.MAC, ":", ""),
}
fields := map[string]interface{}{
"clients.total": leases,
}
tm := time.Now()
point, err := client.NewPoint(
"node",
tags,
fields,
tm,
)
if err != nil {
log.Fatalln("Error: ", err)
}
bp.AddPoint(point) // Generate Dataponts
c := influxDBClient() point, err := client.NewPoint(
err = c.Write(bp) "node",
if err != nil { tags,
log.Fatal(err) fields,
} time.Now(),
)
if err != nil {
log.Fatalln("Error: ", err)
}
// Add Datapoints in InfluxDB
bp.AddPoint(point)
c := influxDBClient()
err = c.Write(bp)
if err != nil {
log.Fatal(err)
} }
// Get info from json file (static) // Get info from json file (static)