2015-08-04 21:48:13 +00:00
|
|
|
package httpjson
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-08-14 21:33:51 +00:00
|
|
|
"errors"
|
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"
|
2015-08-14 21:33:51 +00:00
|
|
|
|
2015-12-14 22:57:17 +00:00
|
|
|
"github.com/influxdb/telegraf/internal"
|
2015-08-14 21:33:51 +00:00
|
|
|
"github.com/influxdb/telegraf/plugins"
|
2015-08-04 21:48:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type HttpJson struct {
|
2015-08-14 21:33:51 +00:00
|
|
|
Name string
|
|
|
|
Servers []string
|
|
|
|
Method string
|
2015-10-17 11:38:50 +00:00
|
|
|
TagKeys []string
|
2015-08-14 21:33:51 +00:00
|
|
|
Parameters map[string]string
|
2015-12-19 22:29:07 +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)
|
|
|
|
}
|
|
|
|
|
|
|
|
type RealHTTPClient struct {
|
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c RealHTTPClient) MakeRequest(req *http.Request) (*http.Response, error) {
|
|
|
|
return c.client.Do(req)
|
2015-08-04 21:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2015-12-19 22:29:07 +00:00
|
|
|
# a name for the service being polled
|
|
|
|
name = "webserver_stats"
|
|
|
|
|
|
|
|
# URL of each server in the service's cluster
|
|
|
|
servers = [
|
|
|
|
"http://localhost:9999/stats/",
|
|
|
|
"http://localhost:9998/stats/",
|
|
|
|
]
|
|
|
|
|
|
|
|
# HTTP method to use (case-sensitive)
|
|
|
|
method = "GET"
|
|
|
|
|
|
|
|
# List of tag names to extract from top-level of JSON server response
|
|
|
|
# tag_keys = [
|
|
|
|
# "my_tag_1",
|
|
|
|
# "my_tag_2"
|
|
|
|
# ]
|
|
|
|
|
|
|
|
# HTTP parameters (all values must be strings)
|
|
|
|
[plugins.httpjson.parameters]
|
|
|
|
event_type = "cpu_spike"
|
|
|
|
threshold = "0.75"
|
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.
|
2015-08-04 21:48:13 +00:00
|
|
|
func (h *HttpJson) Gather(acc plugins.Accumulator) error {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2015-12-19 22:29:07 +00:00
|
|
|
errorChannel := make(chan error, len(h.Servers))
|
|
|
|
|
|
|
|
for _, server := range h.Servers {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(server string) {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := h.gatherServer(acc, server); err != nil {
|
|
|
|
errorChannel <- err
|
|
|
|
}
|
|
|
|
}(server)
|
2015-08-04 21:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
2015-08-14 21:33:51 +00:00
|
|
|
close(errorChannel)
|
2015-08-04 21:48:13 +00:00
|
|
|
|
2015-08-14 21:33:51 +00:00
|
|
|
// Get all errors and return them as one giant error
|
|
|
|
errorStrings := []string{}
|
|
|
|
for err := range errorChannel {
|
|
|
|
errorStrings = append(errorStrings, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errorStrings) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New(strings.Join(errorStrings, "\n"))
|
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(
|
|
|
|
acc plugins.Accumulator,
|
|
|
|
serverURL string,
|
|
|
|
) error {
|
2015-12-19 22:29:07 +00:00
|
|
|
resp, err := h.sendRequest(serverURL)
|
2015-08-04 21:48:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-16 14:10:08 +00:00
|
|
|
var jsonOut map[string]interface{}
|
2015-08-14 21:33:51 +00:00
|
|
|
if err = json.Unmarshal([]byte(resp), &jsonOut); err != nil {
|
|
|
|
return errors.New("Error decoding JSON response")
|
|
|
|
}
|
|
|
|
|
|
|
|
tags := map[string]string{
|
|
|
|
"server": serverURL,
|
2015-08-04 21:48:13 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 22:29:07 +00:00
|
|
|
for _, tag := range h.TagKeys {
|
2015-10-16 14:10:08 +00:00
|
|
|
switch v := jsonOut[tag].(type) {
|
2015-10-17 11:38:50 +00:00
|
|
|
case string:
|
|
|
|
tags[tag] = v
|
2015-10-16 14:10:08 +00:00
|
|
|
}
|
|
|
|
delete(jsonOut, tag)
|
|
|
|
}
|
|
|
|
|
2015-12-14 22:57:17 +00:00
|
|
|
f := internal.JSONFlattener{}
|
|
|
|
err = f.FlattenJSON("", jsonOut)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var msrmnt_name string
|
2015-12-19 22:29:07 +00:00
|
|
|
if h.Name == "" {
|
2015-12-14 22:57:17 +00:00
|
|
|
msrmnt_name = "httpjson"
|
|
|
|
} else {
|
2015-12-19 22:29:07 +00:00
|
|
|
msrmnt_name = "httpjson_" + h.Name
|
2015-12-14 22:57:17 +00:00
|
|
|
}
|
2016-01-06 23:11:16 +00:00
|
|
|
acc.AddFields(msrmnt_name, f.Fields, tags)
|
2015-08-14 21:33:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-08-04 21:48:13 +00:00
|
|
|
|
2015-08-14 21:33:51 +00:00
|
|
|
// Sends an HTTP request to the server using the HttpJson object's HTTPClient
|
|
|
|
// Parameters:
|
|
|
|
// serverURL: endpoint to send request to
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// string: body of the response
|
|
|
|
// error : Any error that may have occurred
|
2015-12-19 22:29:07 +00:00
|
|
|
func (h *HttpJson) sendRequest(serverURL string) (string, 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 {
|
2015-08-14 21:33:51 +00:00
|
|
|
return "", fmt.Errorf("Invalid server URL \"%s\"", serverURL)
|
2015-08-04 21:48:13 +00:00
|
|
|
}
|
|
|
|
|
2015-08-14 21:33:51 +00:00
|
|
|
params := url.Values{}
|
2015-12-19 22:29:07 +00:00
|
|
|
for k, v := range h.Parameters {
|
2015-08-14 21:33:51 +00:00
|
|
|
params.Add(k, v)
|
|
|
|
}
|
|
|
|
requestURL.RawQuery = params.Encode()
|
|
|
|
|
|
|
|
// Create + send request
|
2015-12-19 22:29:07 +00:00
|
|
|
req, err := http.NewRequest(h.Method, requestURL.String(), nil)
|
2015-08-14 21:33:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := h.client.MakeRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-12-19 22:29:07 +00:00
|
|
|
defer resp.Body.Close()
|
2015-08-14 21:33:51 +00:00
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return string(body), err
|
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))
|
|
|
|
return string(body), err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(body), err
|
2015-08-04 21:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
plugins.Add("httpjson", func() plugins.Plugin {
|
2015-08-14 21:33:51 +00:00
|
|
|
return &HttpJson{client: RealHTTPClient{client: &http.Client{}}}
|
2015-08-04 21:48:13 +00:00
|
|
|
})
|
|
|
|
}
|