Skip to content

Commit

Permalink
much needed cleanup
Browse files Browse the repository at this point in the history
Breaking Changes:
- removed ruby `< 2.3` support as they are EOL
- appeased the cops

Signed-off-by: Ben Abrams <me@benabrams.it>
  • Loading branch information
majormoses committed Nov 22, 2018
1 parent a0170d3 commit 90b160a
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 101 deletions.
13 changes: 12 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,15 @@ Style/Documentation:
Enabled: false

Style/Next:
Enabled: false
Enabled: false

Style/NumericPredicate:
Enabled: false

# TODO: when we bump RC this should be:
# Style/TrailingCommaInHashLiteral:
# Enabled: true
# Style/TrailingCommaInArrayLiteral:
# Enabled: true
Style/TrailingCommaInLiteral:
Enabled: false
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ cache:
install:
- bundle install
rvm:
- 2.0
- 2.1
- 2.2
- 2.3.0
- 2.4.1
notifications:
Expand All @@ -27,9 +24,6 @@ deploy:
on:
tags: true
all_branches: true
rvm: 2.0
rvm: 2.1
rvm: 2.2
rvm: 2.3.0
rvm: 2.4.1
repo: sensu-plugins/sensu-plugins-mysql
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins

## [Unreleased]

### Security
- updated rubocop dependency to `~> 0.51.0` per: https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-8418. (@majormoses)

### Breaking Changes
- removed < ruby 2.3 support as they are EOL (@majormoses)

### Changed
- appeased the cops (@majormoses)

## [2.7.0] - 2018-11-25
### Added
- metrics-mysql-select-count.rb script (@nagyt234)
Expand Down
6 changes: 3 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ require 'yard'
require 'yard/rake/yardoc_task'

YARD::Rake::YardocTask.new do |t|
OTHER_PATHS = %w().freeze
OTHER_PATHS = %w[].freeze
t.files = ['lib/**/*.rb', 'bin/**/*.rb', OTHER_PATHS]
t.options = %w(--markup-provider=redcarpet --markup=markdown --main=README.md --files CHANGELOG.md)
t.options = %w[--markup-provider=redcarpet --markup=markdown --main=README.md --files CHANGELOG.md]
end

RuboCop::RakeTask.new
Expand All @@ -35,4 +35,4 @@ task :check_binstubs do
end
end

task default: [:spec, :make_bin_executable, :yard, :rubocop, :check_binstubs]
task default: %i[spec make_bin_executable yard rubocop check_binstubs]
9 changes: 3 additions & 6 deletions bin/check-mysql-disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def run
total_size = 0.0
db = Mysql.real_connect(config[:host], db_user, db_pass, nil, config[:port], config[:socket])

results = db.query <<-EOSQL
results = db.query <<-SQL
SELECT table_schema,
count(*) TABLES,
concat(round(sum(table_rows)/1000000,2),'M') rows,
Expand All @@ -106,7 +106,7 @@ def run
round(sum(data_length+index_length)/(1024*1024*1024),2) total_size,
round(sum(index_length)/sum(data_length),2) idxfrac
FROM information_schema.TABLES group by table_schema
EOSQL
SQL

unless results.nil?
results.each_hash do |row|
Expand All @@ -125,14 +125,11 @@ def run
else
ok diskstr
end

rescue Mysql::Error => e
errstr = "Error code: #{e.errno} Error message: #{e.error}"
critical "#{errstr} SQLSTATE: #{e.sqlstate}" if e.respond_to?('sqlstate')

rescue => e
rescue StandardError => e
critical e

ensure
db.close if db
end
Expand Down
1 change: 0 additions & 1 deletion bin/check-mysql-innodb-lock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ def run
else
critical "Detected Locks #{lock_info}"
end

rescue Mysql::Error => e
critical "MySQL check failed: #{e.error}"
ensure
Expand Down
20 changes: 12 additions & 8 deletions bin/check-mysql-msr-replication-status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ class CheckMysqlMSRReplicationStatus < Sensu::Plugin::Check::CLI
default: 1800,
proc: proc(&:to_i)

def set_status(io_thread_status, sql_thread_status, seconds_behind_master)
if io_thread_status == 'No' || sql_thread_status == 'No' || seconds_behind_master > config[:crit]
2
elsif seconds_behind_master > config[:warn] && seconds_behind_master <= config[:crit]
1
else
0
end
end

