#!/usr/bin/perl

##################################################################################
##################################################################################
######################  Made by Tytus Kurek in November 2012  ####################
##################################################################################
##################################################################################
####     This is a Nagios Plugin destined to check the input power supply     ####
####                     status and volrage of Dell PDU.                      ####
##################################################################################
##################################################################################

use strict;
use vars qw($community $IP);

use Getopt::Long;
use Pod::Usage;

# Subroutines execution

getParameters ();
checkPowerSupply ();

# Subroutines definition

sub checkPowerSupply ()	# Checks power supply of Dell PDU
{
	my $OID = 'enterprises.674.10903.200.2.200.140';
	my $alarmOID = $OID . '.1.0';
	my $voltageOID = $OID . '.2.0';
	my $version = '2c';

	my $command = "/usr/bin/snmpwalk -v $version -c $community $IP $OID 2>&1";
	my $result = `$command`;

	if ($result =~ m/^Timeout.*$/)
	{
		my $output = "UNKNOWN! No SNMP response from $IP.";
		my $code = 3;
		exitScript ($output, $code);
	}

	$command = "/usr/bin/snmpget -v $version -c $community $IP $alarmOID";
	$result = `$command`;
	$result =~ m/^SNMPv2-SMI::$alarmOID\s=\sINTEGER:\s(\d+)$/;
	my $alarm = $1;

	$command = "/usr/bin/snmpget -v $version -c $community $IP $voltageOID";
	$result = `$command`;
	$result =~ m/^SNMPv2-SMI::$voltageOID\s=\sINTEGER:\s(\d+)$/;
	my $voltage = $1;

	if (($alarm != 1) and ($voltage == 0))
	{
		my $output = "CRITICAL! Input power supply is down. Input voltage: $voltage.";
		my $code = 2;
		exitScript ($output, $code);
	}

	else
	{
		my $output = "OK! Input power supply is up. Input voltage: $voltage.";
		my $code = 0;
		exitScript ($output, $code);
	}
}

sub exitScript ()	# Exits the script with an appropriate message and code
{
	print "$_[0]\n";
	exit $_[1];
}

sub getParameters ()	# Obtains script parameters and prints help if needed
{
	my $help = '';

	GetOptions ('help|?' => \$help,
		    'C=s' => \$community,
		    'H=s' => \$IP)

	or pod2usage (1);
	pod2usage (1) if $help;
	pod2usage (1) if (($community eq '') || ($IP eq ''));
	pod2usage (1) if ($IP !~ m/^\d+\.\d+\.\d+\.\d+$/);

=head1 SYNOPSIS

check_dell_pdu.pl [options] (-help || -?)

=head1 OPTIONS

Mandatory:

-H	IP address of monitored Dell PDU

-C	SNMP community

=cut
}
