forked from lablabs/cloudflare-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudflare.go
358 lines (327 loc) · 8.76 KB
/
cloudflare.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package main
import (
"context"
"time"
cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/machinebox/graphql"
log "github.com/sirupsen/logrus"
)
var (
cfGraphQLEndpoint = "https://api.cloudflare.com/client/v4/graphql/"
)
type cloudflareResponse struct {
Viewer struct {
Zones []zoneResp `json:"zones"`
} `json:"viewer"`
}
type cloudflareResponseColo struct {
Viewer struct {
Zones []zoneRespColo `json:"zones"`
} `json:"viewer"`
}
type zoneRespColo struct {
ColoGroups []struct {
Dimensions struct {
Datetime string `json:"datetime"`
ColoCode string `json:"coloCode"`
} `json:"dimensions"`
Count uint64 `json:"count"`
Sum struct {
EdgeResponseBytes uint64 `json:"edgeResponseBytes"`
Visits uint64 `json:"visits"`
} `json:"sum"`
Avg struct {
SampleInterval float64 `json:"sampleInterval"`
} `json:"avg"`
} `json:"httpRequestsAdaptiveGroups"`
ZoneTag string `json:"zoneTag"`
}
type zoneResp struct {
HTTP1mGroups []struct {
Dimensions struct {
Datetime string `json:"datetime"`
} `json:"dimensions"`
Unique struct {
Uniques uint64 `json:"uniques"`
} `json:"uniq"`
Sum struct {
Bytes uint64 `json:"bytes"`
CachedBytes uint64 `json:"cachedBytes"`
CachedRequests uint64 `json:"cachedRequests"`
Requests uint64 `json:"requests"`
BrowserMap []struct {
PageViews uint64 `json:"pageViews"`
UaBrowserFamily string `json:"uaBrowserFamily"`
} `json:"browserMap"`
ClientHTTPVersion []struct {
Protocol string `json:"clientHTTPProtocol"`
Requests uint64 `json:"requests"`
} `json:"clientHTTPVersionMap"`
ClientSSL []struct {
Protocol string `json:"clientSSLProtocol"`
} `json:"clientSSLMap"`
ContentType []struct {
Bytes uint64 `json:"bytes"`
Requests uint64 `json:"requests"`
EdgeResponseContentType string `json:"edgeResponseContentTypeName"`
} `json:"contentTypeMap"`
Country []struct {
Bytes uint64 `json:"bytes"`
ClientCountryName string `json:"clientCountryName"`
Requests uint64 `json:"requests"`
Threats uint64 `json:"threats"`
} `json:"countryMap"`
EncryptedBytes uint64 `json:"encryptedBytes"`
EncryptedRequests uint64 `json:"encryptedRequests"`
IPClass []struct {
Type string `json:"ipType"`
Requests uint64 `json:"requests"`
} `json:"ipClassMap"`
PageViews uint64 `json:"pageViews"`
ResponseStatus []struct {
EdgeResponseStatus int `json:"edgeResponseStatus"`
Requests uint64 `json:"requests"`
} `json:"responseStatusMap"`
ThreatPathing []struct {
Name string `json:"threatPathingName"`
Requests uint64 `json:"requests "`
} `json:"threatPathingMap"`
Threats uint64 `json:"threats"`
} `json:"sum"`
} `json:"httpRequests1mGroups"`
FirewallEventsAdaptiveGroups []struct {
Count uint64 `json:"count"`
Dimensions struct {
Action string `json:"action"`
ClientCountryName string `json:"clientCountryName"`
ClientRequestHTTPHost string `json:"clientRequestHTTPHost"`
} `json:"dimensions"`
} `json:"firewallEventsAdaptiveGroups"`
HTTPRequestsAdaptiveGroups []struct {
Count uint64 `json:"count"`
Dimensions struct {
OriginResponseStatus uint16 `json:"originResponseStatus"`
ClientCountryName string `json:"clientCountryName"`
ClientRequestHTTPHost string `json:"clientRequestHTTPHost"`
} `json:"dimensions"`
} `json:"httpRequestsAdaptiveGroups"`
HTTPRequestsEdgeCountryHost []struct {
Count uint64 `json:"count"`
Dimensions struct {
EdgeResponseStatus uint16 `json:"edgeResponseStatus"`
ClientCountryName string `json:"clientCountryName"`
ClientRequestHTTPHost string `json:"clientRequestHTTPHost"`
} `json:"dimensions"`
} `json:"httpRequestsEdgeCountryHost"`
HealthCheckEventsAdaptiveGroups []struct {
Count uint64 `json:"count"`
Dimensions struct {
HealthStatus string `json:"healthStatus"`
OriginIP string `json:"originIP"`
FailureReason string `json:"failureReason"`
Region string `json:"region"`
} `json:"dimensions"`
} `json:"healthCheckEventsAdaptiveGroups"`
ZoneTag string `json:"zoneTag"`
}
func fetchZones() []cloudflare.Zone {
var api *cloudflare.API
var err error
if len(cfgCfAPIToken) > 0 {
api, err = cloudflare.NewWithAPIToken(cfgCfAPIToken)
} else {
api, err = cloudflare.New(cfgCfAPIKey, cfgCfAPIEmail)
}
if err != nil {
log.Fatal(err)
}
z, err := api.ListZones()
if err != nil {
log.Fatal(err)
}
return z
}
func fetchZoneTotals(zoneIDs []string) (*cloudflareResponse, error) {
now := time.Now().Add(-180 * time.Second).UTC()
s := 60 * time.Second
now = now.Truncate(s)
now1mAgo := now.Add(-60 * time.Second)
request := graphql.NewRequest(`
query ($zoneIDs: [String!], $mintime: Time!, $maxtime: Time!, $limit: Int!) {
viewer {
zones(filter: { zoneTag_in: $zoneIDs }) {
zoneTag
httpRequests1mGroups(limit: $limit filter: { datetime: $maxtime }) {
uniq {
uniques
}
sum {
browserMap {
pageViews
uaBrowserFamily
}
bytes
cachedBytes
cachedRequests
clientHTTPVersionMap {
clientHTTPProtocol
requests
}
clientSSLMap {
clientSSLProtocol
requests
}
contentTypeMap {
bytes
requests
edgeResponseContentTypeName
}
countryMap {
bytes
clientCountryName
requests
threats
}
encryptedBytes
encryptedRequests
ipClassMap {
ipType
requests
}
pageViews
requests
responseStatusMap {
edgeResponseStatus
requests
}
threatPathingMap {
requests
threatPathingName
}
threats
}
dimensions {
datetime
}
}
firewallEventsAdaptiveGroups(limit: $limit, filter: { datetime_geq: $mintime, datetime_lt: $maxtime }) {
count
dimensions {
action
source
clientRequestHTTPHost
clientCountryName
}
}
httpRequestsAdaptiveGroups(limit: $limit, filter: { datetime_geq: $mintime, datetime_lt: $maxtime, cacheStatus_notin: ["hit"] }) {
count
dimensions {
originResponseStatus
clientCountryName
clientRequestHTTPHost
}
}
httpRequestsEdgeCountryHost: httpRequestsAdaptiveGroups(limit: $limit, filter: { datetime_geq: $mintime, datetime_lt: $maxtime }) {
count
dimensions {
edgeResponseStatus
clientCountryName
clientRequestHTTPHost
}
}
healthCheckEventsAdaptiveGroups(limit: $limit, filter: { datetime_geq: $mintime, datetime_lt: $maxtime }) {
count
dimensions {
healthStatus
originIP
region
}
}
}
}
}
`)
if len(cfgCfAPIToken) > 0 {
request.Header.Set("Authorization", "Bearer "+cfgCfAPIToken)
} else {
request.Header.Set("X-AUTH-EMAIL", cfgCfAPIEmail)
request.Header.Set("X-AUTH-KEY", cfgCfAPIKey)
}
request.Var("limit", 9999)
request.Var("maxtime", now)
request.Var("mintime", now1mAgo)
request.Var("zoneIDs", zoneIDs)
ctx := context.Background()
graphqlClient := graphql.NewClient(cfGraphQLEndpoint)
var resp cloudflareResponse
if err := graphqlClient.Run(ctx, request, &resp); err != nil {
log.Error(err)
return nil, err
}
return &resp, nil
}
func fetchColoTotals(zoneIDs []string) (*cloudflareResponseColo, error) {
now := time.Now().Add(-180 * time.Second).UTC()
s := 60 * time.Second
now = now.Truncate(s)
now1mAgo := now.Add(-60 * time.Second)
request := graphql.NewRequest(`
query ($zoneIDs: [String!], $mintime: Time!, $maxtime: Time!, $limit: Int!) {
viewer {
zones(filter: { zoneTag_in: $zoneIDs }) {
zoneTag
httpRequestsAdaptiveGroups(
limit: $limit
filter: { datetime_geq: $mintime, datetime_lt: $maxtime }
) {
count
avg {
sampleInterval
}
dimensions {
coloCode
datetime
}
sum {
edgeResponseBytes
visits
}
}
}
}
}
`)
if len(cfgCfAPIToken) > 0 {
request.Header.Set("Authorization", "Bearer "+cfgCfAPIToken)
} else {
request.Header.Set("X-AUTH-EMAIL", cfgCfAPIEmail)
request.Header.Set("X-AUTH-KEY", cfgCfAPIKey)
}
request.Var("limit", 9999)
request.Var("maxtime", now)
request.Var("mintime", now1mAgo)
request.Var("zoneIDs", zoneIDs)
ctx := context.Background()
graphqlClient := graphql.NewClient(cfGraphQLEndpoint)
var resp cloudflareResponseColo
if err := graphqlClient.Run(ctx, request, &resp); err != nil {
log.Error(err)
return nil, err
}
return &resp, nil
}
func findZoneName(zones []cloudflare.Zone, ID string) string {
for _, z := range zones {
if z.ID == ID {
return z.Name
}
}
return ""
}
func extractZoneIDs(zones []cloudflare.Zone) []string {
var IDs []string
for _, z := range zones {
IDs = append(IDs, z.ID)
}
return IDs
}