Added Links to Unifi Devices
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
Stefan Hoffmann 2021-02-06 11:46:08 +01:00
parent 7a4ed3ae27
commit 2d58f3fd5e
2 changed files with 52 additions and 8 deletions

59
main.go
View File

@ -30,13 +30,14 @@ var token = flag.String("token", "", "Defines the x-auth-token")
var ucUser = flag.String("ucUser", "", "Defines the Unifi API User") var ucUser = flag.String("ucUser", "", "Defines the Unifi API User")
var ucPass = flag.String("ucPass", "", "Defines the Unifi API Password") var ucPass = flag.String("ucPass", "", "Defines the Unifi API Password")
var version = "development" var version = "development"
var delay time.Duration = 60 * time.Second var delay time.Duration = 5 * time.Second
func main() { func main() {
log.Printf("starting version %s...\n", version) log.Printf("starting version %s...\n", version)
// parse all flags // parse all flags
flag.Parse() flag.Parse()
// check if token is set
// check if flags are set
if *token == "" { if *token == "" {
log.Fatalln("Please specify an API token via the flag '-token'") log.Fatalln("Please specify an API token via the flag '-token'")
} }
@ -46,14 +47,15 @@ func main() {
if *ucUser == "" { if *ucUser == "" {
log.Fatalln("Please specify an API User via the flag '-ucUser'") log.Fatalln("Please specify an API User via the flag '-ucUser'")
} }
//getFile()
// start API processing (runs in a loop) // start API processing (runs in a loop)
go processAPIs() go processAPIs()
// start webserver // start webserver on Port 3000
serveJSON() serveJSON()
} }
//switch Unifi AP Mod IDs to Names
func lookupModels(model string) string { func lookupModels(model string) string {
switch model { switch model {
case "BZ2", "U2S48", "U2Sv2": case "BZ2", "U2S48", "U2Sv2":
@ -85,6 +87,7 @@ func lookupModels(model string) string {
} }
} }
//int to bool converter
func itob(i int) bool { func itob(i int) bool {
if i == 1 { if i == 1 {
return true return true
@ -92,22 +95,31 @@ func itob(i int) bool {
return false return false
} }
func processUcAPIs() []node { //Unifi Controller API processing
func processUcAPIs() ([]node, []link) {
//get list of Unifi devices to display
var nodes []node var nodes []node
var links []link
d, err := getDevices("ucDevices.json") d, err := getDevices("ucDevices.json")
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
//call Unifi Controller
ucAPI := newAPI(*ucUser, *ucPass, ucBaseURL) ucAPI := newAPI(*ucUser, *ucPass, ucBaseURL)
//login
ucAPI.ucLogin() ucAPI.ucLogin()
//get all Sites from Controller
sites, err := ucAPI.ucGetSites() sites, err := ucAPI.ucGetSites()
if err != nil { if err != nil {
panic(err) panic(err)
} }
//get all devices in all sites
devices, err := ucAPI.ucGetDevices(sites) devices, err := ucAPI.ucGetDevices(sites)
if err != nil { if err != nil {
panic(err) panic(err)
} }
//build nodes struct
for _, jsonDevice := range d.Devices { for _, jsonDevice := range d.Devices {
var currentDevice ucDevice var currentDevice ucDevice
var currentJSONDevice device var currentJSONDevice device
@ -117,6 +129,10 @@ func processUcAPIs() []node {
currentJSONDevice = jsonDevice currentJSONDevice = jsonDevice
} }
} }
if isRemoteMACpublished(jsonDevice.MAC, d.Devices) == true {
links = ucAddLink(jsonDevice, links)
}
load, err := strconv.ParseFloat(currentDevice.Sysstats.CPU, 64) load, err := strconv.ParseFloat(currentDevice.Sysstats.CPU, 64)
if err != nil { if err != nil {
panic(err) panic(err)
@ -160,9 +176,10 @@ func processUcAPIs() []node {
Model: lookupModels(currentDevice.Model), Model: lookupModels(currentDevice.Model),
}) })
} }
return nodes return nodes, links
} }
//UNMS API processing (Richtfunk)
func processUNMSAPI() ([]node, []link) { func processUNMSAPI() ([]node, []link) {
// Variables for runtime // Variables for runtime
var links []link var links []link
@ -259,10 +276,12 @@ func processAPIs() {
var nodes []node var nodes []node
var links []link var links []link
unmsNodes, links := processUNMSAPI() unmsNodes, unmsLinks := processUNMSAPI()
ucNodes := processUcAPIs() ucNodes, ucLinks := processUcAPIs()
nodes = append(nodes, unmsNodes...) nodes = append(nodes, unmsNodes...)
nodes = append(nodes, ucNodes...) nodes = append(nodes, ucNodes...)
links = append(links, unmsLinks...)
links = append(links, ucLinks...)
// assemble final struct // assemble final struct
o := output{ o := output{
@ -291,6 +310,7 @@ func getFile(url string) []byte {
byteValue, _ := ioutil.ReadAll(data) byteValue, _ := ioutil.ReadAll(data)
return byteValue return byteValue
} }
func getDevices(file string) (devices, error) { func getDevices(file string) (devices, error) {
// get devices from JSON file // get devices from JSON file
jsonFile := getFile("https://git.freifunk-rhein-sieg.net/Freifunk-Troisdorf/ubnt-freifunk-map-api/raw/branch/master/" + file) jsonFile := getFile("https://git.freifunk-rhein-sieg.net/Freifunk-Troisdorf/ubnt-freifunk-map-api/raw/branch/master/" + file)
@ -462,3 +482,26 @@ func (u *ucAPIData) ucGetDevices(sites []ucSite) ([]ucDevice, error) {
} }
return s, nil return s, nil
} }
func ucAddLink(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: 100,
TargetTQ: 100,
SourceAddr: dev.MAC,
TargetAddr: dev.LinkedTo,
})
return links
}

View File

@ -14,6 +14,7 @@ type device struct {
Name string `json:"name"` Name string `json:"name"`
MAC string `json:"mac"` MAC string `json:"mac"`
GatewayNexthop string `json:"gateway_nexthop"` GatewayNexthop string `json:"gateway_nexthop"`
LinkedTo string `json:"linked_to"`
Gateway string `json:"gateway"` Gateway string `json:"gateway"`
Domain string `json:"domain"` Domain string `json:"domain"`
Location struct { Location struct {