Sha256: 536effd3e28e797d219944adc43f50e19ad3176df1687df4964d18c7b651c523

Contents?: true

Size: 1.94 KB

Versions: 5

Compression:

Stored size: 1.94 KB

Contents

#!/usr/bin/env ruby
# frozen_string_literal: false

#
#   check-whois-domain-expiration
#
# DESCRIPTION:
#   This plugin checks domain expiration dates using the 'whois' gem.
#
# OUTPUT:
#   plain text
#
# PLATFORMS:
#   Tested on Mac OS X
#
# DEPENDENCIES:
#   gem: sensu-plugin
#   gem: whois
#
# USAGE:
#   $ ./check-whois-domain-expiration.rb -d mijit.com
#   WhoisDomainExpirationCheck OK: mijit.com expires on 02-07-2016 (325 days away)
#
# LICENSE:
#   Copyright 2015 michael j talarczyk <mjt@mijit.com> and contributors.
#   Released under the same terms as Sensu (the MIT license); see LICENSE
#   for details.

require 'sensu-plugin/check/cli'
require 'whois'
require 'whois-parser'

#
# Check Whois domain expiration
#
class WhoisDomainExpirationCheck < Sensu::Plugin::Check::CLI
  option :domain,
         short: '-d DOMAIN',
         long: '--domain DOMAIN',
         required: true,
         description: 'Domain to check'

  option :warning,
         short: '-w DAYS',
         long: '--warn DAYS',
         default: 30,
         description: 'Warn if fewer than DAYS away'

  option :critical,
         short: '-c DAYS',
         long: '--critical DAYS',
         default: 7,
         description: 'Critical if fewer than DAYS away'

  option :help,
         short: '-h',
         long: '--help',
         description: 'Show this message',
         on: :tail,
         boolean: true,
         show_options: true,
         exit: 0

  def run
    whois = Whois.whois(config[:domain])

    # TODO: figure out which to use `Date` or `Time`
    expires_on = DateTime.parse(whois.parser.expires_on.to_s)
    num_days = (expires_on - DateTime.now).to_i

    message "#{config[:domain]} expires on #{expires_on.strftime('%m-%d-%Y')} (#{num_days} days away)"

    if num_days <= config[:critical].to_i
      critical
    elsif num_days <= config[:warning].to_i
      warning
    else
      ok
    end
  rescue StandardError
    unknown "#{config[:domain]} can't be checked"
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sensu-plugins-network-checks-5.0.0 bin/check-whois-domain-expiration.rb
sensu-plugins-network-checks-4.1.1 bin/check-whois-domain-expiration.rb
sensu-plugins-network-checks-4.1.0 bin/check-whois-domain-expiration.rb
sensu-plugins-network-checks-4.0.0 bin/check-whois-domain-expiration.rb
sensu-plugins-network-checks-3.2.1 bin/check-whois-domain-expiration.rb