lib/arborist/monitor/snmp/ups/battery.rb
author Mahlon E. Smith <mahlon@martini.nu>
Sat, 01 Aug 2020 14:17:50 -0700
changeset 30 4fe00f5f71d9
parent 26 54f2f57cc0b0
permissions -rw-r--r--
Added tag v0.7.0 for changeset 40bcd1565627
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
     1
# -*- ruby -*-
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
     2
# vim: set noet nosta sw=4 ts=4 :
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
     3
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
     4
require 'arborist/monitor/snmp/ups' unless defined?( Arborist::Monitor::SNMP::UPS )
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
     5
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
     6
# Checks for UPS battery health.
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
     7
#
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
     8
# Checks the available battery percentage, if the UPS is on battery,
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
     9
# and the temperature of the battery.
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    10
#
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    11
class Arborist::Monitor::SNMP::UPS::Battery
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    12
	include Arborist::Monitor::SNMP
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    13
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    14
	extend Configurability, Loggability
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    15
	log_to :arborist_snmp
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    16
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    17
	# OIDS for discovering ups status.
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    18
	#
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    19
	OIDS = {
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    20
		battery_status:        '.1.3.6.1.2.1.33.1.2.1.0', # 1 - unk, 2 - normal, 3 - low, 4 - depleted
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    21
		seconds_on_battery:    '.1.3.6.1.2.1.33.1.2.2.0',
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    22
		est_minutes_remaining: '.1.3.6.1.2.1.33.1.2.3.0',
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    23
		est_charge_remaining:  '.1.3.6.1.2.1.33.1.2.4.0', # in percent
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    24
		battery_voltage:       '.1.3.6.1.2.1.33.1.2.5.0', # in 0.1v DC
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    25
		battery_current:       '.1.3.6.1.2.1.33.1.2.6.0', # in 0.1a DC
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    26
		battery_temperature:   '.1.3.6.1.2.1.33.1.2.7.0'  # in Celcius
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    27
	}
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    28
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    29
	# Human-readable translations for battery status OID.
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    30
	#
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    31
	BATTERY_STATUS = {
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    32
		1 => "Battery status is Unknown.",
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    33
		2 => "Battery is OK.",
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    34
		3 => "Battery is Low.",
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    35
		4 => "Battery is Depleted."
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    36
	}
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    37
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    38
	# Global defaults for instances of this monitor
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    39
	#
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    40
	configurability( 'arborist.snmp.ups.battery' ) do
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    41
		# What battery percentage qualifies as a warning
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    42
		setting :capacity_warn_at, default: 60
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    43
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    44
		# What battery temperature qualifies as a warning, in C
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    45
		setting :temperature_warn_at, default: 50
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    46
	end
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    47
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    48
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    49
	### Return the properties used by this monitor.
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    50
	###
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    51
	def self::node_properties
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    52
		return USED_PROPERTIES
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    53
	end
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    54
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    55
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    56
	### Class #run creates a new instance and immediately runs it.
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    57
	###
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    58
	def self::run( nodes )
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    59
		return new.run( nodes )
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    60
	end
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    61
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    62
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    63
	### Perform the monitoring checks.
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    64
	###
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    65
	def run( nodes )
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    66
		super do |host, snmp|
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    67
			self.check_battery( host, snmp )
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    68
		end
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    69
	end
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    70
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    71
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    72
	#########
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    73
	protected
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    74
	#########
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    75
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    76
	### Query SNMP and format information into a hash.
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    77
	###
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    78
	def format_battery( snmp )
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    79
		info = {}
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    80
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    81
		# basic info that's always available
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    82
		info[ :status ] = snmp.get( oid: OIDS[:battery_status] )
23
a363d4d5a895 UPSes that have not yet been on battery apparently do not report 'est_minutes_remaining'
Katelyn Schiesser <kschiesser@laika.com>
parents: 22
diff changeset
    83
		info[ :capacity ] = snmp.get( oid: OIDS[:est_charge_remaining] ) rescue nil
a363d4d5a895 UPSes that have not yet been on battery apparently do not report 'est_minutes_remaining'
Katelyn Schiesser <kschiesser@laika.com>
parents: 22
diff changeset
    84
		info[ :temperature ] = snmp.get( oid: OIDS[:battery_temperature] ) rescue nil
