#!/usr/bin/perl

use haruca;
use strict;

my ($dbh, $from, $hostcode, $hostname, $sql, $k, $v, $summary, $trapname, $elem);
my ($if, $key, $stat, $target, $reason, $trapoid, $value, $description, $address, $str_summary, $str_target, $sth,$ignore_config); 
my @lines;
my @traps;
my %info;

my $debug_trap;
$debug_trap = 1;
# $debug_trap = 1 ; ON
# $debug_trap = 0 ; OFF


while(<STDIN>){
  chomp($_);
  push(@lines,$_);
}


foreach (@lines){
  if($_ =~ /^\ *((\.\d+)+)\ (.+)/){
    $info{$1} = $3;
    $key   = $1;
    $value = $3;
    if($key =~ /.1.3.6.1.6.3.1.1.4.1.0/){
      $trapoid = $value;
    }
  }
}

if($debug_trap){
  system("echo '-------' >> ${main::tmppath}conf");
}

foreach $key (keys %info){
  if($debug_trap){
    system("echo '$key $info{$key}' >> ${main::tmppath}conf");
  }
}

$dbh = haruca::connect_db();

$sql = "select trapname,target,summary from plugin_haruca_traptype where oidstring = \"$trapoid\"";
$sth = $dbh->prepare($sql);
$sth->execute;
$sth->bind_columns(undef,\($trapname,$target,$summary));
$sth->fetch;
$sth->finish;


if($trapname eq ""){
  $dbh->disconnect;
  exit;
}


$summary =~ s/\./\\./g;
$summary =~ s/\$IP/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/g;
$summary =~ s/\$NUM/\\d+/;

$target =~ s/\./\\./g;
$target =~ s/\$IP/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/g;
$target =~ s/\$NUM/\\d+/;


$ignore_config = 0;

foreach $key (keys %info){


  if($key =~ /.1.3.6.1.6.3.18.1.3.0/){ $from = $info{$key}; }

  if(($summary ne "")&&($str_summary eq "")){
    if($summary =~ /\.$/){
      if($key =~ /$summary/){
        $str_summary = (split(/$summary/,$key))[-1];
      }
    }else{
      if($key =~ /$summary/){ $str_summary = $info{$key}; }
    }
  }

  if(($target ne "")&&($str_target eq "")){
    if($target =~ /\.$/){
      if($key =~ /$target/){
        $str_target = (split(/$target/,$key))[-1];
      }
    }else{
      if($key =~ /$target/){ $str_target = $info{$key}; }
    }
  }

  if($key =~ /.1.3.6.1.4.1.9.9.43.1.1.6.1.3.\d+$/){
    if($info{$key} eq "1"){ $ignore_config++; }
  }
  if($key =~ /.1.3.6.1.4.1.9.9.43.1.1.6.1.4.\d+$/){
    if($info{$key} eq "2"){ $ignore_config++; }
  }
  if($key =~ /.1.3.6.1.4.1.9.9.43.1.1.6.1.5.\d+$/){
    if($info{$key} eq "3"){ $ignore_config++; }
  }
}

if(($ignore_config == 1)||($ignore_config == 2)){
  $dbh->disconnect;
  exit;
}

$sql = "select id from host where hostname = \"$from\" ";
$hostcode = $dbh->selectrow_array($sql);
if($hostcode eq ""){
  $dbh->disconnect;
  exit;
}



$hostname = haruca::hostcode_to_hostname($hostcode);

$str_target =~ s/"//g;
$str_summary =~ s/"//g;

if($str_target ne ""){

  foreach(split(/\n/,`${main::binpath}ipsearch --under $hostname`)){
    if($_ =~ /$str_target/){
      $description = (split(/\ +/,$_))[4];
      $address     = (split(/\ +/,$_))[3];
    }
  }

}else{
  $description = "";
  $address     = "";
}

if($dbh->selectrow_array("select alertmail from plugin_haruca_traptype where oidstring = '$trapoid'")){
  haruca::send_alert($hostname,$address,$trapoid,$str_target,$str_summary,$description);
}

$trapoid     = $dbh->quote($trapoid);
$target      = $dbh->quote($str_target);
$summary     = $dbh->quote($str_summary);
$description = $dbh->quote($description);
$address     = $dbh->quote($address);
$sql = "insert into plugin_haruca_traplog (hostcode,oidstring,gettime,target,summary,description,address) values ($hostcode,$trapoid,now(),$target,$summary,$description,$address)";
$dbh->do($sql);
$dbh->disconnect;

exit;

