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

- old
+ new

@@ -51,10 +51,13 @@ # Set polling interval in seconds # # The default, `30`, means poll each host every 30second. config :interval, :validate => :number, :default => 30 + # Add the default "host" field to the event. + config :add_field, :validate => :hash, :default => { "host" => "%{[@metadata][host_address]}" } + def register validate_oids! validate_hosts! mib = LogStash::SnmpMib.new @@ -69,14 +72,23 @@ community = host["community"] || "public" version = host["version"] || "2c" retries = host["retries"] || 2 timeout = host["timeout"] || 1000 + 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 + definition = { :client => LogStash::SnmpClient.new(host_name, 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_community => community, } @client_definitions << definition end end @@ -102,10 +114,18 @@ end end end unless result.empty? + metadata = { + "host_protocol" => definition[:host_protocol], + "host_address" => definition[:host_address], + "host_port" => definition[:host_port], + "host_community" => definition[:host_community], + } + result["@metadata"] = metadata + event = LogStash::Event.new(result) decorate(event) queue << event end end @@ -118,11 +138,11 @@ end private OID_REGEX = /^\.?([0-9\.]+)$/ - HOST_REGEX = /^(udp|tcp):.+\/\d+$/i + HOST_REGEX = /^(?<host_protocol>udp|tcp):(?<host_address>.+)\/(?<host_port>\d+)$/i 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 @@ -147,12 +167,8 @@ raise(LogStash::ConfigurationError, "at least one host definition is required") if Array(@hosts).empty? @hosts.each do |host| raise(LogStash::ConfigurationError, "each host definition must have a \"host\" option") if !host.is_a?(Hash) || host["host"].nil? - unless host["host"] =~ HOST_REGEX - raise(LogStash::ConfigurationError, "invalid host option format") - end - raise(LogStash::ConfigurationError, "tcp is not yet supported, only udp") if $1 =~ /tcp/i end end end