update to allow for following redirects

This commit is contained in:
Luke Swithenbank 2016-03-31 21:18:19 +11:00 committed by Michele Fadda
parent b5fa9b33cb
commit 693376c539
2 changed files with 41 additions and 11 deletions

View File

@ -7,12 +7,18 @@ This input plugin will test HTTP/HTTPS connections.
```
# List of UDP/TCP connections you want to check
[[inputs.http_response]]
# Server address (default http://localhost)
address = "https://github.com"
# Set http response timeout (default 10)
## Server address (default http://localhost)
address = "http://github.com"
## Set response_timeout (default 10 seconds)
response_timeout = 10
# HTTP Method (default "GET")
## HTTP Method
method = "GET"
## HTTP Request Headers
headers = '''
Host: github.com
'''
## Whether to follow redirects from the server (defaults to false)
follow_redirects = true
```
### Measurements & Fields:
@ -25,12 +31,11 @@ This input plugin will test HTTP/HTTPS connections.
- All measurements have the following tags:
- server
- port
- protocol
- method
### Example Output:
```
$ ./telegraf -config telegraf.conf -input-filter http_response -test
http_response,server=http://192.168.2.2:2000,method=GET response_time=0.18070360500000002,http_response_code=200 1454785464182527094
http_response,method=GET,server=http://www.github.com http_response_code=200i,response_time=6.223266528 1459419354977857955
```

View File

@ -3,9 +3,11 @@ package http_response
import (
"bufio"
"errors"
"fmt"
"net/http"
"net/textproto"
"net/url"
"os"
"strings"
"time"
@ -19,6 +21,7 @@ type HTTPResponse struct {
Method string
ResponseTimeout int
Headers string
FollowRedirects bool
}
// Description returns the plugin Description
@ -28,8 +31,8 @@ func (h *HTTPResponse) Description() string {
var sampleConfig = `
## Server address (default http://localhost)
address = "https://github.com"
## Set response_timeout (default 1 seconds)
address = "http://github.com"
## Set response_timeout (default 10 seconds)
response_timeout = 10
## HTTP Method
method = "GET"
@ -37,6 +40,8 @@ var sampleConfig = `
headers = '''
Host: github.com
'''
## Whether to follow redirects from the server (defaults to false)
follow_redirects = true
`
// SampleConfig returns the plugin SampleConfig
@ -44,6 +49,8 @@ func (h *HTTPResponse) SampleConfig() string {
return sampleConfig
}
var ErrRedirectAttempted = errors.New("redirect")
// HTTPGather gathers all fields and returns any errors it encounters
func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
// Prepare fields
@ -52,6 +59,14 @@ func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
client := &http.Client{
Timeout: time.Second * time.Duration(h.ResponseTimeout),
}
if h.FollowRedirects == false {
fmt.Println(h.FollowRedirects)
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return ErrRedirectAttempted
}
}
request, err := http.NewRequest(h.Method, h.Address, nil)
if err != nil {
return nil, err
@ -66,10 +81,20 @@ func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
request.Header = http.Header(mimeHeader)
// Start Timer
start := time.Now()
request.Write(os.Stdout)
resp, err := client.Do(request)
if err != nil {
if h.FollowRedirects {
return nil, err
}
if urlError, ok := err.(*url.Error); ok &&
urlError.Err == ErrRedirectAttempted {
fmt.Println(err)
err = nil
} else {
return nil, err
}
}
fields["response_time"] = time.Since(start).Seconds()
fields["http_response_code"] = resp.StatusCode
return fields, nil
@ -79,7 +104,7 @@ func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
func (h *HTTPResponse) Gather(acc telegraf.Accumulator) error {
// Set default values
if h.ResponseTimeout < 1 {
h.ResponseTimeout = 1
h.ResponseTimeout = 10
}
// Check send and expected string
if h.Method == "" {