-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathtraffic.go
More file actions
201 lines (162 loc) · 5.52 KB
/
Copy pathtraffic.go
File metadata and controls
201 lines (162 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package unifi
import (
"fmt"
)
func (u *Unifi) GetClientTraffic(sites []*Site, epochMillisTimePeriod *EpochMillisTimePeriod, includeUnidentified bool) ([]*ClientUsageByApp, error) {
_, err := epochMillisTimePeriod.isValid()
if err != nil {
return nil, err
}
data := make([]*ClientUsageByApp, 0)
for _, site := range sites {
var response struct {
ClientUsageByApp []*ClientUsageByApp `json:"client_usage_by_app"`
}
trafficSite := &TrafficSite{
SiteID: site.ID,
SiteName: site.SiteName,
SourceName: site.SourceName,
}
u.DebugLog("Polling Controller, retrieving UniFi Client Traffic, site %s ", site.SiteName)
clientPath := fmt.Sprintf(APIClientTrafficPath,
site.Name,
epochMillisTimePeriod.StartEpochMillis,
epochMillisTimePeriod.EndEpochMillis,
includeUnidentified)
if err := u.GetData(clientPath, &response); err != nil {
return nil, err
}
for _, elem := range response.ClientUsageByApp {
elem.TrafficSite = trafficSite
}
data = append(data, response.ClientUsageByApp...)
}
return data, nil
}
func (u *Unifi) GetClientTrafficByMac(site *Site, epochMillisTimePeriod *EpochMillisTimePeriod, includeUnidentified bool, macs ...string) ([]*ClientUsageByApp, error) {
_, err := epochMillisTimePeriod.isValid()
if err != nil {
return nil, err
}
data := make([]*ClientUsageByApp, 0)
var response struct {
ClientUsageByApp []*ClientUsageByApp `json:"client_usage_by_app"`
}
trafficSite := &TrafficSite{
SiteID: site.ID,
SiteName: site.SiteName,
SourceName: site.SourceName,
}
for _, mac := range macs {
u.DebugLog("Polling Controller, retrieving UniFi Client Traffic By MAC address, site %s and mac %s", site.SiteName, mac)
clientPath := fmt.Sprintf(APIClientTrafficByMacPath,
site.Name,
mac,
epochMillisTimePeriod.StartEpochMillis,
epochMillisTimePeriod.EndEpochMillis,
includeUnidentified,
mac)
if err := u.GetData(clientPath, &response); err != nil {
return nil, err
}
for _, elem := range response.ClientUsageByApp {
elem.TrafficSite = trafficSite
}
data = append(data, response.ClientUsageByApp...)
}
return data, nil
}
func (u *Unifi) GetCountryTraffic(sites []*Site, epochMillisTimePeriod *EpochMillisTimePeriod) ([]*UsageByCountry, error) {
_, err := epochMillisTimePeriod.isValid()
if err != nil {
return nil, err
}
data := make([]*UsageByCountry, 0)
for _, site := range sites {
var response struct {
UsageByCountry []*UsageByCountry `json:"usage_by_country"`
}
trafficSite := &TrafficSite{
SiteID: site.ID,
SiteName: site.SiteName,
SourceName: site.SourceName,
}
u.DebugLog("Polling Controller, retrieving UniFi Country Traffic, site %s ", site.SiteName)
clientPath := fmt.Sprintf(APICountryTrafficPath,
site.Name,
epochMillisTimePeriod.StartEpochMillis,
epochMillisTimePeriod.EndEpochMillis)
if err := u.GetData(clientPath, &response); err != nil {
return nil, err
}
for _, elem := range response.UsageByCountry {
elem.TrafficSite = trafficSite
}
data = append(data, response.UsageByCountry...)
}
return data, nil
}
type ClientUsageByApp struct {
TrafficSite *TrafficSite `json:"site"`
Client ClientInfo `json:"client"`
UsageByApp []UsageByApp `json:"usage_by_app"`
}
// ClientInfo contains information about the network client
type ClientInfo struct {
Fingerprint Fingerprint `json:"fingerprint"`
Hostname string `fake:"{domainname}" json:"hostname"`
IsWired bool `json:"is_wired"`
Mac string `fake:"{macaddress}" json:"mac"`
Name string `json:"name"`
Oui string `json:"oui"`
WlanconfID string `json:"wlanconf_id,omitempty"`
}
// Fingerprint contains device fingerprinting information
type Fingerprint struct {
ComputedDevID int `json:"computed_dev_id,omitempty"`
ComputedEngine int `json:"computed_engine,omitempty"`
Confidence int `json:"confidence,omitempty"`
DevCat int `json:"dev_cat,omitempty"`
DevFamily int `json:"dev_family,omitempty"`
DevID int `json:"dev_id,omitempty"`
DevIDOverride int `json:"dev_id_override,omitempty"`
DevVendor int `json:"dev_vendor,omitempty"`
HasOverride bool `json:"has_override"`
OsClass int `json:"os_class,omitempty"`
OsName int `json:"os_name,omitempty"`
}
// UsageByApp contains application usage statistics
type UsageByApp struct {
ActivitySeconds int `json:"activity_seconds,omitempty"`
Application int `json:"application"`
BytesReceived int64 `json:"bytes_received"`
BytesTransmitted int64 `json:"bytes_transmitted"`
Category int `json:"category"`
TotalBytes int64 `json:"total_bytes"`
}
type UsageByCountry struct {
TrafficSite *TrafficSite `json:"site"`
BytesReceived int64 `json:"bytes_received"`
BytesTransmitted int64 `json:"bytes_transmitted"`
Country string `fake:"{countryabr}" json:"country"`
TotalBytes int64 `json:"total_bytes"`
}
type TrafficSite struct {
SiteID string `fake:"{uuid}" json:"site_id"`
SiteName string `json:"name"`
SourceName string `json:"source"`
}
// Parameters
type EpochMillisTimePeriod struct {
StartEpochMillis int64
EndEpochMillis int64
}
func (p *EpochMillisTimePeriod) isValid() (bool, error) {
if p.StartEpochMillis < p.EndEpochMillis {
return true, nil
}
return false, fmt.Errorf("start must be before end (%s)", p.String())
}
func (p *EpochMillisTimePeriod) String() string {
return fmt.Sprintf("%d to %d", p.StartEpochMillis, p.EndEpochMillis)
}