Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
sensu-plugins-ce-scripts / bin / check-cc-text-age.rb
Size: Mime:
#!/usr/bin/env ruby
#
# CC Text age check
# ===
#
# This checks the number of days old the bottom row of the cc_text table is, and alerts if it is
# 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 CheckCcTextAge < 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 :warn_days,
         description: 'Issue warning when timestamp is older than number of days',
         short: '-w DAYS',
         long: '--warn DAYS',
         proc: proc(&:to_i), 
         default: 65

  option :critical_days,
         description: 'Issue alert when timestamp is older than number of days',
         short: '-c DAYS',
         long: '--critical DAYS',
         proc: proc(&:to_i),
         default: 70

  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, 'critical', config[:port].to_i, config[:socket])
    query = <<-EOS
      SELECT DATEDIFF(NOW(), CAST(timestamp AS DATETIME)) AS days_old \
        FROM cc_text \
        WHERE id = (SELECT MIN(id) FROM cc_text);
    EOS
    days_old = db.query(query).
      fetch_hash.
      fetch('days_old').
      to_i
    if days_old >= config[:critical_days]
      critical "Last row from cc_text table is more than #{config[:critical_days]} days old"
    elsif days_old >= config[:warn_days]
      warning "Last row from cc_text 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