Skip to content

Commit

Permalink
Option to define charset
Browse files Browse the repository at this point in the history
  • Loading branch information
VeselaHouba committed May 24, 2021
1 parent e731147 commit f788c83
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion bin/check-mysql-alive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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
Expand Down
11 changes: 10 additions & 1 deletion bin/check-mysql-connections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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
Expand Down
11 changes: 10 additions & 1 deletion bin/check-mysql-replication-status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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'
Expand Down
11 changes: 10 additions & 1 deletion bin/check-mysql-select-count.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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]
Expand Down

0 comments on commit f788c83

Please sign in to comment.