From 3e5355d645ef10510445d58ecfbcfbec7c21f402 Mon Sep 17 00:00:00 2001 From: Colin Surprenant Date: Thu, 31 May 2018 15:53:52 -0400 Subject: [PATCH 1/2] support tcp transport protocol add host info in exception --- lib/logstash/inputs/snmp.rb | 15 ++++++++++----- lib/logstash/inputs/snmp/client.rb | 16 ++++++++++++---- spec/inputs/snmp_spec.rb | 4 +++- test/sample.conf | 2 +- test/test_client.rb | 3 ++- 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/lib/logstash/inputs/snmp.rb b/lib/logstash/inputs/snmp.rb index e9d674e..9729643 100644 --- a/lib/logstash/inputs/snmp.rb +++ b/lib/logstash/inputs/snmp.rb @@ -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 diff --git a/lib/logstash/inputs/snmp/client.rb b/lib/logstash/inputs/snmp/client.rb index 6ffd4e1..b5efda3 100644 --- a/lib/logstash/inputs/snmp/client.rb +++ b/lib/logstash/inputs/snmp/client.rb @@ -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" @@ -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 diff --git a/spec/inputs/snmp_spec.rb b/spec/inputs/snmp_spec.rb index b4239b2..8ff37d7 100644 --- a/spec/inputs/snmp_spec.rb +++ b/spec/inputs/snmp_spec.rb @@ -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"}]}, diff --git a/test/sample.conf b/test/sample.conf index a6bb984..42ca734 100644 --- a/test/sample.conf +++ b/test/sample.conf @@ -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"] diff --git a/test/test_client.rb b/test/test_client.rb index 092317b..049a691 100644 --- a/test/test_client.rb +++ b/test/test_client.rb @@ -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") From a8b8a5ebcd52fcd96b615d598d84287bed878634 Mon Sep 17 00:00:00 2001 From: Colin Surprenant Date: Fri, 1 Jun 2018 10:53:12 -0400 Subject: [PATCH 2/2] bump to version 0.1.0.beta3 --- CHANGELOG.md | 3 +++ logstash-input-snmp.gemspec | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2687e8..5c5d9db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.1.0.beta3 + - add tcp transport protocol support, https://github.com/logstash-plugins/logstash-input-snmp/pull/8 + ## 0.1.0.beta2 - add host info in metadata and host field, https://github.com/logstash-plugins/logstash-input-snmp/pull/7 diff --git a/logstash-input-snmp.gemspec b/logstash-input-snmp.gemspec index 2cfddba..f4fd7dd 100644 --- a/logstash-input-snmp.gemspec +++ b/logstash-input-snmp.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'logstash-input-snmp' - s.version = '0.1.0.beta2' + s.version = '0.1.0.beta3' s.licenses = ['Apache-2.0'] s.summary = "SNMP input plugin" s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"