ubnt-freifunk-map-api/unifi.go

218 lines
5.3 KiB
Go
Raw Normal View History

2021-05-18 19:17:09 +00:00
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
2021-05-18 19:17:09 +00:00
"log"
"net/http"
"strconv"
"strings"
"time"
)
2023-04-28 13:20:42 +00:00
// Unifi Controller API processing
2023-05-14 09:44:20 +00:00
func processUcAPIs() ([]node, []link, error) {
2021-05-18 19:17:09 +00:00
//get list of Unifi devices to display
var nodes []node
var links []link
d := getDevices(conf.Unifi.UCDevicesURL)
//call Unifi Controller
ucAPI := UnifiNewAPI(conf.Unifi.User, conf.Unifi.Password, conf.Unifi.APIURL)
//login
2023-05-14 09:44:20 +00:00
if err := ucAPI.ucLogin(); err != nil {
return nil, nil, err
}
2021-05-18 19:17:09 +00:00
//get all Sites from Controller
sites, err := ucAPI.ucGetSites()
if err != nil {
2023-05-14 09:44:20 +00:00
return nil, nil, err
2021-05-18 19:17:09 +00:00
}
//get all devices in all sites
devices, err := ucAPI.ucGetDevices(sites)
if err != nil {
2023-05-14 09:44:20 +00:00
return nil, nil, err
2021-05-18 19:17:09 +00:00
}
//build nodes struct
//mvJson, err := getMeshviewerJSON()
for _, jsonDevice := range d.Devices {
var currentDevice ucDevice
var currentJSONDevice device
2023-05-14 09:44:20 +00:00
isOnline := currentDevice.State == 1
2021-05-18 19:17:09 +00:00
for _, device := range devices {
2023-04-28 13:20:42 +00:00
if strings.EqualFold(device.Mac, jsonDevice.MAC) {
2021-05-18 19:17:09 +00:00
currentDevice = device
currentJSONDevice = jsonDevice
}
}
if isRemoteMACpublished(jsonDevice.MAC, d.Devices) {
//hier muss gecheckt werden ob der link valide ist
if checkMeshviewerLink(jsonDevice.LinkedTo) {
links = UnifiAddLink(jsonDevice, links)
}
}
load, err := strconv.ParseFloat(currentDevice.Sysstats.CPU, 64)
if err != nil {
2023-05-14 09:44:20 +00:00
log.Println("Error psrsing CPU load from ", currentDevice.Name)
log.Println(err)
2021-05-18 19:17:09 +00:00
load = 0
}
mem, err := strconv.ParseFloat(currentDevice.Sysstats.Memory, 64)
if err != nil {
2023-05-14 09:44:20 +00:00
log.Println("Error psrsing CPU load from ", currentDevice.Name)
log.Println(err)
mem = 0
2021-05-18 19:17:09 +00:00
}
var model = lookupModels(currentDevice.Model)
2023-04-28 13:20:42 +00:00
var clients int
if conf.Unifi.DisplayUsers {
clients = currentDevice.Users
2021-05-18 19:17:09 +00:00
}
nodes = append(nodes, node{
Firstseen: "0",
Lastseen: time.Unix(int64(currentDevice.LastSeen), 0).Format(iso8601),
IsOnline: itob(currentDevice.State),
IsGateway: false,
Clients: clients,
ClientsWifi24: 0,
ClientsWifi5: 0,
ClientsOther: clients,
RootFSUsage: 0,
LoadAVG: load / 100,
MemoryUsage: mem / 100,
Uptime: time.Now().Add(-1 * time.Second * time.Duration(currentDevice.Uptime)).Format(iso8601),
GatewayNexthop: currentJSONDevice.GatewayNexthop,
Gateway: currentJSONDevice.Gateway,
Location: &currentJSONDevice.Location,
NodeID: strings.ReplaceAll(currentDevice.Mac, ":", ""),
MAC: currentDevice.Mac,
Adresses: []string{currentDevice.IP},
Domain: currentJSONDevice.Domain,
Hostname: "[Unifi] " + currentDevice.Name,
Owner: "Freifunk Rhein-Sieg",
Firmware: firmware{
Base: "Ubiquiti - Stock",
Release: currentDevice.Version,
},
Autoupdater: autoupdater{
Enabled: false,
Branch: "stable",
},
NProc: 1,
Model: model,
})
}
2023-05-14 09:44:20 +00:00
return nodes, links, err
2021-05-18 19:17:09 +00:00
}
func UnifiNewAPI(user string, pass string, baseURL string) UnifiAPIData {
return UnifiAPIData{
user: user,
pass: pass,
baseURL: baseURL,
client: httpClient(),
}
}
func (u *UnifiAPIData) ucCallAPI(url string, method string, body *bytes.Buffer, output interface{}) error {
req, err := http.NewRequest(method, u.baseURL+url, body)
if err != nil {
return fmt.Errorf("can't set request %s", u.baseURL+url)
}
req.Header.Set("Content-Type", "application/json")
response, err := u.client.Do(req)
if err != nil {
return fmt.Errorf("can't login %s", u.baseURL+url)
}
defer response.Body.Close()
if response.StatusCode != 200 {
return fmt.Errorf("login failed %s", u.baseURL+url)
2021-05-18 19:17:09 +00:00
}
data, err := io.ReadAll(response.Body)
2021-05-18 19:17:09 +00:00
if err != nil {
return err
}
err = json.Unmarshal(data, &output)
if err != nil {
return err
}
return nil
}
func (u *UnifiAPIData) ucLogin() error {
var loginData = []byte(`{"username":"` + u.user + `","password":"` + u.pass + `"}`)
url := "/api/login"
err := u.ucCallAPI(url, http.MethodPost, bytes.NewBuffer(loginData), nil)
if err != nil {
return err
}
return nil
}
func (u *UnifiAPIData) ucGetSites() ([]ucSite, error) {
var d struct {
Data []ucSite `json:"data"`
}
url := "/api/self/sites"
err := u.ucCallAPI(url, http.MethodGet, bytes.NewBuffer([]byte{}), &d)
if err != nil {
return []ucSite{}, err
}
return d.Data, nil
}
func (u *UnifiAPIData) ucGetDevices(sites []ucSite) ([]ucDevice, error) {
var d struct {
Data []ucDevice `json:"data"`
}
var s []ucDevice
for _, site := range sites {
url := "/api/s/" + site.ID + "/stat/device"
u.ucCallAPI(url, http.MethodGet, bytes.NewBuffer([]byte{}), &d)
s = append(s, d.Data...)
}
return s, nil
}
func UnifiAddLink(dev device, links []link) []link {
for i := range links {
if links[i].SourceAddr == dev.MAC {
// link already exists
return links
}
}
if dev.LinkedTo == "" {
//no LinkedTo in ucDevices.json
return links
}
links = append(links, link{
Type: "cable",
Source: strings.ReplaceAll(dev.MAC, ":", ""),
Target: strings.ReplaceAll(dev.GatewayNexthop, ":", ""),
SourceTQ: 1,
TargetTQ: 1,
SourceAddr: dev.MAC,
TargetAddr: dev.LinkedTo,
})
return links
}
func findNodeID(NodeID string) bool {
for i := range ucDev.Devices {
if ucDev.Devices[i].GatewayNexthop == NodeID {
return true
}
}
return false
}