#!/usr/bin/perl -w
############################## check_amazon ######################
my $Version='0.02';
# Date :    Jul 05 2008
# Author  : tobyyy 
# Help :    http://www.nagios-portal.de User:tobyyy
# TODO : 
# - Output through %ERRORS (done)
# - verbose output (done)
# - Perfdata with warn und crit (done)
##################################################################
#
# Help : ./check_amazon.pl -h

use strict;
use Net::Amazon;
use Net::Amazon::Request::ASIN;
use Getopt::Long;
use Log::Log4perl qw(:easy);

################Options##########
my $o_help=		undef; 	# Hilfe
my $o_verb=		undef;	# verbose mode
my $o_version=		undef;	# print version
my $o_asin=		undef;  # asin
my $o_desc=		undef;  # description
my $o_warn_opt=		undef;  # warning options
my $o_crit_opt=		undef;  # critical options
my $o_token=            undef;  # amazon token


################Vars#############
my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
my $priceact  = "Unknown";
my $var_return = "3";
my $var_output = "Uninitialized";

##################MAIN############
check_options();

my $UA = Net::Amazon->new(
 token  => $o_token,
   locale => 'de',
);

$priceact  = track($o_asin,$o_desc);

$priceact =~ s/EUR //g;
$priceact =~ s/\,/\./g;

if ($priceact > $o_crit_opt)
{
   $var_output = "Critical: Price is too high $priceact Euro";
   $var_return = "2";
}
elsif ($priceact > $o_warn_opt)
{
   $var_output = "Warning: Price is a little bit too high $priceact Euro";
   $var_return = "1";
}
elsif ($priceact < $o_warn_opt)
{
   $var_output = "OK: Buy!!!  $priceact Euro";
   $var_return = "0";
}
else
{
   #"Unknown";
   $var_return = "3";
}

output();

###########################################
sub track {
###########################################
  my($asin, $txt) = @_;

  my $req = 
      Net::Amazon::Request::ASIN->new(
        asin  => $asin);

  my $resp = $UA->request($req);

  if($resp->is_success()) 
  {
    my($prop) = $resp->properties();
    my $price = $prop->OurPrice();
    return $price;

  } 
  else
  {
    my $error=$resp->message;
    $var_output = "Unknown:  $error";
    output();
  }
}
############################################
sub p_version 
############################################
{ 
print "check_amazon : $Version\n";
}

############################################
sub print_usage
############################################
{
    print "Usage: $0 [-v] -y <asin> -d <description> -w<warn levels> -c<crit levels> -z<amazon token>\n";
}

############################################
sub isnnum
############################################
{ # Return true if arg is not a number
my $num = shift;
if
( $num =~ /^(\d+\.?\d*)|(^\.\d+)$/ ) 
{ return 0 ;}
return 1;
}
############################################
sub help
############################################
{
print "\nCheck Amazon for Nagios version ",$Version,"\n";
print "tobyyy\n\n";
print_usage();
print <<EOT;
-v, --verbose
log in check_amazon.log extra debugging information(not for regular work!)
-h, --help
print this help message
-y 
insert here the asin number
-w, --warning=Price (e.g 198.45)
warning level: the price is a little bit too high
-c, --critical=Price (e.g 250.45)
critical level: the price is too high
-V, --version
prints version number
-z 
insert here your amazon token
EOT
}
############################################
sub check_options
############################################
{
Getopt::Long::Configure ("bundling");
GetOptions(
'v'	=> \$o_verb,		'verbose'	=> \$o_verb,
'h'     => \$o_help,    	'help'        	=> \$o_help,
'V'	=> \$o_version,		'version'	=> \$o_version,
'y:s'   => \$o_asin,            'asin:s'        => \$o_asin,
'd:s'   => \$o_desc,            'desc:s'        => \$o_desc,
'w:s'   => \$o_warn_opt,       	'warning:s'   	=> \$o_warn_opt,
'c:s'   => \$o_crit_opt,      	'critical:s'   	=> \$o_crit_opt,
'z:s'   => \$o_token,           'token:s'        =>\$o_token,

);
if (defined ($o_help) ) { help(); exit $ERRORS{"UNKNOWN"}};
if (defined($o_version)) { p_version(); exit $ERRORS{"UNKNOWN"}};
if ( ! defined($o_asin) ||  ! defined($o_desc) || ! defined($o_warn_opt) || ! defined($o_crit_opt) || ! defined($o_token)  ) # check input 
 	{ print_usage(); exit $ERRORS{"UNKNOWN"}}
if (defined ($o_verb) )   { Log::Log4perl->easy_init( { level   => $DEBUG,
                                file    => ">>check_amazon.log" } );}

}
					      

###########################################
sub output
###########################################
{

    if ($var_return == 0)
    {
          #"OK";
          print $var_output , "|Price=", $priceact, ";$o_warn_opt;$o_crit_opt;0;\n";
          exit $ERRORS{"OK"};

    }
    elsif ($var_return == 1)
    {
          # "Warning";
          print $var_output , "|Price=", $priceact, ";$o_warn_opt;$o_crit_opt;0;\n";
          exit $ERRORS{"WARNING"};

    }
    elsif ($var_return == 2)
    {
          # "Critical";
          print $var_output , "|Price=", $priceact, ";$o_warn_opt;$o_crit_opt;0;\n";
          exit $ERRORS{"CRITICAL"};

    }
    else
    {
          # "Unknown";
          print $var_output , "|Price=", $priceact, ";$o_warn_opt;$o_crit_opt;0;\n";
          exit $ERRORS{"UNKNOWN"};

    }
}
