lib/arborist/monitor/snmp/process.rb
author Mahlon E. Smith <mahlon@laika.com>
Wed, 04 Apr 2018 13:25:00 -0700
changeset 12 17400a52624b
parent 8 e0b7c95a154f
child 14 d5cb8bd33170
permissions -rw-r--r--
Added tag v0.4.0 for changeset 9187008b0989
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     1
# -*- ruby -*-
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     2
# vim: set noet nosta sw=4 ts=4 :
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     3
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     4
require 'arborist/monitor/snmp' unless defined?( Arborist::Monitor::SNMP )
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     5
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     6
# SNMP running process checks.
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     7
#
8
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
     8
# This only checks running userland processes.
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
     9
#
4
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    10
class Arborist::Monitor::SNMP::Process
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    11
	include Arborist::Monitor::SNMP
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    12
8
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    13
	extend Configurability, Loggability
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    14
	log_to :arborist_snmp
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    15
4
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    16
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    17
	# OIDS for discovering running processes.
8
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    18
	# Of course, Windows does it slightly differently.
4
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    19
	#
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    20
	PROCESS = {
8
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    21
		netsnmp: {
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    22
			list: '1.3.6.1.2.1.25.4.2.1.4',
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    23
			args: '1.3.6.1.2.1.25.4.2.1.5'
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    24
		},
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    25
		windows: {
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    26
			list: '1.3.6.1.2.1.25.4.2.1.2',
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    27
			path: '1.3.6.1.2.1.25.4.2.1.4',
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    28
			args: '1.3.6.1.2.1.25.4.2.1.5'
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    29
		}
4
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    30
	}
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    31
8
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    32
4
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    33
	# Global defaults for instances of this monitor
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    34
	#
8
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    35
	configurability( 'arborist.snmp.processes' ) do
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    36
		# Default list of processes to check for
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    37
		setting :check, default: [] do |val|
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    38
			Array( val )
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    39
		end
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    40
	end
4
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    41
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    42
8
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    43
	### Return the properties used by this monitor.
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    44
	###
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    45
	def self::node_properties
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    46
		return USED_PROPERTIES
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    47
	end
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    48
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    49
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    50
	### Class #run creates a new instance and immediately runs it.
4
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    51
	###
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    52
	def self::run( nodes )
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    53
		return new.run( nodes )
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    54
	end
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    55
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    56
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    57
	### Perform the monitoring checks.
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    58
	###
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    59
	def run( nodes )
8
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    60
		super do |host, snmp|
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    61
			self.gather_processlist( host, snmp )
4
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    62
		end
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    63
	end
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    64
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    65
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    66
	#########
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    67
	protected
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    68
	#########
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    69
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    70
	### Collect running processes on +host+ from an existing (and open)
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    71
	#### +snmp+ connection.
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    72
	###
8
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    73
	def gather_processlist( host, snmp )
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    74
		config = self.identifiers[ host ].last || {}
4
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    75
		errors = []
8
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    76
		procs  = self.system =~ /windows\s+/i ? self.get_windows( snmp ) : self.get_procs( snmp )
4
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    77
8
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    78
		self.log.debug "Running processes for host: %s: %p" % [ host, procs ]
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    79
		self.results[ host ] = { count: procs.size }
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    80
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    81
		# Check against what is running.
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    82
		#
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    83
		Array( config['processes'] || self.class.check ).each do |process|
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    84
			process_r = Regexp.new( process )
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    85
			found = procs.find{|p| p.match(process_r) }
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    86
			errors << "'%s' is not running" % [ process ] unless found
4
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    87
		end
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    88
8
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    89
		self.results[ host ][ :error ] = errors.join( ', ' ) unless errors.empty?
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    90
	end
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    91
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    92
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    93
	### Parse OIDS and return an Array of running processes.
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    94
	### Windows specific behaviors.
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    95
	###
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    96
	def get_windows( snmp )
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    97
		oids = [ PROCESS[:windows][:path], PROCESS[:windows][:list], PROCESS[:windows][:args] ]
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    98
		return snmp.walk( oids ).each_slice( 3 ). each_with_object( [] ) do |vals, acc|
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    99
			path, process, args = vals[0][1], vals[1][1], vals[2][1]
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   100
			next if path.empty?
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   101
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   102
			process = "%s%s" % [ path, process ]
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   103
			process << " %s" % [ args ] unless args.empty?
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   104
			acc << process
4
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   105
		end
8
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   106
	end
4
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   107
8
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   108
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   109
	### Parse OIDS and return an Array of running processes.
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   110
	###
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   111
	def get_procs( snmp )
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   112
		oids = [ PROCESS[:netsnmp][:list], PROCESS[:netsnmp][:args] ]
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   113
		return snmp.walk( oids ).each_slice( 2 ).each_with_object( [] ) do |vals, acc|
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   114
			process, args = vals[0][1], vals[1][1]
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   115
			next if process.empty?
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   116
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   117
			process << " %s" % [ args ] unless args.empty?
e0b7c95a154f Refactor for real world usage and latest Arborist behaviors.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   118
			acc << process
4
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   119
		end
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   120
	end
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   121
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   122
end # class Arborist::Monitor::SNMP::Process
e6eb11b1e00d Refactor. Move all SNMP "sections" to their own classes.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   123