def run
if config[:ini]
ini = IniFile.load(config[:ini])
Expand Down Expand Up @@ -106,19 +116,13 @@ def run
io_thread_status = row['Slave_IO_Running']
sql_thread_status = row['Slave_SQL_Running']
seconds_behind_master = row['Seconds_Behind_Master'].to_i
status = 0
if io_thread_status == 'No' || sql_thread_status == 'No' || seconds_behind_master > config[:crit]
status = 2
end
if seconds_behind_master > config[:warn] && seconds_behind_master <= config[:crit]
status = 1
end
status = set_status
message = "#{channel['channel_name']} STATES:"
message += " Slave_IO_Running=#{io_thread_status}"
message += ", Slave_SQL_Running=#{sql_thread_status}"
message += ", Seconds_Behind_Master=#{seconds_behind_master}"

if status.zero?
if status == 0
ok_statuses << message
elsif status == 1
warn_statuses << message
Expand Down
5 changes: 1 addition & 4 deletions bin/check-mysql-query-result-count.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,11 @@ def run
else
ok 'Result count length is below thresholds'
end

rescue Mysql::Error => e
errstr = "Error code: #{e.errno} Error message: #{e.error}"
critical "#{errstr} SQLSTATE: #{e.sqlstate}" if e.respond_to?('sqlstate')

rescue => e
rescue StandardError => e
critical e

ensure
db.close if db
end
Expand Down
32 changes: 21 additions & 11 deletions bin/check-mysql-replication-status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,24 @@ class CheckMysqlReplicationStatus < Sensu::Plugin::Check::CLI
# #YELLOW
proc: lambda { |s| s.to_i } # rubocop:disable Lambda

def detect_replication_status?(row)
%w[
Slave_IO_State
Slave_IO_Running
Slave_SQL_Running
Last_IO_Error
Last_SQL_Error
Seconds_Behind_Master
].all? { |key| row.key? key }
end

def slave_running?(row)
%w[
Slave_IO_Running
Slave_SQL_Running
].all? { |key| row[key] =~ /Yes/ }
end

def run
if config[:ini]
ini = IniFile.load(config[:ini])
Expand Down Expand Up @@ -121,14 +139,9 @@ def run

unless results.nil?
results.each_hash do |row|
warn "couldn't detect replication status" unless
%w(Slave_IO_State Slave_IO_Running Slave_SQL_Running Last_IO_Error Last_SQL_Error Seconds_Behind_Master).all? do |key|
row.key? key
end
warn "couldn't detect replication status" unless detect_replication_status?(row)

slave_running = %w(Slave_IO_Running Slave_SQL_Running).all? do |key|
row[key] =~ /Yes/
end
slave_running = slave_running?(row)

output = if db_conn.nil?
'Slave not running!'
Expand Down Expand Up @@ -160,14 +173,11 @@ def run
end
ok 'show slave status was nil. This server is not a slave.'
end

rescue Mysql::Error => e
errstr = "Error code: #{e.errno} Error message: #{e.error}"
critical "#{errstr} SQLSTATE: #{e.sqlstate}" if e.respond_to?('sqlstate')

rescue => e
rescue StandardError => e
critical e

ensure
db.close if db
end
Expand Down
3 changes: 0 additions & 3 deletions bin/check-mysql-select-count.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,11 @@ def run
else
ok "Count is below thresholds : #{count} count"
end

rescue Mysql::Error => e
errstr = "Error code: #{e.errno} Error message: #{e.error}"
critical "#{errstr} SQLSTATE: #{e.sqlstate}" if e.respond_to?('sqlstate')

rescue StandardError => e
critical "unhandled exception: #{e}"

ensure
db.close if db
end
Expand Down
8 changes: 4 additions & 4 deletions bin/check-mysql-status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def status_check(db_user, db_pass, db_socket)
else
critical "Error message: status: #{status}"
end
rescue => e
rescue StandardError => e
critical "Error message: status: #{status} | Exception: #{e}"
ensure
puts ''
Expand All @@ -162,14 +162,14 @@ def replication_check(db_user, db_pass, db_socket)
end
dict = []
table.keys.to_a.each do |k|
%w(Slave_IO_State Slave_IO_Running Slave_SQL_Running Last_IO_Error Last_SQL_Error Seconds_Behind_Master).each do |key|
%w[Slave_IO_State Slave_IO_Running Slave_SQL_Running Last_IO_Error Last_SQL_Error Seconds_Behind_Master].each do |key|
dict.push(k.strip.to_s) if key.strip == k.strip
end
end
table.each do |attribute, value|
puts "#{attribute} : #{value}" if config[:debug]
warn "couldn't detect replication status :#{dict.size}" unless dict.size == 6
slave_running = %w(Slave_IO_Running Slave_SQL_Running).all? do |key|
slave_running = %w[Slave_IO_Running Slave_SQL_Running].all? do |key|
table[key].to_s =~ /Yes/
end
output = 'Slave not running!'
Expand All @@ -189,7 +189,7 @@ def replication_check(db_user, db_pass, db_socket)
end
end
ok 'show slave status was nil. This server is not a slave.'
rescue => e
rescue StandardError => e
critical "Error message: status: #{status} | Exception: #{e}"
end
end
Expand Down
37 changes: 21 additions & 16 deletions bin/metrics-mysql-graphite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,8 @@ class MysqlGraphite < Sensu::Plugin::Metric::CLI::Graphite
long: '--verbose',
boolean: true

