#!/usr/bin/perl
use strict;

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 Physical Security type for
the status of the system enclosure intrusion sensor.

This has only been tested/developed with the Dell 2950 servers.\n";

# 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 "Physical Security" 2>/dev/null | grep -v "Authentication"`) {
	
	# Check for authentication type error, and ignore it if it's there.
	
	my @status = split(/\|\s/s,$result);
	
	chomp $status[4];
	
	# XXX Debug lines XXX
	#print "Raw Status: $result\n";
	#print "My Status = \"$status[4]\"\n";
	
	if ($status[4] =~ m/^\Z/g){
		print "Enclosure secure. | Stat=10\n";
		exit $ERRORS{'OK'};
	} else {
		print "WARNING: Intrusion sensor active, enclosure open. | Stat=0\n";
		exit $ERRORS{'WARNING'};
	}
} else {
	die $usage;
}
