Support array of addresses in http_response input (#5975)

This commit is contained in:
Greg 2019-06-14 13:03:44 -06:00 committed by Daniel Nelson
parent bf03b43d2a
commit fec1b3ec19
2 changed files with 48 additions and 25 deletions

View File

@ -7,9 +7,13 @@ This input plugin checks HTTP/HTTPS connections.
``` ```
# HTTP/HTTPS request given an address a method and a timeout # HTTP/HTTPS request given an address a method and a timeout
[[inputs.http_response]] [[inputs.http_response]]
## Deprecated in 1.12, use 'urls'
## Server address (default http://localhost) ## Server address (default http://localhost)
# address = "http://localhost" # address = "http://localhost"
## List of urls to query.
# urls = ["http://localhost"]
## Set http_proxy (telegraf uses the system wide proxy settings if it's is not set) ## Set http_proxy (telegraf uses the system wide proxy settings if it's is not set)
# http_proxy = "http://localhost:8888" # http_proxy = "http://localhost:8888"

View File

@ -22,8 +22,9 @@ import (
// HTTPResponse struct // HTTPResponse struct
type HTTPResponse struct { type HTTPResponse struct {
Address string Address string // deprecated in 1.12
HTTPProxy string `toml:"http_proxy"` URLs []string `toml:"urls"`
HTTPProxy string `toml:"http_proxy"`
Body string Body string
Method string Method string
ResponseTimeout internal.Duration ResponseTimeout internal.Duration
@ -42,9 +43,13 @@ func (h *HTTPResponse) Description() string {
} }
var sampleConfig = ` var sampleConfig = `
## Deprecated in 1.12, use 'urls'
## Server address (default http://localhost) ## Server address (default http://localhost)
# address = "http://localhost" # address = "http://localhost"
## List of urls to query.
# urls = ["http://localhost"]
## Set http_proxy (telegraf uses the system wide proxy settings if it's is not set) ## Set http_proxy (telegraf uses the system wide proxy settings if it's is not set)
# http_proxy = "http://localhost:8888" # http_proxy = "http://localhost:8888"
@ -171,16 +176,16 @@ func setError(err error, fields map[string]interface{}, tags map[string]string)
} }
// 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{}, map[string]string, error) { func (h *HTTPResponse) httpGather(u string) (map[string]interface{}, map[string]string, error) {
// Prepare fields and tags // Prepare fields and tags
fields := make(map[string]interface{}) fields := make(map[string]interface{})
tags := map[string]string{"server": h.Address, "method": h.Method} tags := map[string]string{"server": u, "method": h.Method}
var body io.Reader var body io.Reader
if h.Body != "" { if h.Body != "" {
body = strings.NewReader(h.Body) body = strings.NewReader(h.Body)
} }
request, err := http.NewRequest(h.Method, h.Address, body) request, err := http.NewRequest(h.Method, u, body)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -201,7 +206,7 @@ func (h *HTTPResponse) httpGather() (map[string]interface{}, map[string]string,
// HTTP error codes do not generate errors in the net/http library // HTTP error codes do not generate errors in the net/http library
if err != nil { if err != nil {
// Log error // Log error
log.Printf("D! Network error while polling %s: %s", h.Address, err.Error()) log.Printf("D! Network error while polling %s: %s", u, err.Error())
// Get error details // Get error details
netErr := setError(err, fields, tags) netErr := setError(err, fields, tags)
@ -284,20 +289,15 @@ func (h *HTTPResponse) Gather(acc telegraf.Accumulator) error {
if h.Method == "" { if h.Method == "" {
h.Method = "GET" h.Method = "GET"
} }
if h.Address == "" {
h.Address = "http://localhost"
}
addr, err := url.Parse(h.Address)
if err != nil {
return err
}
if addr.Scheme != "http" && addr.Scheme != "https" {
return errors.New("Only http and https are supported")
}
// Prepare data if len(h.URLs) == 0 {
var fields map[string]interface{} if h.Address == "" {
var tags map[string]string h.URLs = []string{"http://localhost"}
} else {
log.Printf("W! [inputs.http_response] 'address' deprecated in telegraf 1.12, please use 'urls'")
h.URLs = []string{h.Address}
}
}
if h.client == nil { if h.client == nil {
client, err := h.createHttpClient() client, err := h.createHttpClient()
@ -307,14 +307,33 @@ func (h *HTTPResponse) Gather(acc telegraf.Accumulator) error {
h.client = client h.client = client
} }
// Gather data for _, u := range h.URLs {
fields, tags, err = h.httpGather() addr, err := url.Parse(u)
if err != nil { if err != nil {
return err 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)
} }
// Add metrics
acc.AddFields("http_response", fields, tags)
return nil return nil
} }