Class: Neutrino::Client::MuninMetric

Inherits:
Metric
  • Object
show all
Defined in:
lib/neutrino/munin_metric.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Metric

#metric_id, #to_h, #to_json

Constructor Details

#initialize(opts = {}) ⇒ MuninMetric

Returns a new instance of MuninMetric.



8
9
10
11
12
# File 'lib/neutrino/munin_metric.rb', line 8

def initialize(opts={})
  super(opts)
  configure
  query
end

Class Method Details

.configure_plugin(path) ⇒ Object



40
41
42
# File 'lib/neutrino/munin_metric.rb', line 40

def self.configure_plugin(path)
  self.execute_and_parse("#{path} config")
end

.execute_and_parse(command) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/neutrino/munin_metric.rb', line 14

def self.execute_and_parse(command)
  output = Open3.popen3(command) do |stdin, stdout, stderr, wait_thr|
    stdout.read.strip
  end
  result = {}
  Log.debug("#{command} outputs #{output}")
  output.to_s.split("\n").each do |line|
    whole_line, key, value = line.match(/(\S*) (.*)/).to_a
    key = line.strip if key.nil? # Can be nil if no value is specified
    key_parts = nil
    if key.match("graph_")
      key_parts = key.split(/_/)
    else
      key_parts = key.split(/\./)
    end
    if key_parts.length == 2
      result[key_parts.first] ||= {}
      result[key_parts.first][key_parts.last] = value
    else
      result[key_parts.first] = value
    end
  end
  Log.debug("#{command} output is parsed as #{result.inspect}")
  result
end

.query_plugin(path) ⇒ Object



44
45
46
# File 'lib/neutrino/munin_metric.rb', line 44

def self.query_plugin(path)
  self.execute_and_parse(path)
end

Instance Method Details

#configureObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/neutrino/munin_metric.rb', line 48

def configure
  plugin_configuration = MuninMetric.configure_plugin(self.munin_plugin_path)
  self. ||= {}
  if plugin_configuration["graph"]
    self.["group"] = plugin_configuration["graph"]["category"]
    self.["type"] = plugin_configuration["graph"]["vlabel"]
    self.["name"] = plugin_configuration["graph"]["title"]
    self.name = plugin_configuration["graph"]["title"]
  end
end

#queryObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/neutrino/munin_metric.rb', line 59

def query
  plugin_query = MuninMetric.query_plugin(self.munin_plugin_path)
  begin
    values_hash = {}
    plugin_query.to_a.each do |metric|
      name = metric[0]
      val = metric[1]["value"]
      values_hash[name] = val.nil? ? nil : val.to_f
    end
    self.values = values_hash
  rescue => e
    Log.debug("Error querying munin: #{e}")
  end
end