diff --git a/core/unifi/.travis.yml b/core/unifi/.travis.yml new file mode 100644 index 00000000..b0d7f087 --- /dev/null +++ b/core/unifi/.travis.yml @@ -0,0 +1,14 @@ +language: go +go: +- 1.12.x +before_install: +- mkdir -p $GOPATH/bin + # Download the `dep` binary to bin folder in $GOPATH +- curl -sLo $GOPATH/bin/dep https://github.com/golang/dep/releases/download/v0.5.3/dep-linux-amd64 +- chmod +x $GOPATH/bin/dep + # download super-linter: golangci-lint +- curl -sL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin latest +install: +- dep ensure +script: +- golangci-lint run --enable-all -e G402 diff --git a/core/unifi/Gopkg.lock b/core/unifi/Gopkg.lock index 92114921..0ee3eca3 100644 --- a/core/unifi/Gopkg.lock +++ b/core/unifi/Gopkg.lock @@ -19,7 +19,7 @@ "v2", ] pruneopts = "UT" - revision = "16c852ea613fa2d42fcdccc9a8b0802a8bdc6140" + revision = "8ff2fc3824fcb533795f9a2f233275f0bb18d6c5" [[projects]] digest = "1:cf31692c14422fa27c83a05292eb5cbe0fb2775972e8f1f8446a71549bd8980b" diff --git a/core/unifi/clients_influx.go b/core/unifi/clients_influx.go index 402bb428..9219c785 100644 --- a/core/unifi/clients_influx.go +++ b/core/unifi/clients_influx.go @@ -25,6 +25,7 @@ func (c UCL) Points() ([]*influx.Point, error) { "mac": c.Mac, "user_id": c.UserID, "site_id": c.SiteID, + "site_name": c.SiteName, "network_id": c.NetworkID, "usergroup_id": c.UserGroupID, "ap_mac": c.ApMac, diff --git a/core/unifi/clients_type.go b/core/unifi/clients_type.go index 2ad29ccf..d0a21c04 100644 --- a/core/unifi/clients_type.go +++ b/core/unifi/clients_type.go @@ -66,6 +66,7 @@ type UCL struct { RxRate int64 `json:"rx_rate"` Signal int64 `json:"signal"` SiteID string `json:"site_id"` + SiteName string `json:"-"` SwDepth int `json:"sw_depth"` SwMac string `json:"sw_mac"` SwPort int `json:"sw_port"` diff --git a/core/unifi/parsers.go b/core/unifi/parsers.go index 70c2d193..672187ea 100644 --- a/core/unifi/parsers.go +++ b/core/unifi/parsers.go @@ -3,7 +3,7 @@ package unifi import "encoding/json" // parseDevices parses the raw JSON from the Unifi Controller into device structures. -func (u *Unifi) parseDevices(data []json.RawMessage) *Devices { +func (u *Unifi) parseDevices(data []json.RawMessage, siteName string) *Devices { devices := new(Devices) for _, r := range data { // Loop each item in the raw JSON message, detect its type and unmarshal it. @@ -18,14 +18,17 @@ func (u *Unifi) parseDevices(data []json.RawMessage) *Devices { switch assetType { // Unmarshal again into the correct type.. case "uap": if uap := (UAP{}); u.unmarshalDevice(assetType, r, &uap) == nil { + uap.SiteName = siteName devices.UAPs = append(devices.UAPs, uap) } case "ugw", "usg": // in case they ever fix the name in the api. if usg := (USG{}); u.unmarshalDevice(assetType, r, &usg) == nil { + usg.SiteName = siteName devices.USGs = append(devices.USGs, usg) } case "usw": if usw := (USW{}); u.unmarshalDevice(assetType, r, &usw) == nil { + usw.SiteName = siteName devices.USWs = append(devices.USWs, usw) } default: diff --git a/core/unifi/uap_influx.go b/core/unifi/uap_influx.go index 3b5072f2..e0945ed1 100644 --- a/core/unifi/uap_influx.go +++ b/core/unifi/uap_influx.go @@ -20,6 +20,7 @@ func (u UAP) Points() ([]*influx.Point, error) { "device_oid": u.Stat.Oid, "device_ap": u.Stat.Ap, "site_id": u.SiteID, + "site_name": u.SiteName, "name": u.Name, "adopted": u.Adopted.Txt, "bandsteering_mode": u.BandsteeringMode, @@ -231,6 +232,8 @@ func (u UAP) Points() ([]*influx.Point, error) { tags["vap_id"] = s.ID tags["vap_name"] = s.Name tags["wlanconf_id"] = s.WlanconfID + tags["site_id"] = s.SiteID + tags["site_name"] = s.SiteName fields["ccq"] = s.Ccq fields["essid"] = s.Essid fields["extchannel"] = s.Extchannel diff --git a/core/unifi/uap_type.go b/core/unifi/uap_type.go index 5ceae3f4..3b804501 100644 --- a/core/unifi/uap_type.go +++ b/core/unifi/uap_type.go @@ -163,6 +163,7 @@ type UAP struct { Scanning FlexBool `json:"scanning"` Serial string `json:"serial"` SiteID string `json:"site_id"` + SiteName string `json:"-"` SpectrumScanning FlexBool `json:"spectrum_scanning"` SSHSessionTable []interface{} `json:"ssh_session_table"` Stat struct { @@ -332,6 +333,7 @@ type UAP struct { RxNwids float64 `json:"rx_nwids"` RxPackets float64 `json:"rx_packets"` SiteID string `json:"site_id"` + SiteName string `json:"-"` State string `json:"state"` T string `json:"t"` TxBytes float64 `json:"tx_bytes"` diff --git a/core/unifi/unifi.go b/core/unifi/unifi.go index 0422a529..4c3754fa 100644 --- a/core/unifi/unifi.go +++ b/core/unifi/unifi.go @@ -60,7 +60,7 @@ func (u *Unifi) getController(user, pass string) error { // GetClients returns a response full of clients' data from the Unifi Controller. func (u *Unifi) GetClients(sites []Site) (*Clients, error) { - var data []UCL + data := make([]UCL, 0) for _, site := range sites { var response struct { Data []UCL `json:"data"` @@ -71,6 +71,9 @@ func (u *Unifi) GetClients(sites []Site) (*Clients, error) { if err := u.GetData(clientPath, &response); err != nil { return nil, err } + for i := range response.Data { + response.Data[i].SiteName = site.Name + } data = append(data, response.Data...) } return &Clients{UCLs: data}, nil @@ -78,7 +81,7 @@ func (u *Unifi) GetClients(sites []Site) (*Clients, error) { // GetDevices returns a response full of devices' data from the Unifi Controller. func (u *Unifi) GetDevices(sites []Site) (*Devices, error) { - var data []json.RawMessage + devices := new(Devices) for _, site := range sites { u.dLogf("Polling Site '%s' (%s) Devices", site.Name, site.Desc) var response struct { @@ -88,9 +91,28 @@ func (u *Unifi) GetDevices(sites []Site) (*Devices, error) { if err := u.GetData(devicePath, &response); err != nil { return nil, err } - data = append(data, response.Data...) + loopDevices := u.parseDevices(response.Data, site.Name) + // Add SiteName to each device asset. + for i := range loopDevices.UAPs { + loopDevices.UAPs[i].SiteName = site.Name + for j := range loopDevices.UAPs[i].VapTable { + loopDevices.UAPs[i].VapTable[j].SiteName = site.Name + } + } + for i := range loopDevices.USGs { + loopDevices.USGs[i].SiteName = site.Name + for j := range loopDevices.USGs[i].NetworkTable { + loopDevices.USGs[i].NetworkTable[j].SiteName = site.Name + } + } + for i := range loopDevices.USWs { + loopDevices.USWs[i].SiteName = site.Name + } + devices.UAPs = append(devices.UAPs, loopDevices.UAPs...) + devices.USGs = append(devices.USGs, loopDevices.USGs...) + devices.USWs = append(devices.USWs, loopDevices.USWs...) } - return u.parseDevices(data), nil + return devices, nil } // GetSites returns a list of configured sites on the Unifi controller. @@ -101,7 +123,7 @@ func (u *Unifi) GetSites() ([]Site, error) { if err := u.GetData(SiteList, &response); err != nil { return nil, err } - var sites []string + sites := make([]string, 0) for i := range response.Data { sites = append(sites, response.Data[i].Name) } diff --git a/core/unifi/unifi_test.go b/core/unifi/unifi_test.go index dff7687d..7ab1df31 100644 --- a/core/unifi/unifi_test.go +++ b/core/unifi/unifi_test.go @@ -17,7 +17,8 @@ func TestNewUnifi(t *testing.T) { a.EqualValues(url, authReq.baseURL) a.Contains(err.Error(), "authReq.Do(req):", "an invalid destination should product a .Do(req) error.") /* TODO: OPEN web server, check parameters posted, more. This test is incomplete. - a.EqualValues(`{"username": "user1","password": "pass2"}`, string(post_params), "user/pass json parameters improperly encoded") + a.EqualValues(`{"username": "user1","password": "pass2"}`, string(post_params), + "user/pass json parameters improperly encoded") */ } diff --git a/core/unifi/usg_influx.go b/core/unifi/usg_influx.go index c6225788..bfd8ecc9 100644 --- a/core/unifi/usg_influx.go +++ b/core/unifi/usg_influx.go @@ -16,6 +16,7 @@ func (u USG) Points() ([]*influx.Point, error) { "device_type": u.Stat.O, "device_oid": u.Stat.Oid, "site_id": u.SiteID, + "site_name": u.SiteName, "adopted": u.Adopted.Txt, "name": u.Name, "adopt_ip": u.AdoptIP, @@ -153,6 +154,7 @@ func (u USG) Points() ([]*influx.Point, error) { "is_nat": p.IsNat.Txt, "networkgroup": p.Networkgroup, "site_id": p.SiteID, + "site_name": p.SiteName, } fields := map[string]interface{}{ "dhcpd_ip_1": p.DhcpdIP1, diff --git a/core/unifi/usg_type.go b/core/unifi/usg_type.go index 014fdad1..2f95a552 100644 --- a/core/unifi/usg_type.go +++ b/core/unifi/usg_type.go @@ -68,6 +68,7 @@ type USG struct { RxBytes FlexInt `json:"rx_bytes"` RxPackets float64 `json:"rx_packets"` SiteID string `json:"site_id"` + SiteName string `json:"-"` TxBytes FlexInt `json:"tx_bytes"` TxPackets float64 `json:"tx_packets"` Up FlexBool `json:"up"` @@ -116,6 +117,7 @@ type USG struct { RxBytes FlexInt `json:"rx_bytes"` Serial string `json:"serial"` SiteID string `json:"site_id"` + SiteName string `json:"-"` SpeedtestStatus struct { Latency float64 `json:"latency"` Rundate float64 `json:"rundate"` diff --git a/core/unifi/usw_influx.go b/core/unifi/usw_influx.go index 8435c53e..fa77d2aa 100644 --- a/core/unifi/usw_influx.go +++ b/core/unifi/usw_influx.go @@ -15,6 +15,7 @@ func (u USW) Points() ([]*influx.Point, error) { "device_type": u.Stat.O, "device_oid": u.Stat.Oid, "site_id": u.SiteID, + "site_name": u.SiteName, "name": u.Name, "adopted": u.Adopted.Txt, "adopt_ip": u.AdoptIP, diff --git a/core/unifi/usw_type.go b/core/unifi/usw_type.go index ef0081ff..c234eba7 100644 --- a/core/unifi/usw_type.go +++ b/core/unifi/usw_type.go @@ -117,6 +117,7 @@ type USW struct { RxBytes float64 `json:"rx_bytes"` Serial string `json:"serial"` SiteID string `json:"site_id"` + SiteName string `json:"-"` SSHSessionTable []interface{} `json:"ssh_session_table"` Stat struct {