#!/usr/bin/perl

use strict;

use Net::SNMP;

exit if (!$ARGV[0]);

my $TIMEOUT = 30;
$SIG{ALRM} = sub { print "ERROR: No response\n"; exit 3; };
alarm($TIMEOUT);

my $hostip=$ARGV[0];

my ($session, $error) = Net::SNMP->session(
   -hostname  => shift || $hostip,
   -community => shift || 'public',
   -timeout   => 5,
   -port      => shift || 161 
);

if (!defined($session)) {
   printf("ERROR: %s.\n", $error);
   exit 1;
}

my $exttemp = '1.3.6.1.4.1.318.1.1.10.2.3.2.1.4.1';

my $result = $session->get_request(
   -varbindlist => [$exttemp]
);

if (!defined($result)) {
    ($session, $error) = Net::SNMP->session( -hostname  => $hostip, -community => 'public' );
    $result = $session->get_request( -varbindlist => [$exttemp] );
    }

if (!defined($result)) {
   printf("ERROR: %s.\n", $session->error);
   $session->close;
   exit 2;
}

if ($result->{$exttemp} > 28) {
   printf("ERROR Temperature of server-room: %s Degrees\n",$result->{$exttemp});
   $session->close;
   exit 2;
}

printf("OK Temperature of server-room %s Degrees\n", $result->{$exttemp});
$session->close;
exit 0;
