Changed UISP API for Statistics

This commit is contained in:
Stefan Hoffmann 2024-03-18 18:13:22 +01:00
parent dc1bc7f135
commit 5ba7f23776
Signed by: stefan
GPG Key ID: 230E4E5FC468C133
3 changed files with 84 additions and 20 deletions

View File

@ -330,21 +330,26 @@ type XY struct {
Y int `json:"y"`
}
type AvgMax struct {
AVG []XY `json:"avg"`
MAX []XY `json:"max"`
}
type UNMSstatistics struct {
Period int `json:"period"`
Interval struct {
Start int `json:"start"`
End int `json:"end"`
} `json:"interval"`
CPU []XY `json:"cpu"`
RAM []XY `json:"ram"`
Errors []XY `json:"errors"`
CPU AvgMax `json:"cpu"`
RAM AvgMax `json:"ram"`
Errors AvgMax `json:"errors"`
Interfaces []struct {
ID string `json:"id"`
Priority int `json:"priority"`
Name string `json:"name"`
Receive []XY `json:"receive"`
Transmit []XY `json:"transmit"`
Receive AvgMax `json:"receive"`
Transmit AvgMax `json:"transmit"`
} `json:"interfaces"`
}

View File

@ -10,6 +10,8 @@ import (
"strconv"
"strings"
"time"
client "github.com/influxdata/influxdb1-client/v2"
)
// Unifi Controller API processing
@ -53,23 +55,80 @@ func processUcAPIs() ([]node, []link, error) {
links = UnifiAddLink(jsonDevice, links)
}
}
load, err := strconv.ParseFloat(currentDevice.Sysstats.CPU, 64)
isOnline := currentDevice.State == 1
var load float64
var mem float64
var cpu float64
if isOnline {
load, err = strconv.ParseFloat(currentDevice.Sysstats.CPU, 64)
cpu = load * 100
if err != nil {
log.Println("Error psrsing CPU load from ", currentDevice.Name)
log.Println("Error psrsing CPU of device ", currentDevice.Name)
log.Println(err)
load = 0
cpu = 0
}
mem, err := strconv.ParseFloat(currentDevice.Sysstats.Memory, 64)
mem, err = strconv.ParseFloat(currentDevice.Sysstats.Memory, 64)
if err != nil {
log.Println("Error psrsing CPU load from ", currentDevice.Name)
log.Println("Error parsing Memory of device ", currentDevice.Name)
log.Println(err)
mem = 0
}
}
var model = lookupModels(currentDevice.Model)
var clients int
if conf.Unifi.DisplayUsers {
clients = currentDevice.Users
}
//// INFLUX START
// fields := map[string]interface{}{}
fields := make(map[string]any)
tags := map[string]string{
"hostname": strings.ReplaceAll(currentDevice.Name, " ", "-"),
"nodeid": strings.ReplaceAll(currentDevice.Mac, ":", ""),
}
// Generate fields for all network interfaces (not availible for Unifi Nodes)
//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[interface_name_rx] = details.Interfaces[eth].Statistics.Rxrate
// fields[interface_name_tx] = details.Interfaces[eth].Statistics.Txrate
//}
// set default values if we can't get statistics
fields["cpu"] = 0
fields["load"] = float64(0)
fields["ram"] = 0
if isOnline {
// Generate fields for all Statistics
//load := (float64(load) / float64(100))
fields["cpu"] = int(cpu)
fields["load"] = load
fields["ram"] = int(mem)
}
// Generate field for DHCP Leases
fields["clients.total"] = clients
fields["time.up"] = currentDevice.Uptime
// Generate Dataponts
point, err := client.NewPoint(
"node",
tags,
fields,
time.Now(),
)
if err != nil {
log.Fatalln("Error: ", err)
}
sendInfluxBatchDataPoint(point, conf.General.FreifunkInfluxPort)
// INFLUX STOP
nodes = append(nodes, node{
Firstseen: "0",
Lastseen: time.Unix(int64(currentDevice.LastSeen), 0).Format(iso8601),

View File

@ -141,7 +141,6 @@ func processUNMSAPIRouter() ([]node, error) {
if err := UnmsCallAPI("/devices/"+dev.Identification.ID+"/statistics?interval=hour", &statistics); err != nil {
return nil, err
}
// API CALL FOR DHCP LEASES
log.Println("Getting DHCP Leases of ", d.Devices[i].Name, "from UNMS API")
var dhcpleases UNMSdhcp
@ -174,10 +173,10 @@ func processUNMSAPIRouter() ([]node, error) {
if isOnline {
// Generate fields for all Statistics
load := (float64(statistics.CPU[0].Y) / float64(100))
fields["cpu"] = statistics.CPU[0].Y
load := (float64(statistics.CPU.AVG[0].Y) / float64(100))
fields["cpu"] = statistics.CPU.AVG[0].Y
fields["load"] = load
fields["ram"] = statistics.RAM[0].Y
fields["ram"] = statistics.RAM.AVG[0].Y
}
// Generate field for DHCP Leases
@ -238,6 +237,7 @@ func UnmsCallAPI(url string, i any) error {
if err != nil {
return errors.New(fmt.Sprint("can't set request", conf.Unms.UnmsAPIURL+url))
}
log.Println(conf.Unms.UnmsAPIURL + url)
request.Header.Set("x-auth-token", conf.Unms.APItoken)
client := &http.Client{}
response, err := client.Do(request)