update to allow for following redirects
This commit is contained in:
parent
b5fa9b33cb
commit
693376c539
|
@ -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
|
||||||
```
|
```
|
||||||
|
|
|
@ -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,10 +81,20 @@ 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 {
|
||||||
|
if h.FollowRedirects {
|
||||||
return nil, err
|
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
|
||||||
return fields, nil
|
return fields, nil
|
||||||
|
@ -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 == "" {
|
||||||
|
|
Loading…
Reference in New Issue