feat(inputs/memcached): Support client TLS origination to memcached (#10642)

This commit is contained in:
LINKIWI 2022-02-22 10:53:11 -08:00 committed by GitHub
parent 7715b84773
commit 9bdfb21eee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 4 deletions

View File

@ -12,6 +12,14 @@ This plugin gathers statistics data from a Memcached server.
servers = ["localhost:11211"] servers = ["localhost:11211"]
# An array of unix memcached sockets to gather stats about. # An array of unix memcached sockets to gather stats about.
# unix_sockets = ["/var/run/memcached.sock"] # unix_sockets = ["/var/run/memcached.sock"]
## Optional TLS Config
# enable_tls = true
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## If false, skip chain & host verification
# insecure_skip_verify = true
``` ```
## Measurements & Fields ## Measurements & Fields

View File

@ -3,19 +3,24 @@ package memcached
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"crypto/tls"
"fmt" "fmt"
"net" "net"
"strconv" "strconv"
"time" "time"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
tlsint "github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
"golang.org/x/net/proxy"
) )
// Memcached is a memcached plugin // Memcached is a memcached plugin
type Memcached struct { type Memcached struct {
Servers []string Servers []string `toml:"servers"`
UnixSockets []string UnixSockets []string `toml:"unix_sockets"`
EnableTLS bool `toml:"enable_tls"`
tlsint.ClientConfig
} }
var sampleConfig = ` var sampleConfig = `
@ -23,6 +28,14 @@ var sampleConfig = `
## with optional port. ie localhost, 10.0.0.1:11211, etc. ## with optional port. ie localhost, 10.0.0.1:11211, etc.
servers = ["localhost:11211"] servers = ["localhost:11211"]
# unix_sockets = ["/var/run/memcached.sock"] # unix_sockets = ["/var/run/memcached.sock"]
## Optional TLS Config
# enable_tls = true
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## If false, skip chain & host verification
# insecure_skip_verify = true
` `
var defaultTimeout = 5 * time.Second var defaultTimeout = 5 * time.Second
@ -112,8 +125,23 @@ func (m *Memcached) gatherServer(
) error { ) error {
var conn net.Conn var conn net.Conn
var err error var err error
var dialer proxy.Dialer
dialer = &net.Dialer{Timeout: defaultTimeout}
if m.EnableTLS {
tlsCfg, err := m.ClientConfig.TLSConfig()
if err != nil {
return err
}
dialer = &tls.Dialer{
NetDialer: dialer.(*net.Dialer),
Config: tlsCfg,
}
}
if unix { if unix {
conn, err = net.DialTimeout("unix", address, defaultTimeout) conn, err = dialer.Dial("unix", address)
if err != nil { if err != nil {
return err return err
} }
@ -124,7 +152,7 @@ func (m *Memcached) gatherServer(
address = address + ":11211" address = address + ":11211"
} }
conn, err = net.DialTimeout("tcp", address, defaultTimeout) conn, err = dialer.Dial("tcp", address)
if err != nil { if err != nil {
return err return err
} }