def run
# props to https://github.com/coredump/hoardd/blob/master/scripts-available/mysql.coffee

metrics = {
def metrics_hash
{
'general' => {
'Bytes_received' => 'rxBytes',
'Bytes_sent' => 'txBytes',
Expand All @@ -117,14 +115,14 @@ def run
'Select_range' => 'selectRange',
'Select_range_check' => 'selectRange_check',
'Select_scan' => 'selectScan',
'Slow_queries' => 'slowQueries'
'Slow_queries' => 'slowQueries',
},
'querycache' => {
'Qcache_queries_in_cache' => 'queriesInCache',
'Qcache_hits' => 'cacheHits',
'Qcache_inserts' => 'inserts',
'Qcache_not_cached' => 'notCached',
'Qcache_lowmem_prunes' => 'lowMemPrunes'
'Qcache_lowmem_prunes' => 'lowMemPrunes',
},
'commands' => {
'Com_admin_commands' => 'admin_commands',
Expand Down Expand Up @@ -159,7 +157,7 @@ def run
'Com_lock_tables' => 'lock_tables',
'Com_show_create_table' => 'show_create_table',
'Com_unlock_tables' => 'unlock_tables',
'Com_alter_table' => 'alter_table'
'Com_alter_table' => 'alter_table',
},
'counters' => {
'Handler_write' => 'handlerWrite',
Expand All @@ -174,7 +172,7 @@ def run
'Handler_commit' => 'handlerCommit',
'Handler_rollback' => 'handlerRollback',
'Handler_savepoint' => 'handlerSavepoint',
'Handler_savepoint_rollback' => 'handlerSavepointRollback'
'Handler_savepoint_rollback' => 'handlerSavepointRollback',
},
'innodb' => {
'Innodb_buffer_pool_pages_total' => 'bufferTotal_pages',
Expand All @@ -195,15 +193,22 @@ def run
'Innodb_rows_updated' => 'rowsUpdated',
'Innodb_rows_read' => 'rowsRead',
'Innodb_rows_deleted' => 'rowsDeleted',
'Innodb_rows_inserted' => 'rowsInserted'
'Innodb_rows_inserted' => 'rowsInserted',
},
'configuration' => {
'max_connections' => 'MaxConnections',
'Max_prepared_stmt_count' => 'MaxPreparedStmtCount'
}
'Max_prepared_stmt_count' => 'MaxPreparedStmtCount',
},
}
end

def run
# props to https://github.com/coredump/hoardd/blob/master/scripts-available/mysql.coffee

metrics = metrics_hash

config[:host].split(' ').each do |mysql_host|
# FIXME: break this up
config[:host].split(' ').each do |mysql_host| # rubocop:disable Metrics/BlockLength
mysql_shorthostname = mysql_host.split('.')[0]
if config[:ini]
ini = IniFile.load(config[:ini])
Expand All @@ -218,7 +223,7 @@ def run
mysql = Mysql.new(mysql_host, db_user, db_pass, nil, config[:port], config[:socket])

results = mysql.query('SHOW GLOBAL STATUS')
rescue => e
rescue StandardError => e
puts e.message
end

Expand All @@ -241,7 +246,7 @@ def run
output "#{config[:scheme]}.#{mysql_shorthostname}.general.#{metrics['general'][key]}", value
end
end
rescue => e
rescue StandardError => e
puts "Error querying slave status: #{e}" if config[:verbose]
end

Expand All @@ -251,12 +256,12 @@ def run
category = 'configuration'
variables_results.each_hash do |row|
metrics[category].each do |metric, desc|
if metric.casecmp(row['Variable_name']) == 0
if metric.casecmp(row['Variable_name']).zero?
output "#{config[:scheme]}.#{mysql_shorthostname}.#{category}.#{desc}", row['Value']
end
end
end
rescue => e
rescue StandardError => e
puts e.message
end

Expand Down
Loading

0 comments on commit 90b160a

Please sign in to comment.