Last active
August 29, 2015 14:13
-
-
Save patmandenver/0e753184c789dc0f40eb to your computer and use it in GitHub Desktop.
Sensu code for checking an upcoming date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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