From e5349393f878bb994990d6b2d33e82e731f50802 Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Thu, 16 Feb 2017 22:24:42 +0000 Subject: [PATCH] Check for errors in user stats & process list closes #2414 --- CHANGELOG.md | 1 + plugins/inputs/mysql/mysql.go | 153 ++++++++++++++++++---------------- 2 files changed, 81 insertions(+), 73 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6896a7ad7..8febc3e50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,7 @@ be deprecated eventually. - [#2282](https://github.com/influxdata/telegraf/issues/2282): Reloading telegraf freezes prometheus output. - [#2390](https://github.com/influxdata/telegraf/issues/2390): Empty tag value causes error on InfluxDB output. - [#2380](https://github.com/influxdata/telegraf/issues/2380): buffer_size field value is negative number from "internal" plugin. +- [#2414](https://github.com/influxdata/telegraf/issues/2414): Missing error handling in the MySQL plugin leads to segmentation violation. ## v1.2.1 [2017-02-01] diff --git a/plugins/inputs/mysql/mysql.go b/plugins/inputs/mysql/mysql.go index adc21880b..1ff7c3421 100644 --- a/plugins/inputs/mysql/mysql.go +++ b/plugins/inputs/mysql/mysql.go @@ -4,6 +4,7 @@ import ( "bytes" "database/sql" "fmt" + "log" "strconv" "strings" "sync" @@ -904,92 +905,98 @@ func (m *Mysql) gatherGlobalStatuses(db *sql.DB, serv string, acc telegraf.Accum // gather connection metrics from processlist for each user if m.GatherProcessList { conn_rows, err := db.Query("SELECT user, sum(1) FROM INFORMATION_SCHEMA.PROCESSLIST GROUP BY user") + if err != nil { + log.Printf("E! MySQL Error gathering process list: %s", err) + } else { + for conn_rows.Next() { + var user string + var connections int64 - for conn_rows.Next() { - var user string - var connections int64 + err = conn_rows.Scan(&user, &connections) + if err != nil { + return err + } - err = conn_rows.Scan(&user, &connections) - if err != nil { - return err + tags := map[string]string{"server": servtag, "user": user} + fields := make(map[string]interface{}) + + if err != nil { + return err + } + fields["connections"] = connections + acc.AddFields("mysql_users", fields, tags) } - - tags := map[string]string{"server": servtag, "user": user} - fields := make(map[string]interface{}) - - if err != nil { - return err - } - fields["connections"] = connections - acc.AddFields("mysql_users", fields, tags) } } // gather connection metrics from user_statistics for each user if m.GatherUserStatistics { conn_rows, err := db.Query("select user, total_connections, concurrent_connections, connected_time, busy_time, cpu_time, bytes_received, bytes_sent, binlog_bytes_written, rows_fetched, rows_updated, table_rows_read, select_commands, update_commands, other_commands, commit_transactions, rollback_transactions, denied_connections, lost_connections, access_denied, empty_queries, total_ssl_connections FROM INFORMATION_SCHEMA.USER_STATISTICS GROUP BY user") + if err != nil { + log.Printf("E! MySQL Error gathering user stats: %s", err) + } else { + for conn_rows.Next() { + var user string + var total_connections int64 + var concurrent_connections int64 + var connected_time int64 + var busy_time int64 + var cpu_time int64 + var bytes_received int64 + var bytes_sent int64 + var binlog_bytes_written int64 + var rows_fetched int64 + var rows_updated int64 + var table_rows_read int64 + var select_commands int64 + var update_commands int64 + var other_commands int64 + var commit_transactions int64 + var rollback_transactions int64 + var denied_connections int64 + var lost_connections int64 + var access_denied int64 + var empty_queries int64 + var total_ssl_connections int64 - for conn_rows.Next() { - var user string - var total_connections int64 - var concurrent_connections int64 - var connected_time int64 - var busy_time int64 - var cpu_time int64 - var bytes_received int64 - var bytes_sent int64 - var binlog_bytes_written int64 - var rows_fetched int64 - var rows_updated int64 - var table_rows_read int64 - var select_commands int64 - var update_commands int64 - var other_commands int64 - var commit_transactions int64 - var rollback_transactions int64 - var denied_connections int64 - var lost_connections int64 - var access_denied int64 - var empty_queries int64 - var total_ssl_connections int64 + err = conn_rows.Scan(&user, &total_connections, &concurrent_connections, + &connected_time, &busy_time, &cpu_time, &bytes_received, &bytes_sent, &binlog_bytes_written, + &rows_fetched, &rows_updated, &table_rows_read, &select_commands, &update_commands, &other_commands, + &commit_transactions, &rollback_transactions, &denied_connections, &lost_connections, &access_denied, + &empty_queries, &total_ssl_connections, + ) - err = conn_rows.Scan(&user, &total_connections, &concurrent_connections, - &connected_time, &busy_time, &cpu_time, &bytes_received, &bytes_sent, &binlog_bytes_written, - &rows_fetched, &rows_updated, &table_rows_read, &select_commands, &update_commands, &other_commands, - &commit_transactions, &rollback_transactions, &denied_connections, &lost_connections, &access_denied, - &empty_queries, &total_ssl_connections, - ) + if err != nil { + return err + } - if err != nil { - return err + tags := map[string]string{"server": servtag, "user": user} + fields := map[string]interface{}{ + "total_connections": total_connections, + "concurrent_connections": concurrent_connections, + "connected_time": connected_time, + "busy_time": busy_time, + "cpu_time": cpu_time, + "bytes_received": bytes_received, + "bytes_sent": bytes_sent, + "binlog_bytes_written": binlog_bytes_written, + "rows_fetched": rows_fetched, + "rows_updated": rows_updated, + "table_rows_read": table_rows_read, + "select_commands": select_commands, + "update_commands": update_commands, + "other_commands": other_commands, + "commit_transactions": commit_transactions, + "rollback_transactions": rollback_transactions, + "denied_connections": denied_connections, + "lost_connections": lost_connections, + "access_denied": access_denied, + "empty_queries": empty_queries, + "total_ssl_connections": total_ssl_connections, + } + + acc.AddFields("mysql_user_stats", fields, tags) } - - tags := map[string]string{"server": servtag, "user": user} - fields := map[string]interface{}{ - "total_connections": total_connections, - "concurrent_connections": concurrent_connections, - "connected_time": connected_time, - "busy_time": busy_time, - "cpu_time": cpu_time, - "bytes_received": bytes_received, - "bytes_sent": bytes_sent, - "binlog_bytes_written": binlog_bytes_written, - "rows_fetched": rows_fetched, - "rows_updated": rows_updated, - "table_rows_read": table_rows_read, - "select_commands": select_commands, - "update_commands": update_commands, - "other_commands": other_commands, - "commit_transactions": commit_transactions, - "rollback_transactions": rollback_transactions, - "denied_connections": denied_connections, - "lost_connections": lost_connections, - "access_denied": access_denied, - "empty_queries": empty_queries, - "total_ssl_connections": total_ssl_connections, - } - - acc.AddFields("mysql_user_stats", fields, tags) } }