2016-03-31 07:33:28 +00:00
|
|
|
package http_response
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2016-03-31 11:06:47 +00:00
|
|
|
"io"
|
2017-02-01 14:21:08 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2016-03-31 07:33:28 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2017-02-01 14:21:08 +00:00
|
|
|
"regexp"
|
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"
|
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 {
|
2017-02-01 14:21:08 +00:00
|
|
|
Address string
|
|
|
|
Body string
|
|
|
|
Method string
|
|
|
|
ResponseTimeout internal.Duration
|
|
|
|
Headers map[string]string
|
|
|
|
FollowRedirects bool
|
|
|
|
ResponseStringMatch string
|
2016-05-25 11:41:21 +00:00
|
|
|
|
|
|
|
// Path to CA file
|
|
|
|
SSLCA string `toml:"ssl_ca"`
|
|
|
|
// Path to host cert file
|
|
|
|
SSLCert string `toml:"ssl_cert"`
|
|
|
|
// Path to cert key file
|
|
|
|
SSLKey string `toml:"ssl_key"`
|
|
|
|
// Use SSL but skip chain & host verification
|
|
|
|
InsecureSkipVerify bool
|
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 = `
|
|
|
|
## Server address (default http://localhost)
|
2016-03-31 10:18:19 +00:00
|
|
|
address = "http://github.com"
|
2016-04-07 01:57:49 +00:00
|
|
|
## Set response_timeout (default 5 seconds)
|
2016-05-23 12:33:43 +00:00
|
|
|
response_timeout = "5s"
|
2016-03-31 11:06:47 +00:00
|
|
|
## HTTP Request Method
|
2016-03-31 07:33:28 +00:00
|
|
|
method = "GET"
|
2016-04-04 02:20:07 +00:00
|
|
|
## Whether to follow redirects from the server (defaults to false)
|
|
|
|
follow_redirects = true
|
2016-04-07 16:31:06 +00:00
|
|
|
## HTTP Request Headers (all values must be strings)
|
|
|
|
# [inputs.http_response.headers]
|
|
|
|
# Host = "github.com"
|
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
|
|
|
|
## response_string_match = "\"service_status\": \"up\""
|
|
|
|
## response_string_match = "ok"
|
|
|
|
## response_string_match = "\".*_status\".?:.?\"up\""
|
|
|
|
|
2016-05-25 11:41:21 +00:00
|
|
|
## Optional SSL Config
|
|
|
|
# ssl_ca = "/etc/telegraf/ca.pem"
|
|
|
|
# ssl_cert = "/etc/telegraf/cert.pem"
|
|
|
|
# ssl_key = "/etc/telegraf/key.pem"
|
|
|
|
## Use SSL but skip chain & host verification
|
|
|
|
# insecure_skip_verify = false
|
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")
|
|
|
|
|
2016-04-04 02:20:07 +00:00
|
|
|
// CreateHttpClient creates an http client which will timeout at the specified
|
|
|
|
// timeout period and can follow redirects if specified
|
2016-05-25 11:41:21 +00:00
|
|
|
func (h *HTTPResponse) createHttpClient() (*http.Client, error) {
|
|
|
|
tlsCfg, err := internal.GetTLSConfig(
|
|
|
|
h.SSLCert, h.SSLKey, h.SSLCA, h.InsecureSkipVerify)
|
|
|
|
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{
|
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 {
|
|
|
|
return ErrRedirectAttempted
|
|
|
|
}
|
|
|
|
}
|
2016-05-25 11:41:21 +00:00
|
|
|
return client, nil
|
2016-04-04 02:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPGather gathers all fields and returns any errors it encounters
|
2017-05-09 23:21:04 +00:00
|
|
|
func (h *HTTPResponse) httpGather() (map[string]interface{}, error) {
|
2016-04-04 02:20:07 +00:00
|
|
|
// Prepare fields
|
|
|
|
fields := make(map[string]interface{})
|
|
|
|
|
2016-03-31 11:06:47 +00:00
|
|
|
var body io.Reader
|
|
|
|
if h.Body != "" {
|
|
|
|
body = strings.NewReader(h.Body)
|
|
|
|
}
|
|
|
|
request, err := http.NewRequest(h.Method, h.Address, body)
|
2016-03-31 07:33:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
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)
|
2016-03-31 07:33:28 +00:00
|
|
|
if err != nil {
|
2016-03-31 10:18:19 +00:00
|
|
|
if h.FollowRedirects {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if urlError, ok := err.(*url.Error); ok &&
|
|
|
|
urlError.Err == ErrRedirectAttempted {
|
|
|
|
err = nil
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-31 07:33:28 +00:00
|
|
|
}
|
2017-05-10 21:40:55 +00:00
|
|
|
defer func() {
|
|
|
|
io.Copy(ioutil.Discard, resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
}()
|
|
|
|
|
2016-03-31 07:33:28 +00:00
|
|
|
fields["response_time"] = time.Since(start).Seconds()
|
|
|
|
fields["http_response_code"] = resp.StatusCode
|
2017-02-01 14:21:08 +00:00
|
|
|
|
|
|
|
// Check the response for a regex match.
|
|
|
|
if h.ResponseStringMatch != "" {
|
|
|
|
|
|
|
|
// Compile once and reuse
|
|
|
|
if h.compiledStringMatch == nil {
|
|
|
|
h.compiledStringMatch = regexp.MustCompile(h.ResponseStringMatch)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("E! Failed to compile regular expression %s : %s", h.ResponseStringMatch, err)
|
|
|
|
fields["response_string_match"] = 0
|
|
|
|
return fields, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bodyBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("E! Failed to read body of HTTP Response : %s", err)
|
|
|
|
fields["response_string_match"] = 0
|
|
|
|
return fields, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.compiledStringMatch.Match(bodyBytes) {
|
|
|
|
fields["response_string_match"] = 1
|
|
|
|
} else {
|
|
|
|
fields["response_string_match"] = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-31 07:33:28 +00:00
|
|
|
return fields, nil
|
|
|
|
}
|
|
|
|
|
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 {
|
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
|
|
|
}
|
2016-03-31 09:53:51 +00:00
|
|
|
if h.Address == "" {
|
|
|
|
h.Address = "http://localhost"
|
2016-03-31 07:33:28 +00:00
|
|
|
}
|
2016-03-31 09:53:51 +00:00
|
|
|
addr, err := url.Parse(h.Address)
|
2016-03-31 07:33:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if addr.Scheme != "http" && addr.Scheme != "https" {
|
|
|
|
return errors.New("Only http and https are supported")
|
|
|
|
}
|
|
|
|
// Prepare data
|
2016-03-31 09:53:51 +00:00
|
|
|
tags := map[string]string{"server": h.Address, "method": h.Method}
|
2016-03-31 07:33:28 +00:00
|
|
|
var fields map[string]interface{}
|
2017-05-09 23:21:04 +00:00
|
|
|
|
|
|
|
if h.client == nil {
|
|
|
|
client, err := h.createHttpClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
h.client = client
|
|
|
|
}
|
|
|
|
|
2016-03-31 07:33:28 +00:00
|
|
|
// Gather data
|
2017-05-09 23:21:04 +00:00
|
|
|
fields, err = h.httpGather()
|
2016-03-31 07:33:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Add metrics
|
|
|
|
acc.AddFields("http_response", fields, tags)
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|