This repository has been archived by the owner on Jun 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (75 loc) · 1.93 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
package main
import (
"flag"
"fmt"
"os"
"time"
"github.com/DataDog/datadog-go/statsd"
log "github.com/sirupsen/logrus"
)
func init() {
// log json because cloud
if os.Getenv("ENVIRONMENT") == "production" {
log.SetFormatter(&log.JSONFormatter{})
}
if os.Getenv("DEBUG") != "" {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
log.SetOutput(os.Stdout)
}
func main() {
var (
project string
)
// cli flags
flag.StringVar(&project, "project", "", "Google Cloud Project")
flag.Parse()
if project == "" {
fmt.Println("Usage:\ngo-check-dns -project gcp-project")
os.Exit(1)
}
// start datadog client
c, err := statsd.New(os.Getenv("DATADOG_URL"))
if err != nil {
log.Fatalf("datadog could not start: %v", err)
}
log.Infof("Logging to: %s", os.Getenv("DATADOG_URL"))
// add stat namespace
c.Namespace = "go-check-dns."
// get domains from Google Cloud DNS
log.Infoln("Fetching domains...")
domains := getZones(project)
// iterate over the domains and do things
log.Debugf("Performing WHOIS on: %s", domains)
for _, domain := range domains {
// get domain expiry
log.Debugf("Looking up domain: %s", domain)
expiry, err := lookup(domain)
if err != nil {
// skip this domain if we can't look it up
log.Errorf("Could not get expiry for %s: %v", domain, err)
break
}
// calculate time and debug
timeDiff := expiry.Sub(time.Now())
daysLeft := float64(timeDiff.Hours() / 24)
log.WithFields(log.Fields{
"domain": domain,
"expiry": expiry,
"days left": daysLeft,
}).Debug("Domain Parsed!")
// submit datadog metric
var tags []string
// idk if a tag is good here or not
domainTag := "domain:" + domain
tags = append(tags, domainTag)
// run the gauge
err = c.Gauge("domain.expiration", daysLeft, tags, 1)
if err != nil {
log.Fatalf("Metric failed: %s %s %.0f", domain, tags, daysLeft)
}
}
log.Infof("Finished! Domains checked: %d", len(domains))
}