ruby - CheckMysqlReplicationStatus CRITICAL:nil:NilClass 的未定义方法“编码”
问题描述
数据库服务器上的 sensu CheckMysqlReplication.rb 在 mysql 8.0.13-4 中以 latin1 作为默认字符集名称执行时为 nil:NilClass 返回未定义的方法“编码”。在此错误之前,CheckMysqlReplication.rb 在 mysql 5.0 上运行良好,UTF-8 作为默认字符集。
我已经收集了代码,但我无法确定问题可能出在哪里。这是下面的检查:
require 'sensu-plugin/check/cli'
require 'mysql'
require 'inifile'
class CheckMysqlReplicationStatus < Sensu::Plugin::Check::CLI
option :host,
short: '-h',
long: '--host=VALUE',
description: 'Database host'
option :port,
short: '-P',
long: '--port=VALUE',
description: 'Database port',
default: 3306,
# #YELLOW
proc: lambda { |s| s.to_i } # rubocop:disable Lambda
option :socket,
short: '-s SOCKET',
long: '--socket SOCKET',
description: 'Socket to use'
option :user,
short: '-u',
long: '--username=VALUE',
description: 'Database username'
option :pass,
short: '-p',
long: '--password=VALUE',
description: 'Database password'
option :master_connection,
short: '-m',
long: '--master-connection=VALUE',
description: 'Replication master connection name'
option :ini,
short: '-i',
long: '--ini VALUE',
description: 'My.cnf ini file'
option :ini_section,
description: 'Section in my.cnf ini file',
long: '--ini-section VALUE',
default: 'client'
option :warn,
short: '-w',
long: '--warning=VALUE',
description: 'Warning threshold for replication lag',
default: 900,
# #YELLOW
proc: lambda { |s| s.to_i } # rubocop:disable Lambda
option :crit,
short: '-c',
long: '--critical=VALUE',
description: 'Critical threshold for replication lag',
default: 1800,
# #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])
section = ini[config[:ini_section]]
db_user = section['user']
db_pass = section['password']
else
db_user = config[:user]
db_pass = config[:pass]
end
db_host = config[:host]
db_conn = config[:master_connection]
if [db_host, db_user, db_pass].any?(&:nil?)
unknown 'Must specify host, user, password'
end
begin
db = Mysql.new(db_host, db_user, db_pass, nil, config[:port], config[:socket])
results = if db_conn.nil?
db.query 'SHOW SLAVE STATUS'
else
db.query "SHOW SLAVE '#{db_conn}' STATUS"
end
unless results.nil?
results.each_hash do |row|
warn "couldn't detect replication status" unless detect_replication_status?(row)
slave_running = slave_running?(row)
output = if db_conn.nil?
'Slave not running!'
else
"Slave on master connection #{db_conn} not running!"
end
output += ' STATES:'
output += " Slave_IO_Running=#{row['Slave_IO_Running']}"
output += ", Slave_SQL_Running=#{row['Slave_SQL_Running']}"
output += ", LAST ERROR: #{row['Last_SQL_Error']}"
critical output unless slave_running
replication_delay = row['Seconds_Behind_Master'].to_i
message = "replication delayed by #{replication_delay}"
if replication_delay > config[:warn] &&
replication_delay <= config[:crit]
warning message
elsif replication_delay >= config[:crit]
critical message
elsif db_conn.nil?
ok "slave running: #{slave_running}, #{message}"
else
ok "master connection: #{db_conn}, slave running: #{slave_running}, #{message}"
end
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 StandardError => e
critical e
ensure
db.close if db
end
end
end
解决方案
推荐阅读
- c++ - 使用 QAxObject 计算合并单元格的行数
- graylog - 无法使用 Linux Azure VM 在公共 IP 地址上公开 Graylog Web 界面
- node.js - AWS S3 同步上传文件
- excel - 帮我找出 Excel 的公式
- django - nginx + uwsgi + Django 不使用 venv 激活脚本中设置的环境变量
- r - 获取数据框中不同时间窗口的最后一个值
- c# - 使用带有 C#.net core-3.1 的 Powershell-Core 的用户名/密码登录相关错误
- python - 如何在 Python 中写入 CSV,并将新数据放在最后一行下方?
- r - 有人可以解释端点功能是如何工作的吗?
- javascript - javascript之前和之后需要响应