mirror of
https://github.com/unpoller/unpoller.git
synced 2026-10-18 22:40:03 +02:00
Add DPI gathering methods
This commit is contained in:
@@ -11,5 +11,5 @@ before_install:
|
||||
install:
|
||||
- dep ensure
|
||||
script:
|
||||
- golangci-lint run --enable-all -e G402
|
||||
- golangci-lint run --enable-all -e G402 -D gochecknoglobals
|
||||
- go test ./...
|
||||
|
||||
+29
-1
@@ -1,6 +1,8 @@
|
||||
package unifi
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// GetClients returns a response full of clients' data from the UniFi Controller.
|
||||
func (u *Unifi) GetClients(sites Sites) (Clients, error) {
|
||||
@@ -34,6 +36,32 @@ func (u *Unifi) GetClients(sites Sites) (Clients, error) {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// GetClientsDPI garners dpi data for clients.
|
||||
func (u *Unifi) GetClientsDPI(sites Sites) ([]*DPITable, error) {
|
||||
var data []*DPITable
|
||||
|
||||
for _, site := range sites {
|
||||
u.DebugLog("Polling Controller, retreiving Client DPI data, site %s (%s) ", site.Name, site.Desc)
|
||||
|
||||
var response struct {
|
||||
Data []*DPITable `json:"data"`
|
||||
}
|
||||
|
||||
clientDPIpath := fmt.Sprintf(APIClientDPI, site.Name)
|
||||
if err := u.GetData(clientDPIpath, &response, `{"type":"by_app"}`); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, d := range response.Data {
|
||||
d.SourceName = site.SourceName
|
||||
d.SiteName = site.SiteName
|
||||
data = append(data, d)
|
||||
}
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// Clients contains a list that contains all of the unifi clients from a controller.
|
||||
type Clients []*Client
|
||||
|
||||
|
||||
+2366
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,11 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
|
||||
+34
-1
@@ -1,6 +1,9 @@
|
||||
package unifi
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GetSites returns a list of configured sites on the UniFi controller.
|
||||
func (u *Unifi) GetSites() (Sites, error) {
|
||||
@@ -29,6 +32,36 @@ func (u *Unifi) GetSites() (Sites, error) {
|
||||
return response.Data, nil
|
||||
}
|
||||
|
||||
// GetSiteDPI garners dpi data for sites.
|
||||
func (u *Unifi) GetSiteDPI(sites Sites) ([]*DPITable, error) {
|
||||
data := []*DPITable{}
|
||||
|
||||
for _, site := range sites {
|
||||
u.DebugLog("Polling Controller, retreiving Site DPI data, site %s (%s) ", site.Name, site.Desc)
|
||||
|
||||
var response struct {
|
||||
Data []*DPITable `json:"data"`
|
||||
}
|
||||
|
||||
siteDPIpath := fmt.Sprintf(APISiteDPI, site.Name)
|
||||
if err := u.GetData(siteDPIpath, &response, `{"type":"by_app"}`); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if l := len(response.Data); l > 1 {
|
||||
return nil, fmt.Errorf("dpi data table contains more than 1 item; please open a bug report")
|
||||
} else if l != 1 {
|
||||
continue
|
||||
}
|
||||
|
||||
response.Data[0].SourceName = site.SourceName
|
||||
response.Data[0].SiteName = site.SiteName
|
||||
data = append(data, response.Data[0])
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// Sites is a struct to match Devices and Clients.
|
||||
type Sites []*Site
|
||||
|
||||
|
||||
@@ -15,6 +15,10 @@ const (
|
||||
APIStatusPath string = "/status"
|
||||
// APISiteList is the path to the api site list.
|
||||
APISiteList string = "/api/stat/sites"
|
||||
// APISiteDPI is site DPI data.
|
||||
APISiteDPI string = "/api/s/%s/stat/sitedpi"
|
||||
// APISiteDPI is site DPI data.
|
||||
APIClientDPI string = "/api/s/%s/stat/stadpi"
|
||||
// APIClientPath is Unifi Clients API Path
|
||||
APIClientPath string = "/api/s/%s/stat/sta"
|
||||
// APIDevicePath is where we get data about Unifi devices.
|
||||
|
||||
+39
-69
@@ -133,75 +133,45 @@ type UDM struct {
|
||||
|
||||
// NetworkTable is the list of networks on a gateway.
|
||||
type NetworkTable []struct {
|
||||
ID string `json:"_id"`
|
||||
AttrNoDelete FlexBool `json:"attr_no_delete"`
|
||||
AttrHiddenID string `json:"attr_hidden_id"`
|
||||
Name string `json:"name"`
|
||||
SiteID string `json:"site_id"`
|
||||
VlanEnabled FlexBool `json:"vlan_enabled"`
|
||||
Purpose string `json:"purpose"`
|
||||
IPSubnet string `json:"ip_subnet"`
|
||||
Ipv6InterfaceType string `json:"ipv6_interface_type"`
|
||||
DomainName string `json:"domain_name"`
|
||||
IsNat FlexBool `json:"is_nat"`
|
||||
DhcpdEnabled FlexBool `json:"dhcpd_enabled"`
|
||||
DhcpdStart string `json:"dhcpd_start"`
|
||||
DhcpdStop string `json:"dhcpd_stop"`
|
||||
Dhcpdv6Enabled FlexBool `json:"dhcpdv6_enabled"`
|
||||
Ipv6RaEnabled FlexBool `json:"ipv6_ra_enabled"`
|
||||
LteLanEnabled FlexBool `json:"lte_lan_enabled"`
|
||||
Networkgroup string `json:"networkgroup"`
|
||||
DhcpdLeasetime FlexInt `json:"dhcpd_leasetime"`
|
||||
DhcpdDNSEnabled FlexBool `json:"dhcpd_dns_enabled"`
|
||||
DhcpdGatewayEnabled FlexBool `json:"dhcpd_gateway_enabled"`
|
||||
DhcpdTimeOffsetEnabled FlexBool `json:"dhcpd_time_offset_enabled"`
|
||||
Ipv6PdStart string `json:"ipv6_pd_start"`
|
||||
Ipv6PdStop string `json:"ipv6_pd_stop"`
|
||||
DhcpdDNS1 string `json:"dhcpd_dns_1"`
|
||||
DhcpdDNS2 string `json:"dhcpd_dns_2"`
|
||||
DhcpdDNS3 string `json:"dhcpd_dns_3"`
|
||||
DhcpdDNS4 string `json:"dhcpd_dns_4"`
|
||||
Enabled FlexBool `json:"enabled"`
|
||||
DhcpRelayEnabled FlexBool `json:"dhcp_relay_enabled"`
|
||||
Mac string `json:"mac"`
|
||||
IsGuest FlexBool `json:"is_guest"`
|
||||
IP string `json:"ip"`
|
||||
Up FlexBool `json:"up"`
|
||||
DPIStatsTable DPIStatsTable `json:"dpistats_table,omitempty"`
|
||||
NumSta FlexInt `json:"num_sta"`
|
||||
RxBytes FlexInt `json:"rx_bytes"`
|
||||
RxPackets FlexInt `json:"rx_packets"`
|
||||
TxBytes FlexInt `json:"tx_bytes"`
|
||||
TxPackets FlexInt `json:"tx_packets"`
|
||||
}
|
||||
|
||||
// DPIStatsTable is the Deep Packet Inspection data for each "network"
|
||||
type DPIStatsTable struct {
|
||||
LastUpdated FlexInt `json:"last_updated"`
|
||||
ByCat []struct {
|
||||
Cat FlexInt `json:"cat"`
|
||||
Apps []FlexInt `json:"apps"`
|
||||
RxBytes FlexInt `json:"rx_bytes"`
|
||||
TxBytes FlexInt `json:"tx_bytes"`
|
||||
RxPackets FlexInt `json:"rx_packets"`
|
||||
TxPackets FlexInt `json:"tx_packets"`
|
||||
} `json:"by_cat"`
|
||||
ByApp []struct {
|
||||
App FlexInt `json:"app"`
|
||||
Cat FlexInt `json:"cat"`
|
||||
Clients []struct {
|
||||
Mac string `json:"mac"`
|
||||
RxBytes FlexInt `json:"rx_bytes"`
|
||||
TxBytes FlexInt `json:"tx_bytes"`
|
||||
RxPackets FlexInt `json:"rx_packets"`
|
||||
TxPackets FlexInt `json:"tx_packets"`
|
||||
} `json:"clients"`
|
||||
KnownClients FlexInt `json:"known_clients"`
|
||||
RxBytes FlexInt `json:"rx_bytes"`
|
||||
TxBytes FlexInt `json:"tx_bytes"`
|
||||
RxPackets FlexInt `json:"rx_packets"`
|
||||
TxPackets FlexInt `json:"tx_packets"`
|
||||
} `json:"by_app"`
|
||||
ID string `json:"_id"`
|
||||
AttrNoDelete FlexBool `json:"attr_no_delete"`
|
||||
AttrHiddenID string `json:"attr_hidden_id"`
|
||||
Name string `json:"name"`
|
||||
SiteID string `json:"site_id"`
|
||||
VlanEnabled FlexBool `json:"vlan_enabled"`
|
||||
Purpose string `json:"purpose"`
|
||||
IPSubnet string `json:"ip_subnet"`
|
||||
Ipv6InterfaceType string `json:"ipv6_interface_type"`
|
||||
DomainName string `json:"domain_name"`
|
||||
IsNat FlexBool `json:"is_nat"`
|
||||
DhcpdEnabled FlexBool `json:"dhcpd_enabled"`
|
||||
DhcpdStart string `json:"dhcpd_start"`
|
||||
DhcpdStop string `json:"dhcpd_stop"`
|
||||
Dhcpdv6Enabled FlexBool `json:"dhcpdv6_enabled"`
|
||||
Ipv6RaEnabled FlexBool `json:"ipv6_ra_enabled"`
|
||||
LteLanEnabled FlexBool `json:"lte_lan_enabled"`
|
||||
Networkgroup string `json:"networkgroup"`
|
||||
DhcpdLeasetime FlexInt `json:"dhcpd_leasetime"`
|
||||
DhcpdDNSEnabled FlexBool `json:"dhcpd_dns_enabled"`
|
||||
DhcpdGatewayEnabled FlexBool `json:"dhcpd_gateway_enabled"`
|
||||
DhcpdTimeOffsetEnabled FlexBool `json:"dhcpd_time_offset_enabled"`
|
||||
Ipv6PdStart string `json:"ipv6_pd_start"`
|
||||
Ipv6PdStop string `json:"ipv6_pd_stop"`
|
||||
DhcpdDNS1 string `json:"dhcpd_dns_1"`
|
||||
DhcpdDNS2 string `json:"dhcpd_dns_2"`
|
||||
DhcpdDNS3 string `json:"dhcpd_dns_3"`
|
||||
DhcpdDNS4 string `json:"dhcpd_dns_4"`
|
||||
Enabled FlexBool `json:"enabled"`
|
||||
DhcpRelayEnabled FlexBool `json:"dhcp_relay_enabled"`
|
||||
Mac string `json:"mac"`
|
||||
IsGuest FlexBool `json:"is_guest"`
|
||||
IP string `json:"ip"`
|
||||
Up FlexBool `json:"up"`
|
||||
NumSta FlexInt `json:"num_sta"`
|
||||
RxBytes FlexInt `json:"rx_bytes"`
|
||||
RxPackets FlexInt `json:"rx_packets"`
|
||||
TxBytes FlexInt `json:"tx_bytes"`
|
||||
TxPackets FlexInt `json:"tx_packets"`
|
||||
}
|
||||
|
||||
// UDMStat holds the "stat" data for a dream machine.
|
||||
|
||||
+4
-4
@@ -97,10 +97,10 @@ func (u *Unifi) GetServerData() error {
|
||||
}
|
||||
|
||||
// GetData makes a unifi request and unmarshals the response into a provided pointer.
|
||||
func (u *Unifi) GetData(apiPath string, v interface{}) error {
|
||||
func (u *Unifi) GetData(apiPath string, v interface{}, params ...string) error {
|
||||
start := time.Now()
|
||||
|
||||
body, err := u.GetJSON(apiPath)
|
||||
body, err := u.GetJSON(apiPath, params...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -134,8 +134,8 @@ func (u *Unifi) UniReq(apiPath string, params string) (req *http.Request, err er
|
||||
}
|
||||
|
||||
// GetJSON returns the raw JSON from a path. This is useful for debugging.
|
||||
func (u *Unifi) GetJSON(apiPath string) ([]byte, error) {
|
||||
req, err := u.UniReq(apiPath, "")
|
||||
func (u *Unifi) GetJSON(apiPath string, params ...string) ([]byte, error) {
|
||||
req, err := u.UniReq(apiPath, strings.Join(params, " "))
|
||||
if err != nil {
|
||||
return []byte{}, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user