Add RX/TX and DHCP Leases

This commit is contained in:
Stefan Hoffmann 2023-03-20 20:15:34 +01:00
parent cfdca822ae
commit 12cc8fe7f1
2 changed files with 102 additions and 57 deletions

View File

@ -76,6 +76,29 @@ type unifiAPIDetails struct {
RAM float64 `json:"ram"` RAM float64 `json:"ram"`
} `json:"overview"` } `json:"overview"`
IPAddress string `json:"ipAddress"` IPAddress string `json:"ipAddress"`
Location struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Altitude int `json:"altitude"`
} `json:"location"`
Interfaces []struct {
Identification struct {
Type string `json:"type"`
Name string `json:"name"`
Description string `json:"description"`
Mac string `json:"mac"`
DisplayName string `json:"displayName"`
} `json:"identification"`
Statistics struct {
Rxrate int `json:"rxrate"`
Txrate int `json:"txrate"`
Rxbytes int64 `json:"rxbytes"`
Txbytes int64 `json:"txbytes"`
PoePower int `json:"poePower"`
Dropped int `json:"dropped"`
Errors int `json:"errors"`
} `json:"statistics"`
} `json:"interfaces"`
} }
type unifiAPIAirmax struct { type unifiAPIAirmax struct {
@ -327,3 +350,9 @@ type UNMSstatistics struct {
} `json:"transmit"` } `json:"transmit"`
} `json:"interfaces"` } `json:"interfaces"`
} }
type UNMSdhcp []struct {
Address string `json:"address"`
Hostname string `json:"hostname"`
Type string `json:"type"`
}

130
unms.go
View File

@ -135,13 +135,10 @@ func processUNMSAPIRouter() []node {
} }
// END OF API CALL 1 // END OF API CALL 1
// API CALL 2 // API CALL FOR ROUTER DETAILS
log.Println("calling API 2 for device", d.Devices[i].Name) log.Println("calling API 2 for device", d.Devices[i].Name)
var details unifiAPIDetails var details unifiAPIDetails
UnmsCallAPI("/devices/erouters/"+dev.Identification.ID, &details) UnmsCallAPI("/devices/erouters/"+dev.Identification.ID, &details)
// END OF API CALL 2
// API CALL 3 STATISTICS!
bp, err := client.NewBatchPoints(client.BatchPointsConfig{ bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: "freifunk", Database: "freifunk",
Precision: "s", Precision: "s",
@ -149,6 +146,43 @@ 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)
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 { for i := range d.Devices {
log.Println("calling API 3 for device", d.Devices[i].Name) log.Println("calling API 3 for device", d.Devices[i].Name)
var statistics UNMSstatistics var statistics UNMSstatistics
@ -158,14 +192,14 @@ func processUNMSAPIRouter() []node {
break break
} }
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, ":", ""),
} }
load := (float64(statistics.CPU[t].Y) / float64(100))
fields := map[string]interface{}{ fields := map[string]interface{}{
"cpu": statistics.CPU[t].Y, "cpu": statistics.CPU[t].Y,
"load": float64(statistics.CPU[t].Y / 100), "load": load,
"ram": statistics.RAM[t].Y, "ram": statistics.RAM[t].Y,
//hier fehlen noch die interfaces
} }
slice := strconv.Itoa(statistics.CPU[t].X) slice := strconv.Itoa(statistics.CPU[t].X)
timeunix, err := strconv.Atoi(slice[:10]) timeunix, err := strconv.Atoi(slice[:10])
@ -191,60 +225,42 @@ func processUNMSAPIRouter() []node {
log.Fatal(err) log.Fatal(err)
} }
} }
// ab hier Interfaces }
for i := range d.Devices { // END OF API CALL 3
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 statistics.Interfaces {
for t := range statistics.Interfaces[eth].Transmit {
if t > 9 {
break
}
tags := map[string]string{
"hostname": d.Devices[i].Name,
"nodeid": strings.ReplaceAll(dev.Identification.MAC, ":", ""),
}
interface_name_rx := ("traffic.rx.bytes" + "_" + statistics.Interfaces[eth].ID)
interface_name_tx := ("traffic.tx.bytes" + "_" + statistics.Interfaces[eth].ID)
if statistics.Interfaces[eth].ID == "eth0" {
interface_name_rx = "traffic.rx.bytes"
interface_name_tx = "traffic.tx.bytes"
}
fields := map[string]interface{}{
interface_name_rx: statistics.Interfaces[eth].Receive[t].Y * 100,
interface_name_tx: statistics.Interfaces[eth].Transmit[t].Y * 100,
}
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) // Start API Call 4 (DHCP Leases)
c := influxDBClient() for i := range d.Devices {
err = c.Write(bp) log.Println("calling API 4 for device", d.Devices[i].Name)
if err != nil { var dhcpleases UNMSdhcp
log.Fatal(err) UnmsCallAPI("/devices/erouters/"+dev.Identification.ID+"/dhcp/leases", &dhcpleases)
}
} 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()
point, err := client.NewPoint(
"node",
tags,
fields,
tm,
)
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
// Get info from json file (static) // Get info from json file (static)
nodes = append(nodes, node{ nodes = append(nodes, node{
Firstseen: dev.Overview.CreatedAt.Format(iso8601), Firstseen: dev.Overview.CreatedAt.Format(iso8601),