#!/usr/bin/perl -w
#
# check_hddtemp_socket.pl
#
# v0.2 20.06.2013 Benjamin Alfery (benjamin@alfery.org)
#
# Summary:
# The Plugin checks the (SMART-)Disktemperature using the hddtool-Daemon.
# Be sure to have the hddtemp-Daemon installed, configured and running.
#
# Changelog/History:
# Version 0.2 (20.06.2013): Bugfix: Fixed issue when having more than one Harddisk
# Version 0.1 (01.07.2012): Initial Version
#
#

use strict;

use Nagios::Plugin;
use IO::Socket;

my $oPlugin = Nagios::Plugin->new(
  usage => "Usage: %s [-H <Host>] -D <device> [-p <port>] -w <warning> -c <critical> [-f]",
  blurb => "Get's the Temperature of a specified Harddisk using the hddtool-daemon.",
  extra => "Make sure you have the hddtemp-daemon installed, configured and running.",
  version => 'v0.2',
);

$oPlugin->add_arg(
  spec => 'host|H=s',
  help => 'Hostname or IP-Address the daemon listens on. Default: localhost',
  required => 0,
  default => 'localhost',
);

$oPlugin->add_arg(
  spec => 'device|D=s',
  help => 'Device(path) you want to messure. e.g. /dev/sda (must be an exact match)',
  required => 1,
);

$oPlugin->add_arg(
  spec => 'port|p=i',
  help => 'Port the hddtemp-daemon listens on. Default: 7634',
  required => 0,
  default => 7634,
);

$oPlugin->add_arg(
  spec => 'warning|w=i',
  help => 'Warning threshold of Temperature (Unit depending on Daemon-Configuartion).',
  required => 1,
);

$oPlugin->add_arg(
  spec => 'critical|c=i',
  help => 'Critical threshold of Temperature (Unit depending on Daemon-Configuartion).',
  required => 1,
);

$oPlugin->add_arg(
  spec => 'perfdata|f',
  help => 'Print Performancedata',
  required => 0,
);

$oPlugin->getopts;

my $oSocketConn = new IO::Socket::INET ( Proto => 'tcp', PeerAddr => $oPlugin->opts->host, PeerPort => $oPlugin->opts->port) || die("Could not connect to ".$oPlugin->opts->host." on Port ".$oPlugin->opts->port.": $@");

my @aSocketData;
my @aSocketData_tmp;

while(<$oSocketConn>)
{

        push(@aSocketData_tmp,$_);

}


$oSocketConn->shutdown(2);

for(my $i = 0; $i <= $#aSocketData_tmp; $i++)
{

	@aSocketData=split(/\|\|/,$aSocketData_tmp[$i]);

}

my ($sNull,$sDevicePath, $sDeviceModel, $sDeviceTemperature, $sDeviceUnit);

for(my $i = 0; $i <= $#aSocketData; $i++)
{

	if($aSocketData[$i] =~ m/^\|.*/)
	{
		$aSocketData[$i]=substr($aSocketData[$i],1,length($aSocketData[$i]));

	}

        ($sDevicePath, $sDeviceModel, $sDeviceTemperature, $sDeviceUnit) = split(/\|/,$aSocketData[$i]);

        my $sGivenDevice = $oPlugin->opts->device;

        if ($sDevicePath =~ m/^$sGivenDevice$/)
        {

                last;

        } else
        {

                $sDevicePath = "";
                $sDeviceModel = "";
                $sDeviceTemperature = "";
                $sDeviceUnit = "";

        }


}

if (!$sDeviceTemperature =~ m/^[0-9]+$/)
{

        $oPlugin->nagios_die("Temperature not available.\n");

}

if (!$sDeviceUnit =~ m/^(C|F)$/)
{

        $oPlugin->nagios_die("Unit not available.\n");

}

if ( $oPlugin->opts->perfdata )
{
        $oPlugin->add_perfdata( label => 'temp', value => $sDeviceTemperature, uom => $sDeviceUnit, warning => $oPlugin->opts->warning, critical => $oPlugin->opts->critical, min => 0,);

}

$oPlugin->nagios_exit( $oPlugin->check_threshold( check => $sDeviceTemperature, warning => $oPlugin->opts->warning, critical => $oPlugin->opts->critical ), "Temperature on Hard-Disk ".$oPlugin->opts->device." is ".$sDeviceTemperature.$sDeviceUnit);

