#!/usr/bin/perl
# Extract snmp trap information from trapd.conf file of network node manager
# and writes a line per trap to output, can be easily modified to execute commands
# Example output uses pandora cli (5.0 version) to create SNMP Traps into pandora.

$counter = 0;
$enter_the_dragon = 0;

open (FIDATA, "< trapd.conf");
while (<FIDATA>){
	$counter++;
    $buffer_line = $_;

   # if ($buffer_line =~ /^EVENT ([a-zA-Z]*) (\.[0-9]*) (\"[A-Za-z0-0]*) ([a-zA-Z0-9])/){
   	if ($buffer_line =~ /^EVENT ([a-zA-Z]*) ([\.0-9]*) (\"[A-Za-z0-0]*\") ([a-zA-Z0-9]*)/){
		$enter_the_dragon = 1;
		$parse_long_desc = 0;
    	$OID = $2;
    	$name = $1;
    	$cosa1 = $3;
    	$crit = $4;
    	$short_description = "";
    	$long_description = "";
    } 

    # Here we are "inside" the block of a trap
    if ($enter_the_dragon == 1){
    	if ($buffer_line =~ /^Short Descr.\: \"([\sa-zA-Z0-9]*)\"/ ){
    		$short_description = $1;
    	}
    
    	if ($buffer_line =~ /^EDESC/ ){
    		$enter_the_dragon = 0;
    		
    		$description = $short_description;
    		if ($long_description ne ""){
    			$description = $long_description;
    		}
			
    		$description =~ s/\"//;

			# Command to execute , p.e:
			print "pandora_manage.pl /etc/pandora/pandora_server.conf $name $OID \"$description\" 3 \n";

		}

    	if ($buffer_line =~ /^Variables\:/ ){
    		$parse_long_desc = 0;
    	}

		if ($parse_long_desc == 1){
    		$long_description = $long_description . $_;
    	}

    	if ($buffer_line =~ /^Long Descr.\:/ ){
    		$parse_long_desc = 1;
    	}
    
    }
}
