Add result_type field to net_response input plugin (#2990)

This commit is contained in:
Bob Shannon
2017-07-14 13:43:36 -04:00
committed by Daniel Nelson
parent 08bdb780c6
commit 39a8f73260
3 changed files with 62 additions and 16 deletions

View File

@@ -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
@@ -88,17 +93,21 @@ func (n *NetResponse) TcpGather() (map[string]interface{}, error) {
// 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["result_type"] = "success"
fields["string_found"] = true
} else {
fields["result_type"] = "string_mismatch"
fields["string_found"] = false
}
}
} else {
fields["result_type"] = "success"
}
fields["response_time"] = responseTime
return fields, nil
@@ -114,11 +123,12 @@ 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
fields["result_type"] = "connection_failed"
return fields, nil
}
defer conn.Close()
// Send string
msg := []byte(n.Send)
conn.Write(msg)
@@ -132,14 +142,17 @@ 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["result_type"] = "success"
fields["string_found"] = true
} else {
fields["result_type"] = "string_mismatch"
fields["string_found"] = false
}
}