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