@@ -33,6 +33,7 @@ import (
33
33
"github.com/apache/trafficcontrol/lib/go-log"
34
34
"github.com/apache/trafficcontrol/lib/go-tc"
35
35
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
36
+ "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/util/monitorhlp"
36
37
)
37
38
38
39
func GetCapacity (w http.ResponseWriter , r * http.Request ) {
@@ -50,27 +51,6 @@ const MonitorProxyParameter = "tm.traffic_mon_fwd_proxy"
50
51
const MonitorRequestTimeout = time .Second * 10
51
52
const MonitorOnlineStatus = "ONLINE"
52
53
53
- // CRStates contains the Monitor CRStates members needed for health. It is NOT the full object served by the Monitor, but only the data required by this endpoint.
54
- type CRStates struct {
55
- Caches map [tc.CacheName ]Available `json:"caches"`
56
- }
57
-
58
- type Available struct {
59
- IsAvailable bool `json:"isAvailable"`
60
- }
61
-
62
- // CRConfig contains the Monitor CRConfig members needed for health. It is NOT the full object served by the Monitor, but only the data required by this endpoint.
63
- type CRConfig struct {
64
- ContentServers map [tc.CacheName ]CRConfigServer `json:"contentServers"`
65
- }
66
-
67
- type CRConfigServer struct {
68
- CacheGroup tc.CacheGroupName `json:"locationId"`
69
- Status tc.CacheStatus `json:"status"`
70
- Type tc.CacheType `json:"type"`
71
- Profile string `json:"profile"`
72
- }
73
-
74
54
func getCapacity (tx * sql.Tx ) (CapacityResp , error ) {
75
55
monitors , err := getCDNMonitorFQDNs (tx )
76
56
if err != nil {
@@ -144,15 +124,15 @@ func getCapacityData(monitors map[tc.CDNName][]string, thresholds map[string]flo
144
124
for cdn , monitorFQDNs := range monitors {
145
125
err := error (nil )
146
126
for _ , monitorFQDN := range monitorFQDNs {
147
- crStates := CRStates {}
148
- crConfig := CRConfig {}
127
+ crStates := tc. CRStates {}
128
+ crConfig := tc. CRConfig {}
149
129
cacheStats := CacheStats {}
150
- if crStates , err = getCRStates (monitorFQDN , client ); err != nil {
130
+ if crStates , err = monitorhlp . GetCRStates (monitorFQDN , client ); err != nil {
151
131
err = errors .New ("getting CRStates for CDN '" + string (cdn ) + "' monitor '" + monitorFQDN + "': " + err .Error ())
152
132
log .Warnln ("getCapacity failed to get CRStates from cdn '" + string (cdn ) + " monitor '" + monitorFQDN + "', trying next monitor: " + err .Error ())
153
133
continue
154
134
}
155
- if crConfig , err = getCRConfig (monitorFQDN , client ); err != nil {
135
+ if crConfig , err = monitorhlp . GetCRConfig (monitorFQDN , client ); err != nil {
156
136
err = errors .New ("getting CRConfig for CDN '" + string (cdn ) + "' monitor '" + monitorFQDN + "': " + err .Error ())
157
137
log .Warnln ("getCapacity failed to get CRConfig from cdn '" + string (cdn ) + " monitor '" + monitorFQDN + "', trying next monitor: " + err .Error ())
158
138
continue
@@ -172,30 +152,34 @@ func getCapacityData(monitors map[tc.CDNName][]string, thresholds map[string]flo
172
152
return cap , nil
173
153
}
174
154
175
- func addCapacity (cap CapData , cacheStats CacheStats , crStates CRStates , crConfig CRConfig , thresholds map [string ]float64 ) CapData {
155
+ func addCapacity (cap CapData , cacheStats CacheStats , crStates tc. CRStates , crConfig tc. CRConfig , thresholds map [string ]float64 ) CapData {
176
156
for cacheName , stats := range cacheStats .Caches {
177
- cache , ok := crConfig .ContentServers [cacheName ]
157
+ cache , ok := crConfig .ContentServers [string ( cacheName ) ]
178
158
if ! ok {
179
159
continue
180
160
}
181
- if ! strings .HasPrefix (string (cache .Type ), string (tc .CacheTypeEdge )) {
161
+ if cache .ServerType == nil || cache .ServerStatus == nil || cache .Profile == nil {
162
+ log .Warnln ("addCapacity got cache with nil values! Skipping!" )
163
+ continue
164
+ }
165
+ if ! strings .HasPrefix (* cache .ServerType , string (tc .CacheTypeEdge )) {
182
166
continue
183
167
}
184
168
if len (stats .KBPS ) < 1 || len (stats .MaxKBPS ) < 1 {
185
169
continue
186
170
}
187
- if cache .Status == "REPORTED" || cache .Status == "ONLINE" {
171
+ if string ( * cache .ServerStatus ) == string ( tc . CacheStatusReported ) || string ( * cache .ServerStatus ) == string ( tc . CacheStatusOnline ) {
188
172
if crStates .Caches [cacheName ].IsAvailable {
189
173
cap .Available += float64 (stats .KBPS [0 ].Value )
190
174
} else {
191
175
cap .Unavailable += float64 (stats .KBPS [0 ].Value )
192
176
}
193
- } else if cache .Status == "ADMIN_DOWN" {
177
+ } else if string ( * cache .ServerStatus ) == string ( tc . CacheStatusAdminDown ) {
194
178
cap .Maintenance += float64 (stats .KBPS [0 ].Value )
195
179
} else {
196
180
continue // don't add capacity for OFFLINE or other statuses
197
181
}
198
- cap .Capacity += float64 (stats .MaxKBPS [0 ].Value ) - thresholds [cache .Profile ]
182
+ cap .Capacity += float64 (stats .MaxKBPS [0 ].Value ) - thresholds [* cache .Profile ]
199
183
}
200
184
return cap
201
185
}
@@ -234,35 +218,6 @@ AND pa.name = 'health.threshold.availableBandwidthInKbps'
234
218
return profileThresholds , nil
235
219
}
236
220
237
- func getCRStates (monitorFQDN string , client * http.Client ) (CRStates , error ) {
238
- path := `/publish/CrStates`
239
- resp , err := client .Get ("http://" + monitorFQDN + path )
240
- if err != nil {
241
- return CRStates {}, errors .New ("getting CRStates from Monitor '" + monitorFQDN + "': " + err .Error ())
242
- }
243
- defer resp .Body .Close ()
244
-
245
- crs := CRStates {}
246
- if err := json .NewDecoder (resp .Body ).Decode (& crs ); err != nil {
247
- return CRStates {}, errors .New ("decoding CRStates from monitor '" + monitorFQDN + "': " + err .Error ())
248
- }
249
- return crs , nil
250
- }
251
-
252
- func getCRConfig (monitorFQDN string , client * http.Client ) (CRConfig , error ) {
253
- path := `/publish/CrConfig`
254
- resp , err := client .Get ("http://" + monitorFQDN + path )
255
- if err != nil {
256
- return CRConfig {}, errors .New ("getting CRConfig from Monitor '" + monitorFQDN + "': " + err .Error ())
257
- }
258
- defer resp .Body .Close ()
259
- crs := CRConfig {}
260
- if err := json .NewDecoder (resp .Body ).Decode (& crs ); err != nil {
261
- return CRConfig {}, errors .New ("decoding CRConfig from monitor '" + monitorFQDN + "': " + err .Error ())
262
- }
263
- return crs , nil
264
- }
265
-
266
221
// CacheStats contains the Monitor CacheStats needed by Cachedata. It is NOT the full object served by the Monitor, but only the data required by the caches stats endpoint.
267
222
type CacheStats struct {
268
223
Caches map [tc.CacheName ]CacheStat `json:"caches"`
0 commit comments