lib/arborist/monitor/snmp.rb
changeset 14 d5cb8bd33170
parent 8 e0b7c95a154f
child 20 00a38d493f2c
equal deleted inserted replaced
13:6723f3b07536 14:d5cb8bd33170
     1 # -*- ruby -*-
     1 # -*- ruby -*-
     2 # vim: set noet nosta sw=4 ts=4 :
     2 # vim: set noet nosta sw=4 ts=4 :
     3 #encoding: utf-8
     3 #encoding: utf-8
     4 
     4 
     5 require 'arborist/monitor' unless defined?( Arborist::Monitor )
     5 require 'arborist/monitor' unless defined?( Arborist::Monitor )
     6 require 'net-snmp2'
     6 require 'netsnmp'
     7 
     7 
     8 # SNMP checks for Arborist.  Requires an SNMP agent to be installed
     8 # SNMP checks for Arborist.  Requires an SNMP agent to be installed
     9 # on target machine, and the various "pieces" enabled for your platform.
     9 # on target machine, and the various "pieces" enabled for your platform.
    10 #
    10 #
    11 # For example, for disk monitoring with Net-SNMP, you'll want to set
    11 # For example, for disk monitoring with Net-SNMP, you'll want to set
    35 		setting :port, default: 161
    35 		setting :port, default: 161
    36 
    36 
    37 		# How many hosts to check simultaneously
    37 		# How many hosts to check simultaneously
    38 		setting :batchsize, default: 25
    38 		setting :batchsize, default: 25
    39 	end
    39 	end
    40 
       
    41 	# Indicate to FFI that we're using threads.
       
    42 	Net::SNMP.thread_safe = true
       
    43 
       
    44 
       
    45 	# The system type, as advertised.
       
    46 	attr_reader :system
       
    47 
    40 
    48 	# The mapping of addresses back to node identifiers.
    41 	# The mapping of addresses back to node identifiers.
    49 	attr_reader :identifiers
    42 	attr_reader :identifiers
    50 
    43 
    51 	# The results hash that is sent back to the manager.
    44 	# The results hash that is sent back to the manager.
    84 
    77 
    85 			slice.each do |host|
    78 			slice.each do |host|
    86 				thr = Thread.new do
    79 				thr = Thread.new do
    87 					config = self.identifiers[ host ].last || {}
    80 					config = self.identifiers[ host ].last || {}
    88 					opts = {
    81 					opts = {
    89 						peername:  host,
    82 						host:      host,
    90 						port:      config[ 'port' ]      || Arborist::Monitor::SNMP.port,
    83 						port:      config[ 'port' ]      || Arborist::Monitor::SNMP.port,
    91 						version:   config[ 'version' ]   || Arborist::Monitor::SNMP.version,
    84 						version:   config[ 'version' ]   || Arborist::Monitor::SNMP.version,
    92 						community: config[ 'community' ] || Arborist::Monitor::SNMP.community,
    85 						community: config[ 'community' ] || Arborist::Monitor::SNMP.community,
    93 						timeout:   config[ 'timeout' ]   || Arborist::Monitor::SNMP.timeout,
    86 						timeout:   config[ 'timeout' ]   || Arborist::Monitor::SNMP.timeout,
    94 						retries:   config[ 'retries' ]   || Arborist::Monitor::SNMP.retries
    87 						retries:   config[ 'retries' ]   || Arborist::Monitor::SNMP.retries
    95 					}
    88 					}
    96 
    89 
    97 					snmp = Net::SNMP::Session.open( opts )
       
    98 					begin
    90 					begin
    99 						@system = snmp.get( IDENTIFICATION_OID ).varbinds.first.value
    91 						NETSNMP::Client.new( opts ) do |snmp|
   100 						yield( host, snmp )
    92 							Thread.current[ :system ] = snmp.get( oid: IDENTIFICATION_OID )
       
    93 							yield( host, snmp )
       
    94 						end
   101 
    95 
   102 					rescue Net::SNMP::TimeoutError, Net::SNMP::Error => err
    96 					rescue => err
   103 						self.log.error "%s: %s %s" % [ host, err.message, snmp.error_message ]
    97 						self.log.error "%s: %s\n%s" % [ host, err.message, err.backtrace.join("\n  ") ]
   104 						self.results[ host ] = {
    98 						self.results[ host ] = {
   105 							error: "%s" % [ snmp.error_message ]
    99 							error: "Exception (%s: %s)" % [ err.class.name, err.message ]
   106 						}
   100 						}
   107 					rescue => err
       
   108 						self.results[ host ] = {
       
   109 							error: "Uncaught exception. (%s: %s)" % [ err.class.name, err.message ]
       
   110 						}
       
   111 					ensure
       
   112 						snmp.close
       
   113 					end
   101 					end
   114 				end
   102 				end
   115 
   103 
   116 				threads.add( thr )
   104 				threads.add( thr )
   117 			end
   105 			end
   133 	ensure
   121 	ensure
   134 		@identifiers = {}
   122 		@identifiers = {}
   135 		@results     = {}
   123 		@results     = {}
   136 	end
   124 	end
   137 
   125 
       
   126 
       
   127 	### Return the current SNMP connection system string.
       
   128 	def system
       
   129 		return Thread.current[ :system ]
       
   130 	end
       
   131 
   138 end # Arborist::Monitor::SNMP
   132 end # Arborist::Monitor::SNMP
   139 
   133 
   140 require 'arborist/monitor/snmp/cpu'
   134 require 'arborist/monitor/snmp/cpu'
   141 require 'arborist/monitor/snmp/disk'
   135 require 'arborist/monitor/snmp/disk'
   142 require 'arborist/monitor/snmp/process'
   136 require 'arborist/monitor/snmp/process'