github.com/dim13/unifi@v0.0.0-20230308161331-9b04946f5e93/health.go (about) 1 // Copyright (c) 2014 The unifi Authors. All rights reserved. 2 // Use of this source code is governed by ISC-style license 3 // that can be found in the LICENSE file. 4 5 package unifi 6 7 import ( 8 "encoding/json" 9 "fmt" 10 ) 11 12 type LAN struct { 13 LanIP string `json:"lan_ip"` 14 NumAdopted int `json:"num_adopted"` 15 NumDisconnected int `json:"num_disconnected"` 16 NumGuest int `json:"num_guest"` 17 NumPending int `json:"num_pending"` 18 NumSw int `json:"num_sw"` 19 NumUser int `json:"num_user"` 20 RxBytesR int64 `json:"rx_bytes-r"` 21 Status string `json:"status"` 22 Subsystem string `json:"subsystem"` 23 TxBytesR int64 `json:"tx_bytes-r"` 24 } 25 26 type VPN struct { 27 Status string `json:"status"` 28 Subsystem string `json:"subsystem"` 29 } 30 31 type WAN struct { 32 Gateways []string `json:"gateways"` 33 GwMac string `json:"gw_mac"` 34 GwName string `json:"gw_name"` 35 GwSystemStats struct { 36 CPU string `json:"cpu"` 37 Mem string `json:"mem"` 38 Temps struct { 39 BoardCPU string `json:"Board (CPU)"` 40 BoardPHY string `json:"Board (PHY)"` 41 CPU string `json:"CPU"` 42 PHY string `json:"PHY"` 43 } `json:"temps"` 44 Uptime string `json:"uptime"` 45 } `json:"gw_system-stats"` 46 GwVersion string `json:"gw_version"` 47 Nameservers []string `json:"nameservers"` 48 Netmask string `json:"netmask"` 49 NumAdopted int `json:"num_adopted"` 50 NumDisconnected int `json:"num_disconnected"` 51 NumGw int `json:"num_gw"` 52 NumPending int `json:"num_pending"` 53 NumSta int `json:"num_sta"` 54 RxBytesR int64 `json:"rx_bytes-r"` 55 Status string `json:"status"` 56 Subsystem string `json:"subsystem"` 57 TxBytesR int64 `json:"tx_bytes-r"` 58 WanIP string `json:"wan_ip"` 59 } 60 61 type WLAN struct { 62 NumAdopted int `json:"num_adopted"` 63 NumAp int `json:"num_ap"` 64 NumDisabled int `json:"num_disabled"` 65 NumDisconnected int `json:"num_disconnected"` 66 NumGuest int `json:"num_guest"` 67 NumPending int `json:"num_pending"` 68 NumUser int `json:"num_user"` 69 RxBytesR int64 `json:"rx_bytes-r"` 70 Status string `json:"status"` 71 Subsystem string `json:"subsystem"` 72 TxBytesR int64 `json:"tx_bytes-r"` 73 } 74 75 type WWW struct { 76 Drops int `json:"drops"` 77 GwMac string `json:"gw_mac"` 78 Latency int `json:"latency"` 79 RxBytesR int64 `json:"rx_bytes-r"` 80 SpeedtestLastrun int `json:"speedtest_lastrun"` 81 SpeedtestPing int `json:"speedtest_ping"` 82 SpeedtestStatus string `json:"speedtest_status"` 83 Status string `json:"status"` 84 Subsystem string `json:"subsystem"` 85 TxBytesR int64 `json:"tx_bytes-r"` 86 Uptime int `json:"uptime"` 87 XputDown float64 `json:"xput_down"` 88 XputUp float64 `json:"xput_up"` 89 } 90 91 type Health struct { 92 LAN LAN 93 VPN VPN 94 WAN WAN 95 WLAN WLAN 96 WWW WWW 97 } 98 99 // Returns a slice of access points 100 func (u *Unifi) Health(site *Site) (Health, error) { 101 102 // The health struct 103 var health Health 104 105 // Response[]UAP from controller 106 var response struct { 107 Data []json.RawMessage 108 Meta meta 109 } 110 111 // Fetch the health data 112 err := u.parse(site, "stat/health", nil, &response) 113 114 // Loop thru the data objects to parse them with the corresponding struct 115 for _, d := range response.Data { 116 117 // unmarshal into a map to check the "subsystem" field 118 var obj map[string]any 119 err := json.Unmarshal(d, &obj) 120 if err != nil { 121 return health, err 122 } 123 124 subsystem, ok := obj["subsystem"].(string) 125 if !ok { 126 return health, fmt.Errorf("error on retrieving subsystem from raw json") 127 } 128 129 switch subsystem { 130 case "lan": 131 var lan LAN 132 133 err := json.Unmarshal(d, &lan) 134 if err != nil { 135 return health, err 136 } 137 // Unmarshal successful. Add to health object 138 health.LAN = lan 139 140 case "vpn": 141 var vpn VPN 142 143 err := json.Unmarshal(d, &vpn) 144 if err != nil { 145 return health, err 146 } 147 // Unmarshal successful. Add to health object 148 health.VPN = vpn 149 150 case "wlan": 151 var wlan WLAN 152 153 err := json.Unmarshal(d, &wlan) 154 if err != nil { 155 return health, err 156 } 157 // Unmarshal successful. Add to health object 158 health.WLAN = wlan 159 160 case "wan": 161 var wan WAN 162 163 err := json.Unmarshal(d, &wan) 164 if err != nil { 165 return health, err 166 } 167 // Unmarshal successful. Add to health object 168 health.WAN = wan 169 170 case "www": 171 var www WWW 172 173 err := json.Unmarshal(d, &www) 174 if err != nil { 175 return health, err 176 } 177 // Unmarshal successful. Add to health object 178 health.WWW = www 179 180 } 181 } 182 return health, err 183 }