class Fluent::PingPortInput

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_ping_port.rb, line 5
def initialize
  super
  require 'socket'
  require 'timeout'
end

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_ping_port.rb, line 22
def configure(conf)
  super
  @ports = @port.split(',')
  @state = @ports.inject({}) {|state, port|
    state[port] = 0
    state
  }
end
emit_ping_port() click to toggle source
# File lib/fluent/plugin/in_ping_port.rb, line 46
def emit_ping_port
  begin
    @ports.each do |port|
      unless is_port_open?(@host, port, @timeout)
        @state[port] = @state[port] + 1
        if @state[port] >= @retry_count
          record = {
            'message' => "#{@host}:#{port} Connect Error."
          }
          router.emit @tag, Fluent::Engine.now, record
          @state[port] = 0
        end
      else
        @state[port] = 0
      end
    end
  rescue => e
    log.error e
  end
end
run() click to toggle source
# File lib/fluent/plugin/in_ping_port.rb, line 39
def run
  loop do
    Thread.new(&method(:emit_ping_port))
    sleep @interval
  end
end
shutdown() click to toggle source
# File lib/fluent/plugin/in_ping_port.rb, line 35
def shutdown
  Thread.kill(@thread)
end
start() click to toggle source
# File lib/fluent/plugin/in_ping_port.rb, line 31
def start
  @thread = Thread.new(&method(:run))
end

Private Instance Methods

is_port_open?(host, port, timeout) click to toggle source
# File lib/fluent/plugin/in_ping_port.rb, line 69
def is_port_open?(host, port, timeout)
  begin
    Timeout::timeout(timeout) do
      begin
        s = TCPSocket.new(host, port)
        s.close
        return true
      rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
        return false
      end
    end
  rescue Timeout::Error
  end
  return false
end