lib/arborist/monitor/snmp.rb
author Mahlon E. Smith <mahlon@laika.com>
Mon, 05 Sep 2016 20:59:01 -0700
changeset 2 3d395894a3cf
parent 1 8446f55f7e58
child 3 d46ca2b52efe
permissions -rw-r--r--
Added tag v0.2.0 for changeset 8446f55f7e58
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     1
# -*- ruby -*-
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     2
# vim: set noet nosta sw=4 ts=4 :
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     3
#encoding: utf-8
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     4
#
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     5
# SNMP checks for Arborist.  Requires an SNMP agent to be installed
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     6
# on target machine, and the various "pieces" enabled.  For your platform.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     7
#
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     8
# For example, for disk monitoring with Net-SNMP, you'll want to set
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     9
# 'includeAllDisks' in the snmpd.conf. bsnmpd on FreeBSD benefits from
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    10
# the 'bsnmp-ucd' package.  Etc.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    11
#
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    12
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    13
require 'loggability'
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    14
require 'arborist/monitor' unless defined?( Arborist::Monitor )
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    15
require 'snmp'
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    16
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    17
using Arborist::TimeRefinements
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    18
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    19
# SNMP specific monitors and monitor logic.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    20
#
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    21
class Arborist::Monitor::SNMP
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    22
	extend Loggability
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    23
	log_to :arborist
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    24
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    25
	# The version of this library.
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    26
	VERSION = '0.2.0'
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    27
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    28
	# "Modes" that this monitor understands.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    29
	VALID_MODES = %i[ disk load memory swap process ]
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    30
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    31
	# The OID that returns the system environment.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    32
	IDENTIFICATION_OID = '1.3.6.1.2.1.1.1.0'
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    33
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    34
	# For net-snmp systems, ignore mount types that match
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    35
	# this regular expression.  This includes null/union mounts
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    36
	# and NFS, currently.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    37
	STORAGE_IGNORE = %r{25.3.9.(?:2|14)$}
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    38
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    39
	# The OID that matches a local windows hard disk.  Anything else
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    40
	# is a remote (SMB) mount.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    41
	WINDOWS_DEVICE = '1.3.6.1.2.1.25.2.1.4'
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    42
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    43
	# OIDS required to pull disk information from net-snmp.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    44
	#
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    45
	STORAGE_NET_SNMP = [
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    46
		'1.3.6.1.4.1.2021.9.1.2', # paths
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    47
		'1.3.6.1.2.1.25.3.8.1.4', # types
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    48
		'1.3.6.1.4.1.2021.9.1.9'  # percents
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    49
	]
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    50
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    51
	# OIDS required to pull disk information from Windows.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    52
	#
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    53
	STORAGE_WINDOWS = [
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    54
		'1.3.6.1.2.1.25.2.3.1.2', # types
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    55
		'1.3.6.1.2.1.25.2.3.1.3', # paths
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    56
		'1.3.6.1.2.1.25.2.3.1.5', # totalsize
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    57
		'1.3.6.1.2.1.25.2.3.1.6'  # usedsize
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    58
	]
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    59
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    60
	# OIDS for discovering memory usage.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    61
	#
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    62
	MEMORY = {
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    63
		swap_total: '1.3.6.1.4.1.2021.4.3.0',
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    64
		swap_avail: '1.3.6.1.4.1.2021.4.4.0',
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    65
		mem_avail:  '1.3.6.1.4.1.2021.4.6.0'
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    66
	}
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    67
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    68
	# OIDS for discovering system load.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    69
	#
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    70
	LOAD = {
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    71
		five_min: '1.3.6.1.4.1.2021.10.1.3.2'
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    72
	}
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    73
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    74
	# OIDS for discovering running processes.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    75
	#
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    76
	PROCESS = {
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    77
		 list: '1.3.6.1.2.1.25.4.2.1.4',
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    78
		 args: '1.3.6.1.2.1.25.4.2.1.5'
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    79
	}
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    80
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    81
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    82
	# Global defaults for instances of this monitor
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    83
	#
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    84
	DEFAULT_OPTIONS = {
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    85
		timeout:          2,
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    86
		retries:          1,
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    87
		community:        'public',
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    88
		port:             161,
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    89
		storage_error_at: 95,    # in percent full
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    90
		storage_include:  [],    # if non-empty, only these paths are included in checks
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    91
		storage_exclude:  [],    # paths to exclude from checks
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    92
		load_error_at:    7,
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    93
		swap_error_at:    25,    # in percent remaining
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    94
		mem_error_at:     51200, # in kilobytes
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    95
		processes:        []     # list of procs to match
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    96
	}
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    97
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    98
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    99
	### This monitor is complex enough to require creating an instance from the caller.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   100
	### Provide a friendlier error message the class was provided to exec() directly.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   101
	###
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   102
	def self::run( nodes )
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   103
		self.log.error "Please use %s via an instance." % [ self.name ]
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   104
		return {}
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   105
	end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   106
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   107
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   108
	### Create a new instance of this monitor.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   109
	###
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   110
	def initialize( options=DEFAULT_OPTIONS )
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   111
		options = DEFAULT_OPTIONS.merge( options || {} )
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   112
		%i[ storage_include storage_exclude processes ].each do |opt|
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   113
			options[ opt ] = Array( options[opt] )
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   114
		end
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   115
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   116
		options.each do |name, value|
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   117
			self.public_send( "#{name.to_s}=", value )
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   118
		end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   119
	end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   120
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   121
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   122
	# The mode (section) that this SMMP instance should check.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   123
	# Must be a +VALID_MODES+ mode.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   124
	attr_reader :mode
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   125
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   126
	# Mapping of node addresses back to the node identifier.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   127
	attr_reader :identifiers
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   128
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   129
	# The results from the SNMP daemons, keyed by address.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   130
	attr_reader :results
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   131
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   132
	# A timeout in seconds if the SNMP server isn't responding.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   133
	attr_accessor :timeout
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   134
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   135
	# Retry with the timeout this many times.  Defaults to 1.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   136
	attr_accessor :retries
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   137
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   138
	# The SNMP UDP port, if running on non default.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   139
	attr_accessor :port
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   140
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   141
	# The community string to connect with.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   142
	attr_accessor :community
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   143
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   144
	# Set an error if mount points are above this percentage.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   145
	attr_accessor :storage_error_at
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   146
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   147
	# Only check these specific mount points.
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   148
	attr_accessor :storage_include
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   149
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   150
	# Exclude these mount points (array of paths) from checks.
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   151
	attr_accessor :storage_exclude
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   152
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   153
	# Set an error if the 5 minute load average exceeds this.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   154
	attr_accessor :load_error_at
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   155
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   156
	# Set an error if used swap exceeds this percentage.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   157
	attr_accessor :swap_error_at
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   158
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   159
	# Set an error if memory used is below this many kilobytes.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   160
	attr_accessor :mem_error_at
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   161
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   162
	# Set an error if processes in this array aren't running.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   163
	attr_accessor :processes
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   164
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   165
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   166
	### Set the SNMP mode, after validation.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   167
	###
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   168
	def mode=( mode )
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   169
		unless VALID_MODES.include?( mode.to_sym )
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   170
			self.log.error "Unknown SNMP mode: %s" % [ mode ]
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   171
			return nil
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   172
		end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   173
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   174
		@mode    = mode.to_sym
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   175
		@results = {}
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   176
	end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   177
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   178
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   179
	### Perform the monitoring checks.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   180
	###
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   181
	def run( nodes )
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   182
		self.log.debug "Got nodes to SNMP check: %p" % [ nodes ]
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   183
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   184
		# Sanity check.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   185
		#
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   186
		unless self.mode
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   187
			self.log.error "You must set the 'mode' for the SNMP monitor. (%s)" % [ VALID_MODES.join( ', ' ) ]
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   188
			return {}
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   189
		end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   190
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   191
		# Create mapping of addresses back to node identifiers,
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   192
		# and retain any custom configs per node.
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   193
		#
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   194
		@identifiers = {}
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   195
		nodes.each_pair do |(identifier, props)|
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   196
			next unless props.key?( 'addresses' )
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   197
			address = props[ 'addresses' ].first
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   198
			self.identifiers[ address ] = [ identifier, props['config'] ]
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   199
		end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   200
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   201
		# Perform the work!
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   202
		#
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   203
		threads = []
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   204
		self.identifiers.keys.each do |host|
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   205
			thr = Thread.new do
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   206
				Thread.current.abort_on_exception = true
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   207
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   208
				config = self.identifiers[host].last || {}
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   209
				opts = {
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   210
					host:      host,
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   211
					port:      config[ 'port' ] || self.port,
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   212
					community: config[ 'community' ] || self.community,
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   213
					timeout:   config[ 'timeout' ] || self.timeout,
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   214
					retries:   config[ 'retries' ] || self.retries
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   215
				}
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   216
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   217
				begin
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   218
					SNMP::Manager.open( opts ) do |snmp|
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   219
						case self.mode
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   220
						when :disk
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   221
							self.gather_disks( snmp, host )
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   222
						when :load
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   223
							self.gather_load( snmp, host )
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   224
						when :memory
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   225
							self.gather_free_memory( snmp, host )
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   226
						when :swap
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   227
							self.gather_swap( snmp, host )
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   228
						when :process
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   229
							self.gather_processlist( snmp, host )
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   230
						end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   231
					end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   232
				rescue SNMP::RequestTimeout
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   233
					self.results[ host ] = {
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   234
						error: "Host is not responding to SNMP requests."
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   235
					}
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   236
				rescue StandardError => err
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   237
					self.results[ host ] = {
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   238
						error: "Network is not accessible. (%s: %s)" % [ err.class.name, err.message ]
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   239
					}
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   240
				end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   241
			end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   242
			threads << thr
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   243
		end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   244
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   245
		# Wait for thread completion
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   246
		threads.map( &:join )
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   247
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   248
		# Map everything back to identifier -> attribute(s), and send to the manager.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   249
		#
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   250
		reply = self.results.each_with_object({}) do |(address, results), hash|
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   251
			identifier = self.identifiers[ address ].first
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   252
			hash[ identifier ] = results
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   253
		end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   254
		self.log.debug "Sending to manager: %p" % [ reply ]
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   255
		return reply
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   256
	end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   257
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   258
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   259
	#########
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   260
	protected
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   261
	#########
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   262
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   263
	### Collect the load information for +host+ from an existing
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   264
	### (and open) +snmp+ connection.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   265
	###
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   266
	def gather_load( snmp, host )
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   267
		self.log.debug "Getting system load for: %s" % [ host ]
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   268
		load5 = snmp.get( SNMP::ObjectId.new( LOAD[:five_min] ) ).varbind_list.first.value.to_f
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   269
		self.log.debug "  Load on %s: %0.2f" % [ host, load5 ]
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   270
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   271
		config = self.identifiers[ host ].last || {}
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   272
		error_at = config[ 'load_error_at' ] || self.load_error_at
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   273
		if load5 >= error_at
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   274
			self.results[ host ] = {
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   275
				error: "Load has exceeded %0.2f over a 5 minute average" % [ error_at ],
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   276
				load5: load5
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   277
			}
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   278
		else
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   279
			self.results[ host ] = { load5: load5 }
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   280
		end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   281
	end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   282
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   283
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   284
	### Collect available memory information for +host+ from an existing
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   285
	### (and open) +snmp+ connection.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   286
	###
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   287
	def gather_free_memory( snmp, host )
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   288
		self.log.debug "Getting available memory for: %s" % [ host ]
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   289
		mem_avail = snmp.get( SNMP::ObjectId.new( MEMORY[:mem_avail] ) ).varbind_list.first.value.to_f
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   290
		self.log.debug "  Available memory on %s: %0.2f" % [ host, mem_avail ]
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   291
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   292
		config = self.identifiers[ host ].last || {}
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   293
		error_at = config['mem_error_at'] || self.mem_error_at
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   294
		if mem_avail <= error_at
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   295
			self.results[ host ] = {
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   296
				error: "Available memory is under %0.1fMB" % [ error_at.to_f / 1024 ],
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   297
				available_memory: mem_avail
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   298
			}
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   299
		else
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   300
			self.results[ host ] = { available_memory: mem_avail }
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   301
		end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   302
	end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   303
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   304
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   305
	### Collect used swap information for +host+ from an existing (and
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   306
	### open) +snmp+ connection.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   307
	###
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   308
	def gather_swap( snmp, host )
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   309
		self.log.debug "Getting used swap for: %s" % [ host ]
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   310
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   311
		swap_total = snmp.get( SNMP::ObjectId.new(MEMORY[:swap_total]) ).varbind_list.first.value.to_f
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   312
		swap_avail = snmp.get( SNMP::ObjectId.new(MEMORY[:swap_avail]) ).varbind_list.first.value.to_f
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   313
		swap_used  = ( "%0.2f" % ((swap_avail / swap_total.to_f * 100 ) - 100).abs ).to_f
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   314
		self.log.debug "  Swap in use on %s: %0.2f" % [ host, swap_used ]
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   315
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   316
		config = self.identifiers[ host ].last || {}
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   317
		if swap_used >= ( config['swap_error_at'] || self.swap_error_at )
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   318
			self.results[ host ] = {
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   319
				error: "%0.2f%% swap in use" % [ swap_used ],
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   320
				swap_used: swap_used
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   321
			}
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   322
		else
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   323
			self.results[ host ] = { swap_used: swap_used }
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   324
		end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   325
	end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   326
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   327
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   328
	### Collect mount point usage for +host+ from an existing (and open)
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   329
	#### +snmp+ connection.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   330
	###
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   331
	def gather_disks( snmp, host )
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   332
		self.log.debug "Getting disk information for %s" % [ host ]
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   333
		errors  = []
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   334
		results = {}
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   335
		mounts  = self.get_disk_percentages( snmp )
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   336
		config  = self.identifiers[ host ].last || {}
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   337
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   338
		includes = config[ 'storage_include' ] || self.storage_include
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   339
		excludes = config[ 'storage_exclude' ] || self.storage_exclude
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   340
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   341
		mounts.each_pair do |path, percentage|
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   342
			next if excludes.include?( path )
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   343
			next if ! includes.empty? && ! includes.include?( path )
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   344
			if percentage >= ( config[ 'storage_error_at' ] || self.storage_error_at )
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   345
				errors << "%s at %d%% capacity" % [ path, percentage ]
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   346
			end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   347
		end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   348
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   349
		results[ :mounts ] = mounts
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   350
		results[ :error ] = errors.join( ', ' ) unless errors.empty?
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   351
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   352
		self.results[ host ] = results
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   353
	end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   354
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   355
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   356
	### Collect running processes on +host+ from an existing (and open)
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   357
	#### +snmp+ connection.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   358
	###
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   359
	def gather_processlist( snmp, host )
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   360
		self.log.debug "Getting running process list for %s" % [ host ]
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   361
		config = self.identifiers[ host ].last || {}
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   362
		procs  = []
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   363
		errors = []
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   364
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   365
		snmp.walk([ PROCESS[:list], PROCESS[:args] ]) do |list|
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   366
			process = list[0].value.to_s
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   367
			args    = list[1].value.to_s
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   368
			procs << "%s %s " % [ process, args ]
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   369
		end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   370
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   371
		# Check against the running stuff, setting an error if
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   372
		# one isn't found.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   373
		#
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   374
		Array( config['processes'] || self.processes ).each do |process|
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   375
			process_r = Regexp.new( process )
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   376
			found = procs.find{|p| p.match(process_r) }
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   377
			errors << "Process '%s' is not running" % [ process, host ] unless found
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   378
		end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   379
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   380
		self.log.debug "  %d running processes" % [ procs.length ]
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   381
		if errors.empty?
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   382
			self.results[ host ] = {}
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   383
		else
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   384
			self.results[ host ] = { error: errors.join( ', ' ) }
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   385
		end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   386
	end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   387
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   388
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   389
	### Given a SNMP object, return a hash of:
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   390
	###
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   391
	###    device path => percentage full
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   392
	###
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   393
	def get_disk_percentages( snmp )
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   394
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   395
		# Does this look like a windows system, or a net-snmp based one?
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   396
		system_type = snmp.get( SNMP::ObjectId.new( IDENTIFICATION_OID ) ).varbind_list.first.value
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   397
		disks = {}
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   398
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   399
		# Windows has it's own MIBs.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   400
		#
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   401
		if system_type =~ /windows/i
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   402
			snmp.walk( STORAGE_WINDOWS ) do |list|
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   403
				next unless list[0].value.to_s == WINDOWS_DEVICE
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   404
				disks[ list[1].value.to_s ] = ( list[3].value.to_f / list[2].value.to_f ) * 100
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   405
			end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   406
			return disks
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   407
		end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   408
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   409
		# Everything else.
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   410
		#
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   411
		snmp.walk( STORAGE_NET_SNMP ) do |list|
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   412
			mount = list[0].value.to_s
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   413
			next if mount == 'noSuchInstance'
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   414
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   415
			next if list[2].value.to_s == 'noSuchInstance'
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   416
			used = list[2].value.to_i
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   417
1
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   418
			unless list[1].value.to_s == 'noSuchInstance'
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   419
				typeoid = list[1].value.join('.').to_s
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   420
				next if typeoid =~ STORAGE_IGNORE
8446f55f7e58 Allow local node "config" to override SNMP instanced globals. Add mount point includes and excludes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   421
			end
0
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   422
			next if mount =~ /\/(?:dev|proc)$/
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   423
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   424
			disks[ mount ] = used
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   425
		end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   426
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   427
		return disks
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   428
	end
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   429
end # class Arborist::Monitor::SNMP
8547a1ce445e Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   430