UPSes that have not yet been on battery apparently do not report 'est_minutes_remaining'
authorKatelyn Schiesser <kschiesser@laika.com>
Thu, 18 Apr 2019 12:50:45 -0700
changeset 23 a363d4d5a895
parent 22 d36032444b99
child 24 0224c1cd2094
UPSes that have not yet been on battery apparently do not report 'est_minutes_remaining'
lib/arborist/monitor/snmp/ups/battery.rb
--- a/lib/arborist/monitor/snmp/ups/battery.rb	Thu Apr 18 12:11:01 2019 -0700
+++ b/lib/arborist/monitor/snmp/ups/battery.rb	Thu Apr 18 12:50:45 2019 -0700
@@ -80,9 +80,9 @@
 
 		# basic info that's always available
 		info[ :status ] = snmp.get( oid: OIDS[:battery_status] )
-		info[ :capacity ] = snmp.get( oid: OIDS[:est_charge_remaining] )
-		info[ :temperature ] = snmp.get( oid: OIDS[:battery_temperature] )
-		info[ :minutes_remaining ]  = snmp.get( oid: OIDS[:est_minutes_remaining] )
+		info[ :capacity ] = snmp.get( oid: OIDS[:est_charge_remaining] ) rescue nil
+		info[ :temperature ] = snmp.get( oid: OIDS[:battery_temperature] ) rescue nil
+		info[ :minutes_remaining ]  = snmp.get( oid: OIDS[:est_minutes_remaining] ) rescue nil
 
 		# don't report voltage if the UPS doesn't
 		voltage = snmp.get( oid: OIDS[:battery_voltage] ) rescue nil
@@ -96,7 +96,7 @@
 		info[ :seconds_on_battery ] = snmp.get( oid: OIDS[:seconds_on_battery] ) rescue 0
 		info[ :in_use ] = ( info[ :seconds_on_battery ] != 0 )
 
-		return { battery: info }
+		return { battery: info.compact }
 	end
 
 	### Parse SNMP-provided information and alert based on thresholds.
@@ -115,17 +115,17 @@
 		warnings	= []
 
 		if in_use
-			mins = info.dig( :battery, :minutes_remaining )
+			mins = info.dig( :battery, :minutes_remaining ) || "(unknown)"
 			warnings << "UPS on battery - %s minute(s) remaning." % [ mins ]
 		end
 
 		warnings << BATTERY_STATUS[ status ] if status != 2
 
 		warnings << "Battery remaining capacity %0.1f%% less than %0.1f percent" %
-			[ capacity, cap_warn ] if capacity <= cap_warn
+			[ capacity, cap_warn ] if capacity && capacity <= cap_warn
 
 		warnings << "Battery temperature %dC greater than %dC" %
-			[ temperature, temp_warn ] if temperature >= temp_warn
+			[ temperature, temp_warn ] if temperature && temperature >= temp_warn
 
 		info[ :warning ] = warnings.join( "\n" ) unless warnings.empty?
 		self.results[ host ] = info