2013-04-08 19:46:42 +02:00
|
|
|
module NSCA
|
|
|
|
module PerformanceData
|
2013-04-10 18:21:01 +02:00
|
|
|
class TimeUnitExpected < Exception
|
|
|
|
end
|
|
|
|
|
2013-04-08 19:46:42 +02:00
|
|
|
class Base
|
|
|
|
extend Timeout
|
|
|
|
extend Benchmark
|
|
|
|
|
|
|
|
class <<self
|
|
|
|
attr_reader :label, :unit, :warn, :crit, :min, :max
|
2013-04-10 17:32:19 +02:00
|
|
|
def init *args
|
|
|
|
a, o = args, args.last.is_a?( Hash) ? args.pop : {}
|
|
|
|
@label, @unit = a[0]||o[:label], a[1]||o[:unit]
|
|
|
|
@warn, @crit = a[2]||o[:warn], a[3]||o[:crit]
|
|
|
|
@min, @max = a[4]||o[:min], a[5]||o[:max]
|
|
|
|
raise ArgumentError, "Label expected" unless @label
|
|
|
|
@label = @label.to_s
|
2013-04-08 19:46:42 +02:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def measure &block
|
2013-04-10 18:21:01 +02:00
|
|
|
f = case unit.to_s.to_sym
|
|
|
|
when :s then 1
|
|
|
|
when :ms then 1000
|
|
|
|
else raise TimeUnitExpected, "Unit must be seconds (s) or miliseconds (ms) not (#{unit})"
|
|
|
|
end
|
2013-04-10 17:32:19 +02:00
|
|
|
exception = ::Class.new Timeout::Error
|
2013-04-10 15:29:42 +02:00
|
|
|
timeout = max
|
2013-04-08 19:46:42 +02:00
|
|
|
m = realtime do
|
|
|
|
begin
|
|
|
|
timeout timeout, exception, &block
|
|
|
|
rescue exception
|
|
|
|
end
|
|
|
|
end
|
2013-04-10 18:21:01 +02:00
|
|
|
new f * m
|
2013-04-08 19:46:42 +02:00
|
|
|
end
|
2013-04-09 21:02:17 +02:00
|
|
|
|
|
|
|
def to_sym() label.to_sym end
|
2013-04-10 17:32:19 +02:00
|
|
|
def to_h() {label: @label, unit: @unit, warn: @warn, crit: @crit, min: @min, max: @max } end
|
|
|
|
def to_a() [label, unit, warn, crit, min, max] end
|
|
|
|
def clone( opts = nil) ::Class.new( self).init opts ? to_h.merge( opts) : to_h end
|
2013-04-08 19:46:42 +02:00
|
|
|
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
|
2013-04-10 17:32:19 +02:00
|
|
|
def to_a() [label, value, unit, warn, crit, min, max] end
|
2013-04-08 19:46:42 +02:00
|
|
|
def to_s() "#{label}=#{value}#{unit},#{warn},#{crit},#{min},#{max}" end
|
2013-04-10 17:48:46 +02:00
|
|
|
def to_sym() self.class.to_sym end
|
2013-04-08 19:46:42 +02:00
|
|
|
|
2013-04-10 17:32:19 +02:00
|
|
|
def to_h
|
|
|
|
{label: @label, value: @value, unit: @unit, warn: @warn, crit: @crit, min: @min, max: @max}
|
|
|
|
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
|
2013-04-10 17:32:19 +02:00
|
|
|
def new( *args) ::Class.new( Base).init *args end
|
|
|
|
def create label, *args
|
|
|
|
cl = new label, *args
|
2013-04-08 19:46:42 +02:00
|
|
|
clname = NSCA::Helper.class_name_gen label
|
|
|
|
self.const_set clname, cl if clname
|
|
|
|
cl
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Check
|
|
|
|
class Base
|
2013-04-09 16:41:56 +02:00
|
|
|
attr_accessor :return_code, :status, :timestamp
|
|
|
|
attr_reader :perfdatas
|
|
|
|
|
2013-04-10 17:32:19 +02:00
|
|
|
def initialize return_code = nil, status = nil, perfdatas = nil, timestamp = nil
|
2013-04-08 19:46:42 +02:00
|
|
|
@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-09 21:02:17 +02:00
|
|
|
def perfdata_for label
|
2013-04-09 21:05:37 +02:00
|
|
|
if label.is_a? PerformanceData::Base
|
2013-04-09 21:02:17 +02:00
|
|
|
label
|
|
|
|
else
|
2013-04-09 21:13:05 +02:00
|
|
|
label = label.to_sym
|
|
|
|
self.class.perfdatas[label] || PerformanceData::Base.new( label)
|
2013-04-09 21:02:17 +02:00
|
|
|
end
|
|
|
|
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
|
2013-04-09 21:02:17 +02:00
|
|
|
@perfdatas[perfdata_label] = perfdata_for( perfdata_label).new value
|
2013-04-08 19:46:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def text
|
2013-04-11 14:23:32 +02:00
|
|
|
r = "#{status || ReturnCode.find(retcode)}"
|
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
|
2013-04-09 21:02:17 +02:00
|
|
|
push perfdata_for( perfdata_label).measure( &block)
|
2013-04-09 12:20:55 +02:00
|
|
|
end
|
2013-04-08 19:46:42 +02:00
|
|
|
def send() NSCA::send self end
|
|
|
|
|
2013-04-10 17:32:19 +02:00
|
|
|
def ok( *args) init ReturnCode::OK, *args end
|
|
|
|
def warning( *args) init ReturnCode::WARNING, *args end
|
2013-04-08 19:46:42 +02:00
|
|
|
alias warn warning
|
2013-04-10 17:32:19 +02:00
|
|
|
def critical( *args) init ReturnCode::CRITICAL, *args end
|
2013-04-08 19:46:42 +02:00
|
|
|
alias crit critical
|
2013-04-10 17:32:19 +02:00
|
|
|
def unknown( *args) init ReturnCode::UNKNOWN, *args end
|
2013-04-08 19:46:42 +02:00
|
|
|
|
|
|
|
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
|
2013-04-10 17:32:19 +02:00
|
|
|
def to_a() [timestamp, retcode, hostname, service, text] end
|
|
|
|
def to_h
|
|
|
|
{timestamp: timestamp, return_code: retcode, hostname: hostname, server: service, status: text}
|
|
|
|
end
|
2013-04-08 19:46:42 +02:00
|
|
|
|
|
|
|
class <<self
|
|
|
|
attr_reader :service, :hostname, :perfdatas
|
2013-04-10 17:32:19 +02:00
|
|
|
def init *args
|
|
|
|
a, o = args, args.last.is_a?( Hash) ? args.pop : {}
|
|
|
|
service, hostname = nil, perfdatas = nil
|
2013-04-11 14:23:32 +02:00
|
|
|
@service, @hostname, @perfdatas = a[0]||o[:service], a[1]||o[:hostname]||`hostname`.chomp, {}
|
2013-04-10 17:32:19 +02:00
|
|
|
perfdatas = a[2]||o[:perfdatas]
|
|
|
|
perfdatas.each {|pd| @perfdatas[pd.to_sym] = pd } if perfdatas
|
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
|
2013-04-10 17:32:19 +02:00
|
|
|
|
|
|
|
def to_a() [service, hostname, perfdatas.dup] end
|
|
|
|
def to_h() {service: service, hostname: hostname, perfdatas: perfdatas.values} end
|
|
|
|
def to_sym() "#{hostname}|#{service}".to_sym end
|
|
|
|
def clone( opts = nil) ::Class.new( self).init opts ? to_h.merge( opts) : to_h end
|
2013-04-08 19:46:42 +02:00
|
|
|
end
|
|
|
|
end
|
2013-04-08 20:18:21 +02:00
|
|
|
|
|
|
|
class <<self
|
2013-04-09 16:15:47 +02:00
|
|
|
def new service, hostname = nil, perfdatas = nil
|
2013-04-08 20:18:21 +02:00
|
|
|
cl = Class.new Base
|
|
|
|
cl.init service, hostname, perfdatas
|
|
|
|
cl
|
|
|
|
end
|
|
|
|
|
2013-04-09 16:15:47 +02:00
|
|
|
def create service, hostname = nil, perfdatas = nil
|
|
|
|
cl = new service, hostname, perfdatas
|
2013-04-08 20:18:21 +02:00
|
|
|
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
|