Skip to content

Instantly share code, notes, and snippets.

@rottenbytes
Forked from tfheen/gist:780086
Created July 26, 2011 12:05
Show Gist options
  • Save rottenbytes/1106603 to your computer and use it in GitHub Desktop.
Save rottenbytes/1106603 to your computer and use it in GitHub Desktop.
fixed the case where node has checked in less than 60 seconds ago
#! /usr/bin/env ruby
# Original by Tollef Fog Heen <tfheen _at_ err.no>
require 'chef/config'
require 'chef/search/query'
require 'chef/log'
require 'time'
require 'getoptlong'
require "ohai"
RET = {
:ok => 0,
:warning => 1,
:critical => 2,
:unknown => 3,
:dependent => 4
}
def time_period_to_s(time_period)
out = ""
interval_array = [ [:weeks, 604800], [:days, 86400], [:hours, 3600], [:mins, 60], [:seconds, 1] ]
interval_array.each do |sub|
if time_period >= sub[1] then
time_val, time_period = time_period.divmod( sub[1] )
time_val == 1 ? name = sub[0].to_s.singularize : name = sub[0].to_s
( sub[0] != :mins ? out += ", " : out += " and " ) if out != ''
out += time_val.to_s + " #{name}"
end
end
out
end
opts = GetoptLong.new(
[ '--critical', '-c', GetoptLong::REQUIRED_ARGUMENT],
[ '--warning', '-w', GetoptLong::REQUIRED_ARGUMENT],
[ '--timeout', '-t', GetoptLong::REQUIRED_ARGUMENT],
[ '--help', '-h', GetoptLong::NO_ARGUMENT],
[ '--hostname', '-H', GetoptLong::REQUIRED_ARGUMENT],
[ '--verbose', '-v', GetoptLong::NO_ARGUMENT],
[ '--nodename', '-n', GetoptLong::REQUIRED_ARGUMENT],
[ '--nodekey', '-k', GetoptLong::REQUIRED_ARGUMENT]
)
critical = 14400
warning = 3600
timeout = 10
showhelp = false
hostname = "localhost"
verbose = false
nodename = "nagios"
nodekey = "/etc/chef/nagios.pem"
begin
opts.each do |opt, arg|
case opt
when "--critical"
critical = arg
when "--warning"
warning = arg
when "--timeout"
timeout = arg
when "--help"
showhelp = true
when "--hostname"
hostname = arg
when "--verbose"
verbose = true
when "--nodename"
nodename = arg
when "--nodekey"
nodekey = arg
end
end
rescue
showhelp = true
end
if showhelp
puts "#{$0} [-w warning] [-c critical] [-t timeout] [-n nodename] [-k nodekey] -H host [--verbose]"
exit 1
end
Chef::Config.from_file("/etc/chef/client.rb")
Chef::Log.level :info
if verbose
Chef::Log.level :debug
end
Chef::Config[:node_name] = nodename
Chef::Config[:client_key] = nodekey
node = Chef::Node.load(hostname)
delta = Time.now.to_i - node["ohai_time"].to_i
ret = :ok
if delta > critical
ret = :critical
elsif delta > warning
ret = :warning
end
puts "Chef run freshness : #{ret.to_s.upcase}: Last check #{time_period_to_s delta} ago"
exit RET[ret]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment