class Arborist::Monitor::SNMP::Memory
SNMP memory and swap utilization checks.
Set 'usage' and 'available' keys as properties, in percentage/GBs, respectively.
By default, doesn't warn on memory usage, only swap, since that's more indicitive of a problem. You can still set the 'physical_warn_at' key to force warnings on ram usage, for embedded systems or other similar things without virtual memory.
Constants
- MEMORY
OIDS for discovering memory usage.
- SWAP
OIDS for discovering swap usage.
Public Class Methods
Return the properties used by this monitor.
# File lib/arborist/monitor/snmp/memory.rb, line 54 def self::node_properties return USED_PROPERTIES end
Class run
creates a new instance and immediately runs it.
# File lib/arborist/monitor/snmp/memory.rb, line 62 def self::run( nodes ) return new.run( nodes ) end
Public Instance Methods
Perform the monitoring checks.
Arborist::Monitor::SNMP#run
# File lib/arborist/monitor/snmp/memory.rb, line 69 def run( nodes ) super do |host, snmp| self.gather_memory( host, snmp ) end end
Protected Instance Methods
Format usage and available amount, given an OID hash.
# File lib/arborist/monitor/snmp/memory.rb, line 148 def calc_memory( snmp, oids ) info = { usage: 0, available: 0 } avail = snmp.get( oid: oids[:avail] ).to_f total = snmp.get( oid: oids[:total] ).to_f used = total - avail return info if avail.zero? info[ :usage ] = (( used / total ) * 100 ).round( 2 ) info[ :available ] = (( total - used ) / 1024 ).round( 2 ) return info end
Format usage and available amount for windows.
# File lib/arborist/monitor/snmp/memory.rb, line 164 def calc_windows_memory( snmp, idx) info = { usage: 0, available: 0 } return info unless idx units = snmp.get( oid: MEMORY[:windows][:units] + ".#{idx}" ) total = snmp.get( oid: MEMORY[:windows][:total] + ".#{idx}" ).to_f * units used = snmp.get( oid: MEMORY[:windows][:used] + ".#{idx}" ).to_f * units info[ :usage ] = (( used / total ) * 100 ).round( 2 ) info[ :available ] = (( total - used ) / 1024 / 1024 ).round( 2 ) return info end
Collect available memory information for host
from an existing (and open) snmp
connection.
# File lib/arborist/monitor/snmp/memory.rb, line 83 def gather_memory( host, snmp ) info = self.system =~ /windows\s+/i ? self.get_windows( snmp ) : self.get_mem( snmp ) config = self.identifiers[ host ].last['config'] || {} physical_warn_at = config[ 'physical_warn_at' ] || self.class.physical_warn_at swap_warn_at = config[ 'swap_warn_at' ] || self.class.swap_warn_at self.log.debug "Memory data on %s: %p" % [ host, info ] memory, swap = info[:memory], info[:swap] self.results[ host ] = { memory: memory, swap: swap } memusage = memory[ :usage ].to_i if physical_warn_at && memusage >= physical_warn_at self.results[ host ][ :warning ] = "%0.1f memory utilization exceeds %0.1f percent" % [ memusage, physical_warn_at ] end swapusage = swap[ :usage ].to_i if swapusage >= swap_warn_at self.results[ host ][ :warning ] = "%0.1f swap utilization exceeds %0.1f percent" % [ swapusage, swap_warn_at ] end end
Return a hash of usage percentage in use, and free mem in megs.
# File lib/arborist/monitor/snmp/memory.rb, line 115 def get_mem( snmp ) info = {} info[ :memory ] = self.calc_memory( snmp, MEMORY ) info[ :swap ] = self.calc_memory( snmp, SWAP ) return info end
Windows appends virtual and physical memory onto the last two items of the storage iterator, because that made sense in someone's mind. Walk the whole oid tree, and get the values we're after, return a hash of usage percentage in use and free mem in megs.
# File lib/arborist/monitor/snmp/memory.rb, line 129 def get_windows( snmp ) info = { memory: {}, swap: {} } mem_idx, swap_idx = nil snmp.walk( oid: MEMORY[:windows][:label] ).each_with_index do |(_, val), i| mem_idx = i + 1 if val =~ /physical memory/i swap_idx = i + 1 if val =~ /virtual memory/i end return info unless mem_idx info[ :memory ] = self.calc_windows_memory( snmp, mem_idx ) info[ :swap ] = self.calc_windows_memory( snmp, swap_idx ) return info end