2018-05-15 08:15:40 +08:00
|
|
|
package http
|
|
|
|
|
|
|
|
|
|
import (
|
2021-09-15 05:04:34 +08:00
|
|
|
"bufio"
|
2018-05-15 08:15:40 +08:00
|
|
|
"bytes"
|
2018-09-07 01:54:05 +08:00
|
|
|
"context"
|
2021-12-11 04:06:33 +08:00
|
|
|
"crypto/sha256"
|
2018-05-15 08:15:40 +08:00
|
|
|
"fmt"
|
2018-10-06 06:06:41 +08:00
|
|
|
"io"
|
2018-05-15 08:15:40 +08:00
|
|
|
"net/http"
|
|
|
|
|
"strings"
|
2021-12-11 04:06:33 +08:00
|
|
|
"time"
|
2018-05-15 08:11:44 +08:00
|
|
|
|
2021-12-11 04:06:33 +08:00
|
|
|
awsV2 "github.com/aws/aws-sdk-go-v2/aws"
|
|
|
|
|
"github.com/aws/aws-sdk-go-v2/aws/signer/v4"
|
2018-05-15 08:11:44 +08:00
|
|
|
"github.com/influxdata/telegraf"
|
2021-12-11 04:06:33 +08:00
|
|
|
internalaws "github.com/influxdata/telegraf/config/aws"
|
2018-05-15 08:11:44 +08:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2021-04-23 21:37:27 +08:00
|
|
|
httpconfig "github.com/influxdata/telegraf/plugins/common/http"
|
2018-05-15 08:11:44 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/outputs"
|
|
|
|
|
"github.com/influxdata/telegraf/plugins/serializers"
|
2018-05-15 08:15:40 +08:00
|
|
|
)
|
|
|
|
|
|
2019-06-15 02:29:58 +08:00
|
|
|
const (
|
2021-09-15 05:04:34 +08:00
|
|
|
maxErrMsgLen = 1024
|
|
|
|
|
defaultURL = "http://127.0.0.1:8080/telegraf"
|
2019-06-15 02:29:58 +08:00
|
|
|
)
|
|
|
|
|
|
2018-05-15 08:15:40 +08:00
|
|
|
var sampleConfig = `
|
2018-05-15 08:11:44 +08:00
|
|
|
## URL is the address to send metrics to
|
2019-06-15 02:29:58 +08:00
|
|
|
url = "http://127.0.0.1:8080/telegraf"
|
2018-05-15 08:11:44 +08: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-07 01:54:05 +08: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 08:11:44 +08: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
|
|
|
|
|
|
2021-07-14 05:58:49 +08:00
|
|
|
## Optional Cookie authentication
|
|
|
|
|
# cookie_auth_url = "https://localhost/authMe"
|
|
|
|
|
# cookie_auth_method = "POST"
|
|
|
|
|
# cookie_auth_username = "username"
|
|
|
|
|
# cookie_auth_password = "pa$$word"
|
|
|
|
|
# cookie_auth_body = '{"username": "user", "password": "pa$$word", "authenticate": "me"}'
|
|
|
|
|
## cookie_auth_renewal not set or set to "0" will auth once and never renew the cookie
|
|
|
|
|
# cookie_auth_renewal = "5m"
|
|
|
|
|
|
2018-05-15 08:15:40 +08: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 08:11:44 +08:00
|
|
|
# data_format = "influx"
|
2018-09-07 01:54:05 +08:00
|
|
|
|
2021-10-29 22:05:28 +08:00
|
|
|
## Use batch serialization format (default) instead of line based format.
|
|
|
|
|
## Batch format is more efficient and should be used unless line based
|
|
|
|
|
## format is really needed.
|
|
|
|
|
# use_batch_format = true
|
|
|
|
|
|
2019-06-15 02:29:58 +08: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 09:31:47 +08: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"
|
2020-12-31 02:59:58 +08:00
|
|
|
|
|
|
|
|
## Idle (keep-alive) connection timeout.
|
|
|
|
|
## Maximum amount of time before idle connection is closed.
|
|
|
|
|
## Zero means no limit.
|
|
|
|
|
# idle_conn_timeout = 0
|
2021-12-11 04:06:33 +08:00
|
|
|
|
|
|
|
|
## Amazon Region
|
|
|
|
|
#region = "us-east-1"
|
|
|
|
|
|
|
|
|
|
## Amazon Credentials
|
|
|
|
|
## Credentials are loaded in the following order
|
|
|
|
|
## 1) Web identity provider credentials via STS if role_arn and web_identity_token_file are specified
|
|
|
|
|
## 2) Assumed credentials via STS if role_arn is specified
|
|
|
|
|
## 3) explicit credentials from 'access_key' and 'secret_key'
|
|
|
|
|
## 4) shared profile from 'profile'
|
|
|
|
|
## 5) environment variables
|
|
|
|
|
## 6) shared credentials file
|
|
|
|
|
## 7) EC2 Instance Profile
|
|
|
|
|
#access_key = ""
|
|
|
|
|
#secret_key = ""
|
|
|
|
|
#token = ""
|
|
|
|
|
#role_arn = ""
|
|
|
|
|
#web_identity_token_file = ""
|
|
|
|
|
#role_session_name = ""
|
|
|
|
|
#profile = ""
|
|
|
|
|
#shared_credential_file = ""
|
2018-05-15 08:15:40 +08:00
|
|
|
`
|
|
|
|
|
|
|
|
|
|
const (
|
2021-10-29 22:05:28 +08:00
|
|
|
defaultContentType = "text/plain; charset=utf-8"
|
|
|
|
|
defaultMethod = http.MethodPost
|
|
|
|
|
defaultUseBatchFormat = true
|
2018-05-15 08:15:40 +08:00
|
|
|
)
|
|
|
|
|
|
2018-05-15 08:11:44 +08:00
|
|
|
type HTTP struct {
|
2018-10-06 06:06:41 +08:00
|
|
|
URL string `toml:"url"`
|
|
|
|
|
Method string `toml:"method"`
|
|
|
|
|
Username string `toml:"username"`
|
|
|
|
|
Password string `toml:"password"`
|
|
|
|
|
Headers map[string]string `toml:"headers"`
|
|
|
|
|
ContentEncoding string `toml:"content_encoding"`
|
2021-10-29 22:05:28 +08:00
|
|
|
UseBatchFormat bool `toml:"use_batch_format"`
|
2021-12-11 04:06:33 +08:00
|
|
|
AwsService string `toml:"aws_service"`
|
2021-04-23 21:37:27 +08:00
|
|
|
httpconfig.HTTPClientConfig
|
2021-07-14 05:58:49 +08:00
|
|
|
Log telegraf.Logger `toml:"-"`
|
2018-05-15 08:15:40 +08:00
|
|
|
|
2018-05-15 08:11:44 +08:00
|
|
|
client *http.Client
|
2018-05-15 08:15:40 +08:00
|
|
|
serializer serializers.Serializer
|
2021-12-11 04:06:33 +08:00
|
|
|
|
|
|
|
|
awsCfg *awsV2.Config
|
|
|
|
|
internalaws.CredentialConfig
|
2018-05-15 08:15:40 +08:00
|
|
|
}
|
|
|
|
|
|
2018-05-15 08:11:44 +08:00
|
|
|
func (h *HTTP) SetSerializer(serializer serializers.Serializer) {
|
2018-05-15 08:15:40 +08:00
|
|
|
h.serializer = serializer
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-15 08:11:44 +08:00
|
|
|
func (h *HTTP) Connect() error {
|
2021-12-11 04:06:33 +08:00
|
|
|
if h.AwsService != "" {
|
|
|
|
|
cfg, err := h.CredentialConfig.Credentials()
|
|
|
|
|
if err == nil {
|
|
|
|
|
h.awsCfg = &cfg
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-15 08:11:44 +08:00
|
|
|
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 08:15:40 +08:00
|
|
|
}
|
|
|
|
|
|
2018-09-07 01:54:05 +08:00
|
|
|
ctx := context.Background()
|
2021-07-14 05:58:49 +08:00
|
|
|
client, err := h.HTTPClientConfig.CreateClient(ctx, h.Log)
|
2018-05-15 08:11:44 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2018-05-15 08:15:40 +08:00
|
|
|
}
|
|
|
|
|
|
2018-09-07 01:54:05 +08:00
|
|
|
h.client = client
|
2018-05-15 08:15:40 +08:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-15 08:11:44 +08:00
|
|
|
func (h *HTTP) Close() error {
|
2018-05-15 08:15:40 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-15 08:11:44 +08:00
|
|
|
func (h *HTTP) Description() string {
|
2018-05-15 08:15:40 +08:00
|
|
|
return "A plugin that can transmit metrics over HTTP"
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-15 08:11:44 +08:00
|
|
|
func (h *HTTP) SampleConfig() string {
|
2018-05-15 08:15:40 +08:00
|
|
|
return sampleConfig
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-15 08:11:44 +08:00
|
|
|
func (h *HTTP) Write(metrics []telegraf.Metric) error {
|
2021-10-29 22:05:28 +08:00
|
|
|
var reqBody []byte
|
|
|
|
|
|
|
|
|
|
if h.UseBatchFormat {
|
|
|
|
|
var err error
|
|
|
|
|
reqBody, err = h.serializer.SerializeBatch(metrics)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-25 03:40:25 +08:00
|
|
|
return h.writeMetric(reqBody)
|
2018-05-15 08:15:40 +08:00
|
|
|
}
|
|
|
|
|
|
2021-10-29 22:05:28 +08:00
|
|
|
for _, metric := range metrics {
|
|
|
|
|
var err error
|
|
|
|
|
reqBody, err = h.serializer.Serialize(metric)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-25 03:40:25 +08:00
|
|
|
if err := h.writeMetric(reqBody); err != nil {
|
2021-10-29 22:05:28 +08:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2018-05-15 08:15:40 +08:00
|
|
|
}
|
|
|
|
|
|
2021-11-25 03:40:25 +08:00
|
|
|
func (h *HTTP) writeMetric(reqBody []byte) error {
|
2018-10-06 06:06:41 +08:00
|
|
|
var reqBodyBuffer io.Reader = bytes.NewBuffer(reqBody)
|
|
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
if h.ContentEncoding == "gzip" {
|
2019-11-14 04:56:01 +08:00
|
|
|
rc, err := internal.CompressWithGzip(reqBodyBuffer)
|
2018-10-06 06:06:41 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2019-11-14 04:56:01 +08:00
|
|
|
defer rc.Close()
|
|
|
|
|
reqBodyBuffer = rc
|
2018-10-06 06:06:41 +08:00
|
|
|
}
|
|
|
|
|
|
2021-12-11 04:06:33 +08:00
|
|
|
var payloadHash *string
|
|
|
|
|
if h.awsCfg != nil {
|
|
|
|
|
// We need a local copy of the full buffer, the signature scheme requires a sha256 of the request body.
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
_, err = io.Copy(buf, reqBodyBuffer)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sum := sha256.Sum256(buf.Bytes())
|
|
|
|
|
reqBodyBuffer = buf
|
|
|
|
|
|
|
|
|
|
// sha256 is hex encoded
|
|
|
|
|
hash := fmt.Sprintf("%x", sum)
|
|
|
|
|
payloadHash = &hash
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-06 06:06:41 +08:00
|
|
|
req, err := http.NewRequest(h.Method, h.URL, reqBodyBuffer)
|
2018-07-18 05:54:10 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2018-05-15 08:15:40 +08:00
|
|
|
|
2021-12-11 04:06:33 +08:00
|
|
|
if h.awsCfg != nil {
|
|
|
|
|
signer := v4.NewSigner()
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
credentials, err := h.awsCfg.Credentials.Retrieve(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = signer.SignHTTP(ctx, credentials, req, *payloadHash, h.AwsService, h.Region, time.Now().UTC())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 03:28:29 +08:00
|
|
|
if h.Username != "" || h.Password != "" {
|
|
|
|
|
req.SetBasicAuth(h.Username, h.Password)
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 01:02:18 +08:00
|
|
|
req.Header.Set("User-Agent", internal.ProductToken())
|
2018-05-15 08:11:44 +08:00
|
|
|
req.Header.Set("Content-Type", defaultContentType)
|
2018-10-06 06:06:41 +08:00
|
|
|
if h.ContentEncoding == "gzip" {
|
|
|
|
|
req.Header.Set("Content-Encoding", "gzip")
|
|
|
|
|
}
|
2018-05-15 08:15:40 +08:00
|
|
|
for k, v := range h.Headers {
|
2019-05-07 03:13:51 +08:00
|
|
|
if strings.ToLower(k) == "host" {
|
|
|
|
|
req.Host = v
|
|
|
|
|
}
|
2018-05-15 08:15:40 +08:00
|
|
|
req.Header.Set(k, v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := h.client.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
2018-05-15 08:11:44 +08:00
|
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
2021-09-15 05:04:34 +08:00
|
|
|
errorLine := ""
|
|
|
|
|
scanner := bufio.NewScanner(io.LimitReader(resp.Body, maxErrMsgLen))
|
|
|
|
|
if scanner.Scan() {
|
|
|
|
|
errorLine = scanner.Text()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fmt.Errorf("when writing to [%s] received status code: %d. body: %s", h.URL, resp.StatusCode, errorLine)
|
2018-05-15 08:15:40 +08:00
|
|
|
}
|
2021-09-15 05:04:34 +08:00
|
|
|
|
2021-09-29 05:16:32 +08:00
|
|
|
_, err = io.ReadAll(resp.Body)
|
2021-03-04 03:56:31 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("when writing to [%s] received error: %v", h.URL, err)
|
|
|
|
|
}
|
2018-05-15 08:15:40 +08:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
outputs.Add("http", func() telegraf.Output {
|
2018-05-15 08:11:44 +08:00
|
|
|
return &HTTP{
|
2021-10-29 22:05:28 +08:00
|
|
|
Method: defaultMethod,
|
|
|
|
|
URL: defaultURL,
|
|
|
|
|
UseBatchFormat: defaultUseBatchFormat,
|
2018-05-15 08:15:40 +08:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|