#!/usr/bin/perl -w 

###################################
#
# written by Ahsan Md Sajid Khan
#      
# This script will return Mikrotik router's environment values

#Sample command,

#check_snmp_mikrotik_env.pl -H host -C 'COMMUNITY-STRING' -t #'type of monitoring items temp, power' -w WARNING  -c CRITCAL

#If '-t temp' then Warning and Critical threshold will work on #Internal Temperature.

#If '-t' power then Warning and Critical threshold will work #on Voltage

#check_snmp_mikrotik_env.pl -H 192.168.101.1 -C public -t #temp -w 40 -c 50
#check_snmp_mikrotik_env.pl -H 192.168.101.1 -C public -t #power -w 240 -c 260
# 
#
###################################

use strict;
use Net::SNMP;
use Getopt::Long;

use lib "/usr/lib/nagios/plugins/";
use utils qw($TIMEOUT %ERRORS);


my $BASE_OID = '1.3.6.1.4.1.14988.1.1.3';
my $STORAGE_NAME = '1.3.6.1.2.1.25.2.3.1.3';
my $STORAGE_SIZE = '1.3.6.1.2.1.25.2.3.1.5';
my $STORAGE_USAGE = '1.3.6.1.2.1.25.2.3.1.6';
my $ALLOCATION_FAILURE = '1.3.6.1.2.1.25.2.3.1.7';

my $returnstring = "";
my $perf_data = "";
my $returnvalue = $ERRORS{"OK"};


my $host = 	undef;
my $community = undef;
my $warning = undef;
my $critical = undef;
my $help = undef;
my $type = undef;

Getopt::Long::Configure ("bundling");
GetOptions(
	'h' => \$help,
	'help' => \$help,
	'c:s' => \$critical,
	'critical:s' => \$critical,
	'w:s' => \$warning,
	'warn:s' => \$warning,
	'H:s' => \$host,
	'host:s' => \$host,
	'C:s' => \$community,
	'community:s' => \$community,
        't:s' => \$type,
        'type:s'=> \$type
);

sub nonum
{
  my $num = shift;
  if ( $num =~ /^(\d+\.?\d*)|(^\.\d+)$/ ) { return 0 ;}
  return 1;
}
sub print_usage
{
    print "Usage: $0 -H <host> -C <COMMUNITY-STRING> -t <type temp, power> -w <WARNING>  -c <CRITCAL>\n";
}

# CHECKS
if ( defined($help) )
{
	print_usage();
	exit $ERRORS{"UNKNOWN"};
}
if ( !defined($host) )
{
	print "Need Host-Address!\n";
	print_usage();
	exit $ERRORS{"UNKNOWN"};
}
if ( !defined($community) )
{
	print "Need Community-String!\n";
	print_usage();
	exit $ERRORS{"UNKNOWN"};
}

if ( !defined($type) )
{
        print "Need type [Temperature -t temp or Power consumptionn -t power]!\n";
        print_usage();
        exit $ERRORS{"UNKNOWN"};
}



if (!defined($warning) || !defined($critical))
{
	print "Need Warning- and Critical-Info!\n";
	print_usage();
	exit $ERRORS{"UNKNOWN"};
}
$warning =~ s/\%//g; 
$critical =~ s/\%//g;
if ( nonum($warning) || nonum($critical))
{
	print "Only numerical Values for crit/warn allowed !\n";
	print_usage();
	exit $ERRORS{"UNKNOWN"}
}
if ($warning > $critical) 
{
	print "warning <= critical ! \n";
	print_usage();
	exit $ERRORS{"UNKNOWN"}
}

my ($session, $error) = Net::SNMP->session( -hostname  => $host, -version   => 2, -community => $community, -timeout   => $TIMEOUT);

if (!defined($session)) {
   printf("ERROR opening session: %s.\n", $error);
   exit $ERRORS{"CRITICAL"};
}


my $env_values = $session->get_table(-baseoid => $BASE_OID);
if (!defined($env_values))
{
   printf("ERROR: Description table : %s.\n", $session->error);
   $session->close;
   exit $ERRORS{"CRITICAL"};
}



   if ($type eq 'temp')
    {
          my $temp_internal = $$env_values{$BASE_OID.".10.0"};
          $temp_internal = $temp_internal/10;
          my $temp_cpu = $$env_values{$BASE_OID.".11.0"};
          $temp_cpu = $temp_cpu/10;
          $returnstring = "Internal Temperature = ".$temp_internal." Degree Celsius, CPU Temperature = ".$temp_cpu." Degree Celsius";
          $perf_data = "internal_temp=".$temp_internal." cpu_temperature=".$temp_cpu;

          if($temp_internal >=$warning)
           { $returnvalue = $ERRORS{"WARNING"}} 

          if($temp_internal >=$critical)
           { $returnvalue = $ERRORS{"CRITICAL"}}
       
         
    }


   if ($type eq 'power')
   {
         my $voltage = $$env_values{$BASE_OID.".8.0"};
         my $power = $$env_values{$BASE_OID.".12.0"};
         my $amp = $$env_values{$BASE_OID.".13.0"};
          $amp = $amp/1000;

         $returnstring = " Voltage = ".$voltage."V, Power Consumption = ".$power."Watts, Current = ".$amp."Amp";
         $perf_data = "voltage=".$voltage." power=".$power." current=".$amp;
 
         if($voltage >=$warning)
           { $returnvalue = $ERRORS{"WARNING"}}

          if($voltage >=$critical)
           { $returnvalue = $ERRORS{"CRITICAL"}}



   }


$returnstring=$returnstring."|".$perf_data;



if ($returnvalue == $ERRORS{"CRITICAL"})
{
	print "CRITICAL: ".$returnstring."\n";
	exit $returnvalue;
}


if ($returnvalue == $ERRORS{"WARNING"})
{
	print "WARNING: ".$returnstring."\n";
	exit $returnvalue;
}

print "OK: ".$returnstring."\n";
exit $returnvalue;
