forked from shift/domain_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
253 lines (219 loc) · 7.39 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
package main
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"slices"
"strings"
"time"
"log/slog"
"github.com/alecthomas/kingpin/v2"
"github.com/domainr/whois"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/push"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
"github.com/wwijkander/gotextfsm"
"gopkg.in/yaml.v2"
)
var (
configFile = kingpin.Flag("config", "Domain exporter configuration file.").Default("domains.yml").Envar("CONFIG").String()
templateFile = kingpin.Flag("template", "Registry whois output FSM template file.").Default("whois.textfsm").Envar("CONFIG").String()
pushGateway = kingpin.Flag("pushgateway", "host:port where Pushgateway lives").Default("http://localhost:9091").Envar("CONFIG").String()
debugWhois = kingpin.Flag("debug-whois", "print whois output and skip pushing metrics").Envar("CONFIG").Bool()
domainExpiration = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "domain_expiration_seconds",
Help: "Epoch timestamp when the WHOIS record states this domain will expire",
},
[]string{"domain"},
)
stateConsistent = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "domain_state_desired",
Help: "That the domain is in the configured desired state in registry",
},
[]string{"domain"},
)
parsedSuccessfully = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "domain_information_last_successfully_parsed",
Help: "Last epoch time that the desired domain information was looked up successfully",
},
)
formats = []string{
"2006-01-02",
"2006-01-02T15:04:05Z",
"02-Jan-2006",
"2006.01.02",
"Mon Jan 2 15:04:05 MST 2006",
"02/01/2006",
"2006-01-02 15:04:05 MST",
"2006/01/02",
"Mon Jan 2006 15:04:05",
"2006-01-02 15:04:05-07",
"2006-01-02 15:04:05",
"2.1.2006 15:04:05", // fi.
"02/01/2006 15:04:05",
"02.01.2006", // ax.
"20060102", // br.
}
config promlog.Config
logger log.Logger
)
type Config struct {
Domains []ConfigDomain `yaml:"domains"`
}
type ConfigDomain struct {
Domain string `yaml:"domain"`
Status []string `yaml:"status,omitempty"`
Nameservers []string `yaml:"nameservers,omitempty"`
Dnssec string `yaml:"dnssec,omitempty"`
Registrar string `yaml:"registrar,omitempty"`
}
func main() {
flag.AddFlags(kingpin.CommandLine, &config)
kingpin.Version(version.Print("domain_metric_pusher"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{AddSource: true}))
slog.SetDefault(logger)
log.Println("Starting domain_metric_pusher", "version", version.Info())
log.Println("Build context", version.BuildContext())
prometheus.Register(domainExpiration)
prometheus.Register(stateConsistent)
prometheus.Register(parsedSuccessfully)
config := Config{}
templateFilename, err := filepath.Abs(*templateFile)
if err != nil {
log.Fatalln(err)
}
template, err := ioutil.ReadFile(templateFilename)
if err != nil {
log.Fatalln(err)
}
filename, err := filepath.Abs(*configFile)
if err != nil {
log.Fatalln(err)
}
yamlFile, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatalln(err)
}
err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
log.Fatalln(err)
}
for _, domain := range config.Domains {
req, err := whois.NewRequest(domain.Domain)
if err != nil {
log.Fatalln(err)
}
res, err := whois.DefaultClient.Fetch(req)
if err != nil {
log.Fatalln(err)
}
if *debugWhois {
log.Printf("DEBUG: WHOIS output for : %s\n%s", domain.Domain, res.Body)
}
domainConsistency, date := parse(domain, res.Body, template)
if err != nil {
log.Fatalln(err)
}
log.Printf("Domain %s expires at date %v", domain.Domain, date)
if domainConsistency == 1 {
log.Printf("Domain %s: all other domain settings as expected", domain.Domain)
}
domainExpiration.WithLabelValues(domain.Domain).Set(float64(date.Unix()))
stateConsistent.WithLabelValues(domain.Domain).Set(domainConsistency)
}
parsedSuccessfully.SetToCurrentTime()
log.Println("Successfully collected all data")
if !*debugWhois {
if err := push.New(*pushGateway, "domain_metrics_pusher").
Collector(domainExpiration).
Collector(stateConsistent).
Collector(parsedSuccessfully).
//Grouping("", "").
Push(); err != nil {
log.Fatalln("Could not push metrics to Pushgateway:", err)
}
log.Println("Successfully pushed all data to Pushgateway, done!")
}
return
}
func parse(configDomain ConfigDomain, res []byte, template []byte) (float64, time.Time) {
fsm := gotextfsm.TextFSM{}
err := fsm.ParseString(string(template))
if err != nil {
log.Fatalf("Error while parsing template '%s'\n", err.Error())
}
parser := gotextfsm.ParserOutput{}
err = parser.ParseTextString(string(res), fsm, true)
if err != nil {
log.Fatalf("Error while parsing input '%s'\n", err.Error())
}
var rawDate string
var parametersConsistent float64
parametersConsistent = 1
for _, v := range parser.Dict {
//fmt.Printf("Parsed output: %v\n", v)
rawDate = v["expiryDate"].(string)
//rawDate = strings.TrimSpace(rawDate)
if len(rawDate) < 1 {
log.Fatalf("Domain %s: don't know how to parse domain WHOIS output", configDomain.Domain)
}
configStatus := configDomain.Status
actualStatus := v["status"].([]string)
if len(actualStatus) > 0 && len(configStatus) > 0 {
configStatus = normalizeSlice(configStatus)
actualStatus = normalizeSlice(actualStatus)
if !slices.Equal(configStatus, actualStatus) {
parametersConsistent = 0
log.Printf("Domain %s WHOIS status %s is not the same as configured expected status %s", configDomain.Domain, actualStatus, configStatus)
}
}
if len(configDomain.Dnssec) > 0 && strings.ToLower(configDomain.Dnssec) != strings.ToLower(v["dnssec"].(string)) {
parametersConsistent = 0
log.Printf("Domain %s WHOIS dnssec status %s is not the same as configured expected dnssec status %s", configDomain.Domain, v["dnssec"].(string), configDomain.Dnssec)
}
if len(configDomain.Registrar) > 0 && strings.ToLower(configDomain.Registrar) != strings.ToLower(v["registrar"].(string)) {
parametersConsistent = 0
log.Printf("Domain %s WHOIS registrar %s is not the same as configured expected registrar %s", configDomain.Domain, v["registrar"].(string), configDomain.Registrar)
}
configNameservers := configDomain.Nameservers
actualNameservers := v["nServer"].([]string)
if len(actualNameservers) > 0 && len(configNameservers) > 0 {
configNameservers = normalizeSlice(configNameservers)
actualNameservers = normalizeSlice(actualNameservers)
if !slices.Equal(configNameservers, actualNameservers) {
parametersConsistent = 0
log.Printf("Domain %s WHOIS nameservers %s is not the same as configured expected nameservers %s", configDomain.Domain, actualNameservers, configNameservers)
}
}
}
return parametersConsistent, parseDate(rawDate, configDomain.Domain)
}
func parseDate(rawDate string, domain string) time.Time {
for _, format := range formats {
if date, err := time.Parse(format, rawDate); err == nil {
return date
}
}
log.Fatalf("Domain %s: unable to parse raw date to timestamp: %s\n", domain, rawDate)
return time.Time{}
}
func normalizeSlice(a []string) []string {
var b []string
if len(a) > 0 {
for _, v := range a {
t := strings.ToLower(v)
t = strings.TrimSuffix(t, ".")
b = append(b, t)
}
slices.Sort(b)
}
return b
}