From: Conor Broderick Date: Wed, 12 Jul 2017 13:31:37 +0000 (+0100) Subject: Added metric for http/tcp regex match (#191) X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=89cde43d322e08c41c8ecb3118345fddeda97ffb;p=blackbox_exporter.git Added metric for http/tcp regex match (#191) --- diff --git a/http.go b/http.go index 8c75d2e..bd1f37a 100644 --- a/http.go +++ b/http.go @@ -91,6 +91,11 @@ func probeHTTP(ctx context.Context, target string, module Module, registry *prom Name: "probe_http_version", Help: "Returns the version of HTTP of the probe response", }) + + probeFailedDueToRegex = prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "probe_failed_due_to_regex", + Help: "Indicates if probe failed due to regex", + }) ) registry.MustRegister(contentLengthGauge) @@ -98,6 +103,7 @@ func probeHTTP(ctx context.Context, target string, module Module, registry *prom registry.MustRegister(isSSLGauge) registry.MustRegister(statusCodeGauge) registry.MustRegister(probeHTTPVersionGauge) + registry.MustRegister(probeFailedDueToRegex) httpConfig := module.HTTP @@ -186,6 +192,11 @@ func probeHTTP(ctx context.Context, target string, module Module, registry *prom if success && (len(httpConfig.FailIfMatchesRegexp) > 0 || len(httpConfig.FailIfNotMatchesRegexp) > 0) { success = matchRegularExpressions(resp.Body, httpConfig) + if success { + probeFailedDueToRegex.Set(0) + } else { + probeFailedDueToRegex.Set(1) + } } var httpVersionNumber float64 diff --git a/http_test.go b/http_test.go index f897dfd..eec5c1b 100644 --- a/http_test.go +++ b/http_test.go @@ -241,6 +241,14 @@ func TestFailIfMatchesRegexp(t *testing.T) { if result { t.Fatalf("Regexp test succeeded unexpectedly, got %s", body) } + mfs, err := registry.Gather() + if err != nil { + t.Fatal(err) + } + expectedResults := map[string]float64{ + "probe_failed_due_to_regex": 1, + } + checkRegistryResults(expectedResults, mfs, t) ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Download the latest version here") @@ -255,6 +263,14 @@ func TestFailIfMatchesRegexp(t *testing.T) { if !result { t.Fatalf("Regexp test failed unexpectedly, got %s", body) } + mfs, err = registry.Gather() + if err != nil { + t.Fatal(err) + } + expectedResults = map[string]float64{ + "probe_failed_due_to_regex": 0, + } + checkRegistryResults(expectedResults, mfs, t) // With multiple regexps configured, verify that any matching regexp causes // the probe to fail, but probes succeed when no regexp matches. diff --git a/tcp.go b/tcp.go index 4a31387..61dc9fe 100644 --- a/tcp.go +++ b/tcp.go @@ -68,6 +68,11 @@ func probeTCP(ctx context.Context, target string, module Module, registry *prome Name: "probe_ssl_earliest_cert_expiry", Help: "Returns earliest SSL cert expiry date", }) + probeFailedDueToRegex := prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "probe_failed_due_to_regex", + Help: "Indicates if probe failed due to regex", + }) + registry.MustRegister(probeFailedDueToRegex) deadline := time.Now().Add(module.Timeout) conn, err := dialTCP(ctx, target, module, registry) if err != nil { @@ -112,8 +117,10 @@ func probeTCP(ctx context.Context, target string, module Module, registry *prome return false } if match == nil { + probeFailedDueToRegex.Set(1) return false } + probeFailedDueToRegex.Set(0) send = string(re.Expand(nil, []byte(send), scanner.Bytes(), match)) } if send != "" { diff --git a/tcp_test.go b/tcp_test.go index 222ab06..7c2367c 100644 --- a/tcp_test.go +++ b/tcp_test.go @@ -117,6 +117,14 @@ func TestTCPConnectionQueryResponseIRC(t *testing.T) { if probeTCP(testCTX, ln.Addr().String(), module, registry) { t.Fatalf("TCP module succeeded, expected failure.") } + mfs, err := registry.Gather() + if err != nil { + t.Fatal(err) + } + expectedResults := map[string]float64{ + "probe_failed_due_to_regex": 1, + } + checkRegistryResults(expectedResults, mfs, t) <-ch } @@ -162,6 +170,15 @@ func TestTCPConnectionQueryResponseMatching(t *testing.T) { if got, want := <-ch, "OpenSSH_6.9p1"; got != want { t.Fatalf("Read unexpected version: got %q, want %q", got, want) } + mfs, err := registry.Gather() + if err != nil { + t.Fatal(err) + } + expectedResults := map[string]float64{ + "probe_failed_due_to_regex": 0, + } + checkRegistryResults(expectedResults, mfs, t) + } func TestTCPConnectionProtocol(t *testing.T) { diff --git a/utils_test.go b/utils_test.go index 1cbc4f0..c121c58 100644 --- a/utils_test.go +++ b/utils_test.go @@ -13,7 +13,11 @@ func checkRegistryResults(expRes map[string]float64, mfs []*dto.MetricFamily, t res[mfs[i].GetName()] = mfs[i].Metric[0].GetGauge().GetValue() } for k, v := range expRes { - if val, ok := res[k]; !ok || val != v { + val, ok := res[k] + if !ok { + t.Fatalf("Expected metric %v not found in returned metrics", k) + } + if val != v { t.Fatalf("Expected: %v: %v, got: %v: %v", k, v, k, val) } }