Sensu Check when an upcoming date is approaching

Posted on Tuesday, January 13, 2015






I have a need for a Sensu check that will a warn X number of days before a specific date and/or issue a critical alert X numbers of days before a date.




Writing the check ruby code


Install the following gems (if you don't already have them)


    > sudo gem install chronic
    > sudo gem install sensu-plugin




Create the plugin


    > sudo vi /etc/sensu/plugins/check_date.rb





Code


#!/usr/bin/ruby
#
#
#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



Create a check (to test it out)


    > sudo vi /etc/sensu/conf.d/check_license_ends.json


Here is my simple check (adjust it to your system)


{
    "checks": {
        "check_license_ends": {
            "handlers": [
                "default"
           
],
           
"command": "/etc/sensu/plugins/check_date.rb -d 1/30/15 -m 'License runs out on 1/30/2015'",
           
"interval": 60,
           
"occurrences": 1,
           
"refresh": 3600,
           
"subscribers": [
               
"check-from-sensu-master"
            ]
        }
    }
}


Save it and restart the Sensu Master with the following command, and its client


    > sudo service sensu-server restart && sudo service sensu-api restart && sudo service sensu-client restart




And there is my warning.



If I update the check to the following (To trigger an alert today… today is 1/13/2015)


{
    "checks": {
        "check_license_ends": {
            "handlers": [
                "default"
           
],
           
"command": "/etc/sensu/plugins/check_date.rb -d 1/30/15 -a 20 -w 7 -m 'License runs out on 1/30/2015'",
           
"interval": 60,
           
"occurrences": 1,
           
"refresh": 3600,
           
"subscribers": [
               
"check-from-sensu-master"
            ]
        }
    }
}






And there is my critical
In a real system I would probably update the interval to 21600 (every 6 hours) or 86400 (every 24 hours)




References







This post is a part of and epic, the Sensu Epic.

Epic Goal:    My goal is to just really figure out how to use Sensu. 




No comments:

Post a Comment