From de0fa8bb9fc7400b0f65628b42a99d2d61940b73 Mon Sep 17 00:00:00 2001 From: Johannes 'fish' Ziemke Date: Fri, 1 Sep 2017 17:01:22 +0200 Subject: [PATCH] Move prober and config to own package (#214) --- config.go => config/config.go | 24 ++- config_test.go => config/config_test.go | 12 +- .../testdata}/blackbox-bad.yml | 0 .../testdata}/blackbox-good.yml | 0 .../testdata}/invalid-dns-module.yml | 0 main.go | 54 +++---- main_test.go | 44 ++++++ dns.go => prober/dns.go | 8 +- dns_test.go => prober/dns_test.go | 100 ++++++------- http.go => prober/http.go | 12 +- http_test.go => prober/http_test.go | 138 +++++++----------- icmp.go => prober/icmp.go | 6 +- prober/prober.go | 11 ++ tcp.go => prober/tcp.go | 12 +- tcp_test.go => prober/tcp_test.go | 66 +++++---- tls.go => prober/tls.go | 2 +- utils.go => prober/utils.go | 2 +- utils_test.go => prober/utils_test.go | 2 +- 18 files changed, 267 insertions(+), 226 deletions(-) rename config.go => config/config.go (93%) rename config_test.go => config/config_test.go (76%) rename {testdata => config/testdata}/blackbox-bad.yml (100%) rename {testdata => config/testdata}/blackbox-good.yml (100%) rename {testdata => config/testdata}/invalid-dns-module.yml (100%) create mode 100644 main_test.go rename dns.go => prober/dns.go (95%) rename dns_test.go => prober/dns_test.go (86%) rename http.go => prober/http.go (94%) rename http_test.go => prober/http_test.go (75%) rename icmp.go => prober/icmp.go (94%) create mode 100644 prober/prober.go rename tcp.go => prober/tcp.go (90%) rename tcp_test.go => prober/tcp_test.go (85%) rename tls.go => prober/tls.go (98%) rename utils.go => prober/utils.go (98%) rename utils_test.go => prober/utils_test.go (97%) diff --git a/config.go b/config/config.go similarity index 93% rename from config.go rename to config/config.go index 3d1d324..5d4baf4 100644 --- a/config.go +++ b/config/config.go @@ -1,12 +1,15 @@ -package main +package config import ( "errors" "fmt" + "io/ioutil" "strings" "sync" "time" + yaml "gopkg.in/yaml.v2" + "github.com/prometheus/common/config" ) @@ -22,6 +25,25 @@ type SafeConfig struct { C *Config } +func (sc *SafeConfig) ReloadConfig(confFile string) (err error) { + var c = &Config{} + + yamlFile, err := ioutil.ReadFile(confFile) + if err != nil { + return fmt.Errorf("Error reading config file: %s", err) + } + + if err := yaml.Unmarshal(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"` diff --git a/config_test.go b/config/config_test.go similarity index 76% rename from config_test.go rename to config/config_test.go index 1379098..4042994 100644 --- a/config_test.go +++ b/config/config_test.go @@ -1,4 +1,4 @@ -package main +package config import ( "strings" @@ -12,7 +12,7 @@ func TestLoadConfig(t *testing.T) { C: &Config{}, } - err := sc.reloadConfig("testdata/blackbox-good.yml") + err := sc.ReloadConfig("testdata/blackbox-good.yml") if err != nil { t.Errorf("Error loading config %v: %v", "blackbox.yml", err) } @@ -28,15 +28,15 @@ func TestLoadBadConfigs(t *testing.T) { }{ { ConfigFile: "testdata/blackbox-bad.yml", - ExpectedError: "unknown fields in dns probe: invalid_extra_field", + ExpectedError: "Error parsing config file: unknown fields in dns probe: invalid_extra_field", }, { ConfigFile: "testdata/invalid-dns-module.yml", - ExpectedError: "Query name must be set for DNS module", + ExpectedError: "Error parsing config file: Query name must be set for DNS module", }, } for i, test := range tests { - err := sc.reloadConfig(test.ConfigFile) + err := sc.ReloadConfig(test.ConfigFile) if err.Error() != test.ExpectedError { t.Errorf("In case %v:\nExpected:\n%v\nGot:\n%v", i, test.ExpectedError, err.Error()) } @@ -48,7 +48,7 @@ func TestHideConfigSecrets(t *testing.T) { C: &Config{}, } - err := sc.reloadConfig("testdata/blackbox-good.yml") + err := sc.ReloadConfig("testdata/blackbox-good.yml") if err != nil { t.Errorf("Error loading config %v: %v", "testdata/blackbox-good.yml", err) } diff --git a/testdata/blackbox-bad.yml b/config/testdata/blackbox-bad.yml similarity index 100% rename from testdata/blackbox-bad.yml rename to config/testdata/blackbox-bad.yml diff --git a/testdata/blackbox-good.yml b/config/testdata/blackbox-good.yml similarity index 100% rename from testdata/blackbox-good.yml rename to config/testdata/blackbox-good.yml diff --git a/testdata/invalid-dns-module.yml b/config/testdata/invalid-dns-module.yml similarity index 100% rename from testdata/invalid-dns-module.yml rename to config/testdata/invalid-dns-module.yml diff --git a/main.go b/main.go index 1645976..bb79c03 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,6 @@ package main import ( "context" "fmt" - "io/ioutil" "net/http" "os" "os/signal" @@ -31,48 +30,29 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/log" "github.com/prometheus/common/version" + + "github.com/prometheus/blackbox_exporter/config" + "github.com/prometheus/blackbox_exporter/prober" ) var ( - sc = &SafeConfig{ - C: &Config{}, + sc = &config.SafeConfig{ + C: &config.Config{}, } configFile = kingpin.Flag("config.file", "Blackbox exporter configuration file.").Default("blackbox.yml").String() listenAddress = kingpin.Flag("web.listen-address", "The address to listen on for HTTP requests.").Default(":9115").String() timeoutOffset = kingpin.Flag("timeout-offset", "Offset to subtract from timeout in seconds.").Default("0.5").Float64() -) - -var Probers = map[string]func(context.Context, string, Module, *prometheus.Registry) bool{ - "http": probeHTTP, - "tcp": probeTCP, - "icmp": probeICMP, - "dns": probeDNS, -} - -func (sc *SafeConfig) reloadConfig(confFile string) (err error) { - var c = &Config{} - - yamlFile, err := ioutil.ReadFile(confFile) - if err != nil { - log.Errorf("Error reading config file: %s", err) - return err - } - if err := yaml.Unmarshal(yamlFile, c); err != nil { - log.Errorf("Error parsing config file: %s", err) - return err + Probers = map[string]prober.ProbeFn{ + "http": prober.ProbeHTTP, + "tcp": prober.ProbeTCP, + "icmp": prober.ProbeICMP, + "dns": prober.ProbeDNS, } +) - sc.Lock() - sc.C = c - sc.Unlock() - - log.Infoln("Loaded config file") - return nil -} - -func probeHandler(w http.ResponseWriter, r *http.Request, c *Config) { +func probeHandler(w http.ResponseWriter, r *http.Request, c *config.Config) { moduleName := r.URL.Query().Get("module") if moduleName == "" { moduleName = "http_2xx" @@ -151,9 +131,10 @@ func main() { log.Infoln("Starting blackbox_exporter", version.Info()) log.Infoln("Build context", version.BuildContext()) - if err := sc.reloadConfig(*configFile); err != nil { + if err := sc.ReloadConfig(*configFile); err != nil { log.Fatalf("Error loading config: %s", err) } + log.Infoln("Loaded config file") hup := make(chan os.Signal) reloadCh := make(chan chan error) @@ -162,14 +143,17 @@ func main() { for { select { case <-hup: - if err := sc.reloadConfig(*configFile); err != nil { + if err := sc.ReloadConfig(*configFile); err != nil { log.Errorf("Error reloading config: %s", err) + continue } + log.Infoln("Loaded config file") case rc := <-reloadCh: - if err := sc.reloadConfig(*configFile); err != nil { + if err := sc.ReloadConfig(*configFile); err != nil { log.Errorf("Error reloading config: %s", err) rc <- err } else { + log.Infoln("Loaded config file") rc <- nil } } diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..4665ef5 --- /dev/null +++ b/main_test.go @@ -0,0 +1,44 @@ +package main + +import ( + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/prometheus/blackbox_exporter/config" +) + +var c = &config.Config{ + Modules: map[string]config.Module{ + "http_2xx": config.Module{ + Prober: "http", + Timeout: 10 * time.Second, + }, + }, +} + +func TestPrometheusTimeoutHTTP(t *testing.T) { + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + time.Sleep(2 * time.Second) + })) + defer ts.Close() + + req, err := http.NewRequest("GET", "?target="+ts.URL, nil) + if err != nil { + t.Fatal(err) + } + req.Header.Set("X-Prometheus-Scrape-Timeout-Seconds", "1") + + rr := httptest.NewRecorder() + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + probeHandler(w, r, c) + }) + + handler.ServeHTTP(rr, req) + + if status := rr.Code; status != http.StatusOK { + t.Errorf("probe request handler returned wrong status code: %v, want %v", status, http.StatusOK) + } +} diff --git a/dns.go b/prober/dns.go similarity index 95% rename from dns.go rename to prober/dns.go index 97c5ff2..2a7f0d9 100644 --- a/dns.go +++ b/prober/dns.go @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package main +package prober import ( "context" @@ -22,10 +22,12 @@ import ( "github.com/miekg/dns" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/common/log" + + "github.com/prometheus/blackbox_exporter/config" ) // validRRs checks a slice of RRs received from the server against a DNSRRValidator. -func validRRs(rrs *[]dns.RR, v *DNSRRValidator) bool { +func validRRs(rrs *[]dns.RR, v *config.DNSRRValidator) bool { // Fail the probe if there are no RRs of a given type, but a regexp match is required // (i.e. FailIfNotMatchesRegexp is set). if len(*rrs) == 0 && len(v.FailIfNotMatchesRegexp) > 0 { @@ -82,7 +84,7 @@ func validRcode(rcode int, valid []string) bool { return false } -func probeDNS(ctx context.Context, target string, module Module, registry *prometheus.Registry) bool { +func ProbeDNS(ctx context.Context, target string, module config.Module, registry *prometheus.Registry) bool { var numAnswer, numAuthority, numAdditional int var dialProtocol string probeDNSAnswerRRSGauge := prometheus.NewGauge(prometheus.GaugeOpts{ diff --git a/dns_test.go b/prober/dns_test.go similarity index 86% rename from dns_test.go rename to prober/dns_test.go index 93113b2..6425185 100644 --- a/dns_test.go +++ b/prober/dns_test.go @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package main +package prober import ( "context" @@ -22,6 +22,8 @@ import ( "github.com/miekg/dns" "github.com/prometheus/client_golang/prometheus" + + "github.com/prometheus/blackbox_exporter/config" ) var PROTOCOLS = [...]string{"udp", "tcp"} @@ -70,41 +72,41 @@ func recursiveDNSHandler(w dns.ResponseWriter, r *dns.Msg) { func TestRecursiveDNSResponse(t *testing.T) { tests := []struct { - Probe DNSProbe + Probe config.DNSProbe ShouldSucceed bool }{ { - DNSProbe{ + config.DNSProbe{ QueryName: "example.com", }, true, }, { - DNSProbe{ + config.DNSProbe{ QueryName: "example.com", ValidRcodes: []string{"SERVFAIL", "NXDOMAIN"}, }, false, }, { - DNSProbe{ + config.DNSProbe{ QueryName: "example.com", - ValidateAnswer: DNSRRValidator{ + ValidateAnswer: config.DNSRRValidator{ FailIfMatchesRegexp: []string{".*7200.*"}, FailIfNotMatchesRegexp: []string{".*3600.*"}, }, }, true, }, { - DNSProbe{ + config.DNSProbe{ QueryName: "example.com", - ValidateAuthority: DNSRRValidator{ + ValidateAuthority: config.DNSRRValidator{ FailIfMatchesRegexp: []string{".*7200.*"}, }, }, true, }, { - DNSProbe{ + config.DNSProbe{ QueryName: "example.com", - ValidateAdditional: DNSRRValidator{ + ValidateAdditional: config.DNSRRValidator{ FailIfNotMatchesRegexp: []string{".*3600.*"}, }, }, false, @@ -122,7 +124,7 @@ func TestRecursiveDNSResponse(t *testing.T) { testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result := probeDNS(testCTX, addr.String(), Module{Timeout: time.Second, DNS: test.Probe}, registry) + result := ProbeDNS(testCTX, addr.String(), config.Module{Timeout: time.Second, DNS: test.Probe}, registry) if result != test.ShouldSucceed { t.Fatalf("Test %d had unexpected result: %v", i, result) } @@ -182,58 +184,58 @@ func authoritativeDNSHandler(w dns.ResponseWriter, r *dns.Msg) { func TestAuthoritativeDNSResponse(t *testing.T) { tests := []struct { - Probe DNSProbe + Probe config.DNSProbe ShouldSucceed bool }{ { - DNSProbe{ + config.DNSProbe{ QueryName: "example.com", }, true, }, { - DNSProbe{ + config.DNSProbe{ QueryName: "example.com", ValidRcodes: []string{"SERVFAIL", "NXDOMAIN"}, }, false, }, { - DNSProbe{ + config.DNSProbe{ QueryName: "example.com", - ValidateAnswer: DNSRRValidator{ + ValidateAnswer: config.DNSRRValidator{ FailIfMatchesRegexp: []string{".*3600.*"}, FailIfNotMatchesRegexp: []string{".*3600.*"}, }, }, false, }, { - DNSProbe{ + config.DNSProbe{ QueryName: "example.com", - ValidateAnswer: DNSRRValidator{ + ValidateAnswer: config.DNSRRValidator{ FailIfMatchesRegexp: []string{".*7200.*"}, FailIfNotMatchesRegexp: []string{".*7200.*"}, }, }, false, }, { - DNSProbe{ + config.DNSProbe{ QueryName: "example.com", - ValidateAuthority: DNSRRValidator{ + ValidateAuthority: config.DNSRRValidator{ FailIfNotMatchesRegexp: []string{"ns.*.isp.net"}, }, }, true, }, { - DNSProbe{ + config.DNSProbe{ QueryName: "example.com", - ValidateAdditional: DNSRRValidator{ + ValidateAdditional: config.DNSRRValidator{ FailIfNotMatchesRegexp: []string{"^ns.*.isp"}, }, }, true, }, { - DNSProbe{ + config.DNSProbe{ QueryName: "example.com", - ValidateAdditional: DNSRRValidator{ + ValidateAdditional: config.DNSRRValidator{ FailIfMatchesRegexp: []string{"^ns.*.isp"}, }, }, false, @@ -249,7 +251,7 @@ func TestAuthoritativeDNSResponse(t *testing.T) { registry := prometheus.NewRegistry() testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result := probeDNS(testCTX, addr.String(), Module{Timeout: time.Second, DNS: test.Probe}, registry) + result := ProbeDNS(testCTX, addr.String(), config.Module{Timeout: time.Second, DNS: test.Probe}, registry) if result != test.ShouldSucceed { t.Fatalf("Test %d had unexpected result: %v", i, result) } @@ -269,28 +271,28 @@ func TestAuthoritativeDNSResponse(t *testing.T) { func TestServfailDNSResponse(t *testing.T) { tests := []struct { - Probe DNSProbe + Probe config.DNSProbe ShouldSucceed bool }{ { - DNSProbe{ + config.DNSProbe{ QueryName: "example.com", }, false, }, { - DNSProbe{ + config.DNSProbe{ QueryName: "example.com", ValidRcodes: []string{"SERVFAIL", "NXDOMAIN"}, }, true, }, { - DNSProbe{ + config.DNSProbe{ QueryName: "example.com", QueryType: "NOT_A_VALID_QUERY_TYPE", }, false, }, { - DNSProbe{ + config.DNSProbe{ QueryName: "example.com", ValidRcodes: []string{"NOT_A_VALID_RCODE"}, }, false, @@ -307,7 +309,7 @@ func TestServfailDNSResponse(t *testing.T) { registry := prometheus.NewRegistry() testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result := probeDNS(testCTX, addr.String(), Module{Timeout: time.Second, DNS: test.Probe}, registry) + result := ProbeDNS(testCTX, addr.String(), config.Module{Timeout: time.Second, DNS: test.Probe}, registry) if result != test.ShouldSucceed { t.Fatalf("Test %d had unexpected result: %v", i, result) } @@ -344,9 +346,9 @@ func TestDNSProtocol(t *testing.T) { _, port, _ := net.SplitHostPort(addr.String()) // Force IPv4 - module := Module{ + module := config.Module{ Timeout: time.Second, - DNS: DNSProbe{ + DNS: config.DNSProbe{ QueryName: "example.com", TransportProtocol: protocol, PreferredIPProtocol: "ip4", @@ -355,7 +357,7 @@ func TestDNSProtocol(t *testing.T) { registry := prometheus.NewRegistry() testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result := probeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry) + result := ProbeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry) if !result { t.Fatalf("DNS protocol: \"%v4\" connection test failed, expected success.", protocol) } @@ -370,9 +372,9 @@ func TestDNSProtocol(t *testing.T) { checkRegistryResults(expectedResults, mfs, t) // Force IPv6 - module = Module{ + module = config.Module{ Timeout: time.Second, - DNS: DNSProbe{ + DNS: config.DNSProbe{ QueryName: "example.com", TransportProtocol: protocol, PreferredIPProtocol: "ip6", @@ -381,7 +383,7 @@ func TestDNSProtocol(t *testing.T) { registry = prometheus.NewRegistry() testCTX, cancel = context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result = probeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry) + result = ProbeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry) if !result { t.Fatalf("DNS protocol: \"%v6\" connection test failed, expected success.", protocol) } @@ -395,9 +397,9 @@ func TestDNSProtocol(t *testing.T) { checkRegistryResults(expectedResults, mfs, t) // Prefer IPv6 - module = Module{ + module = config.Module{ Timeout: time.Second, - DNS: DNSProbe{ + DNS: config.DNSProbe{ QueryName: "example.com", TransportProtocol: protocol, PreferredIPProtocol: "ip6", @@ -406,7 +408,7 @@ func TestDNSProtocol(t *testing.T) { registry = prometheus.NewRegistry() testCTX, cancel = context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result = probeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry) + result = ProbeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry) if !result { t.Fatalf("DNS protocol: \"%v\", preferred \"ip6\" connection test failed, expected success.", protocol) } @@ -420,9 +422,9 @@ func TestDNSProtocol(t *testing.T) { checkRegistryResults(expectedResults, mfs, t) // Prefer IPv4 - module = Module{ + module = config.Module{ Timeout: time.Second, - DNS: DNSProbe{ + DNS: config.DNSProbe{ QueryName: "example.com", TransportProtocol: protocol, PreferredIPProtocol: "ip4", @@ -431,7 +433,7 @@ func TestDNSProtocol(t *testing.T) { registry = prometheus.NewRegistry() testCTX, cancel = context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result = probeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry) + result = ProbeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry) if !result { t.Fatalf("DNS protocol: \"%v\", preferred \"ip4\" connection test failed, expected success.", protocol) } @@ -446,9 +448,9 @@ func TestDNSProtocol(t *testing.T) { checkRegistryResults(expectedResults, mfs, t) // Prefer none - module = Module{ + module = config.Module{ Timeout: time.Second, - DNS: DNSProbe{ + DNS: config.DNSProbe{ QueryName: "example.com", TransportProtocol: protocol, }, @@ -456,7 +458,7 @@ func TestDNSProtocol(t *testing.T) { registry = prometheus.NewRegistry() testCTX, cancel = context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result = probeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry) + result = ProbeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry) if !result { t.Fatalf("DNS protocol: \"%v\" connection test failed, expected success.", protocol) } @@ -471,16 +473,16 @@ func TestDNSProtocol(t *testing.T) { checkRegistryResults(expectedResults, mfs, t) // No protocol - module = Module{ + module = config.Module{ Timeout: time.Second, - DNS: DNSProbe{ + DNS: config.DNSProbe{ QueryName: "example.com", }, } registry = prometheus.NewRegistry() testCTX, cancel = context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result = probeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry) + result = ProbeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry) if protocol == "udp" { if !result { t.Fatalf("DNS test connection with protocol %s failed, expected success.", protocol) diff --git a/http.go b/prober/http.go similarity index 94% rename from http.go rename to prober/http.go index bd1f37a..39dbfa0 100644 --- a/http.go +++ b/prober/http.go @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package main +package prober import ( "context" @@ -26,11 +26,13 @@ import ( "strings" "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/common/config" + pconfig "github.com/prometheus/common/config" "github.com/prometheus/common/log" + + "github.com/prometheus/blackbox_exporter/config" ) -func matchRegularExpressions(reader io.Reader, httpConfig HTTPProbe) bool { +func matchRegularExpressions(reader io.Reader, httpConfig config.HTTPProbe) bool { body, err := ioutil.ReadAll(reader) if err != nil { log.Errorf("Error reading HTTP body: %s", err) @@ -59,7 +61,7 @@ func matchRegularExpressions(reader io.Reader, httpConfig HTTPProbe) bool { return true } -func probeHTTP(ctx context.Context, target string, module Module, registry *prometheus.Registry) (success bool) { +func ProbeHTTP(ctx context.Context, target string, module config.Module, registry *prometheus.Registry) (success bool) { var redirects int var ( contentLengthGauge = prometheus.NewGauge(prometheus.GaugeOpts{ @@ -128,7 +130,7 @@ func probeHTTP(ctx context.Context, target string, module Module, registry *prom httpClientConfig := &module.HTTP.HTTPClientConfig - client, err := config.NewHTTPClientFromConfig(httpClientConfig) + client, err := pconfig.NewHTTPClientFromConfig(httpClientConfig) if err != nil { log.Errorf("Error generating HTTP client: %v", err) return false diff --git a/http_test.go b/prober/http_test.go similarity index 75% rename from http_test.go rename to prober/http_test.go index 5e0a632..43b9e31 100644 --- a/http_test.go +++ b/prober/http_test.go @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package main +package prober import ( "context" @@ -23,7 +23,9 @@ import ( "time" "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/common/config" + pconfig "github.com/prometheus/common/config" + + "github.com/prometheus/blackbox_exporter/config" ) func TestHTTPStatusCodes(t *testing.T) { @@ -52,8 +54,8 @@ func TestHTTPStatusCodes(t *testing.T) { recorder := httptest.NewRecorder() testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result := probeHTTP(testCTX, ts.URL, - Module{Timeout: time.Second, HTTP: HTTPProbe{ValidStatusCodes: test.ValidStatusCodes}}, registry) + result := ProbeHTTP(testCTX, ts.URL, + config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{ValidStatusCodes: test.ValidStatusCodes}}, registry) body := recorder.Body.String() if result != test.ShouldSucceed { t.Fatalf("Test %d had unexpected result: %s", i, body) @@ -77,8 +79,8 @@ func TestValidHTTPVersion(t *testing.T) { defer ts.Close() recorder := httptest.NewRecorder() registry := prometheus.NewRegistry() - result := probeHTTP(context.Background(), ts.URL, - Module{Timeout: time.Second, HTTP: HTTPProbe{ + result := ProbeHTTP(context.Background(), ts.URL, + config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{ ValidHTTPVersions: test.ValidHTTPVersions, }}, registry) body := recorder.Body.String() @@ -101,7 +103,7 @@ func TestRedirectFollowed(t *testing.T) { registry := prometheus.NewRegistry() testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result := probeHTTP(testCTX, ts.URL, Module{Timeout: time.Second, HTTP: HTTPProbe{}}, registry) + result := ProbeHTTP(testCTX, ts.URL, config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{}}, registry) body := recorder.Body.String() if !result { t.Fatalf("Redirect test failed unexpectedly, got %s", body) @@ -128,8 +130,8 @@ func TestRedirectNotFollowed(t *testing.T) { registry := prometheus.NewRegistry() testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result := probeHTTP(testCTX, ts.URL, - Module{Timeout: time.Second, HTTP: HTTPProbe{NoFollowRedirects: true, ValidStatusCodes: []int{302}}}, registry) + result := ProbeHTTP(testCTX, ts.URL, + config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{NoFollowRedirects: true, ValidStatusCodes: []int{302}}}, registry) body := recorder.Body.String() if !result { t.Fatalf("Redirect test failed unexpectedly, got %s", body) @@ -149,8 +151,8 @@ func TestPost(t *testing.T) { registry := prometheus.NewRegistry() testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result := probeHTTP(testCTX, ts.URL, - Module{Timeout: time.Second, HTTP: HTTPProbe{Method: "POST"}}, registry) + result := ProbeHTTP(testCTX, ts.URL, + config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{Method: "POST"}}, registry) body := recorder.Body.String() if !result { t.Fatalf("Post test failed unexpectedly, got %s", body) @@ -166,11 +168,11 @@ func TestBasicAuth(t *testing.T) { registry := prometheus.NewRegistry() testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result := probeHTTP(testCTX, ts.URL, - Module{Timeout: time.Second, HTTP: HTTPProbe{ - HTTPClientConfig: config.HTTPClientConfig{ - TLSConfig: config.TLSConfig{InsecureSkipVerify: false}, - BasicAuth: &config.BasicAuth{Username: "username", Password: "password"}, + result := ProbeHTTP(testCTX, ts.URL, + config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{ + HTTPClientConfig: pconfig.HTTPClientConfig{ + TLSConfig: pconfig.TLSConfig{InsecureSkipVerify: false}, + BasicAuth: &pconfig.BasicAuth{Username: "username", Password: "password"}, }, }}, registry) body := recorder.Body.String() @@ -188,10 +190,10 @@ func TestBearerToken(t *testing.T) { registry := prometheus.NewRegistry() testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result := probeHTTP(testCTX, ts.URL, - Module{Timeout: time.Second, HTTP: HTTPProbe{ - HTTPClientConfig: config.HTTPClientConfig{ - BearerToken: config.Secret("mysecret"), + result := ProbeHTTP(testCTX, ts.URL, + config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{ + HTTPClientConfig: pconfig.HTTPClientConfig{ + BearerToken: pconfig.Secret("mysecret"), }, }}, registry) body := recorder.Body.String() @@ -209,8 +211,8 @@ func TestFailIfNotSSL(t *testing.T) { registry := prometheus.NewRegistry() testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result := probeHTTP(testCTX, ts.URL, - Module{Timeout: time.Second, HTTP: HTTPProbe{FailIfNotSSL: true}}, registry) + result := ProbeHTTP(testCTX, ts.URL, + config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{FailIfNotSSL: true}}, registry) body := recorder.Body.String() if result { t.Fatalf("Fail if not SSL test suceeded unexpectedly, got %s", body) @@ -235,8 +237,8 @@ func TestFailIfMatchesRegexp(t *testing.T) { registry := prometheus.NewRegistry() testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result := probeHTTP(testCTX, ts.URL, - Module{Timeout: time.Second, HTTP: HTTPProbe{FailIfMatchesRegexp: []string{"could not connect to database"}}}, registry) + result := ProbeHTTP(testCTX, ts.URL, + config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{FailIfMatchesRegexp: []string{"could not connect to database"}}}, registry) body := recorder.Body.String() if result { t.Fatalf("Regexp test succeeded unexpectedly, got %s", body) @@ -257,8 +259,8 @@ func TestFailIfMatchesRegexp(t *testing.T) { recorder = httptest.NewRecorder() registry = prometheus.NewRegistry() - result = probeHTTP(testCTX, ts.URL, - Module{Timeout: time.Second, HTTP: HTTPProbe{FailIfMatchesRegexp: []string{"could not connect to database"}}}, registry) + result = ProbeHTTP(testCTX, ts.URL, + config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{FailIfMatchesRegexp: []string{"could not connect to database"}}}, registry) body = recorder.Body.String() if !result { t.Fatalf("Regexp test failed unexpectedly, got %s", body) @@ -281,8 +283,8 @@ func TestFailIfMatchesRegexp(t *testing.T) { recorder = httptest.NewRecorder() registry = prometheus.NewRegistry() - result = probeHTTP(testCTX, ts.URL, - Module{Timeout: time.Second, HTTP: HTTPProbe{FailIfMatchesRegexp: []string{"could not connect to database", "internal error"}}}, registry) + result = ProbeHTTP(testCTX, ts.URL, + config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{FailIfMatchesRegexp: []string{"could not connect to database", "internal error"}}}, registry) body = recorder.Body.String() if result { t.Fatalf("Regexp test succeeded unexpectedly, got %s", body) @@ -295,8 +297,8 @@ func TestFailIfMatchesRegexp(t *testing.T) { recorder = httptest.NewRecorder() registry = prometheus.NewRegistry() - result = probeHTTP(testCTX, ts.URL, - Module{Timeout: time.Second, HTTP: HTTPProbe{FailIfMatchesRegexp: []string{"could not connect to database", "internal error"}}}, registry) + result = ProbeHTTP(testCTX, ts.URL, + config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{FailIfMatchesRegexp: []string{"could not connect to database", "internal error"}}}, registry) body = recorder.Body.String() if !result { t.Fatalf("Regexp test failed unexpectedly, got %s", body) @@ -313,8 +315,8 @@ func TestFailIfNotMatchesRegexp(t *testing.T) { registry := prometheus.NewRegistry() testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result := probeHTTP(testCTX, ts.URL, - Module{Timeout: time.Second, HTTP: HTTPProbe{FailIfNotMatchesRegexp: []string{"Download the latest version here"}}}, registry) + result := ProbeHTTP(testCTX, ts.URL, + config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{FailIfNotMatchesRegexp: []string{"Download the latest version here"}}}, registry) body := recorder.Body.String() if result { t.Fatalf("Regexp test succeeded unexpectedly, got %s", body) @@ -327,8 +329,8 @@ func TestFailIfNotMatchesRegexp(t *testing.T) { recorder = httptest.NewRecorder() registry = prometheus.NewRegistry() - result = probeHTTP(testCTX, ts.URL, - Module{Timeout: time.Second, HTTP: HTTPProbe{FailIfNotMatchesRegexp: []string{"Download the latest version here"}}}, registry) + result = ProbeHTTP(testCTX, ts.URL, + config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{FailIfNotMatchesRegexp: []string{"Download the latest version here"}}}, registry) body = recorder.Body.String() if !result { t.Fatalf("Regexp test failed unexpectedly, got %s", body) @@ -343,8 +345,8 @@ func TestFailIfNotMatchesRegexp(t *testing.T) { recorder = httptest.NewRecorder() registry = prometheus.NewRegistry() - result = probeHTTP(testCTX, ts.URL, - Module{Timeout: time.Second, HTTP: HTTPProbe{FailIfNotMatchesRegexp: []string{"Download the latest version here", "Copyright 2015"}}}, registry) + result = ProbeHTTP(testCTX, ts.URL, + config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{FailIfNotMatchesRegexp: []string{"Download the latest version here", "Copyright 2015"}}}, registry) body = recorder.Body.String() if result { t.Fatalf("Regexp test succeeded unexpectedly, got %s", body) @@ -357,8 +359,8 @@ func TestFailIfNotMatchesRegexp(t *testing.T) { recorder = httptest.NewRecorder() registry = prometheus.NewRegistry() - result = probeHTTP(testCTX, ts.URL, - Module{Timeout: time.Second, HTTP: HTTPProbe{FailIfNotMatchesRegexp: []string{"Download the latest version here", "Copyright 2015"}}}, registry) + result = ProbeHTTP(testCTX, ts.URL, + config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{FailIfNotMatchesRegexp: []string{"Download the latest version here", "Copyright 2015"}}}, registry) body = recorder.Body.String() if !result { t.Fatalf("Regexp test failed unexpectedly, got %s", body) @@ -389,7 +391,7 @@ func TestHTTPHeaders(t *testing.T) { registry := prometheus.NewRegistry() testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result := probeHTTP(testCTX, ts.URL, Module{Timeout: time.Second, HTTP: HTTPProbe{ + result := ProbeHTTP(testCTX, ts.URL, config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{ Headers: headers, }}, registry) if !result { @@ -406,10 +408,10 @@ func TestFailIfSelfSignedCA(t *testing.T) { registry := prometheus.NewRegistry() testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result := probeHTTP(testCTX, ts.URL, - Module{Timeout: time.Second, HTTP: HTTPProbe{ - HTTPClientConfig: config.HTTPClientConfig{ - TLSConfig: config.TLSConfig{InsecureSkipVerify: false}, + result := ProbeHTTP(testCTX, ts.URL, + config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{ + HTTPClientConfig: pconfig.HTTPClientConfig{ + TLSConfig: pconfig.TLSConfig{InsecureSkipVerify: false}, }, }}, registry) body := recorder.Body.String() @@ -435,10 +437,10 @@ func TestSucceedIfSelfSignedCA(t *testing.T) { registry := prometheus.NewRegistry() testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result := probeHTTP(testCTX, ts.URL, - Module{Timeout: time.Second, HTTP: HTTPProbe{ - HTTPClientConfig: config.HTTPClientConfig{ - TLSConfig: config.TLSConfig{InsecureSkipVerify: true}, + result := ProbeHTTP(testCTX, ts.URL, + config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{ + HTTPClientConfig: pconfig.HTTPClientConfig{ + TLSConfig: pconfig.TLSConfig{InsecureSkipVerify: true}, }, }}, registry) body := recorder.Body.String() @@ -464,10 +466,10 @@ func TestTLSConfigIsIgnoredForPlainHTTP(t *testing.T) { registry := prometheus.NewRegistry() testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result := probeHTTP(testCTX, ts.URL, - Module{Timeout: time.Second, HTTP: HTTPProbe{ - HTTPClientConfig: config.HTTPClientConfig{ - TLSConfig: config.TLSConfig{InsecureSkipVerify: false}, + result := ProbeHTTP(testCTX, ts.URL, + config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{ + HTTPClientConfig: pconfig.HTTPClientConfig{ + TLSConfig: pconfig.TLSConfig{InsecureSkipVerify: false}, }, }}, registry) body := recorder.Body.String() @@ -483,37 +485,3 @@ func TestTLSConfigIsIgnoredForPlainHTTP(t *testing.T) { } checkRegistryResults(expectedResults, mfs, t) } - -var c = &Config{ - Modules: map[string]Module{ - "http_2xx": Module{ - Prober: "http", - Timeout: 10 * time.Second, - }, - }, -} - -func TestPrometheusTimeoutHTTP(t *testing.T) { - - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - time.Sleep(2 * time.Second) - })) - defer ts.Close() - - req, err := http.NewRequest("GET", "?target="+ts.URL, nil) - if err != nil { - t.Fatal(err) - } - req.Header.Set("X-Prometheus-Scrape-Timeout-Seconds", "1") - - rr := httptest.NewRecorder() - handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - probeHandler(w, r, c) - }) - - handler.ServeHTTP(rr, req) - - if status := rr.Code; status != http.StatusOK { - t.Errorf("probe request handler returned wrong status code: %v, want %v", status, http.StatusOK) - } -} diff --git a/icmp.go b/prober/icmp.go similarity index 94% rename from icmp.go rename to prober/icmp.go index 56754e7..9c1a5d6 100644 --- a/icmp.go +++ b/prober/icmp.go @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package main +package prober import ( "bytes" @@ -27,6 +27,8 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/common/log" + + "github.com/prometheus/blackbox_exporter/config" ) var ( @@ -41,7 +43,7 @@ func getICMPSequence() uint16 { return icmpSequence } -func probeICMP(ctx context.Context, target string, module Module, registry *prometheus.Registry) (success bool) { +func ProbeICMP(ctx context.Context, target string, module config.Module, registry *prometheus.Registry) (success bool) { var ( socket *icmp.PacketConn requestType icmp.Type diff --git a/prober/prober.go b/prober/prober.go new file mode 100644 index 0000000..c3a5fe8 --- /dev/null +++ b/prober/prober.go @@ -0,0 +1,11 @@ +package prober + +import ( + "context" + + "github.com/prometheus/client_golang/prometheus" + + "github.com/prometheus/blackbox_exporter/config" +) + +type ProbeFn func(ctx context.Context, target string, config config.Module, registry *prometheus.Registry) bool diff --git a/tcp.go b/prober/tcp.go similarity index 90% rename from tcp.go rename to prober/tcp.go index 5abbb51..74c9356 100644 --- a/tcp.go +++ b/prober/tcp.go @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package main +package prober import ( "bufio" @@ -22,11 +22,13 @@ import ( "regexp" "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/common/config" + pconfig "github.com/prometheus/common/config" "github.com/prometheus/common/log" + + "github.com/prometheus/blackbox_exporter/config" ) -func dialTCP(ctx context.Context, target string, module Module, registry *prometheus.Registry) (net.Conn, error) { +func dialTCP(ctx context.Context, target string, module config.Module, registry *prometheus.Registry) (net.Conn, error) { var dialProtocol, dialTarget string dialer := &net.Dialer{} targetAddress, port, err := net.SplitHostPort(target) @@ -51,7 +53,7 @@ func dialTCP(ctx context.Context, target string, module Module, registry *promet if !module.TCP.TLS { return dialer.DialContext(ctx, dialProtocol, dialTarget) } - tlsConfig, err := config.NewTLSConfig(&module.TCP.TLSConfig) + tlsConfig, err := pconfig.NewTLSConfig(&module.TCP.TLSConfig) if err != nil { log.Errorf("Error creating TLS configuration: %v", err) return nil, err @@ -62,7 +64,7 @@ func dialTCP(ctx context.Context, target string, module Module, registry *promet return tls.DialWithDialer(dialer, dialProtocol, dialTarget, tlsConfig) } -func probeTCP(ctx context.Context, target string, module Module, registry *prometheus.Registry) bool { +func ProbeTCP(ctx context.Context, target string, module config.Module, registry *prometheus.Registry) bool { probeSSLEarliestCertExpiry := prometheus.NewGauge(prometheus.GaugeOpts{ Name: "probe_ssl_earliest_cert_expiry", Help: "Returns earliest SSL cert expiry date", diff --git a/tcp_test.go b/prober/tcp_test.go similarity index 85% rename from tcp_test.go rename to prober/tcp_test.go index a612317..4e72099 100644 --- a/tcp_test.go +++ b/prober/tcp_test.go @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package main +package prober import ( "context" @@ -22,6 +22,8 @@ import ( "time" "github.com/prometheus/client_golang/prometheus" + + "github.com/prometheus/blackbox_exporter/config" ) func TestTCPConnection(t *testing.T) { @@ -43,7 +45,7 @@ func TestTCPConnection(t *testing.T) { testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() registry := prometheus.NewRegistry() - if !probeTCP(testCTX, ln.Addr().String(), Module{}, registry) { + if !ProbeTCP(testCTX, ln.Addr().String(), config.Module{}, registry) { t.Fatalf("TCP module failed, expected success.") } <-ch @@ -54,7 +56,7 @@ func TestTCPConnectionFails(t *testing.T) { registry := prometheus.NewRegistry() testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - if probeTCP(testCTX, ":0", Module{}, registry) { + if ProbeTCP(testCTX, ":0", config.Module{}, registry) { t.Fatalf("TCP module suceeded, expected failure.") } } @@ -69,9 +71,9 @@ func TestTCPConnectionQueryResponseIRC(t *testing.T) { testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - module := Module{ - TCP: TCPProbe{ - QueryResponse: []QueryResponse{ + module := config.Module{ + TCP: config.TCPProbe{ + QueryResponse: []config.QueryResponse{ {Send: "NICK prober"}, {Send: "USER prober prober prober :prober"}, {Expect: "^:[^ ]+ 001"}, @@ -94,7 +96,7 @@ func TestTCPConnectionQueryResponseIRC(t *testing.T) { ch <- struct{}{} }() registry := prometheus.NewRegistry() - if !probeTCP(testCTX, ln.Addr().String(), module, registry) { + if !ProbeTCP(testCTX, ln.Addr().String(), module, registry) { t.Fatalf("TCP module failed, expected success.") } <-ch @@ -113,7 +115,7 @@ func TestTCPConnectionQueryResponseIRC(t *testing.T) { ch <- struct{}{} }() registry = prometheus.NewRegistry() - if probeTCP(testCTX, ln.Addr().String(), module, registry) { + if ProbeTCP(testCTX, ln.Addr().String(), module, registry) { t.Fatalf("TCP module succeeded, expected failure.") } mfs, err := registry.Gather() @@ -137,9 +139,9 @@ func TestTCPConnectionQueryResponseMatching(t *testing.T) { testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() time.Sleep(time.Millisecond * 100) - module := Module{ - TCP: TCPProbe{ - QueryResponse: []QueryResponse{ + module := config.Module{ + TCP: config.TCPProbe{ + QueryResponse: []config.QueryResponse{ { Expect: "SSH-2.0-(OpenSSH_6.9p1) Debian-2", Send: "CONFIRM ${1}", @@ -162,7 +164,7 @@ func TestTCPConnectionQueryResponseMatching(t *testing.T) { ch <- version }() registry := prometheus.NewRegistry() - if !probeTCP(testCTX, ln.Addr().String(), module, registry) { + if !ProbeTCP(testCTX, ln.Addr().String(), module, registry) { t.Fatalf("TCP module failed, expected success.") } if got, want := <-ch, "OpenSSH_6.9p1"; got != want { @@ -203,14 +205,14 @@ func TestTCPConnectionProtocol(t *testing.T) { _, port, _ := net.SplitHostPort(ln.Addr().String()) // Force IPv4 - module := Module{ - TCP: TCPProbe{ + module := config.Module{ + TCP: config.TCPProbe{ PreferredIPProtocol: "ip4", }, } registry := prometheus.NewRegistry() - result := probeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry) + result := ProbeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry) if !result { t.Fatalf("TCP protocol: \"tcp4\" connection test failed, expected success.") } @@ -224,12 +226,12 @@ func TestTCPConnectionProtocol(t *testing.T) { checkRegistryResults(expectedResults, mfs, t) // Force IPv6 - module = Module{ - TCP: TCPProbe{}, + module = config.Module{ + TCP: config.TCPProbe{}, } registry = prometheus.NewRegistry() - result = probeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry) + result = ProbeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry) if !result { t.Fatalf("TCP protocol: \"tcp6\" connection test failed, expected success.") } @@ -243,14 +245,14 @@ func TestTCPConnectionProtocol(t *testing.T) { checkRegistryResults(expectedResults, mfs, t) // Prefer IPv4 - module = Module{ - TCP: TCPProbe{ + module = config.Module{ + TCP: config.TCPProbe{ PreferredIPProtocol: "ip4", }, } registry = prometheus.NewRegistry() - result = probeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry) + result = ProbeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry) if !result { t.Fatalf("TCP protocol: \"tcp\", prefer: \"ip4\" connection test failed, expected success.") } @@ -264,14 +266,14 @@ func TestTCPConnectionProtocol(t *testing.T) { checkRegistryResults(expectedResults, mfs, t) // Prefer IPv6 - module = Module{ - TCP: TCPProbe{ + module = config.Module{ + TCP: config.TCPProbe{ PreferredIPProtocol: "ip6", }, } registry = prometheus.NewRegistry() - result = probeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry) + result = ProbeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry) if !result { t.Fatalf("TCP protocol: \"tcp\", prefer: \"ip6\" connection test failed, expected success.") } @@ -285,12 +287,12 @@ func TestTCPConnectionProtocol(t *testing.T) { checkRegistryResults(expectedResults, mfs, t) // Prefer nothing - module = Module{ - TCP: TCPProbe{}, + module = config.Module{ + TCP: config.TCPProbe{}, } registry = prometheus.NewRegistry() - result = probeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry) + result = ProbeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry) if !result { t.Fatalf("TCP protocol: \"tcp\" connection test failed, expected success.") } @@ -304,12 +306,12 @@ func TestTCPConnectionProtocol(t *testing.T) { checkRegistryResults(expectedResults, mfs, t) // No protocol - module = Module{ - TCP: TCPProbe{}, + module = config.Module{ + TCP: config.TCPProbe{}, } registry = prometheus.NewRegistry() - result = probeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry) + result = ProbeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry) if !result { t.Fatalf("TCP connection test with protocol unspecified failed, expected success.") } @@ -342,8 +344,8 @@ func TestPrometheusTimeoutTCP(t *testing.T) { testCTX, cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel() registry := prometheus.NewRegistry() - if probeTCP(testCTX, ln.Addr().String(), Module{TCP: TCPProbe{ - QueryResponse: []QueryResponse{ + if ProbeTCP(testCTX, ln.Addr().String(), config.Module{TCP: config.TCPProbe{ + QueryResponse: []config.QueryResponse{ { Expect: "SSH-2.0-(OpenSSH_6.9p1) Debian-2", }, diff --git a/tls.go b/prober/tls.go similarity index 98% rename from tls.go rename to prober/tls.go index fffd8b9..ea21dd1 100644 --- a/tls.go +++ b/prober/tls.go @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package main +package prober import ( "crypto/tls" diff --git a/utils.go b/prober/utils.go similarity index 98% rename from utils.go rename to prober/utils.go index 1828a4f..fb3890c 100644 --- a/utils.go +++ b/prober/utils.go @@ -1,4 +1,4 @@ -package main +package prober import ( "net" diff --git a/utils_test.go b/prober/utils_test.go similarity index 97% rename from utils_test.go rename to prober/utils_test.go index c121c58..a81608a 100644 --- a/utils_test.go +++ b/prober/utils_test.go @@ -1,4 +1,4 @@ -package main +package prober import ( "testing" -- 2.25.1