diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bcf1b2..7257bfc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ This project adheres to [Semantic Versioning](http://semver.org/). This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins/community/blob/master/HOW_WE_CHANGELOG.md) ## [Unreleased] +### Added +- Option to define default charset in following scripts: + - `check-mysql-alive.rb` + - `check-mysql-connections.rb` + - `check-mysql-replication-status.rb` + - `check-mysql-select-count.rb` ## [3.2.0] - 2020-08-26 ### Changed diff --git a/bin/check-mysql-alive.rb b/bin/check-mysql-alive.rb index 4372fbc..a9e841c 100755 --- a/bin/check-mysql-alive.rb +++ b/bin/check-mysql-alive.rb @@ -80,6 +80,11 @@ class CheckMySQL < Sensu::Plugin::Check::CLI short: '-s SOCKET', long: '--socket SOCKET' + option :default_charset, + short: '-D', + long: '--default_charset=VALUE', + description: 'Provide custom charset for connection' + def run if config[:ini] ini = IniFile.load(config[:ini]) @@ -92,7 +97,11 @@ def run end begin - db = Mysql.real_connect(config[:hostname], db_user, db_pass, config[:database], config[:port].to_i, config[:socket]) + db = Mysql.init + if config[:default_charset] + db.options Mysql::SET_CHARSET_NAME, config[:default_charset] + end + db.real_connect(config[:hostname], db_user, db_pass, config[:database], config[:port].to_i, config[:socket]) info = db.get_server_info ok "Server version: #{info}" rescue Mysql::Error => e diff --git a/bin/check-mysql-connections.rb b/bin/check-mysql-connections.rb index 098c43f..847943e 100755 --- a/bin/check-mysql-connections.rb +++ b/bin/check-mysql-connections.rb @@ -73,6 +73,11 @@ class CheckMySQLHealth < Sensu::Plugin::Check::CLI long: '--percentage', default: false + option :default_charset, + short: '-D', + long: '--default_charset=VALUE', + description: 'Provide custom charset for connection' + def run if config[:ini] ini = IniFile.load(config[:ini]) @@ -83,7 +88,11 @@ def run db_user = config[:user] db_pass = config[:password] end - db = Mysql.real_connect(config[:hostname], db_user, db_pass, config[:database], config[:port].to_i, config[:socket]) + db = Mysql.init + if config[:default_charset] + db.options Mysql::SET_CHARSET_NAME, config[:default_charset] + end + db.real_connect(config[:hostname], db_user, db_pass, config[:database], config[:port].to_i, config[:socket]) max_con = db .query("SHOW VARIABLES LIKE 'max_connections'") .fetch_hash diff --git a/bin/check-mysql-replication-status.rb b/bin/check-mysql-replication-status.rb index 668681b..647dcb5 100755 --- a/bin/check-mysql-replication-status.rb +++ b/bin/check-mysql-replication-status.rb @@ -69,6 +69,11 @@ class CheckMysqlReplicationStatus < Sensu::Plugin::Check::CLI long: '--master-connection=VALUE', description: 'Replication master connection name' + option :default_charset, + short: '-D', + long: '--default_charset=VALUE', + description: 'Provide custom charset for connection' + option :ini, short: '-i', long: '--ini VALUE', @@ -131,7 +136,11 @@ def run end begin - db = Mysql.new(db_host, db_user, db_pass, nil, config[:port], config[:socket]) + db = Mysql.init + if config[:default_charset] + db.options Mysql::SET_CHARSET_NAME, config[:default_charset] + end + db.real_connect(db_host, db_user, db_pass, nil, config[:port], config[:socket]) results = if db_conn.nil? db.query 'SHOW SLAVE STATUS' diff --git a/bin/check-mysql-select-count.rb b/bin/check-mysql-select-count.rb index 5d7841d..22e4249 100755 --- a/bin/check-mysql-select-count.rb +++ b/bin/check-mysql-select-count.rb @@ -80,6 +80,11 @@ class MysqlSelectCountCheck < Sensu::Plugin::Check::CLI description: 'Query to execute', required: true + option :default_charset, + short: '-D', + long: '--default_charset=VALUE', + description: 'Provide custom charset for connection' + def run if config[:ini] ini = IniFile.load(config[:ini]) @@ -94,7 +99,11 @@ def run end raise 'Please specify hostname using -h or in mysql.cnf file' unless config[:host] - db = Mysql.real_connect(config[:host], db_user, db_pass, config[:database], config[:port], config[:socket]) + db = Mysql.init + if config[:default_charset] + db.options Mysql::SET_CHARSET_NAME, config[:default_charset] + end + db.real_connect(config[:hostname], db_user, db_pass, config[:database], config[:port].to_i, config[:socket]) count = db.query(config[:query]).fetch_row[0].to_i if count >= config[:crit]