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"
"log"
"net/http"
"strconv"
"strings"
"time"
@ -111,14 +110,16 @@ func processUNMSAPIRouter() []node {
d := getDevices(conf.Unms.RouterURL)
// API CALL 1
log.Println("calling API 1")
// API CALL 1, get all devices list from UNMS
log.Println("Get all devices from UNMS")
var u []unifiAPIResponse
err := UnmsCallAPI("/devices", &u)
if err != nil {
log.Fatalln(err)
}
// END OF API CALL 1
// Get Information for devices device
for i := range d.Devices {
var dev unifiAPIResponse
var currentDevice device
@ -133,12 +134,23 @@ func processUNMSAPIRouter() []node {
if dev.Overview.Status == "active" {
isOnline = true
}
// END OF API CALL 1
// API CALL FOR ROUTER DETAILS
log.Println("calling API 2 for device", d.Devices[i].Name)
// API CALL FOR ROUTER DETAILS (Interface RX/TX)
log.Println("Getting details of ", d.Devices[i].Name, "from UNMS API")
var details unifiAPIDetails
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{
Database: "freifunk",
Precision: "s",
@ -146,120 +158,48 @@ func processUNMSAPIRouter() []node {
if err != nil {
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 {
//
fields := map[string]interface{}{}
tags := map[string]string{
"hostname": d.Devices[i].Name,
"hostname": strings.ReplaceAll(d.Devices[i].Name, " ", "-"),
"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_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)
fields[interface_name_rx] = details.Interfaces[eth].Statistics.Rxrate
fields[interface_name_tx] = details.Interfaces[eth].Statistics.Txrate
}
bp.AddPoint(point)
c := influxDBClient()
err = c.Write(bp)
if err != nil {
log.Fatal(err)
}
}
}
// 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 fields for all Statistics
load := (float64(statistics.CPU[0].Y) / float64(100))
fields["cpu"] = statistics.CPU[0].Y
fields["load"] = load
fields["ram"] = statistics.RAM[0].Y
// Generate field for DHCP Leases
leases := len(dhcpleases)
tags := map[string]string{
"hostname": strings.ReplaceAll(d.Devices[i].Name, " ", "-"),
"nodeid": strings.ReplaceAll(dev.Identification.MAC, ":", ""),
}
fields := map[string]interface{}{
"clients.total": leases,
}
tm := time.Now()
fields["clients.total"] = leases
// Generate Dataponts
point, err := client.NewPoint(
"node",
tags,
fields,
tm,
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)
nodes = append(nodes, node{