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:04:28 +00:00
|
|
|
type ResultType uint64
|
|
|
|
|
|
|
|
const (
|
|
|
|
Success ResultType = 0
|
|
|
|
Timeout = 1
|
|
|
|
ConnectionFailed = 2
|
|
|
|
ReadFailed = 3
|
|
|
|
StringMismatch = 4
|
|
|
|
)
|
|
|
|
|
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:04:28 +00:00
|
|
|
var description = "Collect response time of a TCP or UDP connection"
|
2018-05-08 23:07:15 +00:00
|
|
|
|
|
|
|
// 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"
|
2018-05-08 23:04:28 +00:00
|
|
|
|
2016-05-23 12:33:43 +00:00
|
|
|
## Set timeout
|
2018-05-08 23:04:28 +00:00
|
|
|
# timeout = "1s"
|
2016-05-23 12:33:43 +00:00
|
|
|
|
|
|
|
## Set read timeout (only used if expecting a response)
|
2018-05-08 23:04:28 +00:00
|
|
|
# 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"
|
2018-05-08 23:04:28 +00:00
|
|
|
|
|
|
|
## Uncomment to remove deprecated fields
|
|
|
|
# fieldexclude = ["result_type", "string_found"]
|
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:04:28 +00:00
|
|
|
setResult(Timeout, fields, tags, n.Expect)
|
2017-07-14 17:43:36 +00:00
|
|
|
} else {
|
2018-05-08 23:04:28 +00:00
|
|
|
setResult(ConnectionFailed, fields, tags, n.Expect)
|
2017-07-14 17:43:36 +00:00
|
|
|
}
|
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:04:28 +00:00
|
|
|
setResult(ReadFailed, fields, tags, n.Expect)
|
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:04:28 +00:00
|
|
|
setResult(Success, fields, tags, n.Expect)
|
2016-01-27 00:12:54 +00:00
|
|
|
} else {
|
2018-05-08 23:04:28 +00:00
|
|
|
setResult(StringMismatch, fields, tags, n.Expect)
|
2016-01-27 00:12:54 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-14 17:43:36 +00:00
|
|
|
} else {
|
2018-05-08 23:04:28 +00:00
|
|
|
setResult(Success, fields, tags, n.Expect)
|
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
|
|
|
// Connecting
|
2019-01-02 21:53:58 +00:00
|
|
|
conn, err := net.DialUDP("udp", nil, udpAddr)
|
2016-01-27 00:12:54 +00:00
|
|
|
// Handle error
|
|
|
|
if err != nil {
|
2018-05-08 23:04:28 +00:00
|
|
|
setResult(ConnectionFailed, fields, tags, n.Expect)
|
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:04:28 +00:00
|
|
|
setResult(ReadFailed, fields, tags, n.Expect)
|
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 != "" {
|
2018-05-08 23:04:28 +00:00
|
|
|
setResult(Success, fields, tags, n.Expect)
|
2016-01-27 00:12:54 +00:00
|
|
|
} else {
|
2018-05-08 23:04:28 +00:00
|
|
|
setResult(StringMismatch, fields, tags, n.Expect)
|
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
|
|
|
|
}
|
|
|
|
|
2018-05-08 23:04:28 +00:00
|
|
|
func setResult(result ResultType, fields map[string]interface{}, tags map[string]string, expect string) {
|
|
|
|
var tag string
|
|
|
|
switch result {
|
|
|
|
case Success:
|
|
|
|
tag = "success"
|
|
|
|
case Timeout:
|
|
|
|
tag = "timeout"
|
|
|
|
case ConnectionFailed:
|
|
|
|
tag = "connection_failed"
|
|
|
|
case ReadFailed:
|
|
|
|
tag = "read_failed"
|
|
|
|
case StringMismatch:
|
|
|
|
tag = "string_mismatch"
|
|
|
|
}
|
|
|
|
|
|
|
|
tags["result"] = tag
|
|
|
|
fields["result_code"] = uint64(result)
|
|
|
|
|
|
|
|
// deprecated in 1.7; use result tag
|
|
|
|
fields["result_type"] = tag
|
|
|
|
|
|
|
|
// deprecated in 1.4; use result tag
|
|
|
|
if expect != "" {
|
|
|
|
fields["string_found"] = result == Success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-27 00:12:54 +00:00
|
|
|
func init() {
|
|
|
|
inputs.Add("net_response", func() telegraf.Input {
|
|
|
|
return &NetResponse{}
|
|
|
|
})
|
|
|
|
}
|