Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sensu code for checking an upcoming date
#!/usr/bin/ruby
#
# DESCRIPTION:
# Given a date will notify if within X days of the date.
# Defaults
# Warning: 30 days before date
# Critical: 15 days before date
#
# DEPENDENCIES:
# sensu-plugin
# chronic
#
#The MIT License (MIT)
#
#Copyright (c) 2015 Patrick Bailey
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
#
#
require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'sensu-plugin/check/cli'
require 'chronic'
class CheckDate < Sensu::Plugin::Check::CLI
option :date,
:description => "date to check agains",
:short => '-d DATE',
:long => '--date DATE',
:required => true
option :days_warn_before,
:description => "If set will issue a warning if within X number of days of given date",
:short => '-w DAYS_WARN_BEFORE',
:long => '--warn DAYS_WARN_BEFORE',
:default => 30,
:required => true
option :days_alarm_before,
:description => "If set will issue an alarm if within X number of days of given date",
:short => '-a DAYS_ALARM_BEFORE',
:long => '--alarm DAYS_ALARM_BEFORE',
:default => 15,
:required => true
option :message,
:description => "Message to send",
:short => '-m MESSAGE',
:long => '--message MESSAGE',
:required => true
def initilize
super
end
def run
date = Chronic.parse(config[:date])
#Confirm the date given is valid see https://github.com/mojombo/chronic#usage
if date.nil?
critical("Invalid date format '" + config[:date] + "', An example of a valid forma is '12/3/2014'")
end
if (date - config[:days_alarm_before].to_i*24*3600) < Time.now
critical(config[:message])
elsif (date - config[:days_warn_before].to_i*24*3600) < Time.now
warning(config[:message])
else
ok("No need to issue alarm or warning for date '" + date.to_s)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment