Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move field result_type to be a tag #3451

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions plugins/inputs/net_response/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ It can also check response text.

- net_response
- response_time (float, seconds)
- result_type (string) # success, timeout, connection_failed, read_failed, string_mismatch
- success (int) # success 0, failure 1
- [**DEPRECATED**] string_found (boolean)

### Tags:
Expand All @@ -68,16 +68,17 @@ It can also check response text.
- server
- port
- protocol
- result_type (string) # success, timeout, connection_failed, read_failed, string_mismatch

### Example Output:

```
$ ./telegraf --config telegraf.conf --input-filter net_response --test
net_response,server=influxdata.com,port=8080,protocol=tcp,host=localhost result_type="timeout" 1499310361000000000
net_response,server=influxdata.com,port=443,protocol=tcp,host=localhost result_type="success",response_time=0.088703864 1499310361000000000
net_response,protocol=tcp,host=localhost,server=this.domain.does.not.exist,port=443 result_type="connection_failed" 1499310361000000000
net_response,protocol=udp,host=localhost,server=influxdata.com,port=8080 result_type="read_failed" 1499310362000000000
net_response,port=31338,protocol=udp,host=localhost,server=localhost result_type="string_mismatch",string_found=false,response_time=0.00242682 1499310362000000000
net_response,protocol=udp,host=localhost,server=localhost,port=31338 response_time=0.001128598,result_type="success",string_found=true 1499310362000000000
net_response,server=this.domain.does.not.exist,port=443,protocol=udp,host=localhost result_type="connection_failed" 1499310362000000000
```
net_response,server=influxdata.com,port=8080,protocol=tcp,host=localhost,result_type="timeout" success=1i, 1499310361000000000
net_response,server=influxdata.com,port=443,protocol=tcp,host=localhost,result_type="success", success=0i,response_time=0.088703864 1499310361000000000
net_response,protocol=tcp,host=localhost,server=this.domain.does.not.exist,port=443,result_type="connection_failed" success=1i, 1499310361000000000
net_response,protocol=udp,host=localhost,server=influxdata.com,port=8080,result_type="read_failed" success=1i, 1499310362000000000
net_response,port=31338,protocol=udp,host=localhost,server=localhost,result_type="string_mismatch", success=1i,string_found=false,response_time=0.00242682 1499310362000000000
net_response,protocol=udp,host=localhost,server=localhost,port=31338,result_type="success" success=0i,response_time=0.001128598,string_found=true 1499310362000000000
net_response,server=this.domain.does.not.exist,port=443,protocol=udp,host=localhost,result_type="connection_failed" success=1i, 1499310362000000000
```
91 changes: 59 additions & 32 deletions plugins/inputs/net_response/net_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/influxdata/telegraf/plugins/inputs"
)

// NetResponses struct
// NetResponse struct
type NetResponse struct {
Address string
Timeout internal.Duration
Expand All @@ -23,7 +23,8 @@ type NetResponse struct {
Protocol string
}

func (_ *NetResponse) Description() string {
// Description will return a string with a description of the plugin
func (*NetResponse) Description() string {
return "TCP or UDP 'ping' given url and collect response time in seconds"
}

Expand All @@ -49,13 +50,17 @@ var sampleConfig = `
# expect = "ssh"
`

func (_ *NetResponse) SampleConfig() string {
// SampleConfig will return a string with a full config example
func (*NetResponse) SampleConfig() string {
return sampleConfig
}

func (n *NetResponse) TcpGather() (map[string]interface{}, error) {
// TCPGather will start the gather process for tcp requests
func (n *NetResponse) TCPGather() (tags map[string]string, fields map[string]interface{}) {
// Prepare fields
fields := make(map[string]interface{})
fields = make(map[string]interface{})
// Prepare tags
tags = make(map[string]string)
// Start Timer
start := time.Now()
// Connecting
Expand All @@ -65,11 +70,13 @@ func (n *NetResponse) TcpGather() (map[string]interface{}, error) {
// Handle error
if err != nil {
if e, ok := err.(net.Error); ok && e.Timeout() {
fields["result_type"] = "timeout"
tags["result_type"] = "timeout"
fields["success"] = 1
} else {
fields["result_type"] = "connection_failed"
tags["result_type"] = "connection_failed"
fields["success"] = 1
}
return fields, nil
return tags, fields
}
defer conn.Close()
// Send string if needed
Expand All @@ -93,29 +100,37 @@ func (n *NetResponse) TcpGather() (map[string]interface{}, error) {
// Handle error
if err != nil {
fields["string_found"] = false
fields["result_type"] = "read_failed"
tags["result_type"] = "read_failed"
fields["success"] = 1
} else {
// Looking for string in answer
RegEx := regexp.MustCompile(`.*` + n.Expect + `.*`)
find := RegEx.FindString(string(data))
if find != "" {
fields["result_type"] = "success"
fields["success"] = 0
tags["result_type"] = "success"
fields["string_found"] = true
} else {
fields["result_type"] = "string_mismatch"
tags["result_type"] = "string_mismatch"
fields["success"] = 1
fields["string_found"] = false
}
}
} else {
fields["result_type"] = "success"
tags["result_type"] = "success"
fields["success"] = 0
}
fields["response_time"] = responseTime
return fields, nil
return tags, fields
}

func (n *NetResponse) UdpGather() (map[string]interface{}, error) {
// UDPGather will start the gather process for UDP requests
// it will return the feilds and an error.
func (n *NetResponse) UDPGather() (tags map[string]string, fields map[string]interface{}) {
// Prepare fields
fields := make(map[string]interface{})
fields = make(map[string]interface{})
// Prepare tags
tags = make(map[string]string)
// Start Timer
start := time.Now()
// Resolving
Expand All @@ -125,8 +140,9 @@ func (n *NetResponse) UdpGather() (map[string]interface{}, error) {
conn, err := net.DialUDP("udp", LocalAddr, udpAddr)
// Handle error
if err != nil {
fields["result_type"] = "connection_failed"
return fields, nil
tags["result_type"] = "connection_failed"
fields["success"] = 1
return tags, fields
}
defer conn.Close()
// Send string
Expand All @@ -142,24 +158,30 @@ func (n *NetResponse) UdpGather() (map[string]interface{}, error) {
responseTime := time.Since(start).Seconds()
// Handle error
if err != nil {
fields["result_type"] = "read_failed"
return fields, nil
tags["result_type"] = "read_failed"
fields["success"] = 1
return tags, fields
}

// Looking for string in answer
RegEx := regexp.MustCompile(`.*` + n.Expect + `.*`)
find := RegEx.FindString(string(buf))
if find != "" {
tags["result_type"] = "success"
fields["success"] = 0
fields["string_found"] = true
} else {
// Looking for string in answer
RegEx := regexp.MustCompile(`.*` + n.Expect + `.*`)
find := RegEx.FindString(string(buf))
if find != "" {
fields["result_type"] = "success"
fields["string_found"] = true
} else {
fields["result_type"] = "string_mismatch"
fields["string_found"] = false
}
tags["result_type"] = "string_mismatch"
fields["success"] = 1
fields["string_found"] = false
}

fields["response_time"] = responseTime
return fields, nil
return tags, fields
}

// Gather is fulfils the plugin package requirements for telegraf.
// It is called on every interval for metric gathering.
func (n *NetResponse) Gather(acc telegraf.Accumulator) error {
// Set default values
if n.Timeout.Duration == 0 {
Expand Down Expand Up @@ -189,19 +211,24 @@ func (n *NetResponse) Gather(acc telegraf.Accumulator) error {
// Prepare data
tags := map[string]string{"server": host, "port": port}
var fields map[string]interface{}
var returnedTags map[string]string
// Gather data
if n.Protocol == "tcp" {
fields, err = n.TcpGather()
returnedTags, fields = n.TCPGather()
tags["protocol"] = "tcp"
} else if n.Protocol == "udp" {
fields, err = n.UdpGather()
returnedTags, fields = n.UDPGather()
tags["protocol"] = "udp"
} else {
return errors.New("Bad protocol")
}
if err != nil {
return err
}
// Merge the tags
for k, v := range returnedTags {
tags[k] = v
}
// Add metrics
acc.AddFields("net_response", fields, tags)
return nil
Expand Down
39 changes: 22 additions & 17 deletions plugins/inputs/net_response/net_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ func TestTCPError(t *testing.T) {
acc.AssertContainsTaggedFields(t,
"net_response",
map[string]interface{}{
"result_type": "connection_failed",
"success": 1,
},
map[string]string{
"server": "",
"port": "9999",
"protocol": "tcp",
"server": "",
"port": "9999",
"protocol": "tcp",
"result_type": "connection_failed",
},
)
}
Expand Down Expand Up @@ -77,13 +78,14 @@ func TestTCPOK1(t *testing.T) {
acc.AssertContainsTaggedFields(t,
"net_response",
map[string]interface{}{
"result_type": "success",
"success": 0,
"string_found": true,
"response_time": 1.0,
},
map[string]string{"server": "127.0.0.1",
"port": "2004",
"protocol": "tcp",
"port": "2004",
"protocol": "tcp",
"result_type": "success",
},
)
// Waiting TCPserver
Expand Down Expand Up @@ -118,13 +120,14 @@ func TestTCPOK2(t *testing.T) {
acc.AssertContainsTaggedFields(t,
"net_response",
map[string]interface{}{
"result_type": "string_mismatch",
"success": 1,
"string_found": false,
"response_time": 1.0,
},
map[string]string{"server": "127.0.0.1",
"port": "2004",
"protocol": "tcp",
"port": "2004",
"protocol": "tcp",
"result_type": "string_mismatch",
},
)
// Waiting TCPserver
Expand All @@ -151,13 +154,14 @@ func TestUDPrror(t *testing.T) {
acc.AssertContainsTaggedFields(t,
"net_response",
map[string]interface{}{
"result_type": "read_failed",
"success": 1,
"response_time": 1.0,
},
map[string]string{
"server": "",
"port": "9999",
"protocol": "udp",
"server": "",
"port": "9999",
"protocol": "udp",
"result_type": "read_failed",
},
)
}
Expand Down Expand Up @@ -190,13 +194,14 @@ func TestUDPOK1(t *testing.T) {
acc.AssertContainsTaggedFields(t,
"net_response",
map[string]interface{}{
"result_type": "success",
"success": 0,
"string_found": true,
"response_time": 1.0,
},
map[string]string{"server": "127.0.0.1",
"port": "2004",
"protocol": "udp",
"port": "2004",
"protocol": "udp",
"result_type": "success",
},
)
// Waiting TCPserver
Expand Down