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-tms-schedule-data-age.rb
Size: Mime:
#!/usr/bin/env ruby
#
# TMS schedule data age check
# ===
#
# This checks when the latest available TMS schedule data is and alerts if they are below
# 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 CheckTmsScheduleDataAge < 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 :warn_days,
         description: 'Issue warning when TMS schedule data is available for less than number of days in the future',
         short: '-w DAYS',
         long: '--warn DAYS',
         proc: proc(&:to_i),
         default: 5

  option :critical_days,
         description: 'Issue alert when TMS schedule data is available for less than number of days in the future',
         short: '-c DAYS',
         long: '--critical DAYS',
         proc: proc(&:to_i),
         default: 2

  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])
    query = <<-EOS
      SELECT MAX(schedule_sub.tf_start_time) AS max_start_time FROM \
        (SELECT tf_database_key, \
          tf_start_time \
          FROM schedule_record \
          ORDER BY tf_start_time DESC \
          LIMIT 10)
        AS schedule_sub \
        JOIN program_record ON schedule_sub.tf_database_key = program_record.tf_database_key;
    EOS
    max_time_string = db.query(query).
      fetch_hash.
      fetch('max_start_time')
    max_time = Time.parse(max_time_string)
    sec_diff = max_time - Time.now
    if sec_diff < (config[:critical_days] * 24 * 60 * 60)
      critical "Future TMS schedule data is less than #{config[:critical_days]} days"
    elsif sec_diff < (config[:warn_days] * 24 * 60 * 60)
      warning "Future TMS schedule data is less than #{config[:warn_days]} days"
    else
      ok
    end
  rescue Mysql::Error => e
    critical "MySQL check failed: #{e.error}"
  ensure
    db.close if db
  end
end