lib/arborist/monitor/snmp/load.rb
changeset 4 e6eb11b1e00d
equal deleted inserted replaced
3:d46ca2b52efe 4:e6eb11b1e00d
       
     1 # -*- ruby -*-
       
     2 # vim: set noet nosta sw=4 ts=4 :
       
     3 
       
     4 require 'arborist/monitor/snmp' unless defined?( Arborist::Monitor::SNMP )
       
     5 
       
     6 # SNMP 5 minute load checks.
       
     7 # Sets current 5 minute load as a 'load5' attribute.
       
     8 #
       
     9 class Arborist::Monitor::SNMP::Load
       
    10 	include Arborist::Monitor::SNMP
       
    11 
       
    12 	extend Loggability
       
    13 	log_to :arborist
       
    14 
       
    15 	# OIDS for discovering system load.
       
    16 	#
       
    17 	LOAD = {
       
    18 		five_min: '1.3.6.1.4.1.2021.10.1.3.2'
       
    19 	}
       
    20 
       
    21 	# Global defaults for instances of this monitor
       
    22 	#
       
    23 	DEFAULT_OPTIONS = {
       
    24 		error_at: 7
       
    25 	}
       
    26 
       
    27 
       
    28 	### Class #run creates a new instance.
       
    29 	###
       
    30 	def self::run( nodes )
       
    31 		return new.run( nodes )
       
    32 	end
       
    33 
       
    34 
       
    35 	### Create a new instance of this monitor.
       
    36 	###
       
    37 	def initialize( options=DEFAULT_OPTIONS )
       
    38 		options = DEFAULT_OPTIONS.merge( options || {} )
       
    39 		options.each do |name, value|
       
    40 			self.public_send( "#{name.to_s}=", value )
       
    41 		end
       
    42 	end
       
    43 
       
    44 	# Set an error if mount points are above this percentage.
       
    45 	attr_accessor :error_at
       
    46 
       
    47 
       
    48 	### Perform the monitoring checks.
       
    49 	###
       
    50 	def run( nodes )
       
    51 		super do |snmp, host|
       
    52 			self.gather_load( snmp, host )
       
    53 		end
       
    54 	end
       
    55 
       
    56 
       
    57 	#########
       
    58 	protected
       
    59 	#########
       
    60 
       
    61 	### Collect the load information for +host+ from an existing
       
    62 	### (and open) +snmp+ connection.
       
    63 	###
       
    64 	def gather_load( snmp, host )
       
    65 		self.log.debug "Getting system load for: %s" % [ host ]
       
    66 		load5 = snmp.get( SNMP::ObjectId.new( LOAD[:five_min] ) ).varbind_list.first.value.to_f
       
    67 		self.log.debug "  Load on %s: %0.2f" % [ host, load5 ]
       
    68 
       
    69 		config = @identifiers[ host ].last || {}
       
    70 		error_at = config[ 'error_at' ] || self.error_at
       
    71 		if load5 >= error_at
       
    72 			@results[ host ] = {
       
    73 				error: "Load has exceeded %0.2f over a 5 minute average" % [ error_at ],
       
    74 				load5: load5
       
    75 			}
       
    76 		else
       
    77 			@results[ host ] = { load5: load5 }
       
    78 		end
       
    79 	end
       
    80 
       
    81 end # class Arborist::Monitor::SNMP::Load
       
    82