#!/usr/bin/perl
use strict;
use warnings;
use POSIX ();

my ($sysname, $nodename, $release, $version, $machine);
($sysname, $nodename, $release, $version, $machine) = POSIX::uname();

if ($sysname eq "FreeBSD") {
        use lib "/usr/local/libexec/nagios";
	} else {
	        use lib "/usr/local/nagios/libexec";
		}

use utils qw(%ERRORS);

my $usage = "
Usage: $0 host_addr ipmi_user ipmi_pass

Connects to a Dell BMC and parses the Voltage token for 
problem voltage readings.

Note: There are no actual voltage values given, simply go/no-go
from the BMC.";

# Gets output of ipmi command, this is funny with IPMI v2, we *sometimes* get an Authentication type error.
if (my $result = `/usr/local/bin/ipmitool -I lan -H $ARGV[0] -U $ARGV[1] -P $ARGV[2] sdr type "Voltage" | grep -v "Authentication"`) {

my $status_text = "";
my $err_lvl = 0;
my $disabled = 0;
my $status;

foreach my $line (split /\n/s, $result){
	if (my @fields = split(/\s*\|\s/,$line)){
		chomp($status = $fields[4]);
		
		if ($status =~ m/^State Asserted\Z/g){
			$status_text .= "$fields[0]:ERR! ";
			$err_lvl++;
		} elsif ($status =~ m/^Disabled\Z/g){
			$disabled++;
		} elsif ($status =~ m/^State Deasserted\Z/g){
		} else {
			$status_text .= "$fields[0]:($status) ";
			$err_lvl++;
		}
		
	}
	
}

if ($err_lvl > 0) {
	print "CRITICAL: $status_text\b, $disabled volt sensors disabled/unused.\n";
	exit $ERRORS{'CRITICAL'}
} else {
	print "NORMAL: Voltages OK, $disabled volt sensors disabled/unused.\n";
	exit $ERRORS{'OK'}
}
} else {
	die $usage;
}

