-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
252 lines (228 loc) · 6.5 KB
/
main.go
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"os"
"time"
influx "github.com/influxdata/influxdb/client/v2"
)
var (
unifi *http.Client
stats influx.Client
)
type Response struct {
Data []Client
Meta struct {
Rc string
}
}
// DpiStat is for deep packet inspection stats
type DpiStat struct {
App int64
Cat int64
RxBytes int64
RxPackets int64
TxBytes int64
TxPackets int64
}
type Client struct {
ID string `json:"_id"`
IsGuestByUAP bool `json:"_is_guest_by_uap"`
IsGuestByUGW bool `json:"_is_guest_by_ugw"`
LastSeenByUAP int64 `json:"_last_seen_by_uap"`
LastSeenByUGW int64 `json:"_last_seen_by_ugw"`
UptimeByUAP int64 `json:"_uptime_by_uap"`
UptimeByUGW int64 `json:"_uptime_by_ugw"`
ApMac string `json:"ap_mac"`
AssocTime int64 `json:"assoc_time"`
Authorized bool
Bssid string
BytesR int64 `json:"bytes-r"`
Ccq int64
Channel int64
DpiStats []DpiStat `json:"dpi_stats"`
DpiStatsLastUpdated int64 `json:"dpi_stats_last_updated"`
Essid string
FirstSeen int64 `json:"first_seen"`
FixedIP string `json:"fixed_ip"`
Hostname string
GwMac string `json:"gw_mac"`
IdleTime int64 `json:"idle_time"`
Ip string
IsGuest bool `json:"is_guest"`
IsWired bool `json:"is_wired"`
LastSeen int64 `json:"last_seen"`
LatestAssocTime int64 `json:"latest_assoc_time"`
Mac string
Name string
Network string
NetworkID string `json:"network_id"`
Noise int64
Oui string
PowersaveEnabled bool `json:"powersave_enabled"`
QosPolicyApplied bool `json:"qos_policy_applied"`
Radio string
RadioProto string `json:"radio_proto"`
RoamCount int64 `json:"roam_count"`
Rssi int64
RxBytes int64 `json:"rx_bytes"`
RxBytesR int64 `json:"rx_bytes-r"`
RxPackets int64 `json:"rx_packets"`
RxRate int64 `json:"rx_rate"`
Signal int64
SiteID string `json:"site_id"`
TxBytes int64 `json:"tx_bytes"`
TxBytesR int64 `json:"tx_bytes-r"`
TxPackets int64 `json:"tx_packets"`
TxPower int64 `json:"tx_power"`
TxRate int64 `json:"tx_rate"`
Uptime int64
UserID string `json:"user_id"`
Vlan int64
}
func (c Client) Point() *influx.Point {
tags := map[string]string{
"mac": c.Mac,
"user_id": c.UserID,
"site_id": c.SiteID,
"ip": c.Ip,
"essid": c.Essid,
"network": c.Network,
"ap_mac": c.ApMac,
"name": c.Name,
}
fields := map[string]interface{}{
"is_guest_by_uap": c.IsGuestByUAP,
"is_guest_by_ugw": c.IsGuestByUGW,
"authorized": c.Authorized,
"last_seen_by_uap": c.LastSeenByUAP,
"last_seen_by_ugw": c.LastSeenByUGW,
"uptime_by_uap": c.UptimeByUAP,
"uptime_by_ugw": c.UptimeByUGW,
"assoc_time": c.AssocTime,
"bytes_r": c.BytesR,
"ccq": c.Ccq,
"channel": c.Channel,
"dpi_stats_last_updated": c.DpiStatsLastUpdated,
"first_seen": c.FirstSeen,
"idle_time": c.IdleTime,
"last_seen": c.LastSeen,
"latest_assoc_time": c.LatestAssocTime,
"noise": c.Noise,
"roam_count": c.RoamCount,
"rssi": c.Rssi,
"rx_bytes": c.RxBytes,
"rx_bytes_r": c.RxBytesR,
"rx_packets": c.RxPackets,
"rx_rate": c.RxRate,
"signal": c.Signal,
"tx_bytes": c.TxBytes,
"tx_bytes_r": c.TxBytesR,
"tx_packets": c.TxPackets,
"tx_power": c.TxPower,
"tx_rate": c.TxRate,
"uptime": c.Uptime,
"vlan": c.Vlan,
}
pt, err := influx.NewPoint("client_state", tags, fields, time.Now())
if err != nil {
return nil
}
return pt
}
func main() {
var err error
tickRate := os.Getenv("TICK_RATE")
interval, err := time.ParseDuration(tickRate)
if err != nil {
panic(err)
}
unifi, err = login()
if err != nil {
panic(err)
}
database := os.Getenv("INFLUXDB_DATABASE")
stats, err = influx.NewHTTPClient(influx.HTTPConfig{
Addr: os.Getenv("INFLUXDB_ADDR"),
Username: os.Getenv("INFLUXDB_USERNAME"),
Password: os.Getenv("INFLUXDB_PASSWORD"),
})
if err != nil {
panic(err)
}
log.Printf("Starting to poll Unifi every %+v\n", interval)
for {
devices, err := fetch()
if err != nil {
log.Println(err)
} else {
bp, _ := influx.NewBatchPoints(influx.BatchPointsConfig{
Database: database,
})
for _, device := range devices {
bp.AddPoint(device.Point())
}
err = stats.Write(bp)
if err != nil {
log.Println(err)
}
log.Println("Logged client state...")
}
time.Sleep(interval)
}
}
func fetch() ([]Client, error) {
format := "https://%s:%s/api/s/default/stat/sta"
url := fmt.Sprintf(format, os.Getenv("UNIFI_ADDR"), os.Getenv("UNIFI_PORT"))
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("Accept", "application/json")
resp, err := unifi.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
response := &Response{}
err = json.Unmarshal(body, response)
if err != nil {
return nil, err
}
return response.Data, nil
}
func login() (*http.Client, error) {
url := fmt.Sprintf("https://%s:%s/api/login", os.Getenv("UNIFI_ADDR"), os.Getenv("UNIFI_PORT"))
auth := map[string]string{
"username": os.Getenv("UNIFI_USERNAME"),
"password": os.Getenv("UNIFI_PASSWORD"),
}
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
jar, _ := cookiejar.New(nil)
client := &http.Client{
Transport: transport,
Jar: jar,
}
json, _ := json.Marshal(auth)
params := bytes.NewReader(json)
req, err := http.NewRequest("POST", url, params)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New("Not a successful request")
}
return client, nil
}