lib/logstash/inputs/snmp.rb in logstash-input-snmp-0.1.0.beta2 vs lib/logstash/inputs/snmp.rb in logstash-input-snmp-0.1.0.beta3

- old
+ new

@@ -24,11 +24,11 @@ # Each host definition is a hash and must define the `host` key and value. # `host` must use the format {tcp|udp}:{ip address}/{port} # for example `host => "udp:127.0.0.1/161"` # Each host definition can optionally include the following keys and values: # `community` with a default value of `public` - # `version` with a default value of `2c` + # `version` `1` or `2c` with a default value of `2c` # `retries` with a detault value of `2` # `timeout` in milliseconds with a default value of `1000` config :hosts, :validate => :array #[ {"host" => "udp:127.0.0.1/161", "community" => "public"} ] # List of paths of MIB .dic files of dirs. If a dir path is specified, all files with .dic extension will be loaded. @@ -69,25 +69,32 @@ @client_definitions = [] @hosts.each do |host| host_name = host["host"] community = host["community"] || "public" version = host["version"] || "2c" + raise(LogStash::ConfigurationError, "only protocol version '1' and '2c' are supported for host option '#{host_name}'") unless version =~ VERSION_REGEX + 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 end end @@ -139,9 +146,10 @@ private OID_REGEX = /^\.?([0-9\.]+)$/ HOST_REGEX = /^(?<host_protocol>udp|tcp):(?<host_address>.+)\/(?<host_port>\d+)$/i + VERSION_REGEX =/^1|2c$/ def validate_oids! @get = Array(@get).map do |oid| # verify oids for valid pattern and get rid or any leading dot if present unless oid =~ OID_REGEX