This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathhealth.go
183 lines (157 loc) · 4.62 KB
/
health.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
// Copyright (c) 2014 The unifi Authors. All rights reserved.
// Use of this source code is governed by ISC-style license
// that can be found in the LICENSE file.
package unifi
import (
"encoding/json"
"fmt"
)
type LAN struct {
LanIP string `json:"lan_ip"`
NumAdopted int `json:"num_adopted"`
NumDisconnected int `json:"num_disconnected"`
NumGuest int `json:"num_guest"`
NumPending int `json:"num_pending"`
NumSw int `json:"num_sw"`
NumUser int `json:"num_user"`
RxBytesR int64 `json:"rx_bytes-r"`
Status string `json:"status"`
Subsystem string `json:"subsystem"`
TxBytesR int64 `json:"tx_bytes-r"`
}
type VPN struct {
Status string `json:"status"`
Subsystem string `json:"subsystem"`
}
type WAN struct {
Gateways []string `json:"gateways"`
GwMac string `json:"gw_mac"`
GwName string `json:"gw_name"`
GwSystemStats struct {
CPU string `json:"cpu"`
Mem string `json:"mem"`
Temps struct {
BoardCPU string `json:"Board (CPU)"`
BoardPHY string `json:"Board (PHY)"`
CPU string `json:"CPU"`
PHY string `json:"PHY"`
} `json:"temps"`
Uptime string `json:"uptime"`
} `json:"gw_system-stats"`
GwVersion string `json:"gw_version"`
Nameservers []string `json:"nameservers"`
Netmask string `json:"netmask"`
NumAdopted int `json:"num_adopted"`
NumDisconnected int `json:"num_disconnected"`
NumGw int `json:"num_gw"`
NumPending int `json:"num_pending"`
NumSta int `json:"num_sta"`
RxBytesR int64 `json:"rx_bytes-r"`
Status string `json:"status"`
Subsystem string `json:"subsystem"`
TxBytesR int64 `json:"tx_bytes-r"`
WanIP string `json:"wan_ip"`
}
type WLAN struct {
NumAdopted int `json:"num_adopted"`
NumAp int `json:"num_ap"`
NumDisabled int `json:"num_disabled"`
NumDisconnected int `json:"num_disconnected"`
NumGuest int `json:"num_guest"`
NumPending int `json:"num_pending"`
NumUser int `json:"num_user"`
RxBytesR int64 `json:"rx_bytes-r"`
Status string `json:"status"`
Subsystem string `json:"subsystem"`
TxBytesR int64 `json:"tx_bytes-r"`
}
type WWW struct {
Drops int `json:"drops"`
GwMac string `json:"gw_mac"`
Latency int `json:"latency"`
RxBytesR int64 `json:"rx_bytes-r"`
SpeedtestLastrun int `json:"speedtest_lastrun"`
SpeedtestPing int `json:"speedtest_ping"`
SpeedtestStatus string `json:"speedtest_status"`
Status string `json:"status"`
Subsystem string `json:"subsystem"`
TxBytesR int64 `json:"tx_bytes-r"`
Uptime int `json:"uptime"`
XputDown float64 `json:"xput_down"`
XputUp float64 `json:"xput_up"`
}
type Health struct {
LAN LAN
VPN VPN
WAN WAN
WLAN WLAN
WWW WWW
}
// Returns a slice of access points
func (u *Unifi) Health(site *Site) (Health, error) {
// The health struct
var health Health
// Response[]UAP from controller
var response struct {
Data []json.RawMessage
Meta meta
}
// Fetch the health data
err := u.parse(site, "stat/health", nil, &response)
// Loop thru the data objects to parse them with the corresponding struct
for _, d := range response.Data {
// unmarshal into a map to check the "subsystem" field
var obj map[string]any
err := json.Unmarshal(d, &obj)
if err != nil {
return health, err
}
subsystem, ok := obj["subsystem"].(string)
if !ok {
return health, fmt.Errorf("error on retrieving subsystem from raw json")
}
switch subsystem {
case "lan":
var lan LAN
err := json.Unmarshal(d, &lan)
if err != nil {
return health, err
}
// Unmarshal successful. Add to health object
health.LAN = lan
case "vpn":
var vpn VPN
err := json.Unmarshal(d, &vpn)
if err != nil {
return health, err
}
// Unmarshal successful. Add to health object
health.VPN = vpn
case "wlan":
var wlan WLAN
err := json.Unmarshal(d, &wlan)
if err != nil {
return health, err
}
// Unmarshal successful. Add to health object
health.WLAN = wlan
case "wan":
var wan WAN
err := json.Unmarshal(d, &wan)
if err != nil {
return health, err
}
// Unmarshal successful. Add to health object
health.WAN = wan
case "www":
var www WWW
err := json.Unmarshal(d, &www)
if err != nil {
return health, err
}
// Unmarshal successful. Add to health object
health.WWW = www
}
}
return health, err
}