ubnt-freifunk-map-api/unms.go

283 lines
8.5 KiB
Go
Raw Normal View History

2021-05-18 19:17:09 +00:00
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
2023-03-20 14:38:48 +00:00
"time"
_ "github.com/fatih/structs"
//_ "github.com/influxdata/influxdb1-client" // this is important because of the bug in go mod
client "github.com/influxdata/influxdb1-client/v2"
2021-05-18 19:17:09 +00:00
)
2023-03-20 14:38:48 +00:00
// UNMS API processing (Richtfunk)
2023-05-14 09:44:20 +00:00
func processUNMSAPI() ([]node, []link, error) {
2021-05-18 19:17:09 +00:00
// Variables for runtime
var links []link
var nodes []node
d := getDevices(conf.Unms.DevicesURL)
2023-05-14 09:44:20 +00:00
// API CALL 1 (get Device overview)
log.Println("Starting UISP API Crawler for Rifu devices")
log.Println("Getting device overview from UNMS API")
2021-05-18 19:17:09 +00:00
var u []unifiAPIResponse
2023-05-14 09:44:20 +00:00
if err := UnmsCallAPI("/devices", &u); err != nil {
return nil, nil, err
2021-05-18 19:17:09 +00:00
}
for i := range d.Devices {
var dev unifiAPIResponse
var currentDevice device
for j := range u {
if strings.ToUpper(u[j].Identification.MAC) == strings.ToUpper(d.Devices[i].MAC) {
dev = u[j]
currentDevice = d.Devices[i]
}
}
isOnline := dev.Overview.Status == "active"
2021-05-18 19:17:09 +00:00
// END OF API CALL 1
2023-05-14 09:44:20 +00:00
// Getting details from UISP
log.Println("Getting device details for: ", d.Devices[i].Name)
2021-05-18 19:17:09 +00:00
var details unifiAPIDetails
2023-05-14 09:44:20 +00:00
if err := UnmsCallAPI("/devices/erouters/"+dev.Identification.ID, &details); err != nil {
return nil, nil, err
}
2021-05-18 19:17:09 +00:00
2023-05-14 09:44:20 +00:00
// Getting details for RiFu
log.Println("Getting details for RiFu Link for: ", d.Devices[i].Name)
2021-05-18 19:17:09 +00:00
var airmaxes []unifiAPIAirmax
2023-05-14 09:44:20 +00:00
if err := UnmsCallAPI("/devices/airmaxes/"+dev.Identification.ID+"/stations", &airmaxes); err != nil {
return nil, nil, err
}
2021-05-18 19:17:09 +00:00
// check if remote mac address is part of our published network
for i := range airmaxes {
if isRemoteMACpublished(airmaxes[i].DeviceIdentification.MAC, d.Devices) {
2023-03-20 15:00:11 +00:00
links = UnmsAddLink(dev, airmaxes[i], links)
2021-05-18 19:17:09 +00:00
}
}
// END OF API CALL 3
// Get info from json file (static)
nodes = append(nodes, node{
Firstseen: dev.Overview.CreatedAt.Format(iso8601),
Lastseen: dev.Overview.LastSeen.Format(iso8601),
IsOnline: isOnline,
IsGateway: false,
Clients: 0,
ClientsWifi24: 0,
ClientsWifi5: 0,
ClientsOther: 0,
RootFSUsage: 0,
LoadAVG: details.Overview.CPU / 100,
MemoryUsage: details.Overview.RAM / 100,
Uptime: dev.Identification.Started.Format(iso8601),
GatewayNexthop: currentDevice.GatewayNexthop,
Gateway: currentDevice.Gateway,
Location: &currentDevice.Location,
NodeID: strings.ReplaceAll(dev.Identification.MAC, ":", ""),
MAC: dev.Identification.MAC,
Adresses: UnmsGetAddresses(details.IPAddress),
Domain: currentDevice.Domain,
Hostname: "[RiFu] " + details.Identification.Name,
Owner: "Freifunk Rhein-Sieg",
Firmware: firmware{
Base: "Ubiquiti - Stock",
Release: details.Firmware.Current,
},
Autoupdater: autoupdater{
Enabled: false,
Branch: "stable",
},
NProc: 1,
Model: details.Identification.Model,
})
}
2023-05-14 09:44:20 +00:00
return nodes, links, nil
2021-05-18 19:17:09 +00:00
}
func processUNMSAPIRouter() ([]node, error) {
2023-03-20 14:38:48 +00:00
// Variables for runtime
var nodes []node
d := getDevices(conf.Unms.RouterURL)
2023-03-20 19:49:01 +00:00
// API CALL 1, get all devices list from UNMS
2023-05-14 09:44:20 +00:00
log.Println("Get all Routers from UISP")
2023-03-20 14:38:48 +00:00
var u []unifiAPIResponse
2023-05-14 09:44:20 +00:00
if err := UnmsCallAPI("/devices", &u); err != nil {
return nil, err
2023-03-20 14:38:48 +00:00
}
2023-03-20 19:49:01 +00:00
// Get Information for devices device
2023-03-20 14:38:48 +00:00
for i := range d.Devices {
var dev unifiAPIResponse
var currentDevice device
for j := range u {
if strings.ToUpper(u[j].Identification.MAC) == strings.ToUpper(d.Devices[i].MAC) {
dev = u[j]
currentDevice = d.Devices[i]
}
}
isOnline := dev.Overview.Status == "active"
2023-03-20 14:38:48 +00:00
2023-03-20 19:49:01 +00:00
// API CALL FOR ROUTER DETAILS (Interface RX/TX)
2023-05-14 09:44:20 +00:00
log.Println("Getting details of ", d.Devices[i].Name, "from UISP API")
2023-03-20 14:38:48 +00:00
var details unifiAPIDetails
if err := UnmsCallAPI("/devices/erouters/"+dev.Identification.ID, &details); err != nil {
return nil, err
}
2023-03-20 19:49:01 +00:00
// API CALL FOR DEVICE STATISTICS (CPU, RAM)
2023-05-14 09:44:20 +00:00
log.Println("Getting statistics of ", d.Devices[i].Name, "from UISP API")
2023-03-20 19:49:01 +00:00
var statistics UNMSstatistics
if err := UnmsCallAPI("/devices/"+dev.Identification.ID+"/statistics?interval=hour", &statistics); err != nil {
return nil, err
}
2023-03-20 19:49:01 +00:00
// API CALL FOR DHCP LEASES
log.Println("Getting DHCP Leases of ", d.Devices[i].Name, "from UNMS API")
var dhcpleases UNMSdhcp
if isOnline {
if err := UnmsCallAPI("/devices/erouters/"+dev.Identification.ID+"/dhcp/leases", &dhcpleases); err != nil {
return nil, err
}
} else {
log.Println("Router ist offline, skipping DHCP Leases")
}
2023-03-20 19:49:01 +00:00
// fields := map[string]interface{}{}
fields := make(map[string]any)
2023-03-20 19:49:01 +00:00
tags := map[string]string{
"hostname": strings.ReplaceAll(d.Devices[i].Name, " ", "-"),
"nodeid": strings.ReplaceAll(dev.Identification.MAC, ":", ""),
2023-03-20 19:15:34 +00:00
}
2023-03-20 19:49:01 +00:00
// 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[interface_name_rx] = details.Interfaces[eth].Statistics.Rxrate
fields[interface_name_tx] = details.Interfaces[eth].Statistics.Txrate
2023-03-20 19:15:34 +00:00
}
2023-03-20 14:38:48 +00:00
// 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(statistics.CPU[0].Y) / float64(100))
fields["cpu"] = statistics.CPU[0].Y
fields["load"] = load
fields["ram"] = statistics.RAM[0].Y
}
2023-03-20 19:49:01 +00:00
// Generate field for DHCP Leases
fields["clients.total"] = len(dhcpleases)
2023-03-20 19:49:01 +00:00
// Generate Dataponts
point, err := client.NewPoint(
"node",
tags,
fields,
time.Now(),
)
if err != nil {
log.Fatalln("Error: ", err)
}
2023-03-20 14:38:48 +00:00
2023-05-12 13:14:06 +00:00
sendInfluxBatchDataPoint(point, conf.General.FreifunkInfluxPort)
2023-03-20 14:38:48 +00:00
// Get info from json file (static)
nodes = append(nodes, node{
Firstseen: dev.Overview.CreatedAt.Format(iso8601),
Lastseen: dev.Overview.LastSeen.Format(iso8601),
IsOnline: isOnline,
IsGateway: false,
Clients: 0,
ClientsWifi24: 0,
ClientsWifi5: 0,
ClientsOther: 0,
RootFSUsage: 0,
LoadAVG: details.Overview.CPU / 100,
MemoryUsage: details.Overview.RAM / 100,
Uptime: dev.Identification.Started.Format(iso8601),
GatewayNexthop: currentDevice.GatewayNexthop,
Gateway: currentDevice.Gateway,
Location: &currentDevice.Location,
NodeID: strings.ReplaceAll(dev.Identification.MAC, ":", ""),
MAC: dev.Identification.MAC,
Adresses: UnmsGetAddresses(details.IPAddress),
Domain: currentDevice.Domain,
Hostname: "[VPN-Router] " + details.Identification.Name,
2023-03-20 14:38:48 +00:00
Owner: "Freifunk Rhein-Sieg",
Firmware: firmware{
Base: "Ubiquiti - Stock",
Release: details.Firmware.Current,
},
Autoupdater: autoupdater{
Enabled: false,
Branch: "stable",
},
NProc: 1,
Model: details.Identification.Model,
})
}
return nodes, nil
2023-03-20 14:38:48 +00:00
}
func UnmsCallAPI(url string, i any) error {
2021-05-18 19:17:09 +00:00
request, err := http.NewRequest(http.MethodGet, conf.Unms.UnmsAPIURL+url, nil)
if err != nil {
return errors.New(fmt.Sprint("can't set request", conf.Unms.UnmsAPIURL+url))
}
request.Header.Set("x-auth-token", conf.Unms.APItoken)
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
return fmt.Errorf("can't get request %s with x-auth-token %s", conf.Unms.UnmsAPIURL+url, conf.Unms.APItoken)
}
if response.StatusCode != 200 {
log.Fatalln("Can't call UNMS API, check token and URL. HTTP Status: ", response.StatusCode)
2021-05-18 19:17:09 +00:00
}
data, err := ioutil.ReadAll(response.Body)
defer response.Body.Close()
if err != nil {
return fmt.Errorf("can't read response body: %+v", response.Body)
}
// no error occurred, unmarshal to struct
return json.Unmarshal(data, &i)
2021-05-18 19:17:09 +00:00
}
func UnmsGetAddresses(ip string) []string {
var adresses []string
adresses = append(adresses, strings.Split(ip, "/")[0])
return adresses
}
2023-03-20 15:00:11 +00:00
func UnmsAddLink(dev unifiAPIResponse, airmaxes unifiAPIAirmax, links []link) []link {
for i := range links {
if links[i].SourceAddr == airmaxes.DeviceIdentification.MAC {
// link already exists
return links
}
}
links = append(links, link{
Type: "wifi",
Source: strings.ReplaceAll(dev.Identification.MAC, ":", ""),
Target: strings.ReplaceAll(airmaxes.DeviceIdentification.MAC, ":", ""),
SourceTQ: airmaxes.Statistics.LinkScore,
TargetTQ: airmaxes.Statistics.LinkScore,
SourceAddr: dev.Identification.MAC,
TargetAddr: airmaxes.DeviceIdentification.MAC,
})
return links
}