2015-08-04 21:48:13 +00:00
|
|
|
package httpjson
|
|
|
|
|
|
|
|
import (
|
2017-09-26 22:36:00 +00:00
|
|
|
"bytes"
|
2015-08-04 21:48:13 +00:00
|
|
|
"fmt"
|
2015-08-14 21:33:51 +00:00
|
|
|
"io/ioutil"
|
2015-08-04 21:48:13 +00:00
|
|
|
"net/http"
|
2015-08-14 21:33:51 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2015-08-04 21:48:13 +00:00
|
|
|
"sync"
|
2016-01-08 01:49:14 +00:00
|
|
|
"time"
|
2015-08-14 21:33:51 +00:00
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-03-01 17:13:26 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2018-05-04 23:33:23 +00:00
|
|
|
"github.com/influxdata/telegraf/internal/tls"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2016-02-06 00:36:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers"
|
2015-08-04 21:48:13 +00:00
|
|
|
)
|
|
|
|
|
2017-09-26 22:36:00 +00:00
|
|
|
var (
|
|
|
|
utf8BOM = []byte("\xef\xbb\xbf")
|
|
|
|
)
|
|
|
|
|
2016-08-30 15:23:15 +00:00
|
|
|
// HttpJson struct
|
2015-08-04 21:48:13 +00:00
|
|
|
type HttpJson struct {
|
2016-08-30 15:23:15 +00:00
|
|
|
Name string
|
|
|
|
Servers []string
|
|
|
|
Method string
|
|
|
|
TagKeys []string
|
|
|
|
ResponseTimeout internal.Duration
|
|
|
|
Parameters map[string]string
|
|
|
|
Headers map[string]string
|
2018-05-04 23:33:23 +00:00
|
|
|
tls.ClientConfig
|
2016-03-01 17:13:26 +00:00
|
|
|
|
2016-02-23 16:34:01 +00:00
|
|
|
client HTTPClient
|
2015-08-14 21:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type HTTPClient interface {
|
|
|
|
// Returns the result of an http request
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// req: HTTP request object
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// http.Response: HTTP respons object
|
|
|
|
// error : Any error that may have occurred
|
|
|
|
MakeRequest(req *http.Request) (*http.Response, error)
|
2016-03-01 17:13:26 +00:00
|
|
|
|
|
|
|
SetHTTPClient(client *http.Client)
|
|
|
|
HTTPClient() *http.Client
|
2015-08-14 21:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RealHTTPClient struct {
|
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
|
2016-03-16 16:20:52 +00:00
|
|
|
func (c *RealHTTPClient) MakeRequest(req *http.Request) (*http.Response, error) {
|
2015-08-14 21:33:51 +00:00
|
|
|
return c.client.Do(req)
|
2015-08-04 21:48:13 +00:00
|
|
|
}
|
|
|
|
|
2016-03-16 16:20:52 +00:00
|
|
|
func (c *RealHTTPClient) SetHTTPClient(client *http.Client) {
|
2016-03-01 17:13:26 +00:00
|
|
|
c.client = client
|
|
|
|
}
|
|
|
|
|
2016-03-16 16:20:52 +00:00
|
|
|
func (c *RealHTTPClient) HTTPClient() *http.Client {
|
2016-03-01 17:13:26 +00:00
|
|
|
return c.client
|
|
|
|
}
|
|
|
|
|
2015-08-04 21:48:13 +00:00
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## NOTE This plugin only reads numerical measurements, strings and booleans
|
|
|
|
## will be ignored.
|
2016-01-25 20:29:05 +00:00
|
|
|
|
2017-04-04 01:34:04 +00:00
|
|
|
## Name for the service being polled. Will be appended to the name of the
|
|
|
|
## measurement e.g. httpjson_webserver_stats
|
|
|
|
##
|
|
|
|
## Deprecated (1.3.0): Use name_override, name_suffix, name_prefix instead.
|
2015-12-19 22:29:07 +00:00
|
|
|
name = "webserver_stats"
|
|
|
|
|
2016-02-18 21:26:51 +00:00
|
|
|
## URL of each server in the service's cluster
|
2015-12-19 22:29:07 +00:00
|
|
|
servers = [
|
|
|
|
"http://localhost:9999/stats/",
|
|
|
|
"http://localhost:9998/stats/",
|
|
|
|
]
|
2016-08-30 15:23:15 +00:00
|
|
|
## Set response_timeout (default 5 seconds)
|
|
|
|
response_timeout = "5s"
|
2015-12-19 22:29:07 +00:00
|
|
|
|
2016-02-18 21:26:51 +00:00
|
|
|
## HTTP method to use: GET or POST (case-sensitive)
|
2015-12-19 22:29:07 +00:00
|
|
|
method = "GET"
|
|
|
|
|
2016-02-18 21:26:51 +00:00
|
|
|
## List of tag names to extract from top-level of JSON server response
|
2015-12-19 22:29:07 +00:00
|
|
|
# tag_keys = [
|
|
|
|
# "my_tag_1",
|
|
|
|
# "my_tag_2"
|
|
|
|
# ]
|
|
|
|
|
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
|
2018-01-08 23:06:58 +00:00
|
|
|
# insecure_skip_verify = false
|
|
|
|
|
2017-04-04 01:34:04 +00:00
|
|
|
## HTTP parameters (all values must be strings). For "GET" requests, data
|
|
|
|
## will be included in the query. For "POST" requests, data will be included
|
|
|
|
## in the request body as "x-www-form-urlencoded".
|
|
|
|
# [inputs.httpjson.parameters]
|
|
|
|
# event_type = "cpu_spike"
|
|
|
|
# threshold = "0.75"
|
2016-01-26 13:18:22 +00:00
|
|
|
|
2017-04-04 01:34:04 +00:00
|
|
|
## HTTP Headers (all values must be strings)
|
2016-01-26 13:18:22 +00:00
|
|
|
# [inputs.httpjson.headers]
|
|
|
|
# X-Auth-Token = "my-xauth-token"
|
|
|
|
# apiVersion = "v1"
|
2015-08-04 21:48:13 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (h *HttpJson) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HttpJson) Description() string {
|
|
|
|
return "Read flattened metrics from one or more JSON HTTP endpoints"
|
|
|
|
}
|
|
|
|
|
2015-08-14 21:33:51 +00:00
|
|
|
// Gathers data for all servers.
|
2016-01-27 21:21:36 +00:00
|
|
|
func (h *HttpJson) Gather(acc telegraf.Accumulator) error {
|
2015-08-04 21:48:13 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2016-03-01 17:13:26 +00:00
|
|
|
if h.client.HTTPClient() == nil {
|
2018-05-04 23:33:23 +00:00
|
|
|
tlsCfg, err := h.ClientConfig.TLSConfig()
|
2016-03-01 17:13:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tr := &http.Transport{
|
2016-08-30 15:23:15 +00:00
|
|
|
ResponseHeaderTimeout: h.ResponseTimeout.Duration,
|
2016-03-01 17:13:26 +00:00
|
|
|
TLSClientConfig: tlsCfg,
|
|
|
|
}
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: tr,
|
2016-08-30 15:23:15 +00:00
|
|
|
Timeout: h.ResponseTimeout.Duration,
|
2016-03-01 17:13:26 +00:00
|
|
|
}
|
|
|
|
h.client.SetHTTPClient(client)
|
|
|
|
}
|
|
|
|
|
2015-12-19 22:29:07 +00:00
|
|
|
for _, server := range h.Servers {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(server string) {
|
|
|
|
defer wg.Done()
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.AddError(h.gatherServer(acc, server))
|
2015-12-19 22:29:07 +00:00
|
|
|
}(server)
|
2015-08-04 21:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
2015-08-14 21:33:51 +00:00
|
|
|
|
2017-04-24 18:13:26 +00:00
|
|
|
return nil
|
2015-08-04 21:48:13 +00:00
|
|
|
}
|
|
|
|
|
2015-08-14 21:33:51 +00:00
|
|
|
// Gathers data from a particular server
|
|
|
|
// Parameters:
|
|
|
|
// acc : The telegraf Accumulator to use
|
|
|
|
// serverURL: endpoint to send request to
|
|
|
|
// service : the service being queried
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// error: Any error that may have occurred
|
2015-11-02 23:19:37 +00:00
|
|
|
func (h *HttpJson) gatherServer(
|
2016-01-27 21:21:36 +00:00
|
|
|
acc telegraf.Accumulator,
|
2015-11-02 23:19:37 +00:00
|
|
|
serverURL string,
|
|
|
|
) error {
|
2016-01-08 01:49:14 +00:00
|
|
|
resp, responseTime, err := h.sendRequest(serverURL)
|
2015-08-04 21:48:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
var msrmnt_name string
|
|
|
|
if h.Name == "" {
|
|
|
|
msrmnt_name = "httpjson"
|
|
|
|
} else {
|
|
|
|
msrmnt_name = "httpjson_" + h.Name
|
2015-08-14 21:33:51 +00:00
|
|
|
}
|
|
|
|
tags := map[string]string{
|
|
|
|
"server": serverURL,
|
2015-08-04 21:48:13 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 22:56:36 +00:00
|
|
|
parser, err := parsers.NewParser(&parsers.Config{
|
2018-06-28 22:35:20 +00:00
|
|
|
DataFormat: "json",
|
|
|
|
MetricName: msrmnt_name,
|
|
|
|
TagKeys: h.TagKeys,
|
|
|
|
DefaultTags: tags,
|
2018-06-28 22:56:36 +00:00
|
|
|
})
|
2016-02-06 00:36:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-10-16 14:10:08 +00:00
|
|
|
}
|
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
metrics, err := parser.Parse([]byte(resp))
|
2015-12-14 22:57:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
for _, metric := range metrics {
|
|
|
|
fields := make(map[string]interface{})
|
|
|
|
for k, v := range metric.Fields() {
|
|
|
|
fields[k] = v
|
|
|
|
}
|
|
|
|
fields["response_time"] = responseTime
|
|
|
|
acc.AddFields(metric.Name(), fields, metric.Tags())
|
2015-12-14 22:57:17 +00:00
|
|
|
}
|
2015-08-14 21:33:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-08-04 21:48:13 +00:00
|
|
|
|
2016-02-13 02:37:51 +00:00
|
|
|
// Sends an HTTP request to the server using the HttpJson object's HTTPClient.
|
|
|
|
// This request can be either a GET or a POST.
|
2015-08-14 21:33:51 +00:00
|
|
|
// Parameters:
|
|
|
|
// serverURL: endpoint to send request to
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// string: body of the response
|
|
|
|
// error : Any error that may have occurred
|
2016-01-08 01:49:14 +00:00
|
|
|
func (h *HttpJson) sendRequest(serverURL string) (string, float64, error) {
|
2015-08-14 21:33:51 +00:00
|
|
|
// Prepare URL
|
|
|
|
requestURL, err := url.Parse(serverURL)
|
2015-08-04 21:48:13 +00:00
|
|
|
if err != nil {
|
2016-01-08 01:49:14 +00:00
|
|
|
return "", -1, fmt.Errorf("Invalid server URL \"%s\"", serverURL)
|
2015-08-04 21:48:13 +00:00
|
|
|
}
|
|
|
|
|
2016-02-13 02:37:51 +00:00
|
|
|
data := url.Values{}
|
|
|
|
switch {
|
|
|
|
case h.Method == "GET":
|
2016-02-23 17:54:28 +00:00
|
|
|
params := requestURL.Query()
|
2016-02-13 02:37:51 +00:00
|
|
|
for k, v := range h.Parameters {
|
|
|
|
params.Add(k, v)
|
|
|
|
}
|
2016-02-23 17:07:56 +00:00
|
|
|
requestURL.RawQuery = params.Encode()
|
2016-02-13 02:37:51 +00:00
|
|
|
|
|
|
|
case h.Method == "POST":
|
|
|
|
requestURL.RawQuery = ""
|
|
|
|
for k, v := range h.Parameters {
|
|
|
|
data.Add(k, v)
|
|
|
|
}
|
2015-08-14 21:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create + send request
|
2016-02-23 17:54:28 +00:00
|
|
|
req, err := http.NewRequest(h.Method, requestURL.String(),
|
|
|
|
strings.NewReader(data.Encode()))
|
2015-08-14 21:33:51 +00:00
|
|
|
if err != nil {
|
2016-01-08 01:49:14 +00:00
|
|
|
return "", -1, err
|
2015-08-14 21:33:51 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 13:18:22 +00:00
|
|
|
// Add header parameters
|
|
|
|
for k, v := range h.Headers {
|
2016-02-02 19:22:15 +00:00
|
|
|
if strings.ToLower(k) == "host" {
|
|
|
|
req.Host = v
|
|
|
|
} else {
|
|
|
|
req.Header.Add(k, v)
|
|
|
|
}
|
2016-01-26 13:18:22 +00:00
|
|
|
}
|
|
|
|
|
2016-01-08 01:49:14 +00:00
|
|
|
start := time.Now()
|
2015-08-14 21:33:51 +00:00
|
|
|
resp, err := h.client.MakeRequest(req)
|
|
|
|
if err != nil {
|
2016-01-08 01:49:14 +00:00
|
|
|
return "", -1, err
|
2015-08-14 21:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
2016-01-08 01:49:14 +00:00
|
|
|
responseTime := time.Since(start).Seconds()
|
|
|
|
|
2015-08-14 21:33:51 +00:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2016-01-08 01:49:14 +00:00
|
|
|
return string(body), responseTime, err
|
2015-08-04 21:48:13 +00:00
|
|
|
}
|
2017-09-26 22:36:00 +00:00
|
|
|
body = bytes.TrimPrefix(body, utf8BOM)
|
2015-08-04 21:48:13 +00:00
|
|
|
|
2015-08-14 21:33:51 +00:00
|
|
|
// Process response
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
err = fmt.Errorf("Response from url \"%s\" has status code %d (%s), expected %d (%s)",
|
|
|
|
requestURL.String(),
|
|
|
|
resp.StatusCode,
|
|
|
|
http.StatusText(resp.StatusCode),
|
|
|
|
http.StatusOK,
|
|
|
|
http.StatusText(http.StatusOK))
|
2016-01-08 01:49:14 +00:00
|
|
|
return string(body), responseTime, err
|
2015-08-14 21:33:51 +00:00
|
|
|
}
|
|
|
|
|
2016-01-08 01:49:14 +00:00
|
|
|
return string(body), responseTime, err
|
2015-08-04 21:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("httpjson", func() telegraf.Input {
|
2016-03-16 16:20:52 +00:00
|
|
|
return &HttpJson{
|
|
|
|
client: &RealHTTPClient{},
|
2016-08-30 15:23:15 +00:00
|
|
|
ResponseTimeout: internal.Duration{
|
|
|
|
Duration: 5 * time.Second,
|
|
|
|
},
|
2016-03-16 16:20:52 +00:00
|
|
|
}
|
2015-08-04 21:48:13 +00:00
|
|
|
})
|
|
|
|
}
|