2013-04-08 19:46:42 +02:00
|
|
|
module NSCA
|
|
|
|
module PerformanceData
|
|
|
|
class Base
|
|
|
|
extend Timeout
|
|
|
|
extend Benchmark
|
|
|
|
|
|
|
|
class <<self
|
|
|
|
attr_reader :label, :unit, :warn, :crit, :min, :max
|
|
|
|
def init label, unit = nil, warn = nil, crit = nil, min = nil, max = nil
|
|
|
|
@label, @unit, @warn, @crit, @min, @max = label.to_s, unit, warn, crit, min, max
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def measure &block
|
|
|
|
timeout ||= 0
|
|
|
|
exception = Class.new Timeout::Error
|
2013-04-09 12:20:55 +02:00
|
|
|
pd = perfdatas[perfdata_label.to_sym]
|
2013-04-08 19:46:42 +02:00
|
|
|
timeout = pd.max
|
|
|
|
m = realtime do
|
|
|
|
begin
|
|
|
|
timeout timeout, exception, &block
|
|
|
|
rescue exception
|
|
|
|
end
|
|
|
|
end
|
|
|
|
new m
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :value
|
|
|
|
def initialize( value) @value = value end
|
|
|
|
def label() self.class.label end
|
|
|
|
def unit() self.class.unit end
|
|
|
|
def warn() self.class.warn end
|
|
|
|
def crit() self.class.crit end
|
|
|
|
def min() self.class.min end
|
|
|
|
def max() self.class.max end
|
|
|
|
def to_s() "#{label}=#{value}#{unit},#{warn},#{crit},#{min},#{max}" end
|
2013-04-09 12:20:55 +02:00
|
|
|
def to_sym() self.class.label.to_sym end
|
2013-04-08 19:46:42 +02:00
|
|
|
|
|
|
|
def return_code
|
|
|
|
if @value.nil? then 3
|
|
|
|
elsif crit <= @value then 2
|
|
|
|
elsif warn <= @value then 1
|
|
|
|
else 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class <<self
|
|
|
|
def new label, unit = nil, warn = nil, crit = nil, min = nil, max = nil
|
|
|
|
cl = Class.new Base
|
|
|
|
cl.init label, unit, warn, crit, min, max
|
|
|
|
end
|
|
|
|
|
|
|
|
def create label, unit = nil, warn = nil, crit = nil, min = nil, max = nil
|
|
|
|
cl = new label, unit, warn, crit, min, max
|
|
|
|
clname = NSCA::Helper.class_name_gen label
|
|
|
|
self.const_set clname, cl if clname
|
|
|
|
cl
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Check
|
|
|
|
class Base
|
|
|
|
attr_reader :perfdatas, :return_code, :status, :timestamp
|
|
|
|
def initialize return_code = nil, status = nil, perfdatas = nil
|
|
|
|
@perfdatas = {}
|
|
|
|
init return_code, status, perfdatas, timestamp || Time.now
|
|
|
|
end
|
|
|
|
|
|
|
|
def init return_code = nil, status = nil, perfdatas = nil, timestamp = nil
|
|
|
|
@return_code = return_code if return_code
|
|
|
|
@status = status if status
|
2013-04-09 12:20:55 +02:00
|
|
|
case perfdatas
|
|
|
|
when Hash
|
|
|
|
perfdatas.each &method( :[])
|
|
|
|
when Array
|
|
|
|
push *perfdatas
|
|
|
|
end
|
2013-04-08 19:46:42 +02:00
|
|
|
@timestamp = timestamp if timestamp
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def [] perfdata_label
|
2013-04-09 12:20:55 +02:00
|
|
|
pd = @perfdatas[perfdata_label.to_sym]
|
2013-04-08 19:46:42 +02:00
|
|
|
pd && pd.value
|
|
|
|
end
|
|
|
|
|
2013-04-09 12:20:55 +02:00
|
|
|
def push *perfdatas
|
|
|
|
perfdatas.each {|perfdata| @perfdatas[perfdata.label] = perfdata }
|
|
|
|
@perfdatas
|
|
|
|
end
|
|
|
|
|
2013-04-08 19:46:42 +02:00
|
|
|
def []= perfdata_label, value
|
2013-04-09 12:20:55 +02:00
|
|
|
return push value if value.is_a? PerformanceData::Base
|
|
|
|
perfdata_label = perfdata_label.to_sym
|
2013-04-08 19:46:42 +02:00
|
|
|
cl = self.class.perfdatas[perfdata_label]
|
|
|
|
cl ||= PerformanceData::Base.new perfdata_label
|
|
|
|
@perfdatas[perfdata_label] = cl.new value
|
|
|
|
end
|
|
|
|
|
|
|
|
def text
|
|
|
|
r = "#{status || ReturnCode.find(return_code)}"
|
2013-04-09 12:20:55 +02:00
|
|
|
r += " | #{perfdatas.each_value.map( &:to_s).join ' '}" unless perfdatas.empty?
|
2013-04-08 19:46:42 +02:00
|
|
|
r
|
|
|
|
end
|
|
|
|
|
2013-04-09 12:20:55 +02:00
|
|
|
def measure perfdata_label, &block
|
|
|
|
@perfdatas[perfdata_label.to_sym].measure &block
|
|
|
|
end
|
2013-04-08 19:46:42 +02:00
|
|
|
def send() NSCA::send self end
|
|
|
|
|
|
|
|
def ok status = nil, perfdatas = nil
|
|
|
|
init ReturnCode::OK, status, perfdatas
|
|
|
|
send
|
|
|
|
end
|
|
|
|
|
|
|
|
def warning status = nil, perfdatas = nil
|
|
|
|
init ReturnCode::WARNING, status, perfdatas
|
|
|
|
send
|
|
|
|
end
|
|
|
|
alias warn warning
|
|
|
|
|
|
|
|
def critical status = nil, perfdatas = nil
|
|
|
|
init ReturnCode::CRITICAL, status, perfdatas
|
|
|
|
send
|
|
|
|
end
|
|
|
|
alias crit critical
|
|
|
|
|
|
|
|
def unknown status = nil, perfdatas = nil
|
|
|
|
init ReturnCode::UNKNOWN, status, perfdatas
|
|
|
|
send
|
|
|
|
end
|
|
|
|
|
|
|
|
def determine_return_code
|
|
|
|
self.class.perfdatas.map do |label, pdc|
|
|
|
|
pd = @perfdatas[label]
|
|
|
|
if pd
|
|
|
|
pd.return_code
|
|
|
|
else
|
|
|
|
-1
|
|
|
|
end
|
|
|
|
end.max
|
|
|
|
end
|
|
|
|
|
|
|
|
def retcode
|
|
|
|
rc = return_code || determine_return_code
|
|
|
|
(0..3).include?(rc) ? rc : 3
|
|
|
|
end
|
|
|
|
|
|
|
|
def service() self.class.service end
|
|
|
|
def hostname() self.class.hostname end
|
|
|
|
|
|
|
|
class <<self
|
|
|
|
attr_reader :service, :hostname, :perfdatas
|
|
|
|
def init service, hostname = nil, perfdatas = nil
|
|
|
|
@service, @hostname, @perfdatas = service, hostname || `hostname -f`, {}
|
2013-04-09 12:20:55 +02:00
|
|
|
perfdatas.each {|pd| @perfdatas[pd.label.to_sym] = pd }
|
2013-04-08 19:46:42 +02:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def ok( status = nil, perfdatas = nil) new.ok status, perfdatas end
|
|
|
|
def warning( status = nil, perfdatas = nil) new.warning status, perfdatas end
|
|
|
|
alias warn warning
|
|
|
|
def critical( status = nil, perfdatas = nil) new.warning status, perfdatas end
|
|
|
|
alias crit critical
|
|
|
|
def unknown( status = nil, perfdatas = nil) new.unknown status, perfdatas end
|
|
|
|
end
|
|
|
|
end
|
2013-04-08 20:18:21 +02:00
|
|
|
|
|
|
|
class <<self
|
|
|
|
def create service, hostname = nil, perfdatas = nil
|
|
|
|
cl = Class.new Base
|
|
|
|
cl.init service, hostname, perfdatas
|
|
|
|
cl
|
|
|
|
end
|
|
|
|
|
|
|
|
def new service, hostname = nil, perfdatas = nil
|
|
|
|
cl = create service, hostname, perfdatas
|
|
|
|
clname = NSCA::Helper.class_name_gen service.to_s
|
|
|
|
self.const_set clname, cl if clname
|
|
|
|
cl
|
|
|
|
end
|
|
|
|
end
|
2013-04-08 19:46:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
module Checks
|
2013-04-09 15:33:22 +02:00
|
|
|
def perfdata( *params) NSCA::PerformanceData.new( *params) end
|
2013-04-08 19:46:42 +02:00
|
|
|
|
2013-04-09 15:33:22 +02:00
|
|
|
def check service, hostname, perfdatas = nil
|
2013-04-08 19:46:42 +02:00
|
|
|
perfdatas ||= []
|
|
|
|
perfdatas.map! {|cl| cl.is_a?( Symbol) ? const_get( cl) : cl }
|
2013-04-09 15:33:22 +02:00
|
|
|
NSCA::Check.new service, hostname, perfdatas
|
2013-04-08 19:46:42 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|