Skip to content

Commit 3e5355d

Browse files
support tcp transport protocol
add host info in exception
1 parent 5347859 commit 3e5355d

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

lib/logstash/inputs/snmp.rb

+10-5
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,23 @@ def register
7474
retries = host["retries"] || 2
7575
timeout = host["timeout"] || 1000
7676

77+
# TODO: move these validations in a custom validator so it happens before the register method is called.
7778
host_details = host_name.match(HOST_REGEX)
7879
raise(LogStash::ConfigurationError, "invalid format for host option '#{host_name}'") unless host_details
79-
raise(LogStash::ConfigurationError, "only the udp protocol is supported for now") unless host_details[:host_protocol].to_s =~ /udp/i
80+
raise(LogStash::ConfigurationError, "only udp & tcp protocols are supported for host option '#{host_name}'") unless host_details[:host_protocol].to_s =~ /^(?:udp|tcp)$/i
81+
82+
protocol = host_details[:host_protocol]
83+
address = host_details[:host_address]
84+
port = host_details[:host_port]
8085

8186
definition = {
82-
:client => LogStash::SnmpClient.new(host_name, community, version, retries, timeout, mib),
87+
:client => LogStash::SnmpClient.new(protocol, address, port, community, version, retries, timeout, mib),
8388
:get => Array(get),
8489
:walk => Array(walk),
8590

86-
:host_protocol => host_details[:host_protocol],
87-
:host_address => host_details[:host_address],
88-
:host_port => host_details[:host_port],
91+
:host_protocol => protocol,
92+
:host_address => address,
93+
:host_port => port,
8994
:host_community => community,
9095
}
9196
@client_definitions << definition

lib/logstash/inputs/snmp/client.rb

+12-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
java_import "org.snmp4j.smi.OctetString"
1515
java_import "org.snmp4j.smi.VariableBinding"
1616
java_import "org.snmp4j.transport.DefaultUdpTransportMapping"
17+
java_import "org.snmp4j.transport.DefaultTcpTransportMapping"
1718
java_import "org.snmp4j.util.TreeUtils"
1819
java_import "org.snmp4j.util.DefaultPDUFactory"
1920
java_import "org.snmp4j.asn1.BER"
@@ -24,12 +25,19 @@ class SnmpClientError < StandardError
2425

2526
class SnmpClient
2627

27-
def initialize(address, community, version, retries, timeout, mib)
28-
@target = build_target(address, community, version, retries, timeout)
28+
def initialize(protocol, address, port, community, version, retries, timeout, mib)
29+
transport = case protocol.to_s
30+
when "udp"
31+
DefaultUdpTransportMapping.new
32+
when "tcp"
33+
DefaultTcpTransportMapping.new
34+
else
35+
raise(SnmpClientError, "invalid transport protocol specified '#{protocol.to_s}', expecting 'udp' or 'tcp'")
36+
end
37+
38+
@target = build_target("#{protocol}:#{address}/#{port}", community, version, retries, timeout)
2939
@mib = mib
3040

31-
# for now hardwired udp transport
32-
transport = DefaultUdpTransportMapping.new
3341
@snmp = Snmp.new(transport)
3442
transport.listen()
3543
end

spec/inputs/snmp_spec.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@
5454
{"get" => ["1.0"], "hosts" => [{"host" => "udp:localhost/161"}]},
5555
{"get" => ["1.0"], "hosts" => [{"host" => "udp:127.0.0.1/112345"}]},
5656
{"get" => ["1.0"], "hosts" => [{"host" => "udp:127.0.0.1/161", "community" => "public"}]},
57+
{"get" => ["1.0"], "hosts" => [{"host" => "tcp:127.0.0.1/112345"}]},
58+
{"get" => ["1.0"], "hosts" => [{"host" => "tcp:127.0.0.1/161", "community" => "public"}]},
5759
]
5860
}
5961

6062
let(:invalid_configs) {
6163
[
6264
{"get" => ["1.0"], "hosts" => [{"host" => "aaa:127.0.0.1/161"}]},
63-
{"get" => ["1.0"], "hosts" => [{"host" => "tcp:127.0.0.1/161"}]},
65+
{"get" => ["1.0"], "hosts" => [{"host" => "tcp.127.0.0.1/161"}]},
6466
{"get" => ["1.0"], "hosts" => [{"host" => "localhost"}]},
6567
{"get" => ["1.0"], "hosts" => [{"host" => "localhost/161"}]},
6668
{"get" => ["1.0"], "hosts" => [{"host" => "udp:127.0.0.1"}]},

test/sample.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ input {
22
snmp {
33
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"]
44
mib_paths => ["/Users/colin/dev/src/elasticsearch/logstash-plugins/logstash-input-snmp/test/RFC1213-MIB.dic"]
5-
hosts => [{host => "udp:127.0.0.1/161" community => "public"}]
5+
hosts => [{host => "tcp:127.0.0.1/1161" community => "public"}]
66
}
77
snmp {
88
walk => ["1.3.6.1.2.1.1"]

test/test_client.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

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

1213
pp client.get("1.3.6.1.2.1.1.1.0")
1314

0 commit comments

Comments
 (0)