2016-01-27 00:12:54 +00:00
|
|
|
package net_response
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"net/textproto"
|
|
|
|
"regexp"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2016-05-23 12:33:43 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2016-01-27 00:12:54 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
)
|
|
|
|
|
2018-05-08 23:07:15 +00:00
|
|
|
// NetResponse struct
|
2016-01-27 00:12:54 +00:00
|
|
|
type NetResponse struct {
|
|
|
|
Address string
|
2016-05-23 12:33:43 +00:00
|
|
|
Timeout internal.Duration
|
|
|
|
ReadTimeout internal.Duration
|
2016-01-27 00:12:54 +00:00
|
|
|
Send string
|
|
|
|
Expect string
|
|
|
|
Protocol string
|
|
|
|
}
|
|
|
|
|
2018-05-08 23:07:15 +00:00
|
|
|
var description = "TCP or UDP 'ping' given url and collect response time in seconds"
|
|
|
|
|
|
|
|
// Description will return a short string to explain what the plugin does.
|
|
|
|
func (*NetResponse) Description() string {
|
|
|
|
return description
|
2016-01-27 00:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## Protocol, must be "tcp" or "udp"
|
2016-12-13 16:02:03 +00:00
|
|
|
## NOTE: because the "udp" protocol does not respond to requests, it requires
|
|
|
|
## a send/expect string pair (see below).
|
2016-01-27 00:12:54 +00:00
|
|
|
protocol = "tcp"
|
2016-02-18 21:26:51 +00:00
|
|
|
## Server address (default localhost)
|
2016-12-13 16:02:03 +00:00
|
|
|
address = "localhost:80"
|
2016-05-23 12:33:43 +00:00
|
|
|
## Set timeout
|
|
|
|
timeout = "1s"
|
|
|
|
|
|
|
|
## Set read timeout (only used if expecting a response)
|
|
|
|
read_timeout = "1s"
|
2016-12-13 16:02:03 +00:00
|
|
|
|
|
|
|
## The following options are required for UDP checks. For TCP, they are
|
|
|
|
## optional. The plugin will send the given string to the server and then
|
|
|
|
## expect to receive the given 'expect' string back.
|
|
|
|
## string sent to the server
|
|
|
|
# send = "ssh"
|
|
|
|
## expected string in answer
|
|
|
|
# expect = "ssh"
|
2016-01-27 00:12:54 +00:00
|
|
|
`
|
|
|
|
|
2018-05-08 23:07:15 +00:00
|
|
|
// SampleConfig will return a complete configuration example with details about each field.
|
|
|
|
func (*NetResponse) SampleConfig() string {
|
2016-01-27 00:12:54 +00:00
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
2018-05-08 23:07:15 +00:00
|
|
|
// TCPGather will execute if there are TCP tests defined in the configuration.
|
|
|
|
// It will return a map[string]interface{} for fields and a map[string]string for tags
|
|
|
|
func (n *NetResponse) TCPGather() (tags map[string]string, fields map[string]interface{}) {
|
|
|
|
// Prepare returns
|
|
|
|
tags = make(map[string]string)
|
|
|
|
fields = make(map[string]interface{})
|
2016-01-27 00:12:54 +00:00
|
|
|
// Start Timer
|
|
|
|
start := time.Now()
|
|
|
|
// Connecting
|
2016-05-23 12:33:43 +00:00
|
|
|
conn, err := net.DialTimeout("tcp", n.Address, n.Timeout.Duration)
|
2016-01-27 00:12:54 +00:00
|
|
|
// Stop timer
|
|
|
|
responseTime := time.Since(start).Seconds()
|
|
|
|
// Handle error
|
|
|
|
if err != nil {
|
2017-07-14 17:43:36 +00:00
|
|
|
if e, ok := err.(net.Error); ok && e.Timeout() {
|
2018-05-08 23:07:15 +00:00
|
|
|
tags["result_text"] = "timeout"
|
|
|
|
fields["result_code"] = 1
|
2017-07-14 17:43:36 +00:00
|
|
|
fields["result_type"] = "timeout"
|
|
|
|
} else {
|
2018-05-08 23:07:15 +00:00
|
|
|
tags["result_text"] = "connection_failed"
|
|
|
|
fields["result_code"] = 1
|
2017-07-14 17:43:36 +00:00
|
|
|
fields["result_type"] = "connection_failed"
|
|
|
|
}
|
2018-05-08 23:07:15 +00:00
|
|
|
return tags, fields
|
2016-01-27 00:12:54 +00:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
// Send string if needed
|
2016-05-23 12:33:43 +00:00
|
|
|
if n.Send != "" {
|
|
|
|
msg := []byte(n.Send)
|
2016-01-27 00:12:54 +00:00
|
|
|
conn.Write(msg)
|
|
|
|
// Stop timer
|
|
|
|
responseTime = time.Since(start).Seconds()
|
|
|
|
}
|
|
|
|
// Read string if needed
|
2016-05-23 12:33:43 +00:00
|
|
|
if n.Expect != "" {
|
2016-01-27 00:12:54 +00:00
|
|
|
// Set read timeout
|
2016-05-23 12:33:43 +00:00
|
|
|
conn.SetReadDeadline(time.Now().Add(n.ReadTimeout.Duration))
|
2016-01-27 00:12:54 +00:00
|
|
|
// Prepare reader
|
|
|
|
reader := bufio.NewReader(conn)
|
|
|
|
tp := textproto.NewReader(reader)
|
|
|
|
// Read
|
|
|
|
data, err := tp.ReadLine()
|
|
|
|
// Stop timer
|
|
|
|
responseTime = time.Since(start).Seconds()
|
|
|
|
// Handle error
|
|
|
|
if err != nil {
|
2018-05-08 23:07:15 +00:00
|
|
|
tags["result_text"] = "read_failed"
|
|
|
|
fields["result_code"] = 1
|
2016-01-27 00:12:54 +00:00
|
|
|
fields["string_found"] = false
|
2018-05-08 23:07:15 +00:00
|
|
|
tags["result_type"] = "read_failed"
|
|
|
|
fields["success"] = 1
|
2016-01-27 00:12:54 +00:00
|
|
|
} else {
|
|
|
|
// Looking for string in answer
|
2016-05-23 12:33:43 +00:00
|
|
|
RegEx := regexp.MustCompile(`.*` + n.Expect + `.*`)
|
2016-01-27 00:12:54 +00:00
|
|
|
find := RegEx.FindString(string(data))
|
|
|
|
if find != "" {
|
2018-05-08 23:07:15 +00:00
|
|
|
tags["result_text"] = "success"
|
|
|
|
fields["result_code"] = 0
|
2017-07-14 17:43:36 +00:00
|
|
|
fields["result_type"] = "success"
|
2016-01-27 00:12:54 +00:00
|
|
|
fields["string_found"] = true
|
|
|
|
} else {
|
2018-05-08 23:07:15 +00:00
|
|
|
tags["result_text"] = "string_mismatch"
|
|
|
|
fields["result_code"] = 1
|
2017-07-14 17:43:36 +00:00
|
|
|
fields["result_type"] = "string_mismatch"
|
2016-01-27 00:12:54 +00:00
|
|
|
fields["string_found"] = false
|
|
|
|
}
|
|
|
|
}
|
2017-07-14 17:43:36 +00:00
|
|
|
} else {
|
2018-05-08 23:07:15 +00:00
|
|
|
tags["result_text"] = "success"
|
|
|
|
fields["result_code"] = 0
|
2017-07-14 17:43:36 +00:00
|
|
|
fields["result_type"] = "success"
|
2016-01-27 00:12:54 +00:00
|
|
|
}
|
|
|
|
fields["response_time"] = responseTime
|
2018-05-08 23:07:15 +00:00
|
|
|
return tags, fields
|
2016-01-27 00:12:54 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 23:07:15 +00:00
|
|
|
// UDPGather will execute if there are UDP tests defined in the configuration.
|
|
|
|
// It will return a map[string]interface{} for fields and a map[string]string for tags
|
|
|
|
func (n *NetResponse) UDPGather() (tags map[string]string, fields map[string]interface{}) {
|
|
|
|
// Prepare returns
|
|
|
|
tags = make(map[string]string)
|
|
|
|
fields = make(map[string]interface{})
|
2016-01-27 00:12:54 +00:00
|
|
|
// Start Timer
|
|
|
|
start := time.Now()
|
|
|
|
// Resolving
|
2016-05-23 12:33:43 +00:00
|
|
|
udpAddr, err := net.ResolveUDPAddr("udp", n.Address)
|
2016-01-27 00:12:54 +00:00
|
|
|
LocalAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
|
|
|
|
// Connecting
|
|
|
|
conn, err := net.DialUDP("udp", LocalAddr, udpAddr)
|
|
|
|
// Handle error
|
|
|
|
if err != nil {
|
2018-05-08 23:07:15 +00:00
|
|
|
tags["result_text"] = "connection_failed"
|
|
|
|
fields["result_code"] = 1
|
2017-07-14 17:43:36 +00:00
|
|
|
fields["result_type"] = "connection_failed"
|
2018-05-08 23:07:15 +00:00
|
|
|
return tags, fields
|
2016-01-27 00:12:54 +00:00
|
|
|
}
|
2017-07-14 17:43:36 +00:00
|
|
|
defer conn.Close()
|
2016-01-27 00:12:54 +00:00
|
|
|
// Send string
|
2016-05-23 12:33:43 +00:00
|
|
|
msg := []byte(n.Send)
|
2016-01-27 00:12:54 +00:00
|
|
|
conn.Write(msg)
|
|
|
|
// Read string
|
|
|
|
// Set read timeout
|
2016-05-23 12:33:43 +00:00
|
|
|
conn.SetReadDeadline(time.Now().Add(n.ReadTimeout.Duration))
|
2016-01-27 00:12:54 +00:00
|
|
|
// Read
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
_, _, err = conn.ReadFromUDP(buf)
|
|
|
|
// Stop timer
|
|
|
|
responseTime := time.Since(start).Seconds()
|
|
|
|
// Handle error
|
|
|
|
if err != nil {
|
2018-05-08 23:07:15 +00:00
|
|
|
tags["result_text"] = "read_failed"
|
|
|
|
fields["result_code"] = 1
|
2017-07-14 17:43:36 +00:00
|
|
|
fields["result_type"] = "read_failed"
|
2018-05-08 23:07:15 +00:00
|
|
|
return tags, fields
|
|
|
|
}
|
|
|
|
|
|
|
|
// Looking for string in answer
|
|
|
|
RegEx := regexp.MustCompile(`.*` + n.Expect + `.*`)
|
|
|
|
find := RegEx.FindString(string(buf))
|
|
|
|
if find != "" {
|
|
|
|
tags["result_text"] = "success"
|
|
|
|
fields["result_code"] = 0
|
|
|
|
fields["result_type"] = "success"
|
|
|
|
fields["string_found"] = true
|
2016-01-27 00:12:54 +00:00
|
|
|
} else {
|
2018-05-08 23:07:15 +00:00
|
|
|
tags["result_text"] = "string_mismatch"
|
|
|
|
fields["result_code"] = 1
|
|
|
|
fields["result_type"] = "string_mismatch"
|
|
|
|
fields["string_found"] = false
|
2016-01-27 00:12:54 +00:00
|
|
|
}
|
2018-05-08 23:07:15 +00:00
|
|
|
|
2016-01-27 00:12:54 +00:00
|
|
|
fields["response_time"] = responseTime
|
2018-05-08 23:07:15 +00:00
|
|
|
|
|
|
|
return tags, fields
|
2016-01-27 00:12:54 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 23:07:15 +00:00
|
|
|
// Gather is called by telegraf when the plugin is executed on its interval.
|
|
|
|
// It will call either UDPGather or TCPGather based on the configuration and
|
|
|
|
// also fill an Accumulator that is supplied.
|
2016-05-23 12:33:43 +00:00
|
|
|
func (n *NetResponse) Gather(acc telegraf.Accumulator) error {
|
2016-01-27 00:12:54 +00:00
|
|
|
// Set default values
|
2016-05-23 12:33:43 +00:00
|
|
|
if n.Timeout.Duration == 0 {
|
|
|
|
n.Timeout.Duration = time.Second
|
2016-01-27 00:12:54 +00:00
|
|
|
}
|
2016-05-23 12:33:43 +00:00
|
|
|
if n.ReadTimeout.Duration == 0 {
|
|
|
|
n.ReadTimeout.Duration = time.Second
|
2016-01-27 00:12:54 +00:00
|
|
|
}
|
|
|
|
// Check send and expected string
|
2016-05-23 12:33:43 +00:00
|
|
|
if n.Protocol == "udp" && n.Send == "" {
|
2016-01-27 00:12:54 +00:00
|
|
|
return errors.New("Send string cannot be empty")
|
|
|
|
}
|
2016-05-23 12:33:43 +00:00
|
|
|
if n.Protocol == "udp" && n.Expect == "" {
|
2016-01-27 00:12:54 +00:00
|
|
|
return errors.New("Expected string cannot be empty")
|
|
|
|
}
|
|
|
|
// Prepare host and port
|
2016-05-23 12:33:43 +00:00
|
|
|
host, port, err := net.SplitHostPort(n.Address)
|
2016-01-27 00:12:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if host == "" {
|
2016-05-23 12:33:43 +00:00
|
|
|
n.Address = "localhost:" + port
|
2016-01-27 00:12:54 +00:00
|
|
|
}
|
|
|
|
if port == "" {
|
|
|
|
return errors.New("Bad port")
|
|
|
|
}
|
|
|
|
// Prepare data
|
2016-03-10 19:41:03 +00:00
|
|
|
tags := map[string]string{"server": host, "port": port}
|
2016-01-27 00:12:54 +00:00
|
|
|
var fields map[string]interface{}
|
2018-05-08 23:07:15 +00:00
|
|
|
var returnTags map[string]string
|
2016-01-27 00:12:54 +00:00
|
|
|
// Gather data
|
2016-05-23 12:33:43 +00:00
|
|
|
if n.Protocol == "tcp" {
|
2018-05-08 23:07:15 +00:00
|
|
|
returnTags, fields = n.TCPGather()
|
2016-01-27 00:12:54 +00:00
|
|
|
tags["protocol"] = "tcp"
|
2016-05-23 12:33:43 +00:00
|
|
|
} else if n.Protocol == "udp" {
|
2018-05-08 23:07:15 +00:00
|
|
|
returnTags, fields = n.UDPGather()
|
2016-01-27 00:12:54 +00:00
|
|
|
tags["protocol"] = "udp"
|
|
|
|
} else {
|
|
|
|
return errors.New("Bad protocol")
|
|
|
|
}
|
2018-05-08 23:07:15 +00:00
|
|
|
for key, value := range returnTags {
|
|
|
|
tags[key] = value
|
|
|
|
}
|
|
|
|
// Merge the tags
|
|
|
|
for k, v := range returnTags {
|
|
|
|
tags[k] = v
|
2016-01-27 00:12:54 +00:00
|
|
|
}
|
|
|
|
// Add metrics
|
|
|
|
acc.AddFields("net_response", fields, tags)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
inputs.Add("net_response", func() telegraf.Input {
|
|
|
|
return &NetResponse{}
|
|
|
|
})
|
|
|
|
}
|