Class: LogStash::Inputs::Snmp
- Inherits:
-
Base
- Object
- Base
- LogStash::Inputs::Snmp
- Extended by:
- PluginMixins::ValidatorSupport::FieldReferenceValidationAdapter
- Includes:
- PluginMixins::ECSCompatibilitySupport::TargetCheck, PluginMixins::EventSupport::EventFactoryAdapter, PluginMixins::Snmp::Common
- Defined in:
- lib/logstash/inputs/snmp.rb
Overview
The SNMP input plugin polls network devices using Simple Network Management Protocol (SNMP) to gather information related to the current state of the devices operation.
Defined Under Namespace
Classes: StoppableIntervalRunner
Constant Summary
Constants included from PluginMixins::Snmp::Common
PluginMixins::Snmp::Common::MIB_BASE_PATH, PluginMixins::Snmp::Common::MIB_PROVIDED_PATHS, PluginMixins::Snmp::Common::OID_MAPPING_FORMAT_DEFAULT, PluginMixins::Snmp::Common::OID_MAPPING_FORMAT_RUBY_SNMP
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(params = {}) ⇒ Snmp
constructor
A new instance of Snmp.
- #poll_clients(queue) ⇒ Object
- #poll_hosts_timeout(max_host_timeout) ⇒ Object
- #register ⇒ Object
- #run(queue) ⇒ Object
- #stoppable_interval_runner ⇒ Object
Methods included from PluginMixins::Snmp::Common
#build_mib_manager!, #build_snmp_client!, included
Constructor Details
#initialize(params = {}) ⇒ Snmp
Returns a new instance of Snmp.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/logstash/inputs/snmp.rb', line 70 def initialize(params={}) super(params) @host_protocol_field = ecs_select[disabled: '[@metadata][host_protocol]', v1: '[@metadata][input][snmp][host][protocol]'] @host_address_field = ecs_select[disabled: '[@metadata][host_address]', v1: '[@metadata][input][snmp][host][address]'] @host_port_field = ecs_select[disabled: '[@metadata][host_port]', v1: '[@metadata][input][snmp][host][port]'] @host_community_field = ecs_select[disabled: '[@metadata][host_community]', v1: '[@metadata][input][snmp][host][community]'] # Add the default "host" field to the event, for backwards compatibility, or host.ip in ecs mode unless params.key?('add_field') host_ip_field = ecs_select[disabled: "host", v1: "[host][ip]"] @add_field = { host_ip_field => "%{#{@host_address_field}}" } end end |
Instance Method Details
#close ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/logstash/inputs/snmp.rb', line 227 def close # keep this close order (aggregator, client), so the request_aggregator can gracefully shutdown begin @request_aggregator.close unless @request_aggregator.nil? rescue => e logger.warn('Error closing SNMP client request aggregator. Ignoring', :exception => e) end begin @client.close unless @client.nil? rescue => e logger.warn('Error closing SNMP client. Ignoring', :exception => e) end end |
#poll_clients(queue) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/logstash/inputs/snmp.rb', line 149 def poll_clients(queue) max_host_timeout = 0 in_flight_requests = [] @client_definitions.each do |definition| target = @client.create_target( definition[:host], definition[:version], definition[:retries], definition[:timeout], definition[:host_community], definition[:security_name], definition[:security_level] ) request = @request_aggregator.create_request(@client) in_flight_requests << { request: request, definition: definition } if definition[:get].any? request.get(target, definition[:get].map { |oid| OID.new(oid) }) end definition[:walk]&.each do |oid| request.walk(target, OID.new(oid)) end @tables&.each do |table_entry| oids = table_entry['columns'].map { |oid| OID.new(oid) } request.table(target, table_entry['name'], oids) end max_host_timeout = definition[:timeout] if definition[:timeout] > max_host_timeout end in_flight_requests.each do |req| definition = req[:definition] result_consumer = lambda do |result| if result&.any? event = targeted_event_factory.new_event(result) event.set(@host_protocol_field, definition[:host_protocol]) event.set(@host_address_field, definition[:host_address]) event.set(@host_port_field, definition[:host_port]) event.set(@host_community_field, definition[:host_community]) decorate(event) queue << event else logger.debug? && logger.debug('No SNMP data retrieved', host: definition[:host_address]) end end req[:request].get_result_async(result_consumer) end begin # blocks until all requests are complete @request_aggregator.await(in_flight_requests.map { |p| p[:request] }, poll_hosts_timeout(max_host_timeout)) rescue java.util.concurrent.TimeoutException => _ logger.error("Timed out while waiting for SNMP requests to finish. Consider increasing `poll_hosts_timeout` if the number of hosts is large") end end |
#poll_hosts_timeout(max_host_timeout) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/logstash/inputs/snmp.rb', line 210 def poll_hosts_timeout(max_host_timeout) return @poll_hosts_timeout if @poll_hosts_timeout # this timeout should be a safe-guard and it should never wait for that long. # It might reach this point if there are several hosts operations, the hosts # are unreachable/unhealthy, and/or the configured host timeout is too high. in_flight_timeout = 1000 * 60 * 60 # 1 hour in_flight_timeout = max_host_timeout if max_host_timeout > in_flight_timeout in_flight_timeout = @interval if @interval > in_flight_timeout in_flight_timeout end |
#register ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/logstash/inputs/snmp.rb', line 85 def register validate_config! mib_manager = build_mib_manager! # setup client definitions per provided host @client_definitions = [] supported_transports = Set.new hosts_versions = Set.new @hosts.each do |host| host_name = host['host'] version = host['version'] || '2c' unless version =~ VERSION_REGEX raise(LogStash::ConfigurationError, "only protocol version '1', '2c' and '3' are supported for host option '#{host_name}'") end host_details = host_name.match(HOST_REGEX) raise(LogStash::ConfigurationError, "invalid format for host option '#{host_name}'") unless host_details unless host_details[:host_protocol].to_s =~ /^(?:udp|tcp)$/i raise(LogStash::ConfigurationError, "only udp & tcp protocols are supported for host option '#{host_name}'") end host_protocol = host_details[:host_protocol] definition = { :get => Array(get), :walk => Array(walk), :host => host_name, :host_address => host_details[:host_address], :host_protocol => host_protocol, :host_port => host_details[:host_port], :host_community => host['community'] || 'public', :retries => host['retries'] || 2, :timeout => host['timeout'] || 1000, :version => version, :security_name => @security_name, :security_level => @security_level, } supported_transports << host_protocol hosts_versions << version @client_definitions << definition end @client = build_client!(mib_manager, supported_transports, hosts_versions) @request_aggregator = create_request_aggregator end |
#run(queue) ⇒ Object
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/logstash/inputs/snmp.rb', line 138 def run(queue) # Put the client into the listen mode, so it can send requests and receive responses @client.listen # Single threaded poller which sleeps off the remaining interval between each run. # Each run polls all the defined hosts for the get, table and walk options. stoppable_interval_runner.every(@interval, 'polling hosts') do poll_clients(queue) end end |
#stoppable_interval_runner ⇒ Object
223 224 225 |
# File 'lib/logstash/inputs/snmp.rb', line 223 def stoppable_interval_runner StoppableIntervalRunner.new(self) end |