-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
58 lines (49 loc) · 1.3 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/go-co-op/gocron"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func runCronJobs(apiKey, apiEmail, orgId, s3Bucket string, lookBack int) {
s := gocron.NewScheduler(time.UTC)
s.Every(lookBack).Minute().Do(func() {
getAuditLogs(apiKey, apiEmail, orgId, s3Bucket, lookBack)
})
s.StartBlocking()
fmt.Println()
}
func main() {
apiEmail := os.Getenv("CLOUDFLARE_API_EMAIL")
apiKey := os.Getenv("CLOUDFLARE_API_KEY")
orgId := os.Getenv("CLOUDFLARE_ORGANIZATION_ID")
interval := os.Getenv("CLOUDFLARE_LOOK_BACK_INTERVAL")
s3Bucket := os.Getenv("AWS_S3_BUCKET_NAME")
if apiEmail == "" {
log.Fatal("Must specify CLOUDFLARE_API_EMAIL")
}
if apiKey == "" {
log.Fatal("Must specify CLOUDFLARE_API_KEY")
}
if orgId == "" {
log.Fatal("Must specify CLOUDFLARE_ORGANIZATION_ID")
}
// Convert user supplied look back value to an integer otherwise set a default
var lookBack int
if interval != "" {
envToInt, err := getEnvInt(interval)
if err != nil {
log.Fatal("If CLOUDFLARE_LOOK_BACK_INTERVAL it must be an integer")
}
lookBack = envToInt
}
lookBack = 5
http.Handle("/metrics", promhttp.Handler())
go func() {
http.ListenAndServe(":2112", nil)
}()
runCronJobs(apiKey, apiEmail, orgId, s3Bucket, lookBack)
}