Class: Naplug::Plugin

Inherits:
Object
  • Object
show all
Includes:
Helpers::Grokkers
Defined in:
lib/naplug/plugin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Grokkers

#tagmeta_grok

Constructor Details

#initialize(tag, block, meta) ⇒ Plugin

Returns a new instance of Plugin.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/naplug/plugin.rb', line 17

def initialize(tag, block, meta)
  @tag = tag
  @block = block
  @plugins = Hash.new

  @_args = Hash.new
  @_data = OpenStruct.new :status => Status.new, :output => Output.new, :payload => nil, :perfdata => nil
  @_meta = Meta.new meta

  begin
    instance_eval &block
  rescue ArgumentError => e
    raise
  rescue
    nil
  end

end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



15
16
17
# File 'lib/naplug/plugin.rb', line 15

def block
  @block
end

#pluginsObject (readonly)

Returns the value of attribute plugins.



15
16
17
# File 'lib/naplug/plugin.rb', line 15

def plugins
  @plugins
end

#tagObject (readonly)

Returns the value of attribute tag.



15
16
17
# File 'lib/naplug/plugin.rb', line 15

def tag
  @tag
end

Instance Method Details

#[](k) ⇒ Object



116
117
118
# File 'lib/naplug/plugin.rb', line 116

def [](k)
  @_args[k]
end

#[]=(k, v) ⇒ Object



120
121
122
# File 'lib/naplug/plugin.rb', line 120

def []=(k,v)
  @_args[k] = v
end

#argsObject



103
104
105
# File 'lib/naplug/plugin.rb', line 103

def args
  @_args
end

#args!(args) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/naplug/plugin.rb', line 107

def args!(args)
  @_args.merge! args
  @plugins.each do |tag,plug|
    plug_args = args.key?(tag) ? args[tag] : {}
    shared_args = args.select { |t,a| not @plugins.keys.include? t }
    plug.args! shared_args.merge! plug_args
  end
end

#descriptionObject



44
45
46
# File 'lib/naplug/plugin.rb', line 44

def description
  @_meta.description
end

#evalObject



128
129
130
131
132
133
134
135
# File 'lib/naplug/plugin.rb', line 128

def eval
  unless @plugins.empty?
    wcu_plugins = @plugins.values.select { |plug| plug.status.not_ok? }
    plugins = wcu_plugins.empty? ? @plugins.values : wcu_plugins
    output! plugins.map { |plug| "[#{plug.status.to_y}#{plug.tag} #{plug.output}]" }.join(' ')
    @_data.status = plugins.map { |plug| plug.status }.max
  end
end

#has_plugins?Boolean Also known as: has_plugs?

true when a plugin contains plugs

Returns:

  • (Boolean)


49
50
51
# File 'lib/naplug/plugin.rb', line 49

def has_plugins?
  @plugins.empty? ? false : true
end

#long_outputObject



72
73
74
# File 'lib/naplug/plugin.rb', line 72

def long_output
  @_data.output.long_output
end

#long_output!(long_output) ⇒ Object



76
77
78
# File 'lib/naplug/plugin.rb', line 76

def long_output!(long_output)
  @_data.output.push long_output
end

#metaObject



36
37
38
# File 'lib/naplug/plugin.rb', line 36

def meta
  @_meta
end

#outputString

Gets plugin text output

Returns:

  • (String)

    plugin text output



61
62
63
# File 'lib/naplug/plugin.rb', line 61

def output
  @_data.output
end

#output!(text_output) ⇒ String

Sets plugin text output

Parameters:

  • text_output (String)

    plugin text output

Returns:

  • (String)

    new plugin text output



68
69
70
# File 'lib/naplug/plugin.rb', line 68

def output!(text_output)
  @_data.output.text_output = text_output
end

#parentObject



40
41
42
# File 'lib/naplug/plugin.rb', line 40

def parent
  @_meta.parent
end

#payloadObject



95
96
97
# File 'lib/naplug/plugin.rb', line 95

def payload
  @_data.payload
end

#payload!(p) ⇒ Object



99
100
101
# File 'lib/naplug/plugin.rb', line 99

def payload!(p)
  @_data.payload = p
end

#perfdata(mode = nil) ⇒ Object

returns the performance data of the plugin as a PerformanceData object



81
82
83
84
85
86
87
88
# File 'lib/naplug/plugin.rb', line 81

def perfdata(mode = nil)
  case mode
    when :deep
      plugins.values.map { |p| p.perfdata :deep }.push @_data.perfdata
    else
      @_data.perfdata
  end
end

#perfdata!(label, value, f = {}) ⇒ Object



90
91
92
93
# File 'lib/naplug/plugin.rb', line 90

def perfdata!(label,value,f = {})
  @_data.perfdata ||= PerformanceData.new self
  @_data.perfdata[label] = value, f
end

#statusStatus

Returns plugin status.

Returns:



55
56
57
# File 'lib/naplug/plugin.rb', line 55

def status
  @_data.status
end

#to_strObject



124
125
126
# File 'lib/naplug/plugin.rb', line 124

def to_str
  '%s: %s' % [status,output]
end