From cb5a12de3d45b034b409f64f5d3874cd2199c50b Mon Sep 17 00:00:00 2001 From: Eugene Shilin Date: Mon, 19 Jun 2017 21:42:43 +0300 Subject: [PATCH] Add standard SSL options to mysql input (#2933) --- etc/telegraf.conf | 5 +++++ plugins/inputs/mysql/README.md | 5 +++++ plugins/inputs/mysql/mysql.go | 21 ++++++++++++++++++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/etc/telegraf.conf b/etc/telegraf.conf index 8aa9427c4..c7691666d 100644 --- a/etc/telegraf.conf +++ b/etc/telegraf.conf @@ -1528,6 +1528,11 @@ # ## Some queries we may want to run less often (such as SHOW GLOBAL VARIABLES) # interval_slow = "30m" +# ## Optional SSL Config (will be used if tls=custom parameter specified in server uri) +# ssl_ca = "/etc/telegraf/ca.pem" +# ssl_cert = "/etc/telegraf/cert.pem" +# ssl_key = "/etc/telegraf/key.pem" + # # Read metrics about network interface usage # [[inputs.net]] diff --git a/plugins/inputs/mysql/README.md b/plugins/inputs/mysql/README.md index f941207c9..fb9a18eab 100644 --- a/plugins/inputs/mysql/README.md +++ b/plugins/inputs/mysql/README.md @@ -81,6 +81,11 @@ This plugin gathers the statistic data from MySQL server # ## Some queries we may want to run less often (such as SHOW GLOBAL VARIABLES) interval_slow = "30m" + + ## Optional SSL Config (will be used if tls=custom parameter specified in server uri) + ssl_ca = "/etc/telegraf/ca.pem" + ssl_cert = "/etc/telegraf/cert.pem" + ssl_key = "/etc/telegraf/key.pem" ``` ## Measurements & Fields diff --git a/plugins/inputs/mysql/mysql.go b/plugins/inputs/mysql/mysql.go index 77ca7e47e..2e8c61c8b 100644 --- a/plugins/inputs/mysql/mysql.go +++ b/plugins/inputs/mysql/mysql.go @@ -11,6 +11,7 @@ import ( "time" "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/plugins/inputs" "github.com/go-sql-driver/mysql" @@ -36,11 +37,14 @@ type Mysql struct { GatherFileEventsStats bool `toml:"gather_file_events_stats"` GatherPerfEventsStatements bool `toml:"gather_perf_events_statements"` IntervalSlow string `toml:"interval_slow"` + SSLCA string `toml:"ssl_ca"` + SSLCert string `toml:"ssl_cert"` + SSLKey string `toml:"ssl_key"` } var sampleConfig = ` ## specify servers via a url matching: - ## [username[:password]@][protocol[(address)]]/[?tls=[true|false|skip-verify]] + ## [username[:password]@][protocol[(address)]]/[?tls=[true|false|skip-verify|custom]] ## see https://github.com/go-sql-driver/mysql#dsn-data-source-name ## e.g. ## servers = ["user:passwd@tcp(127.0.0.1:3306)/?tls=false"] @@ -97,6 +101,11 @@ var sampleConfig = ` # ## Some queries we may want to run less often (such as SHOW GLOBAL VARIABLES) interval_slow = "30m" + + ## Optional SSL Config (will be used if tls=custom parameter specified in server uri) + ssl_ca = "/etc/telegraf/ca.pem" + ssl_cert = "/etc/telegraf/cert.pem" + ssl_key = "/etc/telegraf/key.pem" ` var defaultTimeout = time.Second * time.Duration(5) @@ -135,6 +144,16 @@ func (m *Mysql) Gather(acc telegraf.Accumulator) error { if !initDone { m.InitMysql() } + + tlsConfig, err := internal.GetTLSConfig(m.SSLCert, m.SSLKey, m.SSLCA, false) + if err != nil { + log.Printf("E! MySQL Error registering TLS config: %s", err) + } + + if tlsConfig != nil { + mysql.RegisterTLSConfig("custom", tlsConfig) + } + var wg sync.WaitGroup // Loop through each server and collect metrics