lib/arborist/monitor/snmp/cpu.rb
changeset 14 d5cb8bd33170
parent 8 e0b7c95a154f
child 19 77084121952b
equal deleted inserted replaced
13:6723f3b07536 14:d5cb8bd33170
    64 
    64 
    65 	#########
    65 	#########
    66 	protected
    66 	protected
    67 	#########
    67 	#########
    68 
    68 
    69 	### Return system CPU data.
       
    70 	###
       
    71 	def cpu( snmp )
       
    72 		return snmp.walk( OIDS[:cpu] )
       
    73 	end
       
    74 
       
    75 
       
    76 	### Find load data, add additional niceties for reporting.
    69 	### Find load data, add additional niceties for reporting.
    77 	###
    70 	###
    78 	def format_load( snmp )
    71 	def format_load( snmp )
    79 		info = { cpu: {}, load: {} }
    72 		info = { cpu: {}, load: {} }
    80 		cpus = self.cpu( snmp )
    73 		cpus = snmp.walk( oid: OIDS[:cpu] ).each_with_object( [] ) do |(_, value), acc|
       
    74 			acc << value
       
    75 		end
    81 
    76 
    82 		info[ :cpu ][ :count ] = cpus.size
    77 		info[ :cpu ][ :count ] = cpus.size
    83 
    78 
    84 		# Windows SNMP doesn't have a concept of "load" over time,
    79 		# Windows SNMP doesn't have a concept of "load" over time,
    85 		# so we have to just use the current averaged CPU usage.
    80 		# so we have to just use the current averaged CPU usage.
    89 		# it's really just how much of the CPU is used at the time of
    84 		# it's really just how much of the CPU is used at the time of
    90 		# the monitor run, along with liberal use of the Observer "only
    85 		# the monitor run, along with liberal use of the Observer "only
    91 		# alert after X events" pragmas.
    86 		# alert after X events" pragmas.
    92 		#
    87 		#
    93 		if self.system =~ /windows\s+/i
    88 		if self.system =~ /windows\s+/i
    94 			info[ :cpu ][ :usage ] = cpus.values.inject( :+ ).to_f / cpus.size
    89 			info[ :cpu ][ :usage ] = cpus.inject( :+ ).to_f / cpus.size
    95 			info[ :message ] = "System is %0.1f%% in use." % [ info[ :cpu ][ :usage ] ]
    90 			info[ :message ] = "System is %0.1f%% in use." % [ info[ :cpu ][ :usage ] ]
       
    91 
    96 
    92 
    97 		# UCDavis stuff is better for alerting only after there has been
    93 		# UCDavis stuff is better for alerting only after there has been
    98 		# an extended load event.  Use the 5 minute average to avoid
    94 		# an extended load event.  Use the 5 minute average to avoid
    99 		# state changes on transient spikes.
    95 		# state changes on transient spikes.
   100 		#
    96 		#
   101 		else
    97 		else
   102 			snmp.walk( OIDS[:load] ).each_with_index do |(_, value), idx|
    98 			snmp.walk( oid: OIDS[:load] ).each_with_index do |(_, value), idx|
   103 				next unless LOADKEYS[ idx + 1 ]
    99 				next unless LOADKEYS[ idx + 1 ]
   104 				info[ :load ][ LOADKEYS[idx + 1] ] = value.to_f
   100 				info[ :load ][ LOADKEYS[idx + 1] ] = value.to_f
   105 			end
   101 			end
   106 
   102 
   107 			percentage = (( ( info[:load][ :load5 ] / cpus.size ) - 1 ) * 100 ).round( 1 )
   103 			percentage = (( ( info[:load][ :load5 ] / cpus.size) - 1 ) * 100 ).round( 1 )
   108 
   104 
   109 			if percentage < 0
   105 			if percentage < 0
   110 				info[ :message ] = "System is %0.1f%% idle." % [ percentage.abs ]
   106 				info[ :message ] = "System is %0.1f%% idle." % [ percentage.abs ]
   111 				info[ :cpu ][ :usage ] = percentage + 100
   107 				info[ :cpu ][ :usage ] = percentage + 100
   112 			else
   108 			else