Sha256: 5663fd67415d6aecf053bdf12ff30aa3abfda1bd864081376af0fcc926c4b170

Contents?: true

Size: 1.76 KB

Versions: 3

Compression:

Stored size: 1.76 KB

Contents

module OpenNMSHelper

require 'rexml/document'

  # This is used to ensure the (Docker) OpenNMS port is up and running
  def port_open?(ip, port, seconds=1)
    Timeout::timeout(seconds) do
      begin
        TCPSocket.new(ip, port).close
        true
      rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
        false
      end
    end
  rescue Timeout::Error
    false
  end

  def port_responding?(ip, port)
    # Try for up to 10 seconds to get a response
    10.times do
      if port_open?(ip, port)
        return true
      else
        sleep 1
      end
    end
    false
  end
    
  def opennms_server_up?(opennms_ip, port)
    data = {
        "request" => "sender data",
        "data" => [{
          "host"  => "opennms.example.com",
          "key"   => "opennms.key",
          "value" => "This is a log entry.",
          "clock" => 1429100000,
        }],
        "clock" => Time.now.to_i,
    }
    if port_responding?(opennms_ip, port)
      ###
      ### This is a hacky way to guarantee that OpenNMS is responsive, because the
      ### port check alone is insufficient.  TCP tests say the port is open, but
      ### it can take another 2 to 5 seconds (depending on the machine) before it
      ### is responding in the way we need for these tests.
      ###
      resp = ""
      # Try for up to 10 seconds to get a response.
      10.times do
        TCPSocket.open(opennms_ip, port) do |sock|
          sock.print ZabbixProtocol.dump(data)
          resp = sock.read
        end
        if resp.length == 0
          sleep 1
        else
          return true
        end
      end
      if resp.length == 0
        puts "OpenNMS server or db is unreachable"
      end
    else
      puts "Unable to reach OpenNMS server on #{opennms_ip}:#{port}"
    end
    false
  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
logstash-output-opennms-0.1.3 spec/helpers/opennms_helper.rb
logstash-output-opennms-0.1.1 spec/helpers/opennms_helper.rb
logstash-output-opennms-0.1.0 spec/helpers/opennms_helper.rb