#!/usr/bin/perl -w

=head1 NAME

Monitor CyberPower UPS Battery Status.


=head1 AUTHOR

Kai Boenke

=head1 LICENSE

Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)

=back


#####
# Enable SNMP-Discovery
###
=head1 MAGIC MARKERS
  #%# family=snmpauto
  #%# capabilities=snmpconf
=cut
if (defined $ARGV[0] and $ARGV[0] eq "snmpconf") {
	print "require 1.3.6.1.2.1.33.1.2.4.0\n";
	exit 0;
}


#####
# Initialize
###
use strict;
use Munin::Plugin::SNMP;
my $session	= Munin::Plugin::SNMP->session();


#####
# Declare OIDs
###
use constant oid_cps_battery_runtime	=> ".1.3.6.1.2.1.33.1.2.3.0";
use constant oid_cps_battery_charge	=> ".1.3.6.1.2.1.33.1.2.4.0";
use constant oid_cps_input_voltage	=> ".1.3.6.1.2.1.33.1.3.3.1.3.1";
use constant oid_cps_output_voltage	=> ".1.3.6.1.2.1.33.1.4.4.1.2.1";
use constant oid_cps_output_load	=> ".1.3.6.1.2.1.33.1.4.4.1.5.1";
use constant oid_cps_env_temp		=> ".1.3.6.1.4.1.3808.1.1.4.2.1.0";
use constant oid_cps_env_humidity	=> ".1.3.6.1.4.1.3808.1.1.4.3.1.0";


#####
# Config
###
if (defined $ARGV[0] and $ARGV[0] eq "config") {
	my ($host) = Munin::Plugin::SNMP->config_session();
	print "host_name $host\n" unless $host eq 'localhost';
	print "multigraph cyberpower_load
graph_title CyberPower UPS Status
graph_info This graph shows battery status information.
graph_category sensors
graph_vlabel %
graph_args --upper-limit 100 -l 0
graph_scale no
";
	print "load.label Total load
load.draw AREA
load.type GAUGE
load.min 0
load.max 100
";
	print "charge.label Battery charge
charge.draw LINE1
charge.type GAUGE
charge.min 0
charge.max 100
";

	print "multigraph cyberpower_runtime
graph_title CyberPower UPS Runtime
graph_info This graph shows expected runtime informatiom.
graph_category sensors
graph_vlabel minutes
";
	print "runtime.label Expected runtime
runtime.draw AREA
runtime.type GAUGE
";

	print "multigraph cyberpower_voltage
graph_title CyberPower UPS Voltages
graph_info This graph shows voltage information.
graph_category sensors
graph_vlabel V
";
	print "input.label Input voltage
input.draw LINE2
input.type GAUGE
";
	print "output.label Output voltage
output.draw LINE1
output.type GAUGE
";

	if(oidExists(oid_cps_env_temp) && oidExists(oid_cps_env_humidity)){
		print "multigraph cyberpower_environment
graph_title CyberPower UPS Environment
graph_info This graph shows environmental status information.
graph_category sensors
graph_vlabel F/%
";
		print "temp.label Temperature
temp.draw LINE2
temp.type GAUGE
";
		print "humidity.label Humidity
humidity.draw LINE1
humidity.type GAUGE
humidity.min 0
humidity.max 100
";
	}

	exit 0;
}


#####
# Get Values
###

print "multigraph cyberpower_load\n";
my $load	= $session->get_single(oid_cps_output_load);
my $charge	= $session->get_single(oid_cps_battery_charge);
if($load ne 'U'){
	print "load.value ", $load, "\n";
}
if($charge ne 'U'){
	print "charge.value ", $charge, "\n";
}

print "multigraph cyberpower_runtime\n";
my $runtime	= $session->get_single(oid_cps_battery_runtime);
if($runtime ne 'U'){
	print "runtime.value ", $runtime, "\n";
}

print "multigraph cyberpower_voltage\n";
my $input	= $session->get_single(oid_cps_input_voltage);
my $output	= $session->get_single(oid_cps_output_voltage);
if($input ne 'U'){
	print "input.value ", $input, "\n";
}
if($output ne 'U'){
	print "output.value ", $output, "\n";
}

if(oidExists(oid_cps_env_temp) && oidExists(oid_cps_env_humidity)){
	print "multigraph cyberpower_environment\n";
	my $temp	= $session->get_single(oid_cps_env_temp);
	my $humidity	= $session->get_single(oid_cps_env_humidity);
	if($temp ne 'U'){
		$temp /= 10;
		print "temp.value ", $temp, "\n";
	}
	if($humidity ne 'U'){
		print "humidity.value ", $humidity, "\n";
	}
}

#####
# Subroutines
###
sub oidExists {
	if(not defined $_[0]) {
		exit 0;
	}
	my $oid = $_[0];
	my $val = $session->get_single($oid);

	if(!length $val || $val eq 'noSuchInstance' || $val eq 'U'){
		return(0);
	}else{
		return(1);
	}
}
