class Fluent::MuninNodeInput

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_munin_node.rb, line 29
def initialize
  super
  require 'munin-ruby'
end

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_munin_node.rb, line 34
def configure(conf)
  super

  @include_service = Regexp.new(@include_service) if @include_service
  @exclude_service = Regexp.new(@exclude_service) if @exclude_service

  if @include_hostname
    @extra.update(@hostname_key => hostname)
  end
end
shutdown() click to toggle source
# File lib/fluent/plugin/in_munin_node.rb, line 54
def shutdown
  @loop.watchers.each(&:detach)
  @loop.stop

  # XXX: Comment out for exit quickly. Is it OK?
  #@thread.join
end
start() click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_munin_node.rb, line 45
def start
  super

  @loop = Coolio::Loop.new
  timer = TimerWatcher.new(@interval, true, log, &method(:fetch_items))
  @loop.attach(timer)
  @thread = Thread.new(&method(:run))
end

Private Instance Methods

emit_items(values_by_service) click to toggle source
# File lib/fluent/plugin/in_munin_node.rb, line 93
def emit_items(values_by_service)
  if @bulk
    tag = @tag_prefix + '.' + @bulk_suffix
    router.emit(tag, Fluent::Engine.now, values_by_service.merge(extra))
  else
    values_by_service.each do |service, value_by_field|
      value_by_field.each do |fieldname, value|
        tag = [@tag_prefix, service, fieldname].join('.')

        record = {
          @service_key => service,
          @field_key => fieldname,
          @value_key => value =~ /\./ ? value.to_f : value.to_i,
        }

        router.emit(tag, Fluent::Engine.now, record.merge(extra))
      end
    end
  end
end
fetch_items() click to toggle source
# File lib/fluent/plugin/in_munin_node.rb, line 71
def fetch_items
  values_by_service = {}

  # It connect to Munin node every time.
  node = Munin::Node.new(@node_host, @node_port)
  services = filter_service(node.list)

  services.each do |service|
    begin
      service_values = node.fetch(service)
      values_by_service.update(service_values)
    rescue => e
      log.warn("#{service}: #{e.message}")
      log.warn_backtrace(e.backtrace)
    end
  end

  unless values_by_service.empty?
    emit_items(values_by_service)
  end
end
filter_service(services) click to toggle source
# File lib/fluent/plugin/in_munin_node.rb, line 114
def filter_service(services)
  if @exclude_service
    services = services.reject do |srvc|
      srvc =~ @exclude_service
    end
  end

  if @include_service
    services = services.select do |srvc|
      srvc =~ @include_service
    end
  end

  services
end
hostname() click to toggle source
# File lib/fluent/plugin/in_munin_node.rb, line 130
def hostname
  `hostname`.strip
end
run() click to toggle source
# File lib/fluent/plugin/in_munin_node.rb, line 64
def run
  @loop.run
rescue => e
  log.error(e.message)
  log.error_backtrace(e.backtrace)
end