Class: Naplug::Plugin

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

Defined Under Namespace

Classes: DuplicatePlugin

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, meta = false, block) ⇒ Plugin

Returns a new instance of Plugin.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/naplug/plugin.rb', line 15

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

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

  begin; instance_eval &block ; rescue => e; nil ; end

end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



11
12
13
# File 'lib/naplug/plugin.rb', line 11

def block
  @block
end

#pluginsObject (readonly)

Returns the value of attribute plugins.



11
12
13
# File 'lib/naplug/plugin.rb', line 11

def plugins
  @plugins
end

#tagObject (readonly)

Returns the value of attribute tag.



11
12
13
# File 'lib/naplug/plugin.rb', line 11

def tag
  @tag
end

Instance Method Details

#[](k) ⇒ Object



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

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

#[]=(k, v) ⇒ Object



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

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

#argsObject



106
107
108
# File 'lib/naplug/plugin.rb', line 106

def args
  @_args
end

#args!(args) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/naplug/plugin.rb', line 110

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

#disable!Object

disable execution of the plugin; metaplugins cannot be disabled



42
43
44
# File 'lib/naplug/plugin.rb', line 42

def disable!
  is_meta? ? nil : @_meta.enabled = false
end

#enable!Object

enable execution of the plugin; metaplugins are always enabled



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

def enable!
  is_meta? ? nil : @_meta.enabled = true
end

#evalObject



131
132
133
134
135
136
137
138
# File 'lib/naplug/plugin.rb', line 131

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)


57
58
59
# File 'lib/naplug/plugin.rb', line 57

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

#is_disabled?Boolean

true when the plugin is disabled; false otherwise

Returns:

  • (Boolean)


52
53
54
# File 'lib/naplug/plugin.rb', line 52

def is_disabled?
  not @_meta.enabled
end

#is_enabled?Boolean

true when plugin is enabled; false otherwise

Returns:

  • (Boolean)


47
48
49
# File 'lib/naplug/plugin.rb', line 47

def is_enabled?
  @_meta.enabled
end

#is_meta?True, ...

# true if this plugin is a metaplugin, false otherwise

Parameters:

  • format (Symbol)

    the format type, :text or :html

Returns:

  • (True, False<String>, nil)

    the object or objects to find in the database. Can be nil.@return [String] the object converted into the expected format.



32
33
34
# File 'lib/naplug/plugin.rb', line 32

def is_meta?
  @_meta.status
end

#long_outputObject



80
81
82
# File 'lib/naplug/plugin.rb', line 80

def long_output
  @_data.output.long_output
end

#long_output!(long_output) ⇒ Object



84
85
86
# File 'lib/naplug/plugin.rb', line 84

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

#outputString

Gets plugin text output

Returns:

  • (String)

    plugin text output



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

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



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

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

#payloadObject



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

def payload
  @_data.payload
end

#payload!(p) ⇒ Object



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

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

#perfdataObject

returns the performance data of the plugin as a PerformanceData object



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

def perfdata
  @_data.perfdata
end

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



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

def perfdata!(label,value,f = {})
  @_data.perfdata ||= PerformanceData.new @tag
  @_data.perfdata.store label, value, f
end

#statusStatus

Returns plugin status.

Returns:



63
64
65
# File 'lib/naplug/plugin.rb', line 63

def status
  @_data.status
end

#to_sObject



127
128
129
# File 'lib/naplug/plugin.rb', line 127

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