update to allow for following redirects

This commit is contained in:
Luke Swithenbank 2016-03-31 21:18:19 +11:00 committed by Cameron Sparr
parent 7219efbdb7
commit f947fa86e3
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 # List of UDP/TCP connections you want to check
[[inputs.http_response]] [[inputs.http_response]]
# Server address (default http://localhost) ## Server address (default http://localhost)
address = "https://github.com" address = "http://github.com"
# Set http response timeout (default 10) ## Set response_timeout (default 10 seconds)
response_timeout = 10 response_timeout = 10
# HTTP Method (default "GET") ## HTTP Method
method = "GET" method = "GET"
## HTTP Request Headers
headers = '''
Host: github.com
'''
## Whether to follow redirects from the server (defaults to false)
follow_redirects = true
``` ```
### Measurements & Fields: ### Measurements & Fields:
@ -25,12 +31,11 @@ This input plugin will test HTTP/HTTPS connections.
- All measurements have the following tags: - All measurements have the following tags:
- server - server
- port - method
- protocol
### Example Output: ### Example Output:
``` ```
$ ./telegraf -config telegraf.conf -input-filter http_response -test $ ./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 ( import (
"bufio" "bufio"
"errors" "errors"
"fmt"
"net/http" "net/http"
"net/textproto" "net/textproto"
"net/url" "net/url"
"os"
"strings" "strings"
"time" "time"
@ -19,6 +21,7 @@ type HTTPResponse struct {
Method string Method string
ResponseTimeout int ResponseTimeout int
Headers string Headers string
FollowRedirects bool
} }
// Description returns the plugin Description // Description returns the plugin Description
@ -28,8 +31,8 @@ func (h *HTTPResponse) Description() string {
var sampleConfig = ` var sampleConfig = `
## Server address (default http://localhost) ## Server address (default http://localhost)
address = "https://github.com" address = "http://github.com"
## Set response_timeout (default 1 seconds) ## Set response_timeout (default 10 seconds)
response_timeout = 10 response_timeout = 10
## HTTP Method ## HTTP Method
method = "GET" method = "GET"
@ -37,6 +40,8 @@ var sampleConfig = `
headers = ''' headers = '''
Host: github.com Host: github.com
''' '''
## Whether to follow redirects from the server (defaults to false)
follow_redirects = true
` `
// SampleConfig returns the plugin SampleConfig // SampleConfig returns the plugin SampleConfig
@ -44,6 +49,8 @@ func (h *HTTPResponse) SampleConfig() string {
return sampleConfig return sampleConfig
} }
var ErrRedirectAttempted = errors.New("redirect")
// HTTPGather gathers all fields and returns any errors it encounters // HTTPGather gathers all fields and returns any errors it encounters
func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) { func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
// Prepare fields // Prepare fields
@ -52,6 +59,14 @@ func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
client := &http.Client{ client := &http.Client{
Timeout: time.Second * time.Duration(h.ResponseTimeout), 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) request, err := http.NewRequest(h.Method, h.Address, nil)
if err != nil { if err != nil {
return nil, err return nil, err
@ -66,9 +81,19 @@ func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
request.Header = http.Header(mimeHeader) request.Header = http.Header(mimeHeader)
// Start Timer // Start Timer
start := time.Now() start := time.Now()
request.Write(os.Stdout)
resp, err := client.Do(request) resp, err := client.Do(request)
if err != nil { if err != nil {
return nil, err 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["response_time"] = time.Since(start).Seconds()
fields["http_response_code"] = resp.StatusCode fields["http_response_code"] = resp.StatusCode
@ -79,7 +104,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 = 1 h.ResponseTimeout = 10
} }
// Check send and expected string // Check send and expected string
if h.Method == "" { if h.Method == "" {