Skip to content

Instantly share code, notes, and snippets.

@Farzy
Forked from anonymous/gist:379743
Created May 12, 2010 09:27
Show Gist options
  • Save Farzy/398376 to your computer and use it in GitHub Desktop.
Save Farzy/398376 to your computer and use it in GitHub Desktop.
#! /usr/bin/ruby
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] ]
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]
)
critical = 14400
warning = 3600
timeout = 10
showhelp = false
hostname = "localhost"
verbose = false
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
end
end
rescue
showhelp = true
end
if showhelp
print "#{ARGV[0]} [-w warning] [-c critical] [-t timeout] -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] = "nagios"
Chef::Config[:client_key] = "/usr/local/lib/nagios/plugins/nagios_chef.pem"
node = Chef::Node.load(hostname)
delta = Time.now.to_f - node["ohai_time"].to_f
ret = :ok
if delta > critical
ret = :critical
elsif delta > warning
ret = :warning
end
puts "chef_lastrun: #{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