2018-05-15 00:15:40 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-09-06 17:54:05 +00:00
|
|
|
"context"
|
2018-05-15 00:15:40 +00:00
|
|
|
"fmt"
|
2018-10-05 22:06:41 +00:00
|
|
|
"io"
|
2018-05-15 00:15:40 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2018-05-15 00:11:44 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/internal"
|
|
|
|
"github.com/influxdata/telegraf/internal/tls"
|
|
|
|
"github.com/influxdata/telegraf/plugins/outputs"
|
|
|
|
"github.com/influxdata/telegraf/plugins/serializers"
|
2018-09-06 17:54:05 +00:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"golang.org/x/oauth2/clientcredentials"
|
2018-05-15 00:15:40 +00:00
|
|
|
)
|
|
|
|
|
2019-06-14 18:29:58 +00:00
|
|
|
const (
|
|
|
|
defaultURL = "http://127.0.0.1:8080/telegraf"
|
|
|
|
)
|
|
|
|
|
2018-05-15 00:15:40 +00:00
|
|
|
var sampleConfig = `
|
2018-05-15 00:11:44 +00:00
|
|
|
## URL is the address to send metrics to
|
2019-06-14 18:29:58 +00:00
|
|
|
url = "http://127.0.0.1:8080/telegraf"
|
2018-05-15 00:11:44 +00:00
|
|
|
|
|
|
|
## Timeout for HTTP message
|
|
|
|
# timeout = "5s"
|
|
|
|
|
|
|
|
## HTTP method, one of: "POST" or "PUT"
|
|
|
|
# method = "POST"
|
|
|
|
|
|
|
|
## HTTP Basic Auth credentials
|
|
|
|
# username = "username"
|
|
|
|
# password = "pa$$word"
|
|
|
|
|
2018-09-06 17:54:05 +00:00
|
|
|
## OAuth2 Client Credentials Grant
|
|
|
|
# client_id = "clientid"
|
|
|
|
# client_secret = "secret"
|
|
|
|
# token_url = "https://indentityprovider/oauth2/v1/token"
|
|
|
|
# scopes = ["urn:opc:idm:__myscopes__"]
|
|
|
|
|
2018-05-15 00:11:44 +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
|
|
|
|
# insecure_skip_verify = false
|
|
|
|
|
2018-05-15 00:15:40 +00:00
|
|
|
## Data format to output.
|
|
|
|
## Each data format has it's own unique set of configuration options, read
|
|
|
|
## more about them here:
|
|
|
|
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_OUTPUT.md
|
2018-05-15 00:11:44 +00:00
|
|
|
# data_format = "influx"
|
2018-09-06 17:54:05 +00:00
|
|
|
|
2019-06-14 18:29:58 +00:00
|
|
|
## HTTP Content-Encoding for write request body, can be set to "gzip" to
|
|
|
|
## compress body or "identity" to apply no encoding.
|
|
|
|
# content_encoding = "identity"
|
|
|
|
|
2018-06-04 01:31:47 +00:00
|
|
|
## Additional HTTP headers
|
|
|
|
# [outputs.http.headers]
|
|
|
|
# # Should be set manually to "application/json" for json data_format
|
|
|
|
# Content-Type = "text/plain; charset=utf-8"
|
2018-05-15 00:15:40 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
const (
|
2018-05-15 00:11:44 +00:00
|
|
|
defaultClientTimeout = 5 * time.Second
|
|
|
|
defaultContentType = "text/plain; charset=utf-8"
|
|
|
|
defaultMethod = http.MethodPost
|
2018-05-15 00:15:40 +00:00
|
|
|
)
|
|
|
|
|
2018-05-15 00:11:44 +00:00
|
|
|
type HTTP struct {
|
2018-10-05 22:06:41 +00:00
|
|
|
URL string `toml:"url"`
|
|
|
|
Timeout internal.Duration `toml:"timeout"`
|
|
|
|
Method string `toml:"method"`
|
|
|
|
Username string `toml:"username"`
|
|
|
|
Password string `toml:"password"`
|
|
|
|
Headers map[string]string `toml:"headers"`
|
|
|
|
ClientID string `toml:"client_id"`
|
|
|
|
ClientSecret string `toml:"client_secret"`
|
|
|
|
TokenURL string `toml:"token_url"`
|
|
|
|
Scopes []string `toml:"scopes"`
|
|
|
|
ContentEncoding string `toml:"content_encoding"`
|
2018-05-15 00:11:44 +00:00
|
|
|
tls.ClientConfig
|
2018-05-15 00:15:40 +00:00
|
|
|
|
2018-05-15 00:11:44 +00:00
|
|
|
client *http.Client
|
2018-05-15 00:15:40 +00:00
|
|
|
serializer serializers.Serializer
|
|
|
|
}
|
|
|
|
|
2018-05-15 00:11:44 +00:00
|
|
|
func (h *HTTP) SetSerializer(serializer serializers.Serializer) {
|
2018-05-15 00:15:40 +00:00
|
|
|
h.serializer = serializer
|
|
|
|
}
|
|
|
|
|
2018-09-06 17:54:05 +00:00
|
|
|
func (h *HTTP) createClient(ctx context.Context) (*http.Client, error) {
|
|
|
|
tlsCfg, err := h.ClientConfig.TLSConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: tlsCfg,
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
},
|
|
|
|
Timeout: h.Timeout.Duration,
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.ClientID != "" && h.ClientSecret != "" && h.TokenURL != "" {
|
|
|
|
oauthConfig := clientcredentials.Config{
|
|
|
|
ClientID: h.ClientID,
|
|
|
|
ClientSecret: h.ClientSecret,
|
|
|
|
TokenURL: h.TokenURL,
|
|
|
|
Scopes: h.Scopes,
|
|
|
|
}
|
|
|
|
ctx = context.WithValue(ctx, oauth2.HTTPClient, client)
|
|
|
|
client = oauthConfig.Client(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2018-05-15 00:11:44 +00:00
|
|
|
func (h *HTTP) Connect() error {
|
|
|
|
if h.Method == "" {
|
|
|
|
h.Method = http.MethodPost
|
|
|
|
}
|
|
|
|
h.Method = strings.ToUpper(h.Method)
|
|
|
|
if h.Method != http.MethodPost && h.Method != http.MethodPut {
|
|
|
|
return fmt.Errorf("invalid method [%s] %s", h.URL, h.Method)
|
2018-05-15 00:15:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 00:11:44 +00:00
|
|
|
if h.Timeout.Duration == 0 {
|
|
|
|
h.Timeout.Duration = defaultClientTimeout
|
|
|
|
}
|
2018-05-15 00:15:40 +00:00
|
|
|
|
2018-09-06 17:54:05 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
client, err := h.createClient(ctx)
|
2018-05-15 00:11:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-05-15 00:15:40 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 17:54:05 +00:00
|
|
|
h.client = client
|
2018-05-15 00:15:40 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-15 00:11:44 +00:00
|
|
|
func (h *HTTP) Close() error {
|
2018-05-15 00:15:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-15 00:11:44 +00:00
|
|
|
func (h *HTTP) Description() string {
|
2018-05-15 00:15:40 +00:00
|
|
|
return "A plugin that can transmit metrics over HTTP"
|
|
|
|
}
|
|
|
|
|
2018-05-15 00:11:44 +00:00
|
|
|
func (h *HTTP) SampleConfig() string {
|
2018-05-15 00:15:40 +00:00
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
2018-05-15 00:11:44 +00:00
|
|
|
func (h *HTTP) Write(metrics []telegraf.Metric) error {
|
2018-05-15 00:15:40 +00:00
|
|
|
reqBody, err := h.serializer.SerializeBatch(metrics)
|
|
|
|
if err != nil {
|
2018-05-15 00:11:44 +00:00
|
|
|
return err
|
2018-05-15 00:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.write(reqBody); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-15 00:11:44 +00:00
|
|
|
func (h *HTTP) write(reqBody []byte) error {
|
2018-10-05 22:06:41 +00:00
|
|
|
var reqBodyBuffer io.Reader = bytes.NewBuffer(reqBody)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if h.ContentEncoding == "gzip" {
|
2019-11-13 20:56:01 +00:00
|
|
|
rc, err := internal.CompressWithGzip(reqBodyBuffer)
|
2018-10-05 22:06:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-13 20:56:01 +00:00
|
|
|
defer rc.Close()
|
|
|
|
reqBodyBuffer = rc
|
2018-10-05 22:06:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(h.Method, h.URL, reqBodyBuffer)
|
2018-07-17 21:54:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-15 00:15:40 +00:00
|
|
|
|
2018-08-29 19:28:29 +00:00
|
|
|
if h.Username != "" || h.Password != "" {
|
|
|
|
req.SetBasicAuth(h.Username, h.Password)
|
|
|
|
}
|
|
|
|
|
2020-04-21 17:02:18 +00:00
|
|
|
req.Header.Set("User-Agent", internal.ProductToken())
|
2018-05-15 00:11:44 +00:00
|
|
|
req.Header.Set("Content-Type", defaultContentType)
|
2018-10-05 22:06:41 +00:00
|
|
|
if h.ContentEncoding == "gzip" {
|
|
|
|
req.Header.Set("Content-Encoding", "gzip")
|
|
|
|
}
|
2018-05-15 00:15:40 +00:00
|
|
|
for k, v := range h.Headers {
|
2019-05-06 19:13:51 +00:00
|
|
|
if strings.ToLower(k) == "host" {
|
|
|
|
req.Host = v
|
|
|
|
}
|
2018-05-15 00:15:40 +00:00
|
|
|
req.Header.Set(k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := h.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
_, err = ioutil.ReadAll(resp.Body)
|
|
|
|
|
2018-05-15 00:11:44 +00:00
|
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
|
|
return fmt.Errorf("when writing to [%s] received status code: %d", h.URL, resp.StatusCode)
|
2018-05-15 00:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
outputs.Add("http", func() telegraf.Output {
|
2018-05-15 00:11:44 +00:00
|
|
|
return &HTTP{
|
|
|
|
Timeout: internal.Duration{Duration: defaultClientTimeout},
|
|
|
|
Method: defaultMethod,
|
2019-06-14 18:29:58 +00:00
|
|
|
URL: defaultURL,
|
2018-05-15 00:15:40 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|