2016-03-31 07:33:28 +00:00
|
|
|
package http_response
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2018-03-08 22:55:59 +00:00
|
|
|
"fmt"
|
2016-03-31 11:06:47 +00:00
|
|
|
"io"
|
2017-02-01 14:21:08 +00:00
|
|
|
"io/ioutil"
|
2017-06-06 20:39:07 +00:00
|
|
|
"net"
|
2016-03-31 07:33:28 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2017-02-01 14:21:08 +00:00
|
|
|
"regexp"
|
2018-03-08 22:55:59 +00:00
|
|
|
"strconv"
|
2016-03-31 09:53:51 +00:00
|
|
|
"strings"
|
2016-03-31 07:33:28 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2016-05-23 12:33:43 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2018-05-04 23:33:23 +00:00
|
|
|
"github.com/influxdata/telegraf/internal/tls"
|
2016-03-31 07:33:28 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
)
|
|
|
|
|
2016-03-31 09:53:51 +00:00
|
|
|
// HTTPResponse struct
|
|
|
|
type HTTPResponse struct {
|
2019-06-14 19:03:44 +00:00
|
|
|
Address string // deprecated in 1.12
|
|
|
|
URLs []string `toml:"urls"`
|
|
|
|
HTTPProxy string `toml:"http_proxy"`
|
2017-02-01 14:21:08 +00:00
|
|
|
Body string
|
|
|
|
Method string
|
|
|
|
ResponseTimeout internal.Duration
|
|
|
|
Headers map[string]string
|
|
|
|
FollowRedirects bool
|
|
|
|
ResponseStringMatch string
|
2019-06-19 20:40:53 +00:00
|
|
|
Interface string
|
2018-05-04 23:33:23 +00:00
|
|
|
tls.ClientConfig
|
2017-05-09 23:21:04 +00:00
|
|
|
|
2019-09-23 22:39:50 +00:00
|
|
|
Log telegraf.Logger
|
|
|
|
|
2017-05-09 23:21:04 +00:00
|
|
|
compiledStringMatch *regexp.Regexp
|
|
|
|
client *http.Client
|
2016-03-31 07:33:28 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 09:53:51 +00:00
|
|
|
// Description returns the plugin Description
|
|
|
|
func (h *HTTPResponse) Description() string {
|
2016-03-31 07:33:28 +00:00
|
|
|
return "HTTP/HTTPS request given an address a method and a timeout"
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2019-06-14 19:03:44 +00:00
|
|
|
## Deprecated in 1.12, use 'urls'
|
2016-03-31 07:33:28 +00:00
|
|
|
## Server address (default http://localhost)
|
2017-08-15 18:50:08 +00:00
|
|
|
# address = "http://localhost"
|
|
|
|
|
2019-06-14 19:03:44 +00:00
|
|
|
## List of urls to query.
|
|
|
|
# urls = ["http://localhost"]
|
|
|
|
|
2018-03-06 20:11:38 +00:00
|
|
|
## Set http_proxy (telegraf uses the system wide proxy settings if it's is not set)
|
|
|
|
# http_proxy = "http://localhost:8888"
|
|
|
|
|
2016-04-07 01:57:49 +00:00
|
|
|
## Set response_timeout (default 5 seconds)
|
2017-08-15 18:50:08 +00:00
|
|
|
# response_timeout = "5s"
|
|
|
|
|
2016-03-31 11:06:47 +00:00
|
|
|
## HTTP Request Method
|
2017-08-15 18:50:08 +00:00
|
|
|
# method = "GET"
|
|
|
|
|
2016-04-04 02:20:07 +00:00
|
|
|
## Whether to follow redirects from the server (defaults to false)
|
2017-08-15 18:50:08 +00:00
|
|
|
# follow_redirects = false
|
|
|
|
|
2016-04-04 02:20:07 +00:00
|
|
|
## Optional HTTP Request Body
|
2016-04-07 16:31:06 +00:00
|
|
|
# body = '''
|
|
|
|
# {'fake':'data'}
|
|
|
|
# '''
|
2016-05-25 11:41:21 +00:00
|
|
|
|
2017-02-01 14:21:08 +00:00
|
|
|
## Optional substring or regex match in body of the response
|
2017-08-15 18:50:08 +00:00
|
|
|
# response_string_match = "\"service_status\": \"up\""
|
|
|
|
# response_string_match = "ok"
|
|
|
|
# response_string_match = "\".*_status\".?:.?\"up\""
|
2017-02-01 14:21:08 +00:00
|
|
|
|
2018-05-04 23:33:23 +00:00
|
|
|
## Optional TLS Config
|
|
|
|
# tls_ca = "/etc/telegraf/ca.pem"
|
|
|
|
# tls_cert = "/etc/telegraf/cert.pem"
|
|
|
|
# tls_key = "/etc/telegraf/key.pem"
|
|
|
|
## Use TLS but skip chain & host verification
|
2016-05-25 11:41:21 +00:00
|
|
|
# insecure_skip_verify = false
|
2017-08-15 18:50:08 +00:00
|
|
|
|
|
|
|
## HTTP Request Headers (all values must be strings)
|
|
|
|
# [inputs.http_response.headers]
|
|
|
|
# Host = "github.com"
|
2019-06-19 20:40:53 +00:00
|
|
|
|
|
|
|
## Interface to use when dialing an address
|
|
|
|
# interface = "eth0"
|
2016-03-31 07:33:28 +00:00
|
|
|
`
|
|
|
|
|
2016-03-31 09:53:51 +00:00
|
|
|
// SampleConfig returns the plugin SampleConfig
|
|
|
|
func (h *HTTPResponse) SampleConfig() string {
|
2016-03-31 07:33:28 +00:00
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
2016-03-31 11:06:47 +00:00
|
|
|
// ErrRedirectAttempted indicates that a redirect occurred
|
2016-03-31 10:18:19 +00:00
|
|
|
var ErrRedirectAttempted = errors.New("redirect")
|
|
|
|
|
2018-03-06 20:11:38 +00:00
|
|
|
// Set the proxy. A configured proxy overwrites the system wide proxy.
|
|
|
|
func getProxyFunc(http_proxy string) func(*http.Request) (*url.URL, error) {
|
|
|
|
if http_proxy == "" {
|
|
|
|
return http.ProxyFromEnvironment
|
|
|
|
}
|
|
|
|
proxyURL, err := url.Parse(http_proxy)
|
|
|
|
if err != nil {
|
|
|
|
return func(_ *http.Request) (*url.URL, error) {
|
|
|
|
return nil, errors.New("bad proxy: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return func(r *http.Request) (*url.URL, error) {
|
|
|
|
return proxyURL, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 20:40:53 +00:00
|
|
|
// createHttpClient creates an http client which will timeout at the specified
|
2016-04-04 02:20:07 +00:00
|
|
|
// timeout period and can follow redirects if specified
|
2016-05-25 11:41:21 +00:00
|
|
|
func (h *HTTPResponse) createHttpClient() (*http.Client, error) {
|
2018-05-04 23:33:23 +00:00
|
|
|
tlsCfg, err := h.ClientConfig.TLSConfig()
|
2016-05-25 11:41:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-19 20:40:53 +00:00
|
|
|
|
|
|
|
dialer := &net.Dialer{}
|
|
|
|
|
|
|
|
if h.Interface != "" {
|
|
|
|
dialer.LocalAddr, err = localAddress(h.Interface)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 07:33:28 +00:00
|
|
|
client := &http.Client{
|
2017-05-09 23:21:04 +00:00
|
|
|
Transport: &http.Transport{
|
2018-03-30 18:10:49 +00:00
|
|
|
Proxy: getProxyFunc(h.HTTPProxy),
|
2019-06-19 20:40:53 +00:00
|
|
|
DialContext: dialer.DialContext,
|
2017-05-10 21:40:55 +00:00
|
|
|
DisableKeepAlives: true,
|
|
|
|
TLSClientConfig: tlsCfg,
|
2017-05-09 23:21:04 +00:00
|
|
|
},
|
|
|
|
Timeout: h.ResponseTimeout.Duration,
|
2016-03-31 07:33:28 +00:00
|
|
|
}
|
2016-03-31 10:18:19 +00:00
|
|
|
|
2016-05-25 11:41:21 +00:00
|
|
|
if h.FollowRedirects == false {
|
2016-03-31 10:18:19 +00:00
|
|
|
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
|
2019-09-10 18:04:24 +00:00
|
|
|
return http.ErrUseLastResponse
|
2016-03-31 10:18:19 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-25 11:41:21 +00:00
|
|
|
return client, nil
|
2016-04-04 02:20:07 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 20:40:53 +00:00
|
|
|
func localAddress(interfaceName string) (net.Addr, error) {
|
|
|
|
i, err := net.InterfaceByName(interfaceName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs, err := i.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range addrs {
|
|
|
|
if naddr, ok := addr.(*net.IPNet); ok {
|
|
|
|
// leaving port set to zero to let kernel pick
|
|
|
|
return &net.TCPAddr{IP: naddr.IP}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("cannot create local address for interface %q", interfaceName)
|
|
|
|
}
|
|
|
|
|
2018-03-08 22:55:59 +00:00
|
|
|
func setResult(result_string string, fields map[string]interface{}, tags map[string]string) {
|
|
|
|
result_codes := map[string]int{
|
|
|
|
"success": 0,
|
|
|
|
"response_string_mismatch": 1,
|
|
|
|
"body_read_error": 2,
|
|
|
|
"connection_failed": 3,
|
|
|
|
"timeout": 4,
|
|
|
|
"dns_error": 5,
|
|
|
|
}
|
|
|
|
|
|
|
|
tags["result"] = result_string
|
|
|
|
fields["result_type"] = result_string
|
|
|
|
fields["result_code"] = result_codes[result_string]
|
|
|
|
}
|
|
|
|
|
|
|
|
func setError(err error, fields map[string]interface{}, tags map[string]string) error {
|
|
|
|
if timeoutError, ok := err.(net.Error); ok && timeoutError.Timeout() {
|
|
|
|
setResult("timeout", fields, tags)
|
|
|
|
return timeoutError
|
|
|
|
}
|
|
|
|
|
|
|
|
urlErr, isUrlErr := err.(*url.Error)
|
|
|
|
if !isUrlErr {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
opErr, isNetErr := (urlErr.Err).(*net.OpError)
|
|
|
|
if isNetErr {
|
|
|
|
switch e := (opErr.Err).(type) {
|
|
|
|
case (*net.DNSError):
|
|
|
|
setResult("dns_error", fields, tags)
|
|
|
|
return e
|
|
|
|
case (*net.ParseError):
|
|
|
|
// Parse error has to do with parsing of IP addresses, so we
|
|
|
|
// group it with address errors
|
|
|
|
setResult("address_error", fields, tags)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-04 02:20:07 +00:00
|
|
|
// HTTPGather gathers all fields and returns any errors it encounters
|
2019-06-14 19:03:44 +00:00
|
|
|
func (h *HTTPResponse) httpGather(u string) (map[string]interface{}, map[string]string, error) {
|
2018-03-08 22:55:59 +00:00
|
|
|
// Prepare fields and tags
|
2016-04-04 02:20:07 +00:00
|
|
|
fields := make(map[string]interface{})
|
2019-06-14 19:03:44 +00:00
|
|
|
tags := map[string]string{"server": u, "method": h.Method}
|
2016-04-04 02:20:07 +00:00
|
|
|
|
2016-03-31 11:06:47 +00:00
|
|
|
var body io.Reader
|
|
|
|
if h.Body != "" {
|
|
|
|
body = strings.NewReader(h.Body)
|
|
|
|
}
|
2019-06-14 19:03:44 +00:00
|
|
|
request, err := http.NewRequest(h.Method, u, body)
|
2016-03-31 07:33:28 +00:00
|
|
|
if err != nil {
|
2018-03-08 22:55:59 +00:00
|
|
|
return nil, nil, err
|
2016-03-31 07:33:28 +00:00
|
|
|
}
|
2016-05-19 11:08:25 +00:00
|
|
|
|
|
|
|
for key, val := range h.Headers {
|
|
|
|
request.Header.Add(key, val)
|
|
|
|
if key == "Host" {
|
|
|
|
request.Host = val
|
|
|
|
}
|
|
|
|
}
|
2016-04-07 01:57:49 +00:00
|
|
|
|
2016-03-31 07:33:28 +00:00
|
|
|
// Start Timer
|
|
|
|
start := time.Now()
|
2017-05-09 23:21:04 +00:00
|
|
|
resp, err := h.client.Do(request)
|
2018-03-08 22:55:59 +00:00
|
|
|
response_time := time.Since(start).Seconds()
|
2017-06-06 20:39:07 +00:00
|
|
|
|
2018-03-08 22:55:59 +00:00
|
|
|
// If an error in returned, it means we are dealing with a network error, as
|
|
|
|
// HTTP error codes do not generate errors in the net/http library
|
2016-03-31 07:33:28 +00:00
|
|
|
if err != nil {
|
2018-03-08 22:55:59 +00:00
|
|
|
// Log error
|
2019-09-23 22:39:50 +00:00
|
|
|
h.Log.Debugf("Network error while polling %s: %s", u, err.Error())
|
2018-03-08 22:55:59 +00:00
|
|
|
|
|
|
|
// Get error details
|
|
|
|
netErr := setError(err, fields, tags)
|
|
|
|
|
|
|
|
// If recognize the returnded error, get out
|
|
|
|
if netErr != nil {
|
|
|
|
return fields, tags, nil
|
2016-03-31 10:18:19 +00:00
|
|
|
}
|
2018-03-08 22:55:59 +00:00
|
|
|
|
|
|
|
// Any error not recognized by `set_error` is considered a "connection_failed"
|
|
|
|
setResult("connection_failed", fields, tags)
|
2019-09-10 18:04:24 +00:00
|
|
|
return fields, tags, nil
|
2016-03-31 07:33:28 +00:00
|
|
|
}
|
2018-03-08 22:55:59 +00:00
|
|
|
|
|
|
|
if _, ok := fields["response_time"]; !ok {
|
|
|
|
fields["response_time"] = response_time
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function closes the response body, as
|
|
|
|
// required by the net/http library
|
2019-08-16 22:45:20 +00:00
|
|
|
defer resp.Body.Close()
|
2017-05-10 21:40:55 +00:00
|
|
|
|
2018-03-08 22:55:59 +00:00
|
|
|
// Set log the HTTP response code
|
|
|
|
tags["status_code"] = strconv.Itoa(resp.StatusCode)
|
2016-03-31 07:33:28 +00:00
|
|
|
fields["http_response_code"] = resp.StatusCode
|
2017-02-01 14:21:08 +00:00
|
|
|
|
2019-08-16 22:45:20 +00:00
|
|
|
bodyBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2019-09-23 22:39:50 +00:00
|
|
|
h.Log.Debugf("Failed to read body of HTTP Response : %s", err.Error())
|
2019-08-16 22:45:20 +00:00
|
|
|
setResult("body_read_error", fields, tags)
|
|
|
|
fields["content_length"] = len(bodyBytes)
|
|
|
|
if h.ResponseStringMatch != "" {
|
2017-02-01 14:21:08 +00:00
|
|
|
fields["response_string_match"] = 0
|
|
|
|
}
|
2019-08-16 22:45:20 +00:00
|
|
|
return fields, tags, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fields["content_length"] = len(bodyBytes)
|
2017-02-01 14:21:08 +00:00
|
|
|
|
2019-08-16 22:45:20 +00:00
|
|
|
// Check the response for a regex match.
|
|
|
|
if h.ResponseStringMatch != "" {
|
2017-02-01 14:21:08 +00:00
|
|
|
if h.compiledStringMatch.Match(bodyBytes) {
|
2018-03-08 22:55:59 +00:00
|
|
|
setResult("success", fields, tags)
|
2017-02-01 14:21:08 +00:00
|
|
|
fields["response_string_match"] = 1
|
|
|
|
} else {
|
2018-03-08 22:55:59 +00:00
|
|
|
setResult("response_string_mismatch", fields, tags)
|
2017-02-01 14:21:08 +00:00
|
|
|
fields["response_string_match"] = 0
|
|
|
|
}
|
2017-06-06 20:39:07 +00:00
|
|
|
} else {
|
2018-03-08 22:55:59 +00:00
|
|
|
setResult("success", fields, tags)
|
2017-02-01 14:21:08 +00:00
|
|
|
}
|
|
|
|
|
2018-03-08 22:55:59 +00:00
|
|
|
return fields, tags, nil
|
2016-03-31 07:33:28 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 09:53:51 +00:00
|
|
|
// Gather gets all metric fields and tags and returns any errors it encounters
|
|
|
|
func (h *HTTPResponse) Gather(acc telegraf.Accumulator) error {
|
2018-03-08 22:55:59 +00:00
|
|
|
// Compile the body regex if it exist
|
|
|
|
if h.compiledStringMatch == nil {
|
|
|
|
var err error
|
|
|
|
h.compiledStringMatch, err = regexp.Compile(h.ResponseStringMatch)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to compile regular expression %s : %s", h.ResponseStringMatch, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 07:33:28 +00:00
|
|
|
// Set default values
|
2016-05-23 12:33:43 +00:00
|
|
|
if h.ResponseTimeout.Duration < time.Second {
|
|
|
|
h.ResponseTimeout.Duration = time.Second * 5
|
2016-03-31 07:33:28 +00:00
|
|
|
}
|
|
|
|
// Check send and expected string
|
2016-03-31 09:53:51 +00:00
|
|
|
if h.Method == "" {
|
|
|
|
h.Method = "GET"
|
2016-03-31 07:33:28 +00:00
|
|
|
}
|
2018-03-08 22:55:59 +00:00
|
|
|
|
2019-06-14 19:03:44 +00:00
|
|
|
if len(h.URLs) == 0 {
|
|
|
|
if h.Address == "" {
|
|
|
|
h.URLs = []string{"http://localhost"}
|
|
|
|
} else {
|
2019-09-23 22:39:50 +00:00
|
|
|
h.Log.Warn("'address' deprecated in telegraf 1.12, please use 'urls'")
|
2019-06-14 19:03:44 +00:00
|
|
|
h.URLs = []string{h.Address}
|
|
|
|
}
|
|
|
|
}
|
2017-05-09 23:21:04 +00:00
|
|
|
|
|
|
|
if h.client == nil {
|
|
|
|
client, err := h.createHttpClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
h.client = client
|
|
|
|
}
|
|
|
|
|
2019-06-14 19:03:44 +00:00
|
|
|
for _, u := range h.URLs {
|
|
|
|
addr, err := url.Parse(u)
|
|
|
|
if err != nil {
|
|
|
|
acc.AddError(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if addr.Scheme != "http" && addr.Scheme != "https" {
|
|
|
|
acc.AddError(errors.New("Only http and https are supported"))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare data
|
|
|
|
var fields map[string]interface{}
|
|
|
|
var tags map[string]string
|
|
|
|
|
|
|
|
// Gather data
|
|
|
|
fields, tags, err = h.httpGather(u)
|
|
|
|
if err != nil {
|
|
|
|
acc.AddError(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add metrics
|
|
|
|
acc.AddFields("http_response", fields, tags)
|
2016-03-31 07:33:28 +00:00
|
|
|
}
|
2018-03-08 22:55:59 +00:00
|
|
|
|
2016-03-31 07:33:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
inputs.Add("http_response", func() telegraf.Input {
|
2016-03-31 09:53:51 +00:00
|
|
|
return &HTTPResponse{}
|
2016-03-31 07:33:28 +00:00
|
|
|
})
|
|
|
|
}
|