Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gather all mysql channels #5517

Merged
merged 15 commits into from
Apr 14, 2021
5 changes: 4 additions & 1 deletion plugins/inputs/mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ This plugin gathers the statistic data from MySQL server
## gather metrics from SHOW SLAVE STATUS command output
gather_slave_status = true
#
## gather metrics from all channels from SHOW SLAVE STATUS command output
gather_all_slave_channels = false
#
## gather metrics from SHOW BINARY LOGS command output
gather_binary_logs = false
#
Expand Down Expand Up @@ -177,7 +180,7 @@ measurement name.
* Slave status - metrics from `SHOW SLAVE STATUS` the metrics are gathered when
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.
replication, unless you set `gather_all_slave_channels = true`
* slave_[column name]()
* Binary logs - all metrics including size and count of all binary files.
Requires to be turned on in configuration.
Expand Down
27 changes: 23 additions & 4 deletions plugins/inputs/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Mysql struct {
GatherInfoSchemaAutoInc bool `toml:"gather_info_schema_auto_inc"`
GatherInnoDBMetrics bool `toml:"gather_innodb_metrics"`
GatherSlaveStatus bool `toml:"gather_slave_status"`
GatherAllSlaveChannels bool `toml:"gather_all_slave_channels"`
GatherBinaryLogs bool `toml:"gather_binary_logs"`
GatherTableIOWaits bool `toml:"gather_table_io_waits"`
GatherTableLockWaits bool `toml:"gather_table_lock_waits"`
Expand Down Expand Up @@ -91,6 +92,9 @@ var sampleConfig = `
## gather metrics from SHOW SLAVE STATUS command output
gather_slave_status = true
#
## gather metrics from all channels from SHOW SLAVE STATUS command output
gather_all_slave_channels = false
#
## gather metrics from SHOW BINARY LOGS command output
gather_binary_logs = false
#
Expand Down Expand Up @@ -588,9 +592,11 @@ func (m *Mysql) gatherSlaveStatuses(db *sql.DB, serv string, acc telegraf.Accumu
tags := map[string]string{"server": servtag}
fields := make(map[string]interface{})

// to save the column names as a field key
// scanning keys and values separately
if rows.Next() {
// for each channel record
for rows.Next() {
// to save the column names as a field key
// scanning keys and values separately

// get columns names, and create an array with its length
cols, err := rows.Columns()
if err != nil {
Expand All @@ -609,11 +615,24 @@ func (m *Mysql) gatherSlaveStatuses(db *sql.DB, serv string, acc telegraf.Accumu
if m.MetricVersion >= 2 {
col = strings.ToLower(col)
}
if value, ok := m.parseValue(*vals[i].(*sql.RawBytes)); ok {

if m.GatherAllSlaveChannels && strings.ToLower(col) == "channel_name" {
// Since the default channel name is empty, we need this block
channelName := "default"
if len(*vals[i].(*sql.RawBytes)) > 0 {
channelName = string(*vals[i].(*sql.RawBytes))
}
tags["channel"] = channelName
acc.AddError(fmt.Errorf("DEBUG! %s = (%s)", col, channelName))
} else if value, ok := m.parseValue(*vals[i].(*sql.RawBytes)); ok {
fields["slave_"+col] = value
}
}
acc.AddFields("mysql", fields, tags)

if !m.GatherAllSlaveChannels {
break
}
}

return nil
Expand Down