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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NetResponses struct
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (_ *NetResponse) Description() string {
|
|
|
|
return "TCP or UDP 'ping' given url and collect response time in seconds"
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
`
|
|
|
|
|
|
|
|
func (_ *NetResponse) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
2016-05-23 12:33:43 +00:00
|
|
|
func (n *NetResponse) TcpGather() (map[string]interface{}, error) {
|
2016-01-27 00:12:54 +00:00
|
|
|
// Prepare fields
|
|
|
|
fields := make(map[string]interface{})
|
|
|
|
// 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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
fields["string_found"] = false
|
|
|
|
} 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 != "" {
|
|
|
|
fields["string_found"] = true
|
|
|
|
} else {
|
|
|
|
fields["string_found"] = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
fields["response_time"] = responseTime
|
|
|
|
return fields, nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 12:33:43 +00:00
|
|
|
func (n *NetResponse) UdpGather() (map[string]interface{}, error) {
|
2016-01-27 00:12:54 +00:00
|
|
|
// Prepare fields
|
|
|
|
fields := make(map[string]interface{})
|
|
|
|
// 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)
|
|
|
|
defer conn.Close()
|
|
|
|
// Handle error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// 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 {
|
|
|
|
return nil, err
|
|
|
|
} 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(buf))
|
|
|
|
if find != "" {
|
|
|
|
fields["string_found"] = true
|
|
|
|
} else {
|
|
|
|
fields["string_found"] = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fields["response_time"] = responseTime
|
|
|
|
return fields, nil
|
|
|
|
}
|
|
|
|
|
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{}
|
|
|
|
// Gather data
|
2016-05-23 12:33:43 +00:00
|
|
|
if n.Protocol == "tcp" {
|
|
|
|
fields, err = 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" {
|
|
|
|
fields, err = n.UdpGather()
|
2016-01-27 00:12:54 +00:00
|
|
|
tags["protocol"] = "udp"
|
|
|
|
} else {
|
|
|
|
return errors.New("Bad protocol")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Add metrics
|
|
|
|
acc.AddFields("net_response", fields, tags)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
inputs.Add("net_response", func() telegraf.Input {
|
|
|
|
return &NetResponse{}
|
|
|
|
})
|
|
|
|
}
|