Add result_type field for http_response input (#2814)
This commit is contained in:
parent
ef72c5703b
commit
38b760936d
|
@ -41,6 +41,7 @@ This input plugin will test HTTP/HTTPS connections.
|
||||||
- http_response
|
- http_response
|
||||||
- response_time (float, seconds)
|
- response_time (float, seconds)
|
||||||
- http_response_code (int) #The code received
|
- http_response_code (int) #The code received
|
||||||
|
- result_type (string) # success, timeout, response_string_mismatch, connection_failed
|
||||||
|
|
||||||
### Tags:
|
### Tags:
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
@ -130,15 +131,21 @@ func (h *HTTPResponse) httpGather() (map[string]interface{}, error) {
|
||||||
// Start Timer
|
// Start Timer
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
resp, err := h.client.Do(request)
|
resp, err := h.client.Do(request)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
||||||
|
fields["result_type"] = "timeout"
|
||||||
|
return fields, nil
|
||||||
|
}
|
||||||
|
fields["result_type"] = "connection_failed"
|
||||||
if h.FollowRedirects {
|
if h.FollowRedirects {
|
||||||
return nil, err
|
return fields, nil
|
||||||
}
|
}
|
||||||
if urlError, ok := err.(*url.Error); ok &&
|
if urlError, ok := err.(*url.Error); ok &&
|
||||||
urlError.Err == ErrRedirectAttempted {
|
urlError.Err == ErrRedirectAttempted {
|
||||||
err = nil
|
err = nil
|
||||||
} else {
|
} else {
|
||||||
return nil, err
|
return fields, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
|
@ -157,7 +164,7 @@ func (h *HTTPResponse) httpGather() (map[string]interface{}, error) {
|
||||||
h.compiledStringMatch = regexp.MustCompile(h.ResponseStringMatch)
|
h.compiledStringMatch = regexp.MustCompile(h.ResponseStringMatch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("E! Failed to compile regular expression %s : %s", h.ResponseStringMatch, err)
|
log.Printf("E! Failed to compile regular expression %s : %s", h.ResponseStringMatch, err)
|
||||||
fields["response_string_match"] = 0
|
fields["result_type"] = "response_string_mismatch"
|
||||||
return fields, nil
|
return fields, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -165,16 +172,20 @@ func (h *HTTPResponse) httpGather() (map[string]interface{}, error) {
|
||||||
bodyBytes, err := ioutil.ReadAll(resp.Body)
|
bodyBytes, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("E! Failed to read body of HTTP Response : %s", err)
|
log.Printf("E! Failed to read body of HTTP Response : %s", err)
|
||||||
|
fields["result_type"] = "response_string_mismatch"
|
||||||
fields["response_string_match"] = 0
|
fields["response_string_match"] = 0
|
||||||
return fields, nil
|
return fields, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if h.compiledStringMatch.Match(bodyBytes) {
|
if h.compiledStringMatch.Match(bodyBytes) {
|
||||||
|
fields["result_type"] = "success"
|
||||||
fields["response_string_match"] = 1
|
fields["response_string_match"] = 1
|
||||||
} else {
|
} else {
|
||||||
|
fields["result_type"] = "response_string_mismatch"
|
||||||
fields["response_string_match"] = 0
|
fields["response_string_match"] = 0
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
fields["result_type"] = "success"
|
||||||
}
|
}
|
||||||
|
|
||||||
return fields, nil
|
return fields, nil
|
||||||
|
|
|
@ -106,6 +106,9 @@ func TestFields(t *testing.T) {
|
||||||
value, ok := acc.IntField("http_response", "http_response_code")
|
value, ok := acc.IntField("http_response", "http_response_code")
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
require.Equal(t, http.StatusOK, value)
|
require.Equal(t, http.StatusOK, value)
|
||||||
|
response_value, ok := acc.StringField("http_response", "result_type")
|
||||||
|
require.True(t, ok)
|
||||||
|
require.Equal(t, "success", response_value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRedirects(t *testing.T) {
|
func TestRedirects(t *testing.T) {
|
||||||
|
@ -143,10 +146,13 @@ func TestRedirects(t *testing.T) {
|
||||||
}
|
}
|
||||||
acc = testutil.Accumulator{}
|
acc = testutil.Accumulator{}
|
||||||
err = h.Gather(&acc)
|
err = h.Gather(&acc)
|
||||||
require.Error(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
value, ok = acc.IntField("http_response", "http_response_code")
|
value, ok = acc.IntField("http_response", "http_response_code")
|
||||||
require.False(t, ok)
|
require.False(t, ok)
|
||||||
|
response_value, ok := acc.StringField("http_response", "result_type")
|
||||||
|
require.True(t, ok)
|
||||||
|
require.Equal(t, "connection_failed", response_value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMethod(t *testing.T) {
|
func TestMethod(t *testing.T) {
|
||||||
|
@ -277,6 +283,9 @@ func TestStringMatch(t *testing.T) {
|
||||||
value, ok = acc.IntField("http_response", "response_string_match")
|
value, ok = acc.IntField("http_response", "response_string_match")
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
require.Equal(t, 1, value)
|
require.Equal(t, 1, value)
|
||||||
|
response_value, ok := acc.StringField("http_response", "result_type")
|
||||||
|
require.True(t, ok)
|
||||||
|
require.Equal(t, "success", response_value)
|
||||||
_, ok = acc.FloatField("http_response", "response_time")
|
_, ok = acc.FloatField("http_response", "response_time")
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
}
|
}
|
||||||
|
@ -307,6 +316,9 @@ func TestStringMatchJson(t *testing.T) {
|
||||||
value, ok = acc.IntField("http_response", "response_string_match")
|
value, ok = acc.IntField("http_response", "response_string_match")
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
require.Equal(t, 1, value)
|
require.Equal(t, 1, value)
|
||||||
|
response_value, ok := acc.StringField("http_response", "result_type")
|
||||||
|
require.True(t, ok)
|
||||||
|
require.Equal(t, "success", response_value)
|
||||||
_, ok = acc.FloatField("http_response", "response_time")
|
_, ok = acc.FloatField("http_response", "response_time")
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
}
|
}
|
||||||
|
@ -338,6 +350,9 @@ func TestStringMatchFail(t *testing.T) {
|
||||||
value, ok = acc.IntField("http_response", "response_string_match")
|
value, ok = acc.IntField("http_response", "response_string_match")
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
require.Equal(t, 0, value)
|
require.Equal(t, 0, value)
|
||||||
|
response_value, ok := acc.StringField("http_response", "result_type")
|
||||||
|
require.True(t, ok)
|
||||||
|
require.Equal(t, "response_string_mismatch", response_value)
|
||||||
_, ok = acc.FloatField("http_response", "response_time")
|
_, ok = acc.FloatField("http_response", "response_time")
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
}
|
}
|
||||||
|
@ -363,8 +378,13 @@ func TestTimeout(t *testing.T) {
|
||||||
}
|
}
|
||||||
var acc testutil.Accumulator
|
var acc testutil.Accumulator
|
||||||
err := h.Gather(&acc)
|
err := h.Gather(&acc)
|
||||||
require.Error(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
ok := acc.HasIntField("http_response", "http_response_code")
|
_, ok := acc.IntField("http_response", "http_response_code")
|
||||||
|
require.False(t, ok)
|
||||||
|
response_value, ok := acc.StringField("http_response", "result_type")
|
||||||
|
require.True(t, ok)
|
||||||
|
require.Equal(t, "timeout", response_value)
|
||||||
|
_, ok = acc.FloatField("http_response", "response_time")
|
||||||
require.False(t, ok)
|
require.False(t, ok)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue