Class: Nagi::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/nagi/dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ DSL

Returns a new instance of DSL.



5
6
7
8
9
10
# File 'lib/nagi/dsl.rb', line 5

def initialize(&block)
  @plugin = Nagi::Plugin.new
  @collect = nil
  @collected = []
  instance_eval &block
end

Instance Attribute Details

#pluginObject (readonly)

Returns the value of attribute plugin.



3
4
5
# File 'lib/nagi/dsl.rb', line 3

def plugin
  @plugin
end

Instance Method Details

#argument(name) ⇒ Object



12
13
14
# File 'lib/nagi/dsl.rb', line 12

def argument(name)
  @plugin.optionparser.argument(name)
end

#check(&block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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) ⇒ Object



37
38
39
40
# 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) ⇒ Object



42
43
44
45
46
47
# 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) ⇒ Object



49
50
51
# File 'lib/nagi/dsl.rb', line 49

def execute(command)
  return Nagi::Utility.execute(command)
end

#fallback(status, message) ⇒ Object



53
54
55
56
57
58
59
60
61
# 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) ⇒ Object



63
64
65
# File 'lib/nagi/dsl.rb', line 63

def name(name)
  @plugin.name = name
end

#ok(message, force = false) ⇒ Object



67
68
69
70
71
72
# 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) ⇒ Object



74
75
76
# File 'lib/nagi/dsl.rb', line 74

def prefix(prefix)
  @plugin.prefix = prefix
end

#switch(name, *args) ⇒ Object



78
79
80
# File 'lib/nagi/dsl.rb', line 78

def switch(name, *args)
  @plugin.optionparser.switch(name, *args)
end

#unknown(message, force = false) ⇒ Object



82
83
84
85
86
87
# 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) ⇒ Object



89
90
91
# File 'lib/nagi/dsl.rb', line 89

def version(version)
  @plugin.version = version
end

#warning(message, force = false) ⇒ Object



93
94
95
96
97
98
# 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