github.com/influxdata/influxdb/v2@v2.7.6/telegraf/plugins/inputs/redis.go (about)

     1  package inputs
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strconv"
     7  	"strings"
     8  )
     9  
    10  // Redis is based on telegraf Redis plugin.
    11  type Redis struct {
    12  	baseInput
    13  	Servers  []string `json:"servers"`
    14  	Password string   `json:"password"`
    15  }
    16  
    17  // PluginName is based on telegraf plugin name.
    18  func (r *Redis) PluginName() string {
    19  	return "redis"
    20  }
    21  
    22  // TOML encodes to toml string
    23  func (r *Redis) TOML() string {
    24  	s := make([]string, len(r.Servers))
    25  	for k, v := range r.Servers {
    26  		s[k] = strconv.Quote(v)
    27  	}
    28  	password := `  # password = ""`
    29  	if r.Password != "" {
    30  		password = fmt.Sprintf(`  # password = "%s"`, r.Password)
    31  	}
    32  	return fmt.Sprintf(`[[inputs.%s]]
    33    ## specify servers via a url matching:
    34    ##  [protocol://][:password]@address[:port]
    35    ##  e.g.
    36    ##    tcp://localhost:6379
    37    ##    tcp://:password@192.168.99.100
    38    ##    unix:///var/run/redis.sock
    39    ##
    40    ## If no servers are specified, then localhost is used as the host.
    41    ## If no port is specified, 6379 is used
    42    servers = [%s]
    43  
    44    ## Optional. Specify redis commands to retrieve values
    45    # [[inputs.redis.commands]]
    46    #   # The command to run where each argument is a separate element
    47    #   command = ["get", "sample-key"]
    48    #   # The field to store the result in
    49    #   field = "sample-key-value"
    50    #   # The type of the result
    51    #   # Can be "string", "integer", or "float"
    52    #   type = "string"
    53  
    54    ## specify server password
    55  %s
    56  
    57    ## Optional TLS Config
    58    # tls_ca = "/etc/telegraf/ca.pem"
    59    # tls_cert = "/etc/telegraf/cert.pem"
    60    # tls_key = "/etc/telegraf/key.pem"
    61    ## Use TLS but skip chain & host verification
    62    # insecure_skip_verify = true
    63  `, r.PluginName(), strings.Join(s, ", "), password)
    64  }
    65  
    66  // UnmarshalTOML decodes the parsed data to the object
    67  func (r *Redis) UnmarshalTOML(data interface{}) error {
    68  	dataOK, ok := data.(map[string]interface{})
    69  	if !ok {
    70  		return errors.New("bad servers for redis input plugin")
    71  	}
    72  	servers, ok := dataOK["servers"].([]interface{})
    73  	if !ok {
    74  		return errors.New("servers is not an array for redis input plugin")
    75  	}
    76  	for _, server := range servers {
    77  		r.Servers = append(r.Servers, server.(string))
    78  	}
    79  
    80  	r.Password, _ = dataOK["password"].(string)
    81  
    82  	return nil
    83  }