Module: Naplug::InstanceMethods

Defined in:
lib/naplug.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



202
203
204
205
206
207
208
209
210
# File 'lib/naplug.rb', line 202

def method_missing(method, *args, &block)
  message = "undefined instance variable or method #{method}"
  case @_runinng
    when nil?
      begin; raise Naplug::Error, message; rescue => e; eject! e ; end
    else
      raise Naplug::Error, message
  end
end

Instance Attribute Details

#pluginsObject (readonly)

Returns the value of attribute plugins.



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

def plugins
  @plugins
end

Instance Method Details

#argsHash

Returns the arguments of the plugin

Returns:

  • (Hash)

    a hash by argument key of argument values



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

def args
  @_args
end

#args!(args) ⇒ Object

Sets and propagates plugin arguments

Parameters:

  • args (Hash <Symbol, Object>)


89
90
91
92
93
94
95
96
# File 'lib/naplug.rb', line 89

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

#eject!(payload = nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# File 'lib/naplug.rb', line 141

def eject!(payload = nil)
  o = case payload
        when String then payload
        when Exception then "#{payload.backtrace[1][/.+:\d+/]}: #{payload.message}"
        else nil
          caller[0][/.+:\d+/]
      end
  print "UNKNOWN: plugin eject! in %s\n" % [o]
  Kernel::exit 3
end

#eval(tag = default_plugin.tag) ⇒ Object



132
133
134
# File 'lib/naplug.rb', line 132

def eval(tag = default_plugin.tag)
  @plugins[tag].eval
end

#eval!(tag = default_plugin.tag) ⇒ Object



136
137
138
139
# File 'lib/naplug.rb', line 136

def eval!(tag = default_plugin.tag)
  @plugins[tag].eval
  exit tag
end

#exec(t = default_plugin.tag) ⇒ Object

Execute the plugin

Parameters:

  • tag (Symbol)

    a plugin tag



123
124
125
126
127
128
129
130
# File 'lib/naplug.rb', line 123

def exec(t = default_plugin.tag)
  plugin = target_plugin t
  if plugin.has_plugins?
    plugin.plugins.each_value { |p| exec p }
  else
    plexec plugin
  end
end

#exec!(tag = default_plugin.tag) ⇒ Object

Execute, evaluate and exit the plugin according to the plugin status, outputting the plugin’s text output (and performance data, if applicable)

Parameters:

  • tag (Symbol) (defaults to: default_plugin.tag)

    a plugin tag



112
113
114
115
116
117
118
119
# File 'lib/naplug.rb', line 112

def exec!(tag = default_plugin.tag)
  t = Benchmark.realtime do
    exec tag
    eval tag
  end
#  @plugins[tag].perfdata! "monitoring.#{File.basename($0)}.#{tag}", t if @plugins[tag].meta.benchmark
  exit tag
end

#initialize(args = {}) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/naplug.rb', line 73

def initialize(args = {})
  @plugins = Hash.new
  plugins!

  @_args = Hash.new
  args! args
end

#perfdata(tag = default_plugin.tag) ⇒ Array<PerformanceData>

Returns a list of performance data objects.

Returns:



153
154
155
# File 'lib/naplug.rb', line 153

def perfdata(tag = default_plugin.tag)
  @plugins[tag].perfdata(:deep).flatten.select { |pd| pd}
end

#to_str(tag = default_plugin.tag) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/naplug.rb', line 98

def to_str(tag = default_plugin.tag)
  pd = perfdata(tag)
  if pd.empty?
    s_format = '%s: %s'
    s_array = [@plugins[tag].status,@plugins[tag].output]
  else
    s_format = '%s: %s | %s'
    s_array = [@plugins[tag].status,@plugins[tag].output,pd.join(' ').strip]
  end
  s_format % s_array
end