update to 5 second default and string map for headers

This commit is contained in:
Luke Swithenbank 2016-04-07 11:57:49 +10:00 committed by Michele Fadda
parent 3fcc81866a
commit 42e4cba6bd
3 changed files with 54 additions and 67 deletions

View File

@ -9,14 +9,13 @@ This input plugin will test HTTP/HTTPS connections.
[[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 10 seconds) ## Set response_timeout (default 5 seconds)
response_timeout = 10 response_timeout = 5
## HTTP Request Method ## HTTP Request Method
method = "GET" method = "GET"
## HTTP Request Headers ## HTTP Request Headers
headers = ''' [inputs.http_response.headers]
Host: github.com 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
## Optional HTTP Request Body ## Optional HTTP Request Body

View File

@ -1,11 +1,9 @@
package http_response package http_response
import ( import (
"bufio"
"errors" "errors"
"io" "io"
"net/http" "net/http"
"net/textproto"
"net/url" "net/url"
"strings" "strings"
"time" "time"
@ -20,7 +18,7 @@ type HTTPResponse struct {
Body string Body string
Method string Method string
ResponseTimeout int ResponseTimeout int
Headers string Headers map[string]string
FollowRedirects bool FollowRedirects bool
} }
@ -32,14 +30,13 @@ func (h *HTTPResponse) Description() string {
var sampleConfig = ` var sampleConfig = `
## Server address (default http://localhost) ## Server address (default http://localhost)
address = "http://github.com" address = "http://github.com"
## Set response_timeout (default 10 seconds) ## Set response_timeout (default 5 seconds)
response_timeout = 10 response_timeout = 5
## HTTP Request Method ## HTTP Request Method
method = "GET" method = "GET"
## HTTP Request Headers ## HTTP Request Headers (all values must be strings)
headers = ''' [inputs.http_response.headers]
Host: github.com # 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
## Optional HTTP Request Body ## Optional HTTP Request Body
@ -71,17 +68,14 @@ func CreateHttpClient(followRedirects bool, ResponseTimeout time.Duration) *http
return client return client
} }
// ParseHeaders takes a string of newline seperated http headers and returns a // CreateHeaders takes a map of header strings and puts them
// http.Header object. An error is returned if the headers cannot be parsed. // into a http.Header Object
func ParseHeaders(headers string) (http.Header, error) { func CreateHeaders(headers map[string]string) http.Header {
headers = strings.TrimSpace(headers) + "\n\n" httpHeaders := make(http.Header)
reader := bufio.NewReader(strings.NewReader(headers)) for key := range headers {
tp := textproto.NewReader(reader) httpHeaders.Add(key, headers[key])
mimeHeader, err := tp.ReadMIMEHeader()
if err != nil {
return nil, err
} }
return http.Header(mimeHeader), nil return httpHeaders
} }
// HTTPGather gathers all fields and returns any errors it encounters // HTTPGather gathers all fields and returns any errors it encounters
@ -99,10 +93,8 @@ func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
request.Header, err = ParseHeaders(h.Headers) request.Header = CreateHeaders(h.Headers)
if err != nil {
return nil, err
}
// Start Timer // Start Timer
start := time.Now() start := time.Now()
resp, err := client.Do(request) resp, err := client.Do(request)
@ -126,7 +118,7 @@ func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
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 < 1 {
h.ResponseTimeout = 10 h.ResponseTimeout = 5
} }
// Check send and expected string // Check send and expected string
if h.Method == "" { if h.Method == "" {

View File

@ -11,22 +11,18 @@ import (
"time" "time"
) )
func TestParseHeaders(t *testing.T) { func TestCreateHeaders(t *testing.T) {
fakeHeaders := ` fakeHeaders := map[string]string{
Accept: text/plain "Accept": "text/plain",
Content-Type: application/json "Content-Type": "application/json",
Cache-Control: no-cache "Cache-Control": "no-cache",
` }
headers, err := ParseHeaders(fakeHeaders) headers := CreateHeaders(fakeHeaders)
require.NoError(t, err)
testHeaders := make(http.Header) testHeaders := make(http.Header)
testHeaders.Add("Accept", "text/plain") testHeaders.Add("Accept", "text/plain")
testHeaders.Add("Content-Type", "application/json") testHeaders.Add("Content-Type", "application/json")
testHeaders.Add("Cache-Control", "no-cache") testHeaders.Add("Cache-Control", "no-cache")
assert.Equal(t, testHeaders, headers) assert.Equal(t, testHeaders, headers)
headers, err = ParseHeaders("Accept text/plain")
require.Error(t, err)
} }
func setUpTestMux() http.Handler { func setUpTestMux() http.Handler {
@ -77,9 +73,9 @@ func TestFields(t *testing.T) {
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "GET", Method: "GET",
ResponseTimeout: 20, ResponseTimeout: 20,
Headers: ` Headers: map[string]string{
Content-Type: application/json "Content-Type": "application/json",
`, },
FollowRedirects: true, FollowRedirects: true,
} }
fields, err := h.HTTPGather() fields, err := h.HTTPGather()
@ -102,9 +98,9 @@ func TestRedirects(t *testing.T) {
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "GET", Method: "GET",
ResponseTimeout: 20, ResponseTimeout: 20,
Headers: ` Headers: map[string]string{
Content-Type: application/json "Content-Type": "application/json",
`, },
FollowRedirects: true, FollowRedirects: true,
} }
fields, err := h.HTTPGather() fields, err := h.HTTPGather()
@ -119,9 +115,9 @@ Content-Type: application/json
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "GET", Method: "GET",
ResponseTimeout: 20, ResponseTimeout: 20,
Headers: ` Headers: map[string]string{
Content-Type: application/json "Content-Type": "application/json",
`, },
FollowRedirects: true, FollowRedirects: true,
} }
fields, err = h.HTTPGather() fields, err = h.HTTPGather()
@ -138,9 +134,9 @@ func TestMethod(t *testing.T) {
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "POST", Method: "POST",
ResponseTimeout: 20, ResponseTimeout: 20,
Headers: ` Headers: map[string]string{
Content-Type: application/json "Content-Type": "application/json",
`, },
FollowRedirects: true, FollowRedirects: true,
} }
fields, err := h.HTTPGather() fields, err := h.HTTPGather()
@ -155,9 +151,9 @@ Content-Type: application/json
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "GET", Method: "GET",
ResponseTimeout: 20, ResponseTimeout: 20,
Headers: ` Headers: map[string]string{
Content-Type: application/json "Content-Type": "application/json",
`, },
FollowRedirects: true, FollowRedirects: true,
} }
fields, err = h.HTTPGather() fields, err = h.HTTPGather()
@ -173,9 +169,9 @@ Content-Type: application/json
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "head", Method: "head",
ResponseTimeout: 20, ResponseTimeout: 20,
Headers: ` Headers: map[string]string{
Content-Type: application/json "Content-Type": "application/json",
`, },
FollowRedirects: true, FollowRedirects: true,
} }
fields, err = h.HTTPGather() fields, err = h.HTTPGather()
@ -196,9 +192,9 @@ func TestBody(t *testing.T) {
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "GET", Method: "GET",
ResponseTimeout: 20, ResponseTimeout: 20,
Headers: ` Headers: map[string]string{
Content-Type: application/json "Content-Type": "application/json",
`, },
FollowRedirects: true, FollowRedirects: true,
} }
fields, err := h.HTTPGather() fields, err := h.HTTPGather()
@ -212,9 +208,9 @@ Content-Type: application/json
Address: ts.URL + "/musthaveabody", Address: ts.URL + "/musthaveabody",
Method: "GET", Method: "GET",
ResponseTimeout: 20, ResponseTimeout: 20,
Headers: ` Headers: map[string]string{
Content-Type: application/json "Content-Type": "application/json",
`, },
FollowRedirects: true, FollowRedirects: true,
} }
fields, err = h.HTTPGather() fields, err = h.HTTPGather()
@ -235,9 +231,9 @@ func TestTimeout(t *testing.T) {
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "GET", Method: "GET",
ResponseTimeout: 1, ResponseTimeout: 1,
Headers: ` Headers: map[string]string{
Content-Type: application/json "Content-Type": "application/json",
`, },
FollowRedirects: true, FollowRedirects: true,
} }
_, err := h.HTTPGather() _, err := h.HTTPGather()