Utilize timeout in net_response plugin.
Also changing the net_response and http_response plugins to only accept duration strings for their timeout parameters. This is a breaking config file change. closes #1214
This commit is contained in:
parent
c6699c36d3
commit
c44ecf54a5
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
### Release Notes
|
### Release Notes
|
||||||
|
|
||||||
|
- net_response and http_response plugins timeouts will now accept duration
|
||||||
|
strings, ie, "2s" or "500ms".
|
||||||
- Input plugin Gathers will no longer be logged by default, but a Gather for
|
- Input plugin Gathers will no longer be logged by default, but a Gather for
|
||||||
_each_ plugin will be logged in Debug mode.
|
_each_ plugin will be logged in Debug mode.
|
||||||
- Debug mode will no longer print every point added to the accumulator. This
|
- Debug mode will no longer print every point added to the accumulator. This
|
||||||
|
@ -24,6 +26,7 @@ to "stdout".
|
||||||
- [#1228](https://github.com/influxdata/telegraf/pull/1228): Fix service plugin host tag overwrite.
|
- [#1228](https://github.com/influxdata/telegraf/pull/1228): Fix service plugin host tag overwrite.
|
||||||
- [#1198](https://github.com/influxdata/telegraf/pull/1198): http_response: override request Host header properly
|
- [#1198](https://github.com/influxdata/telegraf/pull/1198): http_response: override request Host header properly
|
||||||
- [#1230](https://github.com/influxdata/telegraf/issues/1230): Fix Telegraf process hangup due to a single plugin hanging.
|
- [#1230](https://github.com/influxdata/telegraf/issues/1230): Fix Telegraf process hangup due to a single plugin hanging.
|
||||||
|
- [#1214](https://github.com/influxdata/telegraf/issues/1214): Use TCP timeout argument in net_response plugin.
|
||||||
|
|
||||||
## v0.13 [2016-05-11]
|
## v0.13 [2016-05-11]
|
||||||
|
|
||||||
|
|
|
@ -137,16 +137,15 @@ func (a *Agent) gatherer(
|
||||||
}
|
}
|
||||||
|
|
||||||
gatherWithTimeout(shutdown, input, acc, interval)
|
gatherWithTimeout(shutdown, input, acc, interval)
|
||||||
|
|
||||||
elapsed := time.Since(start)
|
elapsed := time.Since(start)
|
||||||
if a.Config.Agent.Debug {
|
|
||||||
log.Printf("Input [%s] gathered metrics, (%s interval) in %s\n",
|
|
||||||
input.Name, interval, elapsed)
|
|
||||||
}
|
|
||||||
|
|
||||||
if outerr != nil {
|
if outerr != nil {
|
||||||
return outerr
|
return outerr
|
||||||
}
|
}
|
||||||
|
if a.Config.Agent.Debug {
|
||||||
|
log.Printf("Input [%s] gathered metrics, (%s interval) in %s\n",
|
||||||
|
input.Name, interval, elapsed)
|
||||||
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-shutdown:
|
case <-shutdown:
|
||||||
|
|
|
@ -680,7 +680,7 @@
|
||||||
# ## Server address (default http://localhost)
|
# ## Server address (default http://localhost)
|
||||||
# address = "http://github.com"
|
# address = "http://github.com"
|
||||||
# ## Set response_timeout (default 5 seconds)
|
# ## Set response_timeout (default 5 seconds)
|
||||||
# response_timeout = 5
|
# response_timeout = "5s"
|
||||||
# ## HTTP Request Method
|
# ## HTTP Request Method
|
||||||
# method = "GET"
|
# method = "GET"
|
||||||
# ## Whether to follow redirects from the server (defaults to false)
|
# ## Whether to follow redirects from the server (defaults to false)
|
||||||
|
@ -946,14 +946,15 @@
|
||||||
# protocol = "tcp"
|
# protocol = "tcp"
|
||||||
# ## Server address (default localhost)
|
# ## Server address (default localhost)
|
||||||
# address = "github.com:80"
|
# address = "github.com:80"
|
||||||
# ## Set timeout (default 1.0 seconds)
|
# ## Set timeout
|
||||||
# timeout = 1.0
|
# timeout = "1s"
|
||||||
# ## Set read timeout (default 1.0 seconds)
|
#
|
||||||
# read_timeout = 1.0
|
|
||||||
# ## Optional string sent to the server
|
# ## Optional string sent to the server
|
||||||
# # send = "ssh"
|
# # send = "ssh"
|
||||||
# ## Optional expected string in answer
|
# ## Optional expected string in answer
|
||||||
# # expect = "ssh"
|
# # expect = "ssh"
|
||||||
|
# ## Set read timeout (only used if expecting a response)
|
||||||
|
# read_timeout = "1s"
|
||||||
|
|
||||||
|
|
||||||
# # Read TCP metrics such as established, time wait and sockets counts.
|
# # Read TCP metrics such as established, time wait and sockets counts.
|
||||||
|
@ -1144,6 +1145,9 @@
|
||||||
# ## user as argument for pgrep (ie, pgrep -u <user>)
|
# ## user as argument for pgrep (ie, pgrep -u <user>)
|
||||||
# # user = "nginx"
|
# # user = "nginx"
|
||||||
#
|
#
|
||||||
|
# ## override for process_name
|
||||||
|
# ## This is optional; default is sourced from /proc/<pid>/status
|
||||||
|
# # process_name = "bar"
|
||||||
# ## Field name prefix
|
# ## Field name prefix
|
||||||
# prefix = ""
|
# prefix = ""
|
||||||
# ## comment this out if you want raw cpu_time stats
|
# ## comment this out if you want raw cpu_time stats
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
@ -32,12 +33,25 @@ type Duration struct {
|
||||||
|
|
||||||
// UnmarshalTOML parses the duration from the TOML config file
|
// UnmarshalTOML parses the duration from the TOML config file
|
||||||
func (d *Duration) UnmarshalTOML(b []byte) error {
|
func (d *Duration) UnmarshalTOML(b []byte) error {
|
||||||
dur, err := time.ParseDuration(string(b[1 : len(b)-1]))
|
var err error
|
||||||
if err != nil {
|
// Parse string duration, ie, "1s"
|
||||||
return err
|
d.Duration, err = time.ParseDuration(string(b[1 : len(b)-1]))
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
d.Duration = dur
|
// First try parsing as integer seconds
|
||||||
|
sI, err := strconv.ParseInt(string(b), 10, 64)
|
||||||
|
if err == nil {
|
||||||
|
d.Duration = time.Second * time.Duration(sI)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// Second try parsing as float seconds
|
||||||
|
sF, err := strconv.ParseFloat(string(b), 64)
|
||||||
|
if err == nil {
|
||||||
|
d.Duration = time.Second * time.Duration(sF)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,23 +5,23 @@ This input plugin will test HTTP/HTTPS connections.
|
||||||
### Configuration:
|
### Configuration:
|
||||||
|
|
||||||
```
|
```
|
||||||
# List of UDP/TCP connections you want to check
|
# HTTP/HTTPS request given an address a method and a timeout
|
||||||
[[inputs.http_response]]
|
[[inputs.http_response]]
|
||||||
## Server address (default http://localhost)
|
## Server address (default http://localhost)
|
||||||
address = "http://github.com"
|
address = "http://github.com"
|
||||||
## Set response_timeout (default 5 seconds)
|
## Set response_timeout (default 5 seconds)
|
||||||
response_timeout = 5
|
response_timeout = "5s"
|
||||||
## HTTP Request Method
|
## HTTP Request Method
|
||||||
method = "GET"
|
method = "GET"
|
||||||
## HTTP Request Headers
|
|
||||||
[inputs.http_response.headers]
|
|
||||||
Host = github.com
|
|
||||||
## Whether to follow redirects from the server (defaults to false)
|
## Whether to follow redirects from the server (defaults to false)
|
||||||
follow_redirects = true
|
follow_redirects = true
|
||||||
|
## HTTP Request Headers (all values must be strings)
|
||||||
|
# [inputs.http_response.headers]
|
||||||
|
# Host = "github.com"
|
||||||
## Optional HTTP Request Body
|
## Optional HTTP Request Body
|
||||||
body = '''
|
# body = '''
|
||||||
{'fake':'data'}
|
# {'fake':'data'}
|
||||||
'''
|
# '''
|
||||||
```
|
```
|
||||||
|
|
||||||
### Measurements & Fields:
|
### Measurements & Fields:
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
|
"github.com/influxdata/telegraf/internal"
|
||||||
"github.com/influxdata/telegraf/plugins/inputs"
|
"github.com/influxdata/telegraf/plugins/inputs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,7 +18,7 @@ type HTTPResponse struct {
|
||||||
Address string
|
Address string
|
||||||
Body string
|
Body string
|
||||||
Method string
|
Method string
|
||||||
ResponseTimeout int
|
ResponseTimeout internal.Duration
|
||||||
Headers map[string]string
|
Headers map[string]string
|
||||||
FollowRedirects bool
|
FollowRedirects bool
|
||||||
}
|
}
|
||||||
|
@ -31,7 +32,7 @@ var sampleConfig = `
|
||||||
## Server address (default http://localhost)
|
## Server address (default http://localhost)
|
||||||
address = "http://github.com"
|
address = "http://github.com"
|
||||||
## Set response_timeout (default 5 seconds)
|
## Set response_timeout (default 5 seconds)
|
||||||
response_timeout = 5
|
response_timeout = "5s"
|
||||||
## HTTP Request Method
|
## HTTP Request Method
|
||||||
method = "GET"
|
method = "GET"
|
||||||
## Whether to follow redirects from the server (defaults to false)
|
## Whether to follow redirects from the server (defaults to false)
|
||||||
|
@ -57,7 +58,7 @@ var ErrRedirectAttempted = errors.New("redirect")
|
||||||
// timeout period and can follow redirects if specified
|
// timeout period and can follow redirects if specified
|
||||||
func CreateHttpClient(followRedirects bool, ResponseTimeout time.Duration) *http.Client {
|
func CreateHttpClient(followRedirects bool, ResponseTimeout time.Duration) *http.Client {
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: time.Second * ResponseTimeout,
|
Timeout: ResponseTimeout,
|
||||||
}
|
}
|
||||||
|
|
||||||
if followRedirects == false {
|
if followRedirects == false {
|
||||||
|
@ -73,7 +74,7 @@ func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
|
||||||
// Prepare fields
|
// Prepare fields
|
||||||
fields := make(map[string]interface{})
|
fields := make(map[string]interface{})
|
||||||
|
|
||||||
client := CreateHttpClient(h.FollowRedirects, time.Duration(h.ResponseTimeout))
|
client := CreateHttpClient(h.FollowRedirects, h.ResponseTimeout.Duration)
|
||||||
|
|
||||||
var body io.Reader
|
var body io.Reader
|
||||||
if h.Body != "" {
|
if h.Body != "" {
|
||||||
|
@ -113,8 +114,8 @@ func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
|
||||||
// Gather gets all metric fields and tags and returns any errors it encounters
|
// Gather gets all metric fields and tags and returns any errors it encounters
|
||||||
func (h *HTTPResponse) Gather(acc telegraf.Accumulator) error {
|
func (h *HTTPResponse) Gather(acc telegraf.Accumulator) error {
|
||||||
// Set default values
|
// Set default values
|
||||||
if h.ResponseTimeout < 1 {
|
if h.ResponseTimeout.Duration < time.Second {
|
||||||
h.ResponseTimeout = 5
|
h.ResponseTimeout.Duration = time.Second * 5
|
||||||
}
|
}
|
||||||
// Check send and expected string
|
// Check send and expected string
|
||||||
if h.Method == "" {
|
if h.Method == "" {
|
||||||
|
|
|
@ -2,13 +2,16 @@ package http_response
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/influxdata/telegraf/internal"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func setUpTestMux() http.Handler {
|
func setUpTestMux() http.Handler {
|
||||||
|
@ -61,7 +64,7 @@ func TestHeaders(t *testing.T) {
|
||||||
h := &HTTPResponse{
|
h := &HTTPResponse{
|
||||||
Address: ts.URL,
|
Address: ts.URL,
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
ResponseTimeout: 2,
|
ResponseTimeout: internal.Duration{Duration: time.Second * 2},
|
||||||
Headers: map[string]string{
|
Headers: map[string]string{
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Host": "Hello",
|
"Host": "Hello",
|
||||||
|
@ -85,7 +88,7 @@ func TestFields(t *testing.T) {
|
||||||
Address: ts.URL + "/good",
|
Address: ts.URL + "/good",
|
||||||
Body: "{ 'test': 'data'}",
|
Body: "{ 'test': 'data'}",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
ResponseTimeout: 20,
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
||||||
Headers: map[string]string{
|
Headers: map[string]string{
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
|
@ -109,7 +112,7 @@ func TestRedirects(t *testing.T) {
|
||||||
Address: ts.URL + "/redirect",
|
Address: ts.URL + "/redirect",
|
||||||
Body: "{ 'test': 'data'}",
|
Body: "{ 'test': 'data'}",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
ResponseTimeout: 20,
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
||||||
Headers: map[string]string{
|
Headers: map[string]string{
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
|
@ -126,7 +129,7 @@ func TestRedirects(t *testing.T) {
|
||||||
Address: ts.URL + "/badredirect",
|
Address: ts.URL + "/badredirect",
|
||||||
Body: "{ 'test': 'data'}",
|
Body: "{ 'test': 'data'}",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
ResponseTimeout: 20,
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
||||||
Headers: map[string]string{
|
Headers: map[string]string{
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
|
@ -145,7 +148,7 @@ func TestMethod(t *testing.T) {
|
||||||
Address: ts.URL + "/mustbepostmethod",
|
Address: ts.URL + "/mustbepostmethod",
|
||||||
Body: "{ 'test': 'data'}",
|
Body: "{ 'test': 'data'}",
|
||||||
Method: "POST",
|
Method: "POST",
|
||||||
ResponseTimeout: 20,
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
||||||
Headers: map[string]string{
|
Headers: map[string]string{
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
|
@ -162,7 +165,7 @@ func TestMethod(t *testing.T) {
|
||||||
Address: ts.URL + "/mustbepostmethod",
|
Address: ts.URL + "/mustbepostmethod",
|
||||||
Body: "{ 'test': 'data'}",
|
Body: "{ 'test': 'data'}",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
ResponseTimeout: 20,
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
||||||
Headers: map[string]string{
|
Headers: map[string]string{
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
|
@ -180,7 +183,7 @@ func TestMethod(t *testing.T) {
|
||||||
Address: ts.URL + "/mustbepostmethod",
|
Address: ts.URL + "/mustbepostmethod",
|
||||||
Body: "{ 'test': 'data'}",
|
Body: "{ 'test': 'data'}",
|
||||||
Method: "head",
|
Method: "head",
|
||||||
ResponseTimeout: 20,
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
||||||
Headers: map[string]string{
|
Headers: map[string]string{
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
|
@ -203,7 +206,7 @@ func TestBody(t *testing.T) {
|
||||||
Address: ts.URL + "/musthaveabody",
|
Address: ts.URL + "/musthaveabody",
|
||||||
Body: "{ 'test': 'data'}",
|
Body: "{ 'test': 'data'}",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
ResponseTimeout: 20,
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
||||||
Headers: map[string]string{
|
Headers: map[string]string{
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
|
@ -219,7 +222,7 @@ func TestBody(t *testing.T) {
|
||||||
h = &HTTPResponse{
|
h = &HTTPResponse{
|
||||||
Address: ts.URL + "/musthaveabody",
|
Address: ts.URL + "/musthaveabody",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
ResponseTimeout: 20,
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
||||||
Headers: map[string]string{
|
Headers: map[string]string{
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
|
@ -242,7 +245,7 @@ func TestTimeout(t *testing.T) {
|
||||||
Address: ts.URL + "/twosecondnap",
|
Address: ts.URL + "/twosecondnap",
|
||||||
Body: "{ 'test': 'data'}",
|
Body: "{ 'test': 'data'}",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
ResponseTimeout: 1,
|
ResponseTimeout: internal.Duration{Duration: time.Second * 1},
|
||||||
Headers: map[string]string{
|
Headers: map[string]string{
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
|
|
|
@ -6,41 +6,30 @@ It can also check response text.
|
||||||
### Configuration:
|
### Configuration:
|
||||||
|
|
||||||
```
|
```
|
||||||
# List of UDP/TCP connections you want to check
|
|
||||||
[[inputs.net_response]]
|
|
||||||
protocol = "tcp"
|
|
||||||
# Server address (default IP localhost)
|
|
||||||
address = "github.com:80"
|
|
||||||
# Set timeout (default 1.0)
|
|
||||||
timeout = 1.0
|
|
||||||
# Set read timeout (default 1.0)
|
|
||||||
read_timeout = 1.0
|
|
||||||
# String sent to the server
|
|
||||||
send = "ssh"
|
|
||||||
# Expected string in answer
|
|
||||||
expect = "ssh"
|
|
||||||
|
|
||||||
[[inputs.net_response]]
|
[[inputs.net_response]]
|
||||||
protocol = "tcp"
|
protocol = "tcp"
|
||||||
address = ":80"
|
address = ":80"
|
||||||
|
|
||||||
|
# TCP or UDP 'ping' given url and collect response time in seconds
|
||||||
[[inputs.net_response]]
|
[[inputs.net_response]]
|
||||||
protocol = "udp"
|
## Protocol, must be "tcp" or "udp"
|
||||||
# Server address (default IP localhost)
|
protocol = "tcp"
|
||||||
|
## Server address (default localhost)
|
||||||
address = "github.com:80"
|
address = "github.com:80"
|
||||||
# Set timeout (default 1.0)
|
## Set timeout
|
||||||
timeout = 1.0
|
timeout = "1s"
|
||||||
# Set read timeout (default 1.0)
|
|
||||||
read_timeout = 1.0
|
## Optional string sent to the server
|
||||||
# String sent to the server
|
|
||||||
send = "ssh"
|
send = "ssh"
|
||||||
# Expected string in answer
|
## Optional expected string in answer
|
||||||
expect = "ssh"
|
expect = "ssh"
|
||||||
|
## Set read timeout (only used if expecting a response)
|
||||||
|
read_timeout = "1s"
|
||||||
|
|
||||||
[[inputs.net_response]]
|
[[inputs.net_response]]
|
||||||
protocol = "udp"
|
protocol = "udp"
|
||||||
address = "localhost:161"
|
address = "localhost:161"
|
||||||
timeout = 2.0
|
timeout = "2s"
|
||||||
```
|
```
|
||||||
|
|
||||||
### Measurements & Fields:
|
### Measurements & Fields:
|
||||||
|
|
|
@ -9,14 +9,15 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
|
"github.com/influxdata/telegraf/internal"
|
||||||
"github.com/influxdata/telegraf/plugins/inputs"
|
"github.com/influxdata/telegraf/plugins/inputs"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NetResponses struct
|
// NetResponses struct
|
||||||
type NetResponse struct {
|
type NetResponse struct {
|
||||||
Address string
|
Address string
|
||||||
Timeout float64
|
Timeout internal.Duration
|
||||||
ReadTimeout float64
|
ReadTimeout internal.Duration
|
||||||
Send string
|
Send string
|
||||||
Expect string
|
Expect string
|
||||||
Protocol string
|
Protocol string
|
||||||
|
@ -31,29 +32,28 @@ var sampleConfig = `
|
||||||
protocol = "tcp"
|
protocol = "tcp"
|
||||||
## Server address (default localhost)
|
## Server address (default localhost)
|
||||||
address = "github.com:80"
|
address = "github.com:80"
|
||||||
## Set timeout (default 1.0 seconds)
|
## Set timeout
|
||||||
timeout = 1.0
|
timeout = "1s"
|
||||||
## Set read timeout (default 1.0 seconds)
|
|
||||||
read_timeout = 1.0
|
|
||||||
## Optional string sent to the server
|
## Optional string sent to the server
|
||||||
# send = "ssh"
|
# send = "ssh"
|
||||||
## Optional expected string in answer
|
## Optional expected string in answer
|
||||||
# expect = "ssh"
|
# expect = "ssh"
|
||||||
|
## Set read timeout (only used if expecting a response)
|
||||||
|
read_timeout = "1s"
|
||||||
`
|
`
|
||||||
|
|
||||||
func (_ *NetResponse) SampleConfig() string {
|
func (_ *NetResponse) SampleConfig() string {
|
||||||
return sampleConfig
|
return sampleConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *NetResponse) TcpGather() (map[string]interface{}, error) {
|
func (n *NetResponse) TcpGather() (map[string]interface{}, error) {
|
||||||
// Prepare fields
|
// Prepare fields
|
||||||
fields := make(map[string]interface{})
|
fields := make(map[string]interface{})
|
||||||
// Start Timer
|
// Start Timer
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
// Resolving
|
|
||||||
tcpAddr, err := net.ResolveTCPAddr("tcp", t.Address)
|
|
||||||
// Connecting
|
// Connecting
|
||||||
conn, err := net.DialTCP("tcp", nil, tcpAddr)
|
conn, err := net.DialTimeout("tcp", n.Address, n.Timeout.Duration)
|
||||||
// Stop timer
|
// Stop timer
|
||||||
responseTime := time.Since(start).Seconds()
|
responseTime := time.Since(start).Seconds()
|
||||||
// Handle error
|
// Handle error
|
||||||
|
@ -62,17 +62,16 @@ func (t *NetResponse) TcpGather() (map[string]interface{}, error) {
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
// Send string if needed
|
// Send string if needed
|
||||||
if t.Send != "" {
|
if n.Send != "" {
|
||||||
msg := []byte(t.Send)
|
msg := []byte(n.Send)
|
||||||
conn.Write(msg)
|
conn.Write(msg)
|
||||||
conn.CloseWrite()
|
|
||||||
// Stop timer
|
// Stop timer
|
||||||
responseTime = time.Since(start).Seconds()
|
responseTime = time.Since(start).Seconds()
|
||||||
}
|
}
|
||||||
// Read string if needed
|
// Read string if needed
|
||||||
if t.Expect != "" {
|
if n.Expect != "" {
|
||||||
// Set read timeout
|
// Set read timeout
|
||||||
conn.SetReadDeadline(time.Now().Add(time.Duration(t.ReadTimeout) * time.Second))
|
conn.SetReadDeadline(time.Now().Add(n.ReadTimeout.Duration))
|
||||||
// Prepare reader
|
// Prepare reader
|
||||||
reader := bufio.NewReader(conn)
|
reader := bufio.NewReader(conn)
|
||||||
tp := textproto.NewReader(reader)
|
tp := textproto.NewReader(reader)
|
||||||
|
@ -85,7 +84,7 @@ func (t *NetResponse) TcpGather() (map[string]interface{}, error) {
|
||||||
fields["string_found"] = false
|
fields["string_found"] = false
|
||||||
} else {
|
} else {
|
||||||
// Looking for string in answer
|
// Looking for string in answer
|
||||||
RegEx := regexp.MustCompile(`.*` + t.Expect + `.*`)
|
RegEx := regexp.MustCompile(`.*` + n.Expect + `.*`)
|
||||||
find := RegEx.FindString(string(data))
|
find := RegEx.FindString(string(data))
|
||||||
if find != "" {
|
if find != "" {
|
||||||
fields["string_found"] = true
|
fields["string_found"] = true
|
||||||
|
@ -99,13 +98,13 @@ func (t *NetResponse) TcpGather() (map[string]interface{}, error) {
|
||||||
return fields, nil
|
return fields, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *NetResponse) UdpGather() (map[string]interface{}, error) {
|
func (n *NetResponse) UdpGather() (map[string]interface{}, error) {
|
||||||
// Prepare fields
|
// Prepare fields
|
||||||
fields := make(map[string]interface{})
|
fields := make(map[string]interface{})
|
||||||
// Start Timer
|
// Start Timer
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
// Resolving
|
// Resolving
|
||||||
udpAddr, err := net.ResolveUDPAddr("udp", u.Address)
|
udpAddr, err := net.ResolveUDPAddr("udp", n.Address)
|
||||||
LocalAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
|
LocalAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
|
||||||
// Connecting
|
// Connecting
|
||||||
conn, err := net.DialUDP("udp", LocalAddr, udpAddr)
|
conn, err := net.DialUDP("udp", LocalAddr, udpAddr)
|
||||||
|
@ -115,11 +114,11 @@ func (u *NetResponse) UdpGather() (map[string]interface{}, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Send string
|
// Send string
|
||||||
msg := []byte(u.Send)
|
msg := []byte(n.Send)
|
||||||
conn.Write(msg)
|
conn.Write(msg)
|
||||||
// Read string
|
// Read string
|
||||||
// Set read timeout
|
// Set read timeout
|
||||||
conn.SetReadDeadline(time.Now().Add(time.Duration(u.ReadTimeout) * time.Second))
|
conn.SetReadDeadline(time.Now().Add(n.ReadTimeout.Duration))
|
||||||
// Read
|
// Read
|
||||||
buf := make([]byte, 1024)
|
buf := make([]byte, 1024)
|
||||||
_, _, err = conn.ReadFromUDP(buf)
|
_, _, err = conn.ReadFromUDP(buf)
|
||||||
|
@ -130,7 +129,7 @@ func (u *NetResponse) UdpGather() (map[string]interface{}, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
// Looking for string in answer
|
// Looking for string in answer
|
||||||
RegEx := regexp.MustCompile(`.*` + u.Expect + `.*`)
|
RegEx := regexp.MustCompile(`.*` + n.Expect + `.*`)
|
||||||
find := RegEx.FindString(string(buf))
|
find := RegEx.FindString(string(buf))
|
||||||
if find != "" {
|
if find != "" {
|
||||||
fields["string_found"] = true
|
fields["string_found"] = true
|
||||||
|
@ -142,28 +141,28 @@ func (u *NetResponse) UdpGather() (map[string]interface{}, error) {
|
||||||
return fields, nil
|
return fields, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *NetResponse) Gather(acc telegraf.Accumulator) error {
|
func (n *NetResponse) Gather(acc telegraf.Accumulator) error {
|
||||||
// Set default values
|
// Set default values
|
||||||
if c.Timeout == 0 {
|
if n.Timeout.Duration == 0 {
|
||||||
c.Timeout = 1.0
|
n.Timeout.Duration = time.Second
|
||||||
}
|
}
|
||||||
if c.ReadTimeout == 0 {
|
if n.ReadTimeout.Duration == 0 {
|
||||||
c.ReadTimeout = 1.0
|
n.ReadTimeout.Duration = time.Second
|
||||||
}
|
}
|
||||||
// Check send and expected string
|
// Check send and expected string
|
||||||
if c.Protocol == "udp" && c.Send == "" {
|
if n.Protocol == "udp" && n.Send == "" {
|
||||||
return errors.New("Send string cannot be empty")
|
return errors.New("Send string cannot be empty")
|
||||||
}
|
}
|
||||||
if c.Protocol == "udp" && c.Expect == "" {
|
if n.Protocol == "udp" && n.Expect == "" {
|
||||||
return errors.New("Expected string cannot be empty")
|
return errors.New("Expected string cannot be empty")
|
||||||
}
|
}
|
||||||
// Prepare host and port
|
// Prepare host and port
|
||||||
host, port, err := net.SplitHostPort(c.Address)
|
host, port, err := net.SplitHostPort(n.Address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if host == "" {
|
if host == "" {
|
||||||
c.Address = "localhost:" + port
|
n.Address = "localhost:" + port
|
||||||
}
|
}
|
||||||
if port == "" {
|
if port == "" {
|
||||||
return errors.New("Bad port")
|
return errors.New("Bad port")
|
||||||
|
@ -172,11 +171,11 @@ func (c *NetResponse) Gather(acc telegraf.Accumulator) error {
|
||||||
tags := map[string]string{"server": host, "port": port}
|
tags := map[string]string{"server": host, "port": port}
|
||||||
var fields map[string]interface{}
|
var fields map[string]interface{}
|
||||||
// Gather data
|
// Gather data
|
||||||
if c.Protocol == "tcp" {
|
if n.Protocol == "tcp" {
|
||||||
fields, err = c.TcpGather()
|
fields, err = n.TcpGather()
|
||||||
tags["protocol"] = "tcp"
|
tags["protocol"] = "tcp"
|
||||||
} else if c.Protocol == "udp" {
|
} else if n.Protocol == "udp" {
|
||||||
fields, err = c.UdpGather()
|
fields, err = n.UdpGather()
|
||||||
tags["protocol"] = "udp"
|
tags["protocol"] = "udp"
|
||||||
} else {
|
} else {
|
||||||
return errors.New("Bad protocol")
|
return errors.New("Bad protocol")
|
||||||
|
|
|
@ -5,7 +5,9 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/influxdata/telegraf/internal"
|
||||||
"github.com/influxdata/telegraf/testutil"
|
"github.com/influxdata/telegraf/testutil"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -35,7 +37,7 @@ func TestTCPError(t *testing.T) {
|
||||||
// Error
|
// Error
|
||||||
err1 := c.Gather(&acc)
|
err1 := c.Gather(&acc)
|
||||||
require.Error(t, err1)
|
require.Error(t, err1)
|
||||||
assert.Equal(t, "dial tcp 127.0.0.1:9999: getsockopt: connection refused", err1.Error())
|
assert.Contains(t, err1.Error(), "getsockopt: connection refused")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTCPOK1(t *testing.T) {
|
func TestTCPOK1(t *testing.T) {
|
||||||
|
@ -46,8 +48,8 @@ func TestTCPOK1(t *testing.T) {
|
||||||
Address: "127.0.0.1:2004",
|
Address: "127.0.0.1:2004",
|
||||||
Send: "test",
|
Send: "test",
|
||||||
Expect: "test",
|
Expect: "test",
|
||||||
ReadTimeout: 3.0,
|
ReadTimeout: internal.Duration{Duration: time.Second * 3},
|
||||||
Timeout: 1.0,
|
Timeout: internal.Duration{Duration: time.Second},
|
||||||
Protocol: "tcp",
|
Protocol: "tcp",
|
||||||
}
|
}
|
||||||
// Start TCP server
|
// Start TCP server
|
||||||
|
@ -86,8 +88,8 @@ func TestTCPOK2(t *testing.T) {
|
||||||
Address: "127.0.0.1:2004",
|
Address: "127.0.0.1:2004",
|
||||||
Send: "test",
|
Send: "test",
|
||||||
Expect: "test2",
|
Expect: "test2",
|
||||||
ReadTimeout: 3.0,
|
ReadTimeout: internal.Duration{Duration: time.Second * 3},
|
||||||
Timeout: 1.0,
|
Timeout: internal.Duration{Duration: time.Second},
|
||||||
Protocol: "tcp",
|
Protocol: "tcp",
|
||||||
}
|
}
|
||||||
// Start TCP server
|
// Start TCP server
|
||||||
|
@ -141,8 +143,8 @@ func TestUDPOK1(t *testing.T) {
|
||||||
Address: "127.0.0.1:2004",
|
Address: "127.0.0.1:2004",
|
||||||
Send: "test",
|
Send: "test",
|
||||||
Expect: "test",
|
Expect: "test",
|
||||||
ReadTimeout: 3.0,
|
ReadTimeout: internal.Duration{Duration: time.Second * 3},
|
||||||
Timeout: 1.0,
|
Timeout: internal.Duration{Duration: time.Second},
|
||||||
Protocol: "udp",
|
Protocol: "udp",
|
||||||
}
|
}
|
||||||
// Start UDP server
|
// Start UDP server
|
||||||
|
|
Loading…
Reference in New Issue