forked from lablabs/cloudflare-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
115 lines (97 loc) · 3.04 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
package main
import (
"net/http"
"os"
"strings"
"sync"
"time"
cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/namsral/flag"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
var (
cfgListen = ":8080"
cfgCfAPIKey = ""
cfgCfAPIEmail = ""
cfgCfAPIToken = ""
cfgMetricsPath = "/metrics"
cfgZones = ""
)
func getTargetZones() []string {
var zoneIDs []string
if len(cfgZones) > 0 {
zoneIDs = strings.Split(cfgZones, ",")
} else {
//depricated
for _, e := range os.Environ() {
if strings.HasPrefix(e, "ZONE_") {
split := strings.SplitN(e, "=", 2)
zoneIDs = append(zoneIDs, split[1])
}
}
}
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 fetchMetrics() {
var wg sync.WaitGroup
zones := fetchZones()
filteredZones := filterZones(zones, getTargetZones())
// Make requests in groups of 10 to avoid rate limit
// 10 is the maximum amount of zones you can request at once
for len(filteredZones) > 0 {
sliceLength := 10
if len(filteredZones) < 10 {
sliceLength = len(filteredZones)
}
targetZones := filteredZones[:sliceLength]
filteredZones = filteredZones[len(targetZones):]
go fetchZoneAnalytics(targetZones, &wg)
go fetchZoneColocationAnalytics(targetZones, &wg)
}
wg.Wait()
}
func main() {
flag.StringVar(&cfgListen, "listen", cfgListen, "listen on addr:port ( default :8080), omit addr to listen on all interfaces")
flag.StringVar(&cfgMetricsPath, "metrics_path", cfgMetricsPath, "path for metrics, default /metrics")
flag.StringVar(&cfgCfAPIKey, "cf_api_key", cfgCfAPIKey, "cloudflare api key, works with api_email flag")
flag.StringVar(&cfgCfAPIEmail, "cf_api_email", cfgCfAPIEmail, "cloudflare api email, works with api_key flag")
flag.StringVar(&cfgCfAPIToken, "cf_api_token", cfgCfAPIToken, "cloudflare api token (preferred)")
flag.StringVar(&cfgZones, "cf_zones", cfgZones, "cloudflare zones to export, comma delimited list")
flag.Parse()
if !(len(cfgCfAPIToken) > 0 || (len(cfgCfAPIEmail) > 0 && len(cfgCfAPIKey) > 0)) {
log.Fatal("Please provide CF_API_KEY+CF_API_EMAIL or CF_API_TOKEN")
}
customFormatter := new(log.TextFormatter)
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
log.SetFormatter(customFormatter)
customFormatter.FullTimestamp = true
go func() {
for ; true; <-time.NewTicker(60 * time.Second).C {
go fetchMetrics()
}
}()
//This section will start the HTTP server and expose
//any metrics on the /metrics endpoint.
if !strings.HasPrefix(cfgMetricsPath, "/") {
cfgMetricsPath = "/" + cfgMetricsPath
}
http.Handle(cfgMetricsPath, promhttp.Handler())
log.Info("Beginning to serve on port", cfgListen, ", metrics path ", cfgMetricsPath)
log.Fatal(http.ListenAndServe(cfgListen, nil))
}