Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support tcp transport protocol #8

Merged
merged 2 commits into from
Jun 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
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
2 changes: 1 addition & 1 deletion logstash-input-snmp.gemspec
Original file line number Diff line number Diff line change
@@ -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"
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"}]},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should't this be 1161?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't have to be - note that we are only verifying the format here but in any case, the port number can be any number.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, I was just aiming at consistency in the examples that reference 1161 for tcp in two other places.

]
}

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