From a90f940b0fa03e63b4299a784202a3b0178d946d Mon Sep 17 00:00:00 2001 From: Bobby Shannon Date: Wed, 5 Jul 2017 21:53:23 -0400 Subject: [PATCH] Add result_type field to net_response input plugin Fix test Simplify states --- plugins/inputs/net_response/README.md | 2 +- plugins/inputs/net_response/net_response.go | 32 +++++++++----- .../inputs/net_response/net_response_test.go | 43 +++++++++++++++---- 3 files changed, 57 insertions(+), 20 deletions(-) diff --git a/plugins/inputs/net_response/README.md b/plugins/inputs/net_response/README.md index 16c73e8ee5e6a..c6f2361a847cd 100644 --- a/plugins/inputs/net_response/README.md +++ b/plugins/inputs/net_response/README.md @@ -59,7 +59,7 @@ It can also check response text. - net_response - response_time (float, seconds) - - string_found (bool) # Only if "expected: option is set + - result_type (string) # success, timeout, connection_failed, read_failed, string_mismatch ### Tags: diff --git a/plugins/inputs/net_response/net_response.go b/plugins/inputs/net_response/net_response.go index ad0de46c3151c..b983b0a3f29cf 100644 --- a/plugins/inputs/net_response/net_response.go +++ b/plugins/inputs/net_response/net_response.go @@ -64,7 +64,12 @@ func (n *NetResponse) TcpGather() (map[string]interface{}, error) { responseTime := time.Since(start).Seconds() // Handle error if err != nil { - return nil, err + if e, ok := err.(net.Error); ok && e.Timeout() { + fields["result_type"] = "timeout" + } else { + fields["result_type"] = "connection_failed" + } + return fields, nil } defer conn.Close() // Send string if needed @@ -87,18 +92,19 @@ func (n *NetResponse) TcpGather() (map[string]interface{}, error) { responseTime = time.Since(start).Seconds() // Handle error if err != nil { - fields["string_found"] = false + fields["result_type"] = "read_failed" } else { // Looking for string in answer RegEx := regexp.MustCompile(`.*` + n.Expect + `.*`) find := RegEx.FindString(string(data)) if find != "" { - fields["string_found"] = true + fields["result_type"] = "success" } else { - fields["string_found"] = false + fields["result_type"] = "string_mismatch" } } - + } else { + fields["result_type"] = "success" } fields["response_time"] = responseTime return fields, nil @@ -114,11 +120,16 @@ func (n *NetResponse) UdpGather() (map[string]interface{}, error) { LocalAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0") // Connecting conn, err := net.DialUDP("udp", LocalAddr, udpAddr) - defer conn.Close() // Handle error if err != nil { - return nil, err + if e, ok := err.(net.Error); ok && e.Timeout() { + fields["result_type"] = "timeout" + } else { + fields["result_type"] = "connection_failed" + } + return fields, nil } + defer conn.Close() // Send string msg := []byte(n.Send) conn.Write(msg) @@ -132,15 +143,16 @@ func (n *NetResponse) UdpGather() (map[string]interface{}, error) { responseTime := time.Since(start).Seconds() // Handle error if err != nil { - return nil, err + fields["result_type"] = "read_failed" + return fields, nil } else { // Looking for string in answer RegEx := regexp.MustCompile(`.*` + n.Expect + `.*`) find := RegEx.FindString(string(buf)) if find != "" { - fields["string_found"] = true + fields["result_type"] = "success" } else { - fields["string_found"] = false + fields["result_type"] = "string_mismatch" } } fields["response_time"] = responseTime diff --git a/plugins/inputs/net_response/net_response_test.go b/plugins/inputs/net_response/net_response_test.go index a005c06f53a95..521501957c2d6 100644 --- a/plugins/inputs/net_response/net_response_test.go +++ b/plugins/inputs/net_response/net_response_test.go @@ -2,7 +2,6 @@ package net_response import ( "net" - "regexp" "sync" "testing" "time" @@ -36,8 +35,18 @@ func TestTCPError(t *testing.T) { } // Error err1 := c.Gather(&acc) - require.Error(t, err1) - assert.Contains(t, err1.Error(), "getsockopt: connection refused") + require.NoError(t, err1) + acc.AssertContainsTaggedFields(t, + "net_response", + map[string]interface{}{ + "result_type": "connection_failed", + }, + map[string]string{ + "server": "", + "port": "9999", + "protocol": "tcp", + }, + ) } func TestTCPOK1(t *testing.T) { @@ -68,7 +77,7 @@ func TestTCPOK1(t *testing.T) { acc.AssertContainsTaggedFields(t, "net_response", map[string]interface{}{ - "string_found": true, + "result_type": "success", "response_time": 1.0, }, map[string]string{"server": "127.0.0.1", @@ -108,7 +117,7 @@ func TestTCPOK2(t *testing.T) { acc.AssertContainsTaggedFields(t, "net_response", map[string]interface{}{ - "string_found": false, + "result_type": "string_mismatch", "response_time": 1.0, }, map[string]string{"server": "127.0.0.1", @@ -129,10 +138,26 @@ func TestUDPrror(t *testing.T) { Expect: "test", Protocol: "udp", } - // Error + // Gather err1 := c.Gather(&acc) - require.Error(t, err1) - assert.Regexp(t, regexp.MustCompile(`read udp 127.0.0.1:[0-9]*->127.0.0.1:9999: recvfrom: connection refused`), err1.Error()) + // Override response time + for _, p := range acc.Metrics { + p.Fields["response_time"] = 1.0 + } + // Error + require.NoError(t, err1) + acc.AssertContainsTaggedFields(t, + "net_response", + map[string]interface{}{ + "result_type": "read_failed", + "response_time": 1.0, + }, + map[string]string{ + "server": "", + "port": "9999", + "protocol": "udp", + }, + ) } func TestUDPOK1(t *testing.T) { @@ -163,7 +188,7 @@ func TestUDPOK1(t *testing.T) { acc.AssertContainsTaggedFields(t, "net_response", map[string]interface{}{ - "string_found": true, + "result_type": "success", "response_time": 1.0, }, map[string]string{"server": "127.0.0.1",