Skip to content

Commit

Permalink
support tcp transport protocol
Browse files Browse the repository at this point in the history
add host info in exception
  • Loading branch information
colinsurprenant committed Jun 1, 2018
1 parent 5347859 commit 3e5355d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
15 changes: 10 additions & 5 deletions lib/logstash/inputs/snmp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,23 @@ def register
retries = host["retries"] || 2
timeout = host["timeout"] || 1000

# TODO: move these validations in a custom validator so it happens before the register method is called.
host_details = host_name.match(HOST_REGEX)
raise(LogStash::ConfigurationError, "invalid format for host option '#{host_name}'") unless host_details
raise(LogStash::ConfigurationError, "only the udp protocol is supported for now") unless host_details[:host_protocol].to_s =~ /udp/i
raise(LogStash::ConfigurationError, "only udp & tcp protocols are supported for host option '#{host_name}'") unless host_details[:host_protocol].to_s =~ /^(?:udp|tcp)$/i

protocol = host_details[:host_protocol]
address = host_details[:host_address]
port = host_details[:host_port]

definition = {
:client => LogStash::SnmpClient.new(host_name, community, version, retries, timeout, mib),
:client => LogStash::SnmpClient.new(protocol, address, port, community, version, retries, timeout, mib),
:get => Array(get),
:walk => Array(walk),

:host_protocol => host_details[:host_protocol],
:host_address => host_details[:host_address],
:host_port => host_details[:host_port],
:host_protocol => protocol,
:host_address => address,
:host_port => port,
:host_community => community,
}
@client_definitions << definition
Expand Down
16 changes: 12 additions & 4 deletions lib/logstash/inputs/snmp/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
java_import "org.snmp4j.smi.OctetString"
java_import "org.snmp4j.smi.VariableBinding"
java_import "org.snmp4j.transport.DefaultUdpTransportMapping"
java_import "org.snmp4j.transport.DefaultTcpTransportMapping"
java_import "org.snmp4j.util.TreeUtils"
java_import "org.snmp4j.util.DefaultPDUFactory"
java_import "org.snmp4j.asn1.BER"
Expand All @@ -24,12 +25,19 @@ class SnmpClientError < StandardError

class SnmpClient

def initialize(address, community, version, retries, timeout, mib)
@target = build_target(address, community, version, retries, timeout)
def initialize(protocol, address, port, community, version, retries, timeout, mib)
transport = case protocol.to_s
when "udp"
DefaultUdpTransportMapping.new
when "tcp"
DefaultTcpTransportMapping.new
else
raise(SnmpClientError, "invalid transport protocol specified '#{protocol.to_s}', expecting 'udp' or 'tcp'")
end

@target = build_target("#{protocol}:#{address}/#{port}", community, version, retries, timeout)
@mib = mib

# for now hardwired udp transport
transport = DefaultUdpTransportMapping.new
@snmp = Snmp.new(transport)
transport.listen()
end
Expand Down
4 changes: 3 additions & 1 deletion spec/inputs/snmp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@
{"get" => ["1.0"], "hosts" => [{"host" => "udp:localhost/161"}]},
{"get" => ["1.0"], "hosts" => [{"host" => "udp:127.0.0.1/112345"}]},
{"get" => ["1.0"], "hosts" => [{"host" => "udp:127.0.0.1/161", "community" => "public"}]},
{"get" => ["1.0"], "hosts" => [{"host" => "tcp:127.0.0.1/112345"}]},
{"get" => ["1.0"], "hosts" => [{"host" => "tcp:127.0.0.1/161", "community" => "public"}]},
]
}

let(:invalid_configs) {
[
{"get" => ["1.0"], "hosts" => [{"host" => "aaa:127.0.0.1/161"}]},
{"get" => ["1.0"], "hosts" => [{"host" => "tcp:127.0.0.1/161"}]},
{"get" => ["1.0"], "hosts" => [{"host" => "tcp.127.0.0.1/161"}]},
{"get" => ["1.0"], "hosts" => [{"host" => "localhost"}]},
{"get" => ["1.0"], "hosts" => [{"host" => "localhost/161"}]},
{"get" => ["1.0"], "hosts" => [{"host" => "udp:127.0.0.1"}]},
Expand Down
2 changes: 1 addition & 1 deletion test/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ input {
snmp {
get => ["1.3.6.1.2.1.1.1.0", "1.3.6.1.2.1.1.3.0", "1.3.6.1.2.1.1.5.0"]
mib_paths => ["/Users/colin/dev/src/elasticsearch/logstash-plugins/logstash-input-snmp/test/RFC1213-MIB.dic"]
hosts => [{host => "udp:127.0.0.1/161" community => "public"}]
hosts => [{host => "tcp:127.0.0.1/1161" community => "public"}]
}
snmp {
walk => ["1.3.6.1.2.1.1"]
Expand Down
3 changes: 2 additions & 1 deletion test/test_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

mib = LogStash::SnmpMib.new
mib.add_mib_path(File.expand_path(File.join("..", "..", "spec", "fixtures", "RFC1213-MIB.dic"), __FILE__))
client = LogStash::SnmpClient.new("udp:127.0.0.1/161", "public", "2c", 2, 1000, mib)
#client = LogStash::SnmpClient.new("tcp", "127.0.0.1", "1161", "public", "2c", 2, 1000, mib)
client = LogStash::SnmpClient.new("udp", "127.0.0.1", "161", "public", "2c", 2, 1000, mib)

pp client.get("1.3.6.1.2.1.1.1.0")

Expand Down

0 comments on commit 3e5355d

Please sign in to comment.