influxdb/telegraf/plugins/inputs/redis.go

67 lines
1.5 KiB
Go
Raw Normal View History

2018-10-05 21:43:01 +00:00
package inputs
import (
2018-10-17 19:51:35 +00:00
"errors"
2018-10-05 21:43:01 +00:00
"fmt"
"strconv"
"strings"
)
// Redis is based on telegraf Redis plugin.
type Redis struct {
2018-10-16 00:38:36 +00:00
baseInput
2018-10-05 21:43:01 +00:00
Servers []string `json:"servers"`
Password string `json:"password"`
}
2018-10-16 00:38:36 +00:00
// PluginName is based on telegraf plugin name.
func (r *Redis) PluginName() string {
return "redis"
}
2018-10-05 21:43:01 +00:00
// TOML encodes to toml string
func (r *Redis) TOML() string {
s := make([]string, len(r.Servers))
2018-10-05 21:43:01 +00:00
for k, v := range r.Servers {
2018-10-05 21:43:01 +00:00
s[k] = strconv.Quote(v)
}
2018-10-05 21:43:01 +00:00
password := ` # password = ""`
if r.Password != "" {
password = fmt.Sprintf(` password = "%s"`, r.Password)
}
2018-10-16 00:38:36 +00:00
return fmt.Sprintf(`[[inputs.%s]]
2018-10-05 21:43:01 +00:00
## specify servers via a url matching:
## [protocol://][:password]@address[:port]
## e.g.
## tcp://localhost:6379
## tcp://:password@192.168.99.100
## unix:///var/run/redis.sock
##
## If no servers are specified, then localhost is used as the host.
## If no port is specified, 6379 is used
servers = [%s]
## specify server password
2018-10-05 21:43:01 +00:00
%s
2018-10-16 00:38:36 +00:00
`, r.PluginName(), strings.Join(s, ", "), password)
2018-10-05 21:43:01 +00:00
}
2018-10-17 19:51:35 +00:00
// UnmarshalTOML decodes the parsed data to the object
func (r *Redis) UnmarshalTOML(data interface{}) error {
dataOK, ok := data.(map[string]interface{})
if !ok {
return errors.New("bad servers for redis input plugin")
}
servers, ok := dataOK["servers"].([]interface{})
if !ok {
return errors.New("servers is not an array for redis input plugin")
}
for _, server := range servers {
r.Servers = append(r.Servers, server.(string))
}
r.Password, _ = dataOK["password"].(string)
return nil
}