lib/arborist/monitor/snmp/process.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 running process checks.
       
     7 #
       
     8 class Arborist::Monitor::SNMP::Process
       
     9 	include Arborist::Monitor::SNMP
       
    10 
       
    11 	extend Loggability
       
    12 	log_to :arborist
       
    13 
       
    14 	# OIDS for discovering running processes.
       
    15 	#
       
    16 	PROCESS = {
       
    17 		 list: '1.3.6.1.2.1.25.4.2.1.4',
       
    18 		 args: '1.3.6.1.2.1.25.4.2.1.5'
       
    19 	}
       
    20 
       
    21 	# Global defaults for instances of this monitor
       
    22 	#
       
    23 	DEFAULT_OPTIONS = {
       
    24 		processes: [] # list of procs to match
       
    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 		%i[ processes ].each do |opt|
       
    41 			options[ opt ] = Array( options[opt] )
       
    42 		end
       
    43 
       
    44 		options.each do |name, value|
       
    45 			self.public_send( "#{name.to_s}=", value )
       
    46 		end
       
    47 	end
       
    48 
       
    49 	# Set an error if processes in this array aren't running.
       
    50 	attr_accessor :processes
       
    51 
       
    52 
       
    53 	### Perform the monitoring checks.
       
    54 	###
       
    55 	def run( nodes )
       
    56 		super do |snmp, host|
       
    57 			self.gather_processlist( snmp, host )
       
    58 		end
       
    59 	end
       
    60 
       
    61 
       
    62 	#########
       
    63 	protected
       
    64 	#########
       
    65 
       
    66 	### Collect running processes on +host+ from an existing (and open)
       
    67 	#### +snmp+ connection.
       
    68 	###
       
    69 	def gather_processlist( snmp, host )
       
    70 		self.log.debug "Getting running process list for %s" % [ host ]
       
    71 		config = @identifiers[ host ].last || {}
       
    72 		procs  = []
       
    73 		errors = []
       
    74 
       
    75 		snmp.walk([ PROCESS[:list], PROCESS[:args] ]) do |list|
       
    76 			process = list[0].value.to_s
       
    77 			args    = list[1].value.to_s
       
    78 			procs << "%s %s " % [ process, args ]
       
    79 		end
       
    80 
       
    81 		# Check against the running stuff, setting an error if
       
    82 		# one isn't found.
       
    83 		#
       
    84 		Array( config['processes'] || self.processes ).each do |process|
       
    85 			process_r = Regexp.new( process )
       
    86 			found = procs.find{|p| p.match(process_r) }
       
    87 			errors << "Process '%s' is not running" % [ process, host ] unless found
       
    88 		end
       
    89 
       
    90 		self.log.debug "  %d running processes" % [ procs.length ]
       
    91 		if errors.empty?
       
    92 			@results[ host ] = {}
       
    93 		else
       
    94 			@results[ host ] = { error: errors.join( ', ' ) }
       
    95 		end
       
    96 	end
       
    97 
       
    98 end # class Arborist::Monitor::SNMP::Process
       
    99