forked from lablabs/cloudflare-exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
278 lines (226 loc) · 7.45 KB
/
main.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
package main
import (
"context"
"fmt"
"net/http"
"runtime/debug"
"slices"
"strings"
"sync"
"time"
"github.com/machinebox/graphql"
"github.com/nelkinda/health-go"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/cloudflare/cloudflare-go"
log "github.com/sirupsen/logrus"
)
// var (
// cfgListen = ":8080"
// cfgCfAPIKey = ""
// cfgCfAPIEmail = ""
// cfgCfAPIToken = ""
// cfgMetricsPath = "/metrics"
// cfgZones = ""
// cfgExcludeZones = ""
// cfgScrapeDelay = 300
// cfgFreeTier = false
// cfgBatchSize = 10
// cfgMetricsDenylist = ""
// )
var (
cloudflareAPI *cloudflare.API
version = "DEV"
date = "unknown"
versionString = fmt.Sprintf("%s (built %s)", version, date)
)
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 []cloudflare.Zone, target []string) []cloudflare.Zone {
var filtered []cloudflare.Zone
if (len(target)) == 0 {
return all
}
for _, tz := range target {
for _, z := range all {
if tz == z.ID {
filtered = append(filtered, z)
log.Info("Filtering zone: ", z.ID, " ", z.Name)
}
}
}
return filtered
}
func filterExcludedZones(all []cloudflare.Zone, exclude []string) []cloudflare.Zone {
var filtered []cloudflare.Zone
if (len(exclude)) == 0 {
return all
}
for _, z := range all {
if slices.Contains(exclude, z.ID) {
log.Info("Exclude zone: ", z.ID, " ", z.Name)
} else {
filtered = append(filtered, z)
}
}
return filtered
}
func fetchMetrics() {
var wg sync.WaitGroup
zones := fetchZones()
accounts := fetchAccounts()
filteredZones := filterExcludedZones(filterZones(zones, getTargetZones()), getExcludedZones())
for _, a := range accounts {
go fetchWorkerAnalytics(a, &wg)
go fetchLogpushAnalyticsForAccount(a, &wg)
go fetchR2StorageForAccount(a, &wg)
}
// Make requests in groups of cfgBatchSize to avoid rate limit
// 10 is the maximum amount of zones you can request at once
for len(filteredZones) > 0 {
sliceLength := viper.GetInt("cf_batch_size")
if len(filteredZones) < viper.GetInt("cf_batch_size") {
sliceLength = len(filteredZones)
}
targetZones := filteredZones[:sliceLength]
filteredZones = filteredZones[len(targetZones):]
go fetchZoneAnalytics(targetZones, &wg)
go fetchZoneColocationAnalytics(targetZones, &wg)
go fetchLoadBalancerAnalytics(targetZones, &wg)
go fetchLogpushAnalyticsForZone(targetZones, &wg)
}
wg.Wait()
}
func runExporter() {
cfgMetricsPath := viper.GetString("metrics_path")
if !(len(viper.GetString("cf_api_token")) > 0 || (len(viper.GetString("cf_api_email")) > 0 && len(viper.GetString("cf_api_key")) > 0)) {
log.Fatal("Please provide CF_API_KEY+CF_API_EMAIL or CF_API_TOKEN")
}
if viper.GetInt("cf_batch_size") < 1 || viper.GetInt("cf_batch_size") > 10 {
log.Fatal("CF_BATCH_SIZE must be between 1 and 10")
}
customFormatter := new(log.TextFormatter)
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
log.SetFormatter(customFormatter)
customFormatter.FullTimestamp = true
logLevel, err := log.ParseLevel(viper.GetString("log_level"))
if err != nil {
log.Fatalf("Invalid log level: %s", viper.GetString("log_level"))
}
log.SetLevel(logLevel)
if len(viper.GetString("cf_api_token")) > 0 {
cloudflareAPI, err = cloudflare.NewWithAPIToken(viper.GetString("cf_api_token"))
} else {
cloudflareAPI, err = cloudflare.New(viper.GetString("cf_api_key"), viper.GetString("cf_api_email"))
}
if err != nil {
log.Fatalf("Error creating Cloudflare API client: %s", err)
}
graphqlClient = graphql.NewClient(cfGraphQLEndpoint)
if len(viper.GetString("cf_api_token")) > 0 {
status, err := cloudflareAPI.VerifyAPIToken(context.Background())
if err != nil {
log.Fatalf("Error creating Cloudflare API client: %s", err)
}
log.Debugf("API Token status: %s", status.Status)
}
var 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: %s", err)
}
mustRegisterMetrics(metricsSet)
go func() {
for ; true; <-time.NewTicker(60 * time.Second).C {
go fetchMetrics()
}
}()
goVersion := ""
if goBuildInfo, available := debug.ReadBuildInfo(); available {
goVersion = goBuildInfo.GoVersion
}
buildInfoMetric(version, goVersion, date)
// 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{
ReleaseID: version,
Version: versionString,
})
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() {
if goBuildInfo, available := debug.ReadBuildInfo(); available {
versionString = fmt.Sprintf("%s (built %s with %s)", version, date, goBuildInfo.GoVersion)
}
var cmd = &cobra.Command{
Use: "cloudflare_exporter",
Short: "Export Cloudflare metrics to Prometheus",
Run: func(_ *cobra.Command, _ []string) {
runExporter()
},
Version: versionString,
}
viper.AutomaticEnv()
flags := cmd.Flags()
flags.String("listen", ":8080", "listen on addr:port, omit addr to listen on all interfaces")
viper.BindEnv("listen")
viper.SetDefault("listen", ":8080")
flags.String("metrics_path", "/metrics", "path for 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")
viper.BindEnv("cf_zones")
viper.SetDefault("cf_zones", "")
flags.String("cf_exclude_zones", "", "cloudflare zones to exclude, comma delimited list")
viper.BindEnv("cf_exclude_zones")
viper.SetDefault("cf_exclude_zones", "")
flags.Int("scrape_delay", 300, "scrape delay in seconds")
viper.BindEnv("scrape_delay")
viper.SetDefault("scrape_delay", 300)
flags.Int("cf_batch_size", 10, "cloudflare zones batch size (1-10)")
viper.BindEnv("cf_batch_size")
viper.SetDefault("cf_batch_size", 10)
flags.Bool("free_tier", false, "scrape only metrics included in free plan")
viper.BindEnv("free_tier")
viper.SetDefault("free_tier", false)
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")
viper.BindPFlags(flags)
cmd.Execute()
}