Repository URL to install this package:
Version:
0.0.2 ▾
|
#!/usr/bin/env ruby
#
# TMS table age check
# ===
#
# This checks when the last timestamp was for a particular table, and alerts if they are
# older than set thresholds.
#
# Copyright 2017 Critical Mention <ce@criticalmedia.com>
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
require 'inifile'
require 'mysql'
require 'sensu-plugin/check/cli'
require 'time'
class CheckTmsTableAge < Sensu::Plugin::Check::CLI
option :user,
description: 'MySQL User',
short: '-u USER',
long: '--user USER',
default: 'root'
option :password,
description: 'MySQL Password',
short: '-p PASS',
long: '--password PASS'
option :ini,
description: 'My.cnf ini file',
short: '-i',
long: '--ini VALUE'
option :hostname,
description: 'Hostname to login to',
short: '-h HOST',
long: '--hostname HOST',
required: true
option :port,
description: 'Port to connect to',
short: '-P PORT',
long: '--port PORT',
default: '3306'
option :socket,
description: 'Socket to use',
short: '-s SOCKET',
long: '--socket SOCKET'
option :database,
description: 'Database to connect to',
short: '-d DATABASE',
long: '--database DATABASE',
default: 'tms'
option :table,
description: 'Table to check timestamp of',
short: '-t TABLE',
long: '--table TABLE',
required: true
option :column,
description: 'Timestamp column name',
short: '-C COLUMN',
long: '--column COLUMN',
required: true
option :warn_days,
description: 'Issue warning when cc_text is older than number of days',
short: '-w DAYS',
long: '--warn DAYS',
proc: proc(&:to_i),
default: 2
option :critical_days,
description: 'Issue alert when cc_text is older than number of days',
short: '-c DAYS',
long: '--critical DAYS',
proc: proc(&:to_i),
default: 3
def run
if config[:ini]
ini = IniFile.load(config[:ini])
section = ini['client']
db_user = section['user']
db_pass = section['password']
else
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])
max_time_string = db.query("SELECT MAX(#{config[:column]}) AS max FROM #{config[:table]};").
fetch_hash.
fetch('max')
max_time = Time.parse(max_time_string)
sec_diff = Time.now - max_time
if sec_diff >= (config[:critical_days] * 24 * 60 * 60)
critical "Last '#{config[:column]}' time from #{config[:table]} is more than #{config[:critical_days]} days old"
elsif sec_diff >= (config[:warn_days] * 24 * 60 * 60)
warning "Last '#{config[:column]}' time from #{config[:table]} is more than #{config[:warn_days]} days old"
else
ok
end
rescue Mysql::Error => e
critical "MySQL check failed: #{e.error}"
ensure
db.close if db
end
end