github.com/influxdata/influxdb/v2@v2.7.6/telegraf/plugins/outputs/influxdb_v2.go (about)

     1  package outputs
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strconv"
     7  	"strings"
     8  )
     9  
    10  // InfluxDBV2 is based on telegraf influxdb_v2 output plugin.
    11  type InfluxDBV2 struct {
    12  	baseOutput
    13  	URLs         []string `json:"urls"`
    14  	Token        string   `json:"token"`
    15  	Organization string   `json:"organization"`
    16  	Bucket       string   `json:"bucket"`
    17  }
    18  
    19  // PluginName is based on telegraf plugin name.
    20  func (i *InfluxDBV2) PluginName() string {
    21  	return "influxdb_v2"
    22  }
    23  
    24  // TOML encodes to toml string.
    25  func (i *InfluxDBV2) TOML() string {
    26  	s := make([]string, len(i.URLs))
    27  	for k, v := range i.URLs {
    28  		s[k] = strconv.Quote(v)
    29  	}
    30  	return fmt.Sprintf(`[[outputs.%s]]
    31    ## The URLs of the InfluxDB cluster nodes.
    32    ##
    33    ## Multiple URLs can be specified for a single cluster, only ONE of the
    34    ## urls will be written to each interval.
    35    ##   ex: urls = ["https://us-west-2-1.aws.cloud2.influxdata.com"]
    36    urls = [%s]
    37  
    38    ## Token for authentication.
    39    token = "%s"
    40  
    41    ## Organization is the name of the organization you wish to write to; must exist.
    42    organization = "%s"
    43  
    44    ## Destination bucket to write into.
    45    bucket = "%s"
    46  
    47    ## The value of this tag will be used to determine the bucket.  If this
    48    ## tag is not set the 'bucket' option is used as the default.
    49    # bucket_tag = ""
    50  
    51    ## If true, the bucket tag will not be added to the metric.
    52    # exclude_bucket_tag = false
    53  
    54    ## Timeout for HTTP messages.
    55    # timeout = "5s"
    56  
    57    ## Additional HTTP headers
    58    # http_headers = {"X-Special-Header" = "Special-Value"}
    59  
    60    ## HTTP Proxy override, if unset values the standard proxy environment
    61    ## variables are consulted to determine which proxy, if any, should be used.
    62    # http_proxy = "http://corporate.proxy:3128"
    63  
    64    ## HTTP User-Agent
    65    # user_agent = "telegraf"
    66  
    67    ## Content-Encoding for write request body, can be set to "gzip" to
    68    ## compress body or "identity" to apply no encoding.
    69    # content_encoding = "gzip"
    70  
    71    ## Enable or disable uint support for writing uints influxdb 2.0.
    72    # influx_uint_support = false
    73  
    74    ## Optional TLS Config for use on HTTP connections.
    75    # tls_ca = "/etc/telegraf/ca.pem"
    76    # tls_cert = "/etc/telegraf/cert.pem"
    77    # tls_key = "/etc/telegraf/key.pem"
    78    ## Use TLS but skip chain & host verification
    79    # insecure_skip_verify = false
    80  `, i.PluginName(), strings.Join(s, ", "), i.Token, i.Organization, i.Bucket)
    81  }
    82  
    83  // UnmarshalTOML decodes the parsed data to the object
    84  func (i *InfluxDBV2) UnmarshalTOML(data interface{}) error {
    85  	dataOK, ok := data.(map[string]interface{})
    86  	if !ok {
    87  		return errors.New("bad urls for influxdb_v2 output plugin")
    88  	}
    89  	urls, ok := dataOK["urls"].([]interface{})
    90  	if !ok {
    91  		return errors.New("urls is not an array for influxdb_v2 output plugin")
    92  	}
    93  	for _, url := range urls {
    94  		i.URLs = append(i.URLs, url.(string))
    95  	}
    96  
    97  	i.Token, ok = dataOK["token"].(string)
    98  	if !ok {
    99  		return errors.New("token is missing for influxdb_v2 output plugin")
   100  	}
   101  
   102  	i.Organization, ok = dataOK["organization"].(string)
   103  	if !ok {
   104  		return errors.New("organization is missing for influxdb_v2 output plugin")
   105  	}
   106  
   107  	i.Bucket, ok = dataOK["bucket"].(string)
   108  	if !ok {
   109  		return errors.New("bucket is missing for influxdb_v2 output plugin")
   110  	}
   111  	return nil
   112  }