forked from lablabs/cloudflare-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
326 lines (270 loc) · 9.26 KB
/
main.go
File metadata and controls
326 lines (270 loc) · 9.26 KB
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
package main
import (
"net/http"
_ "net/http/pprof" // #nosec G108 - pprof is controlled via enable_pprof flag
"runtime"
"strings"
"sync"
"time"
"github.com/nelkinda/health-go"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
"github.com/spf13/viper"
cf "github.com/cloudflare/cloudflare-go/v4"
cfoption "github.com/cloudflare/cloudflare-go/v4/option"
cfzones "github.com/cloudflare/cloudflare-go/v4/zones"
"github.com/sirupsen/logrus"
)
var (
cfclient *cf.Client
cftimeout time.Duration
gql *GraphQL
log = logrus.New()
)
// var (
// cfgListen = ":8080"
// cfgCfAPIKey = ""
// cfgCfAPIEmail = ""
// cfgCfAPIToken = ""
// cfgMetricsPath = "/metrics"
// cfgZones = ""
// cfgExcludeZones = ""
// cfgScrapeDelay = 300
// cfgFreeTier = false
// cfgMetricsDenylist = ""
// )
func getTargetZones() []string {
var zoneIDs []string
if len(viper.GetString("cf_zones")) > 0 {
zoneIDs = strings.Split(viper.GetString("cf_zones"), ",")
}
return zoneIDs
}
func getExcludedZones() []string {
var zoneIDs []string
if len(viper.GetString("cf_exclude_zones")) > 0 {
zoneIDs = strings.Split(viper.GetString("cf_exclude_zones"), ",")
}
return zoneIDs
}
func filterZones(all []cfzones.Zone, target []string) []cfzones.Zone {
var filtered []cfzones.Zone
if (len(target)) == 0 {
return all
}
for _, tz := range target {
for _, z := range all {
if tz == z.ID {
filtered = append(filtered, z)
log.Debug("Filtering zone: ", z.ID, " ", z.Name)
}
}
}
return filtered
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func filterExcludedZones(all []cfzones.Zone, exclude []string) []cfzones.Zone {
var filtered []cfzones.Zone
if (len(exclude)) == 0 {
return all
}
for _, z := range all {
if contains(exclude, z.ID) {
log.Info("Exclude zone: ", z.ID, " ", z.Name)
} else {
filtered = append(filtered, z)
}
}
return filtered
}
func fetchMetrics(deniedMetricsSet MetricsSet) {
var wg sync.WaitGroup
accounts := fetchAccounts()
for _, a := range accounts {
go fetchWorkerAnalytics(a, &wg)
go fetchLogpushAnalyticsForAccount(a, &wg)
go fetchR2StorageForAccount(a, &wg)
go fetchLoadblancerPoolsHealth(a, &wg)
go fetchZeroTrustAnalyticsForAccount(a, &wg)
go fetchDNSFirewallAnalytics(a, &wg, deniedMetricsSet)
}
zones := fetchZones(accounts)
tzones := getTargetZones()
fzones := filterZones(zones, tzones)
ezones := getExcludedZones()
filteredZones := filterExcludedZones(fzones, ezones)
if !viper.GetBool("free_tier") {
filteredZones = filterNonFreePlanZones(filteredZones)
}
zoneCount := len(filteredZones)
if zoneCount > 0 && zoneCount <= cfgraphqlreqlimit {
go fetchZoneAnalytics(filteredZones, &wg, deniedMetricsSet)
go fetchZoneAdaptiveHTTPStats(filteredZones, &wg, deniedMetricsSet)
go fetchZoneColocationAnalytics(filteredZones, &wg, deniedMetricsSet)
go fetchLoadBalancerAnalytics(filteredZones, &wg, deniedMetricsSet)
go fetchLogpushAnalyticsForZone(filteredZones, &wg, deniedMetricsSet)
} else if zoneCount > cfgraphqlreqlimit {
for s := 0; s < zoneCount; s += cfgraphqlreqlimit {
e := s + cfgraphqlreqlimit
if e > zoneCount {
e = zoneCount
}
go fetchZoneAnalytics(filteredZones[s:e], &wg, deniedMetricsSet)
go fetchZoneAdaptiveHTTPStats(filteredZones[s:e], &wg, deniedMetricsSet)
go fetchZoneColocationAnalytics(filteredZones[s:e], &wg, deniedMetricsSet)
go fetchLoadBalancerAnalytics(filteredZones[s:e], &wg, deniedMetricsSet)
go fetchLogpushAnalyticsForZone(filteredZones[s:e], &wg, deniedMetricsSet)
}
}
wg.Wait()
}
func runExporter() {
cfgMetricsPath := viper.GetString("metrics_path")
// Handle pprof configuration
if !viper.GetBool("enable_pprof") {
// Remove pprof handlers from default mux if disabled
http.DefaultServeMux = http.NewServeMux()
log.Info("pprof disabled")
} else {
log.Warn("pprof enabled - profiling endpoints available at /debug/pprof/")
}
metricsDenylist := []string{}
if len(viper.GetString("metrics_denylist")) > 0 {
metricsDenylist = strings.Split(viper.GetString("metrics_denylist"), ",")
}
metricsSet, err := buildFilteredMetricsSet(metricsDenylist)
if err != nil {
log.Fatalf("Error building metrics set: %v", err)
}
log.Debugf("Metrics set: %v", metricsSet)
mustRegisterMetrics(metricsSet)
scrapeInterval := time.Duration(viper.GetInt("scrape_interval")) * time.Second
log.Info("Scrape interval set to ", scrapeInterval)
go func() {
for ; true; <-time.NewTicker(scrapeInterval).C {
go fetchMetrics(metricsSet)
}
}()
// This section will start the HTTP server and expose
// any metrics on the /metrics endpoint.
if !strings.HasPrefix(viper.GetString("metrics_path"), "/") {
cfgMetricsPath = "/" + viper.GetString("metrics_path")
}
http.Handle(cfgMetricsPath, promhttp.Handler())
h := health.New(health.Health{})
http.HandleFunc("/health", h.Handler)
log.Info("Beginning to serve metrics on ", viper.GetString("listen"), cfgMetricsPath)
server := &http.Server{
Addr: viper.GetString("listen"),
ReadHeaderTimeout: 3 * time.Second,
}
log.Fatal(server.ListenAndServe())
}
func main() {
cmd := &cobra.Command{
Use: "cloudflare_exporter",
Short: "Prometheus exporter exposing Cloudflare Analytics dashboard data on a per-zone basis, as well as Worker metrics",
Run: func(_ *cobra.Command, _ []string) {
runExporter()
},
}
viper.AutomaticEnv()
flags := cmd.Flags()
flags.String("listen", ":8080", "listen on addr:port (default :8080), omit addr to listen on all interfaces")
viper.BindEnv("listen")
viper.SetDefault("listen", ":8080")
flags.String("metrics_path", "/metrics", "path for metrics, default /metrics")
viper.BindEnv("metrics_path")
viper.SetDefault("metrics_path", "/metrics")
flags.String("cf_api_key", "", "cloudflare api key, required with api_email flag")
viper.BindEnv("cf_api_key")
flags.String("cf_api_email", "", "cloudflare api email, required with api_key flag")
viper.BindEnv("cf_api_email")
flags.String("cf_api_token", "", "cloudflare api token (preferred)")
viper.BindEnv("cf_api_token")
flags.String("cf_zones", "", "cloudflare zones to export, comma delimited list of zone ids")
viper.BindEnv("cf_zones")
viper.SetDefault("cf_zones", "")
flags.String("cf_exclude_zones", "", "cloudflare zones to exclude, comma delimited list of zone ids")
viper.BindEnv("cf_exclude_zones")
viper.SetDefault("cf_exclude_zones", "")
flags.Int("scrape_delay", 300, "scrape delay in seconds, defaults to 300")
viper.BindEnv("scrape_delay")
viper.SetDefault("scrape_delay", 300)
flags.Int("scrape_interval", 60, "scrape interval in seconds, defaults to 60")
viper.BindEnv("scrape_interval")
viper.SetDefault("scrape_interval", 60)
flags.Bool("free_tier", false, "scrape only metrics included in free plan")
viper.BindEnv("free_tier")
viper.SetDefault("free_tier", false)
flags.Duration("cf_timeout", 10*time.Second, "cloudflare request timeout, default 10 seconds")
viper.BindEnv("cf_timeout")
viper.SetDefault("cf_timeout", 10*time.Second)
flags.String("metrics_denylist", "", "metrics to not expose, comma delimited list")
viper.BindEnv("metrics_denylist")
viper.SetDefault("metrics_denylist", "")
flags.String("log_level", "info", "log level")
viper.BindEnv("log_level")
viper.SetDefault("log_level", "info")
flags.Bool("enable_pprof", false, "enable pprof profiling endpoints at /debug/pprof/")
viper.BindEnv("enable_pprof")
viper.SetDefault("enable_pprof", false)
viper.BindPFlags(flags)
logLevel := viper.GetString("log_level")
switch logLevel {
case "debug":
log.Level = logrus.DebugLevel
log.SetReportCaller(true)
case "warn":
log.Level = logrus.WarnLevel
case "error":
log.Level = logrus.ErrorLevel
default:
log.Level = logrus.InfoLevel
}
log.SetFormatter(&logrus.TextFormatter{
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
funcPath := strings.Split(f.File, "/")
file := funcPath[len(funcPath)-1]
return "file:" + file, " func:" + f.Function
},
})
cftimeout = viper.GetDuration("cf_timeout")
if len(viper.GetString("cf_api_token")) > 0 {
cfclient = cf.NewClient(
cfoption.WithAPIToken(viper.GetString("cf_api_token")),
cfoption.WithRequestTimeout(cftimeout),
)
middlewares := NewHeaderMiddleware("Authorization", "Bearer "+viper.GetString("cf_api_token"), http.DefaultTransport)
gqlHTTPClient := &http.Client{
Timeout: cftimeout,
Transport: middlewares,
}
gql = NewGraphQLClient(gqlHTTPClient)
} else if len(viper.GetString("cf_api_email")) > 0 && len(viper.GetString("cf_api_key")) > 0 {
cfclient = cf.NewClient(
cfoption.WithAPIKey(viper.GetString("cf_api_key")),
cfoption.WithAPIEmail(viper.GetString("cf_api_email")),
cfoption.WithRequestTimeout(cftimeout),
)
authEmailHeader := NewHeaderMiddleware("X-AUTH-EMAIL", viper.GetString("cf_api_email"), http.DefaultTransport)
middlewares := NewHeaderMiddleware("X-AUTH-KEY", viper.GetString("cf_api_key"), authEmailHeader)
gqlHTTPClient := &http.Client{
Timeout: cftimeout,
Transport: middlewares,
}
gql = NewGraphQLClient(gqlHTTPClient)
} else {
log.Fatal("Please provide CF_API_KEY+CF_API_EMAIL or CF_API_TOKEN")
}
cmd.Execute()
}