forked from DeviaVir/blackbox_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
278 lines (239 loc) · 8.47 KB
/
config.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 config
import (
"errors"
"fmt"
"io/ioutil"
"runtime"
"sync"
"time"
yaml "gopkg.in/yaml.v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/config"
)
var (
configReloadSuccess = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "blackbox_exporter",
Name: "config_last_reload_successful",
Help: "Blackbox exporter config loaded successfully.",
})
configReloadSeconds = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "blackbox_exporter",
Name: "config_last_reload_success_timestamp_seconds",
Help: "Timestamp of the last successful configuration reload.",
})
// DefaultModule set default configuration for the Module
DefaultModule = Module{
HTTP: DefaultHTTPProbe,
TCP: DefaultTCPProbe,
ICMP: DefaultICMPProbe,
DNS: DefaultDNSProbe,
}
// DefaultHTTPProbe set default value for HTTPProbe
DefaultHTTPProbe = HTTPProbe{
IPProtocolFallback: true,
}
// DefaultTCPProbe set default value for TCPProbe
DefaultTCPProbe = TCPProbe{
IPProtocolFallback: true,
}
// DefaultICMPProbe set default value for ICMPProbe
DefaultICMPProbe = ICMPProbe{
IPProtocolFallback: true,
}
// DefaultDNSProbe set default value for DNSProbe
DefaultDNSProbe = DNSProbe{
IPProtocolFallback: true,
}
)
func init() {
prometheus.MustRegister(configReloadSuccess)
prometheus.MustRegister(configReloadSeconds)
}
type Config struct {
Modules map[string]Module `yaml:"modules"`
}
type SafeConfig struct {
sync.RWMutex
C *Config
}
func (sc *SafeConfig) ReloadConfig(confFile string) (err error) {
var c = &Config{}
defer func() {
if err != nil {
configReloadSuccess.Set(0)
} else {
configReloadSuccess.Set(1)
configReloadSeconds.SetToCurrentTime()
}
}()
yamlFile, err := ioutil.ReadFile(confFile)
if err != nil {
return fmt.Errorf("error reading config file: %s", err)
}
if err := yaml.UnmarshalStrict(yamlFile, c); err != nil {
return fmt.Errorf("error parsing config file: %s", err)
}
sc.Lock()
sc.C = c
sc.Unlock()
return nil
}
type Module struct {
Prober string `yaml:"prober,omitempty"`
Timeout time.Duration `yaml:"timeout,omitempty"`
HTTP HTTPProbe `yaml:"http,omitempty"`
TCP TCPProbe `yaml:"tcp,omitempty"`
ICMP ICMPProbe `yaml:"icmp,omitempty"`
DNS DNSProbe `yaml:"dns,omitempty"`
}
type HTTPProbe struct {
// Defaults to 2xx.
ValidStatusCodes []int `yaml:"valid_status_codes,omitempty"`
ValidHTTPVersions []string `yaml:"valid_http_versions,omitempty"`
IPProtocol string `yaml:"preferred_ip_protocol,omitempty"`
IPProtocolFallback bool `yaml:"ip_protocol_fallback,omitempty"`
NoFollowRedirects bool `yaml:"no_follow_redirects,omitempty"`
FailIfSSL bool `yaml:"fail_if_ssl,omitempty"`
FailIfNotSSL bool `yaml:"fail_if_not_ssl,omitempty"`
Method string `yaml:"method,omitempty"`
Headers map[string]string `yaml:"headers,omitempty"`
FailIfBodyMatchesRegexp []string `yaml:"fail_if_body_matches_regexp,omitempty"`
FailIfBodyNotMatchesRegexp []string `yaml:"fail_if_body_not_matches_regexp,omitempty"`
FailIfHeaderMatchesRegexp []HeaderMatch `yaml:"fail_if_header_matches,omitempty"`
FailIfHeaderNotMatchesRegexp []HeaderMatch `yaml:"fail_if_header_not_matches,omitempty"`
Body string `yaml:"body,omitempty"`
HTTPClientConfig config.HTTPClientConfig `yaml:"http_client_config,inline"`
}
type HeaderMatch struct {
Header string `yaml:"header,omitempty"`
Regexp string `yaml:"regexp,omitempty"`
AllowMissing bool `yaml:"allow_missing,omitempty"`
}
type QueryResponse struct {
Expect string `yaml:"expect,omitempty"`
Send string `yaml:"send,omitempty"`
StartTLS bool `yaml:"starttls,omitempty"`
}
type TCPProbe struct {
IPProtocol string `yaml:"preferred_ip_protocol,omitempty"`
IPProtocolFallback bool `yaml:"ip_protocol_fallback,omitempty"`
SourceIPAddress string `yaml:"source_ip_address,omitempty"`
QueryResponse []QueryResponse `yaml:"query_response,omitempty"`
TLS bool `yaml:"tls,omitempty"`
TLSConfig config.TLSConfig `yaml:"tls_config,omitempty"`
}
type ICMPProbe struct {
IPProtocol string `yaml:"preferred_ip_protocol,omitempty"` // Defaults to "ip6".
IPProtocolFallback bool `yaml:"ip_protocol_fallback,omitempty"`
SourceIPAddress string `yaml:"source_ip_address,omitempty"`
PayloadSize int `yaml:"payload_size,omitempty"`
DontFragment bool `yaml:"dont_fragment,omitempty"`
}
type DNSProbe struct {
IPProtocol string `yaml:"preferred_ip_protocol,omitempty"`
IPProtocolFallback bool `yaml:"ip_protocol_fallback,omitempty"`
SourceIPAddress string `yaml:"source_ip_address,omitempty"`
TransportProtocol string `yaml:"transport_protocol,omitempty"`
QueryName string `yaml:"query_name,omitempty"`
QueryType string `yaml:"query_type,omitempty"` // Defaults to ANY.
ValidRcodes []string `yaml:"valid_rcodes,omitempty"` // Defaults to NOERROR.
ValidateAnswer DNSRRValidator `yaml:"validate_answer_rrs,omitempty"`
ValidateAuthority DNSRRValidator `yaml:"validate_authority_rrs,omitempty"`
ValidateAdditional DNSRRValidator `yaml:"validate_additional_rrs,omitempty"`
}
type DNSRRValidator struct {
FailIfMatchesRegexp []string `yaml:"fail_if_matches_regexp,omitempty"`
FailIfNotMatchesRegexp []string `yaml:"fail_if_not_matches_regexp,omitempty"`
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (s *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain Config
if err := unmarshal((*plain)(s)); err != nil {
return err
}
return nil
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (s *Module) UnmarshalYAML(unmarshal func(interface{}) error) error {
*s = DefaultModule
type plain Module
if err := unmarshal((*plain)(s)); err != nil {
return err
}
return nil
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (s *HTTPProbe) UnmarshalYAML(unmarshal func(interface{}) error) error {
*s = DefaultHTTPProbe
type plain HTTPProbe
if err := unmarshal((*plain)(s)); err != nil {
return err
}
if err := s.HTTPClientConfig.Validate(); err != nil {
return err
}
return nil
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (s *DNSProbe) UnmarshalYAML(unmarshal func(interface{}) error) error {
*s = DefaultDNSProbe
type plain DNSProbe
if err := unmarshal((*plain)(s)); err != nil {
return err
}
if s.QueryName == "" {
return errors.New("query name must be set for DNS module")
}
return nil
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (s *TCPProbe) UnmarshalYAML(unmarshal func(interface{}) error) error {
*s = DefaultTCPProbe
type plain TCPProbe
if err := unmarshal((*plain)(s)); err != nil {
return err
}
return nil
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (s *DNSRRValidator) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain DNSRRValidator
if err := unmarshal((*plain)(s)); err != nil {
return err
}
return nil
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (s *ICMPProbe) UnmarshalYAML(unmarshal func(interface{}) error) error {
*s = DefaultICMPProbe
type plain ICMPProbe
if err := unmarshal((*plain)(s)); err != nil {
return err
}
if runtime.GOOS == "windows" && s.DontFragment {
return errors.New("\"dont_fragment\" is not supported on windows platforms")
}
return nil
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (s *QueryResponse) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain QueryResponse
if err := unmarshal((*plain)(s)); err != nil {
return err
}
return nil
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (s *HeaderMatch) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain HeaderMatch
if err := unmarshal((*plain)(s)); err != nil {
return err
}
if s.Header == "" {
return errors.New("header name must be set for HTTP header matchers")
}
if s.Regexp == "" {
return errors.New("regexp must be set for HTTP header matchers")
}
return nil
}