Compare commits
No commits in common. "e4a372e29abd690f89e20adbee56633dd9492f1e" and "46bdb4aa6520950e5329faeabb2cbda72fbfbbf6" have entirely different histories.
e4a372e29a
...
46bdb4aa65
1
.gitignore
vendored
1
.gitignore
vendored
@ -14,4 +14,3 @@
|
|||||||
output/*
|
output/*
|
||||||
|
|
||||||
ubnt-freifunk-map-api
|
ubnt-freifunk-map-api
|
||||||
config.json
|
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"unms": {
|
|
||||||
"enabled": true,
|
|
||||||
"unmsAPIUrl": "https://unifi.freifunk-troisdorf.de/v2.1",
|
|
||||||
"APItoken": "API TOKEN",
|
|
||||||
"devicesURL": "https://git.freifunk-rhein-sieg.net/Freifunk-Troisdorf/ubnt-freifunk-map-api/raw/branch/master/"
|
|
||||||
},
|
|
||||||
"unifi": {
|
|
||||||
"enabled": true,
|
|
||||||
"APIUrl": "https://unifi.freifunk-troisdorf.de:8443",
|
|
||||||
"user": "APIuser",
|
|
||||||
"password": "PASSWORD",
|
|
||||||
"ucDevicesURL": "https://git.freifunk-rhein-sieg.net/Freifunk-Troisdorf/ubnt-freifunk-map-api/raw/branch/master/"
|
|
||||||
}
|
|
||||||
}
|
|
120
main.go
120
main.go
@ -10,7 +10,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/cookiejar"
|
"net/http/cookiejar"
|
||||||
"os"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -18,15 +17,20 @@ import (
|
|||||||
_ "git.nils.zone/nils/prettify"
|
_ "git.nils.zone/nils/prettify"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// types
|
||||||
|
|
||||||
const (
|
const (
|
||||||
iso8601 = "2006-01-02T15:04:05-0700"
|
baseURL = "https://unifi.freifunk-troisdorf.de/v2.1"
|
||||||
|
ucBaseURL = "https://unifi.freifunk-troisdorf.de:8443"
|
||||||
|
iso8601 = "2006-01-02T15:04:05-0700"
|
||||||
)
|
)
|
||||||
|
|
||||||
// flags
|
// flags
|
||||||
var configPath = flag.String("configPath", "config.json", "Path to config.json")
|
var token = flag.String("token", "", "Defines the x-auth-token")
|
||||||
|
var ucUser = flag.String("ucUser", "", "Defines the Unifi API User")
|
||||||
|
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 = 60 * time.Second
|
||||||
var conf = loadconfig(*configPath)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.Printf("starting version %s...\n", version)
|
log.Printf("starting version %s...\n", version)
|
||||||
@ -34,9 +38,16 @@ func main() {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// check if flags are set
|
// check if flags are set
|
||||||
if *configPath == "" {
|
if *token == "" {
|
||||||
log.Fatalln("Please specify path to config.json flag '-configPath'")
|
log.Fatalln("Please specify an API token via the flag '-token'")
|
||||||
}
|
}
|
||||||
|
if *ucPass == "" {
|
||||||
|
log.Fatalln("Please specify an API Password via the flag '-ucPass'")
|
||||||
|
}
|
||||||
|
if *ucUser == "" {
|
||||||
|
log.Fatalln("Please specify an API User via the flag '-ucUser'")
|
||||||
|
}
|
||||||
|
|
||||||
// start API processing (runs in a loop)
|
// start API processing (runs in a loop)
|
||||||
go processAPIs()
|
go processAPIs()
|
||||||
|
|
||||||
@ -44,15 +55,36 @@ func main() {
|
|||||||
serveJSON()
|
serveJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadconfig(file string) config {
|
//switch Unifi AP Mod IDs to Names
|
||||||
var config config
|
func lookupModels(model string) string {
|
||||||
configFile, err := os.Open(file)
|
switch model {
|
||||||
if err != nil {
|
case "BZ2", "U2S48", "U2Sv2":
|
||||||
log.Fatalln(err)
|
return "Unifi AP"
|
||||||
|
case "BZ2LR", "U2L48", "U2Lv2":
|
||||||
|
return "UniFi AP-LR"
|
||||||
|
case "U7E", "U7Ev2":
|
||||||
|
return "UniFi AP-AC"
|
||||||
|
case "U7HD", "U7SHD":
|
||||||
|
return "UniFi AP-HD"
|
||||||
|
case "UXSDM":
|
||||||
|
return "UniFi AP-BaseStationXG"
|
||||||
|
case "UCMSH":
|
||||||
|
return "AP-MeshXG"
|
||||||
|
case "U7MP":
|
||||||
|
return "AP-AC-Mesh-Pro"
|
||||||
|
case "U7LR":
|
||||||
|
return "UniFi AP-AC-LR"
|
||||||
|
case "U7LT":
|
||||||
|
return "UniFi AP-AC-Lite"
|
||||||
|
case "U7P":
|
||||||
|
return "UniFi AP-Pro"
|
||||||
|
case "U7MSH":
|
||||||
|
return "UniFi AP-AC-Mesh"
|
||||||
|
case "U7PG2":
|
||||||
|
return "UniFi AP-AC-Pro"
|
||||||
|
default:
|
||||||
|
return "Unifi Gerät"
|
||||||
}
|
}
|
||||||
jsonParse := json.NewDecoder(configFile)
|
|
||||||
jsonParse.Decode(&config)
|
|
||||||
return config
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//int to bool converter
|
//int to bool converter
|
||||||
@ -68,23 +100,23 @@ func processUcAPIs() ([]node, []link) {
|
|||||||
//get list of Unifi devices to display
|
//get list of Unifi devices to display
|
||||||
var nodes []node
|
var nodes []node
|
||||||
var links []link
|
var links []link
|
||||||
d, err := getDevices(conf.Unifi.UCDevicesURL)
|
d, err := getDevices("ucDevices.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
//call Unifi Controller
|
//call Unifi Controller
|
||||||
ucAPI := newAPI(conf.Unifi.User, conf.Unifi.Password, conf.Unifi.APIURL)
|
ucAPI := newAPI(*ucUser, *ucPass, ucBaseURL)
|
||||||
//login
|
//login
|
||||||
ucAPI.ucLogin()
|
ucAPI.ucLogin()
|
||||||
//get all Sites from Controller
|
//get all Sites from Controller
|
||||||
sites, err := ucAPI.ucGetSites()
|
sites, err := ucAPI.ucGetSites()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
//get all devices in all sites
|
//get all devices in all sites
|
||||||
devices, err := ucAPI.ucGetDevices(sites)
|
devices, err := ucAPI.ucGetDevices(sites)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
//build nodes struct
|
//build nodes struct
|
||||||
@ -103,11 +135,11 @@ func processUcAPIs() ([]node, []link) {
|
|||||||
|
|
||||||
load, err := strconv.ParseFloat(currentDevice.Sysstats.CPU, 64)
|
load, err := strconv.ParseFloat(currentDevice.Sysstats.CPU, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
mem, err := strconv.ParseFloat(currentDevice.Sysstats.Memory, 64)
|
mem, err := strconv.ParseFloat(currentDevice.Sysstats.Memory, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
nodes = append(nodes, node{
|
nodes = append(nodes, node{
|
||||||
@ -153,7 +185,7 @@ func processUNMSAPI() ([]node, []link) {
|
|||||||
var links []link
|
var links []link
|
||||||
var nodes []node
|
var nodes []node
|
||||||
|
|
||||||
d, err := getDevices(conf.Unms.DevicesURL)
|
d, err := getDevices("devices.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
@ -244,16 +276,12 @@ func processAPIs() {
|
|||||||
var nodes []node
|
var nodes []node
|
||||||
var links []link
|
var links []link
|
||||||
|
|
||||||
if conf.Unms.Enabled == true {
|
unmsNodes, unmsLinks := processUNMSAPI()
|
||||||
unmsNodes, unmsLinks := processUNMSAPI()
|
ucNodes, ucLinks := processUcAPIs()
|
||||||
nodes = append(nodes, unmsNodes...)
|
nodes = append(nodes, unmsNodes...)
|
||||||
links = append(links, unmsLinks...)
|
nodes = append(nodes, ucNodes...)
|
||||||
}
|
links = append(links, unmsLinks...)
|
||||||
if conf.Unifi.Enabled == true {
|
links = append(links, ucLinks...)
|
||||||
ucNodes, ucLinks := processUcAPIs()
|
|
||||||
nodes = append(nodes, ucNodes...)
|
|
||||||
links = append(links, ucLinks...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// assemble final struct
|
// assemble final struct
|
||||||
o := output{
|
o := output{
|
||||||
@ -283,41 +311,43 @@ func getFile(url string) []byte {
|
|||||||
return byteValue
|
return byteValue
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDevices(url string) (devices, error) {
|
func getDevices(file string) (devices, error) {
|
||||||
// get devices from JSON file
|
// get devices from JSON file
|
||||||
jsonFile := getFile(url)
|
jsonFile := getFile("https://git.freifunk-rhein-sieg.net/Freifunk-Troisdorf/ubnt-freifunk-map-api/raw/branch/master/" + file)
|
||||||
|
|
||||||
// read file to bytes
|
// read file to bytes
|
||||||
// variable for d
|
// variable for d
|
||||||
var d devices
|
var d devices
|
||||||
// unmarshal to struct
|
// unmarshal to struct
|
||||||
err := json.Unmarshal(jsonFile, &d)
|
json.Unmarshal(jsonFile, &d)
|
||||||
if err != nil {
|
|
||||||
fmt.Println("can´t get devices file from " + url)
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
return d, nil
|
return d, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func callUnifiAPI(url string, i interface{}) error {
|
func callUnifiAPI(url string, i interface{}) error {
|
||||||
request, err := http.NewRequest(http.MethodGet, conf.Unms.UnmsAPIURL+url, nil)
|
request, err := http.NewRequest(http.MethodGet, baseURL+url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New(fmt.Sprint("can't set request", conf.Unms.UnmsAPIURL+url))
|
return errors.New(fmt.Sprint("can't set request", baseURL+url))
|
||||||
}
|
}
|
||||||
request.Header.Set("x-auth-token", conf.Unms.APItoken)
|
request.Header.Set("x-auth-token", *token)
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
response, err := client.Do(request)
|
response, err := client.Do(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("can't get request %s with x-auth-token %s", conf.Unms.UnmsAPIURL+url, conf.Unms.APItoken)
|
return fmt.Errorf("can't get request %s with x-auth-token %s", baseURL+url, *token)
|
||||||
}
|
|
||||||
if response.StatusCode != 200 {
|
|
||||||
log.Fatalln("Can´t call UNMS API, check token and URL. HTTP Status: ", response.StatusCode)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := ioutil.ReadAll(response.Body)
|
data, err := ioutil.ReadAll(response.Body)
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("can't read response body: %+v", response.Body)
|
return fmt.Errorf("can't read response body: %+v", response.Body)
|
||||||
}
|
}
|
||||||
|
// try to fetch errors
|
||||||
|
var a apiResponse
|
||||||
|
json.Unmarshal(data, &a)
|
||||||
|
|
||||||
|
if a.StatusCode != 0 {
|
||||||
|
return fmt.Errorf("got following errorcode from API: %d %s %s", a.StatusCode, a.Error, a.Message)
|
||||||
|
}
|
||||||
|
|
||||||
// no error occurred, unmarshal to struct
|
// no error occurred, unmarshal to struct
|
||||||
json.Unmarshal(data, &i)
|
json.Unmarshal(data, &i)
|
||||||
return nil
|
return nil
|
||||||
|
48
types.go
48
types.go
@ -10,22 +10,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type config struct {
|
|
||||||
Unms struct {
|
|
||||||
Enabled bool `json:"enabled"`
|
|
||||||
UnmsAPIURL string `json:"unmsAPIUrl"`
|
|
||||||
APItoken string `json:"APItoken"`
|
|
||||||
DevicesURL string `json:"devicesURL"`
|
|
||||||
} `json:"unms"`
|
|
||||||
Unifi struct {
|
|
||||||
Enabled bool `json:"enabled"`
|
|
||||||
APIURL string `json:"APIUrl"`
|
|
||||||
User string `json:"user"`
|
|
||||||
Password string `json:"password"`
|
|
||||||
UCDevicesURL string `json:"ucDevicesURL"`
|
|
||||||
} `json:"unifi"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type device struct {
|
type device struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
MAC string `json:"mac"`
|
MAC string `json:"mac"`
|
||||||
@ -194,35 +178,3 @@ type ucAPIData struct {
|
|||||||
baseURL string
|
baseURL string
|
||||||
client *http.Client
|
client *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
//switch Unifi AP Mod IDs to Names
|
|
||||||
func lookupModels(model string) string {
|
|
||||||
switch model {
|
|
||||||
case "BZ2", "U2S48", "U2Sv2":
|
|
||||||
return "Unifi AP"
|
|
||||||
case "BZ2LR", "U2L48", "U2Lv2":
|
|
||||||
return "UniFi AP-LR"
|
|
||||||
case "U7E", "U7Ev2":
|
|
||||||
return "UniFi AP-AC"
|
|
||||||
case "U7HD", "U7SHD":
|
|
||||||
return "UniFi AP-HD"
|
|
||||||
case "UXSDM":
|
|
||||||
return "UniFi AP-BaseStationXG"
|
|
||||||
case "UCMSH":
|
|
||||||
return "AP-MeshXG"
|
|
||||||
case "U7MP":
|
|
||||||
return "AP-AC-Mesh-Pro"
|
|
||||||
case "U7LR":
|
|
||||||
return "UniFi AP-AC-LR"
|
|
||||||
case "U7LT":
|
|
||||||
return "UniFi AP-AC-Lite"
|
|
||||||
case "U7P":
|
|
||||||
return "UniFi AP-Pro"
|
|
||||||
case "U7MSH":
|
|
||||||
return "UniFi AP-AC-Mesh"
|
|
||||||
case "U7PG2":
|
|
||||||
return "UniFi AP-AC-Pro"
|
|
||||||
default:
|
|
||||||
return "Unifi Gerät"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user