From f82d06cc26f1880dc325ed47ae740d0b6c711972 Mon Sep 17 00:00:00 2001 From: HungNT Date: Tue, 20 Aug 2024 19:24:54 +0700 Subject: [PATCH] feat(inputs.mysql): Add support for replica status (#15749) --- plugins/inputs/mysql/README.md | 7 ++++++- plugins/inputs/mysql/mysql.go | 15 +++++++++++---- plugins/inputs/mysql/sample.conf | 4 ++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/plugins/inputs/mysql/README.md b/plugins/inputs/mysql/README.md index 676cf482cedbb..22aa69271df40 100644 --- a/plugins/inputs/mysql/README.md +++ b/plugins/inputs/mysql/README.md @@ -85,7 +85,11 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details. ## gather metrics from SHOW SLAVE STATUS command output # gather_slave_status = false + ## gather metrics from SHOW REPLICA STATUS command output + # gather_replica_status = false + ## use SHOW ALL SLAVES STATUS command output for MariaDB + ## use SHOW ALL REPLICAS STATUS command if enable gather replica status # mariadb_dialect = false ## gather metrics from SHOW BINARY LOGS command output @@ -260,7 +264,8 @@ the single-source replication is on. If the multi-source replication is set, then everything works differently, this metric does not work with multi-source replication, unless you set `gather_all_slave_channels = true`. For MariaDB, `mariadb_dialect = true` should be set to address the field names and commands -differences. +differences. If enable `gather_replica_status` metrics gather from command +`SHOW REPLICA STATUS`, for MariaDB will be `SHOW ALL REPLICAS STATUS` * slave_[column name] * Binary logs - all metrics including size and count of all binary files. Requires to be turned on in configuration. diff --git a/plugins/inputs/mysql/mysql.go b/plugins/inputs/mysql/mysql.go index 1f9a1b18aa0a8..be3b3e85b8243 100644 --- a/plugins/inputs/mysql/mysql.go +++ b/plugins/inputs/mysql/mysql.go @@ -40,6 +40,7 @@ type Mysql struct { GatherInfoSchemaAutoInc bool `toml:"gather_info_schema_auto_inc"` GatherInnoDBMetrics bool `toml:"gather_innodb_metrics"` GatherSlaveStatus bool `toml:"gather_slave_status"` + GatherReplicaStatus bool `toml:"gather_replica_status"` GatherAllSlaveChannels bool `toml:"gather_all_slave_channels"` MariadbDialect bool `toml:"mariadb_dialect"` GatherBinaryLogs bool `toml:"gather_binary_logs"` @@ -77,12 +78,16 @@ func (*Mysql) SampleConfig() string { } func (m *Mysql) Init() error { - if m.MariadbDialect { + switch { + case m.MariadbDialect && m.GatherReplicaStatus: + m.getStatusQuery = replicaStatusQueryMariadb + case m.MariadbDialect: m.getStatusQuery = slaveStatusQueryMariadb - } else { + case m.GatherReplicaStatus: + m.getStatusQuery = replicaStatusQuery + default: m.getStatusQuery = slaveStatusQuery } - // Default to localhost if nothing specified. if len(m.Servers) == 0 { s := config.NewSecret([]byte(localhost)) @@ -250,7 +255,9 @@ const ( globalStatusQuery = `SHOW GLOBAL STATUS` globalVariablesQuery = `SHOW GLOBAL VARIABLES` slaveStatusQuery = `SHOW SLAVE STATUS` + replicaStatusQuery = `SHOW REPLICA STATUS` slaveStatusQueryMariadb = `SHOW ALL SLAVES STATUS` + replicaStatusQueryMariadb = `SHOW ALL REPLICAS STATUS` binaryLogsQuery = `SHOW BINARY LOGS` infoSchemaProcessListQuery = ` SELECT COALESCE(command,''),COALESCE(state,''),count(*) @@ -475,7 +482,7 @@ func (m *Mysql) gatherServer(server *config.Secret, acc telegraf.Accumulator) er } } - if m.GatherSlaveStatus { + if m.GatherSlaveStatus || m.GatherReplicaStatus { err = m.gatherSlaveStatuses(db, servtag, acc) if err != nil { return err diff --git a/plugins/inputs/mysql/sample.conf b/plugins/inputs/mysql/sample.conf index 35e5e91d166ac..a3a439c90f7eb 100644 --- a/plugins/inputs/mysql/sample.conf +++ b/plugins/inputs/mysql/sample.conf @@ -48,7 +48,11 @@ ## gather metrics from SHOW SLAVE STATUS command output # gather_slave_status = false + ## gather metrics from SHOW REPLICA STATUS command output + # gather_replica_status = false + ## use SHOW ALL SLAVES STATUS command output for MariaDB + ## use SHOW ALL REPLICAS STATUS command if enable gather replica status # mariadb_dialect = false ## gather metrics from SHOW BINARY LOGS command output