a363d4d5a895 UPSes that have not yet been on battery apparently do not report 'est_minutes_remaining'
Katelyn Schiesser <kschiesser@laika.com>
parents: 22
diff changeset
    85
		info[ :minutes_remaining ]  = snmp.get( oid: OIDS[:est_minutes_remaining] ) rescue nil
20
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    86
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    87
		# don't report voltage if the UPS doesn't
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    88
		voltage = snmp.get( oid: OIDS[:battery_voltage] ) rescue nil
22
d36032444b99 remove nil predicate
Katelyn Schiesser <kschiesser@laika.com>
parents: 20
diff changeset
    89
		info[ :voltage ] = voltage / 10 if voltage
20
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    90
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    91
		# don't report current if the UPS doesn't
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    92
		current = snmp.get( oid: OIDS[:battery_current] ) rescue nil
22
d36032444b99 remove nil predicate
Katelyn Schiesser <kschiesser@laika.com>
parents: 20
diff changeset
    93
		info[ :current ] = current/10 if current
20
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    94
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    95
		# see if we are on battery
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    96
		info[ :seconds_on_battery ] = snmp.get( oid: OIDS[:seconds_on_battery] ) rescue 0
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    97
		info[ :in_use ] = ( info[ :seconds_on_battery ] != 0 )
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
    98
23
a363d4d5a895 UPSes that have not yet been on battery apparently do not report 'est_minutes_remaining'
Katelyn Schiesser <kschiesser@laika.com>
parents: 22
diff changeset
    99
		return { battery: info.compact }
20
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   100
	end
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   101
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   102
	### Parse SNMP-provided information and alert based on thresholds.
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   103
	###
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   104
	def check_battery( host, snmp )
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   105
		info = self.format_battery( snmp )
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   106
26
54f2f57cc0b0 Automatically clear stale mounts from node properties during disk checks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 23
diff changeset
   107
		config    = self.identifiers[ host ].last['config'] || {}
20
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   108
		cap_warn  = config[ 'capacity_warn_at' ] || self.class.capacity_warn_at
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   109
		temp_warn = config[ 'temperature_warn_at' ] || self.class.temperature_warn_at
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   110
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   111
		in_use      = info.dig( :battery, :in_use )
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   112
		status      = info.dig( :battery, :status )
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   113
		capacity    = info.dig( :battery, :capacity )
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   114
		temperature = info.dig( :battery, :temperature )
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   115
		warnings	= []
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   116
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   117
		if in_use
23
a363d4d5a895 UPSes that have not yet been on battery apparently do not report 'est_minutes_remaining'
Katelyn Schiesser <kschiesser@laika.com>
parents: 22
diff changeset
   118
			mins = info.dig( :battery, :minutes_remaining ) || "(unknown)"
20
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   119
			warnings << "UPS on battery - %s minute(s) remaning." % [ mins ]
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   120
		end
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   121
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   122
		warnings << BATTERY_STATUS[ status ] if status != 2
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   123
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   124
		warnings << "Battery remaining capacity %0.1f%% less than %0.1f percent" %
23
a363d4d5a895 UPSes that have not yet been on battery apparently do not report 'est_minutes_remaining'
Katelyn Schiesser <kschiesser@laika.com>
parents: 22
diff changeset
   125
			[ capacity, cap_warn ] if capacity && capacity <= cap_warn
20
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   126
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   127
		warnings << "Battery temperature %dC greater than %dC" %
23
a363d4d5a895 UPSes that have not yet been on battery apparently do not report 'est_minutes_remaining'
Katelyn Schiesser <kschiesser@laika.com>
parents: 22
diff changeset
   128
			[ temperature, temp_warn ] if temperature && temperature >= temp_warn
20
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   129
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   130
		info[ :warning ] = warnings.join( "\n" ) unless warnings.empty?
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   131
		self.results[ host ] = info
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   132
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   133
	end
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   134
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   135
end # class Arborist::Monitor::UPS::Battery
00a38d493f2c add UPS monitoring capability
Katelyn Schiesser <kschiesser@laika.com>
parents:
diff changeset
   136