class Nagi::DSL

Attributes

plugin[R]

Public Class Methods

new(&block) click to toggle source
# File lib/nagi/dsl.rb, line 5
def initialize(&block)
  @plugin = Nagi::Plugin.new
  @collect = nil
  @collected = []
  instance_eval &block
end

Public Instance Methods

argument(name) click to toggle source
# File lib/nagi/dsl.rb, line 12
def argument(name)
  @plugin.optionparser.argument(name)
end
check(&block) click to toggle source
# File lib/nagi/dsl.rb, line 16
def check(&block)
  # make data available to block
  collect = @collect
  collected = @collected

  p = class << @plugin; self; end
  p.send(:define_method, :check) do |options|
    status = catch(:status) {
      block.call(options)
      nil # to avoid returning status if not thrown
    }
    return status if status or not collect
    return nil if collected.empty?
    return collected.reverse.max if collect == :severe
    return collected.reverse.max.class.new(
      collected.map { |s| s.message }.join(', ')
    ) if collect == :all
    return nil
  end
end
collect(type) click to toggle source
# File lib/nagi/dsl.rb, line 37
def collect(type)
  raise "Invalid collect type #{type.to_s}" unless [:all, :severe].include?(type)
  @collect = type
end
critical(message, force=false) click to toggle source
# File lib/nagi/dsl.rb, line 42
def critical(message, force=false)
  status = Nagi::Status::Critical.new(message)
  throw :status, status if force or not @collect
  @collected.push(status)
  return status
end
execute(command) click to toggle source
# File lib/nagi/dsl.rb, line 49
def execute(command)
  return Nagi::Utility.execute(command)
end
fallback(status, message) click to toggle source
# File lib/nagi/dsl.rb, line 53
def fallback(status, message)
  @plugin.fallback = case status
    when :ok then Nagi::Status::OK.new(message)
    when :warning then Nagi::Status::Warning.new(message)
    when :critical then Nagi::Status::Critical.new(message)
    when :unknown then Nagi::Status::Unknown.new(message)
    else raise "Unknown fallback status #{status}"
  end
end
name(name) click to toggle source
# File lib/nagi/dsl.rb, line 63
def name(name)
  @plugin.name = name
end
ok(message, force=false) click to toggle source
# File lib/nagi/dsl.rb, line 67
def ok(message, force=false)
  status = Nagi::Status::OK.new(message)
  throw :status, status if force or not @collect
  @collected.push(status)
  return status
end
prefix(prefix) click to toggle source
# File lib/nagi/dsl.rb, line 74
def prefix(prefix)
  @plugin.prefix = prefix
end
switch(name, *args) click to toggle source
# File lib/nagi/dsl.rb, line 78
def switch(name, *args)
  @plugin.optionparser.switch(name, *args)
end
unknown(message, force=false) click to toggle source
# File lib/nagi/dsl.rb, line 82
def unknown(message, force=false)
  status = Nagi::Status::Unknown.new(message)
  throw :status, status if force or not @collect
  @collected.push(status)
  return status
end
version(version) click to toggle source
# File lib/nagi/dsl.rb, line 89
def version(version)
  @plugin.version = version
end
warning(message, force=false) click to toggle source
# File lib/nagi/dsl.rb, line 93
def warning(message, force=false)
  status = Nagi::Status::Warning.new(message)
  throw :status, status if force or not @collect
  @collected.push(status)
  return status
end