2018-05-15 08:15:40 +08:00
|
|
|
package http
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2018-09-07 01:54:05 +08:00
|
|
|
"context"
|
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
|
|
|
"io/ioutil"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
2018-05-15 08:11:44 +08:00
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
|
"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 (
|
|
|
|
|
defaultURL = "http://127.0.0.1:8080/telegraf"
|
|
|
|
|
)
|
|
|
|
|
|
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
|
|
|
|
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
|
2018-05-15 08:15:40 +08:00
|
|
|
`
|
|
|
|
|
|
|
|
|
|
const (
|
2018-05-15 08:11:44 +08:00
|
|
|
defaultClientTimeout = 5 * time.Second
|
|
|
|
|
defaultContentType = "text/plain; charset=utf-8"
|
|
|
|
|
defaultMethod = http.MethodPost
|
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-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
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
|
|
|
|
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 {
|
2018-05-15 08:15:40 +08:00
|
|
|
reqBody, err := h.serializer.SerializeBatch(metrics)
|
|
|
|
|
if err != nil {
|
2018-05-15 08:11:44 +08:00
|
|
|
return err
|
2018-05-15 08:15:40 +08:00
|
|
|
}
|
|
|
|
|
|
2021-02-17 07:19:50 +08:00
|
|
|
return h.write(reqBody)
|
2018-05-15 08:15:40 +08:00
|
|
|
}
|
|
|
|
|
|
2018-05-15 08:11:44 +08:00
|
|
|
func (h *HTTP) write(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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
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()
|
|
|
|
|
_, err = ioutil.ReadAll(resp.Body)
|
|
|
|
|
|
2018-05-15 08:11:44 +08: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 08:15:40 +08:00
|
|
|
}
|
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-04-23 21:37:27 +08:00
|
|
|
Method: defaultMethod,
|
|
|
|
|
URL: defaultURL,
|
2018-05-15 08:15:40 +08:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|