lib/arborist/monitor/snmp/memory.rb
changeset 4 e6eb11b1e00d
child 8 e0b7c95a154f
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 memory availability checks.
       
     7 # Returns total available memory in Kb to the 'available_memory' attribute.
       
     8 #
       
     9 class Arborist::Monitor::SNMP::Memory
       
    10 	include Arborist::Monitor::SNMP
       
    11 
       
    12 	extend Loggability
       
    13 	log_to :arborist
       
    14 
       
    15 	# OIDS for discovering memory usage.
       
    16 	#
       
    17 	MEMORY = {
       
    18 		mem_avail: '1.3.6.1.4.1.2021.4.6.0'
       
    19 	}
       
    20 
       
    21 	# Global defaults for instances of this monitor
       
    22 	#
       
    23 	DEFAULT_OPTIONS = {
       
    24 		error_at: 95, # in percent full
       
    25 	}
       
    26 
       
    27 
       
    28 	### This monitor is complex enough to require creating an instance from the caller.
       
    29 	### Provide a friendlier error message the class was provided to exec() directly.
       
    30 	###
       
    31 	def self::run( nodes )
       
    32 		return new.run( nodes )
       
    33 	end
       
    34 
       
    35 
       
    36 	### Create a new instance of this monitor.
       
    37 	###
       
    38 	def initialize( options=DEFAULT_OPTIONS )
       
    39 		options = DEFAULT_OPTIONS.merge( options || {} )
       
    40 		options.each do |name, value|
       
    41 			self.public_send( "#{name.to_s}=", value )
       
    42 		end
       
    43 	end
       
    44 
       
    45 	# Set an error if memory used is below this many kilobytes.
       
    46 	attr_accessor :error_at
       
    47 
       
    48 
       
    49 	### Perform the monitoring checks.
       
    50 	###
       
    51 	def run( nodes )
       
    52 		super do |snmp, host|
       
    53 			self.gather_free_memory( snmp, host )
       
    54 		end
       
    55 	end
       
    56 
       
    57 
       
    58 	#########
       
    59 	protected
       
    60 	#########
       
    61 
       
    62 	### Collect available memory information for +host+ from an existing
       
    63 	### (and open) +snmp+ connection.
       
    64 	###
       
    65 	def gather_free_memory( snmp, host )
       
    66 		self.log.debug "Getting available memory for: %s" % [ host ]
       
    67 		mem_avail = snmp.get( SNMP::ObjectId.new( MEMORY[:mem_avail] ) ).varbind_list.first.value.to_f
       
    68 		self.log.debug "  Available memory on %s: %0.2f" % [ host, mem_avail ]
       
    69 
       
    70 		config = @identifiers[ host ].last || {}
       
    71 		error_at = config['error_at'] || self.error_at
       
    72 		if mem_avail <= error_at
       
    73 			@results[ host ] = {
       
    74 				error: "Available memory is under %0.1fMB" % [ error_at.to_f / 1024 ],
       
    75 				available_memory: mem_avail
       
    76 			}
       
    77 		else
       
    78 			@results[ host ] = { available_memory: mem_avail }
       
    79 		end
       
    80 	end
       
    81 
       
    82 end # class Arborist::Monitor::SNMP::Memory
       
    83