#! /usr/bin/perl -w
################################################################################
# Copyright 2005-2011 MERETHIS
# Centreon is developped by : Julien Mathis and Romain Le Merlus under
# GPL Licence 2.0.
# 
# This program is free software; you can redistribute it and/or modify it under 
# the terms of the GNU General Public License as published by the Free Software 
# Foundation ; either version 2 of the License.
# 
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along with 
# this program; if not, see <http://www.gnu.org/licenses>.
# 
# Linking this program statically or dynamically with other modules is making a 
# combined work based on this program. Thus, the terms and conditions of the GNU 
# General Public License cover the whole combination.
# 
# As a special exception, the copyright holders of this program give MERETHIS 
# permission to link this program with independent modules to produce an executable, 
# regardless of the license terms of these independent modules, and to copy and 
# distribute the resulting executable under terms of MERETHIS choice, provided that 
# MERETHIS also meet, for each linked independent module, the terms  and conditions 
# of the license of that module. An independent module is a module which is not 
# derived from this program. If you modify this program, you may extend this 
# exception to your version of the program, but you are not obliged to do so. If you
# do not wish to do so, delete this exception statement from your version.
# 
# For more information : contact@centreon.com
# 
# SVN : $URL$
# SVN : $Id$
#
####################################################################################
#
# Script init
#

use strict;
use DBI;
use Getopt::Long;

eval "use HTML::Entities";
if ($@) {
	print "NOT using HTML::Entities, you might have problem with special characters. Install HTML::Entities.\n";
} else {
	print "Using HTML::Entities\n";
}

use vars qw($mysql_database_oreon $mysql_database_ods $mysql_host $mysql_user $mysql_passwd);
require "/DBA/centreon/2.4.5/etc/conf.pm";

######################################
## Get snmptt configuration files path
#

sub getPath($) {
    my $dbh = shift;
    my $sth = $dbh->prepare("SELECT * FROM `options` WHERE `key` LIKE 'snmp_trapd_path_conf'") 
		or die "Couldn't prepare 'get snmp_trapd_path_conf' statement: ". $dbh->errstr;
    $sth->execute()
		or die "Couldn't execute 'get snmp_trapd_path_conf' statement: " . $sth->errstr;
    my $pathRes = $sth->fetchrow_hashref();
    undef($sth);
    
    if (($pathRes->{'value'} =~ /\/$/)) {
    	return $pathRes->{'value'};
    } else {
    	return "/etc/snmp/centreon_traps/";
    }
}

sub main() {
    print "Generating SNMPTT configuration files...\n";
    my ($nbMan, $nbTraps) = (0,0);

    my $dbh = DBI->connect("DBI:mysql:database=".$mysql_database_oreon.";host=".$mysql_host, $mysql_user, $mysql_passwd, {'RaiseError' => 0, 'PrintError' => 0, 'AutoCommit' => 1}) 
		or die "Couldn't connect to database: " . DBI->errstr;
    my $confFiles_path = getPath($dbh);

    my $sth = $dbh->prepare("SELECT * FROM `options` WHERE `key` LIKE 'oreon_path'")
		or die "Couldn't prepare 'get opreon_path' statement: " . $dbh->errstr;
    $sth->execute()
		or die "Couldn't execute 'get opreon_path' statement: " . $sth->errstr;
    my $conf = $sth->fetchrow_hashref();
    $sth->finish();
    my $NAGIOS_TRAPS = $conf->{'value'}."bin/";

    $sth = $dbh->prepare("SELECT DISTINCT tv.`id`, tv.`name` FROM `traps` t, `traps_service_relation` tsr, `traps_vendor` tv WHERE tsr.`traps_id` = t.`traps_id` AND t.`manufacturer_id` = tv.`id` ORDER BY tv.name")
		or die "Couldn't prepare 'get id and name' statement: " . $dbh->errstr;
    $sth->execute()
		or die "Couldn't execute 'get id and name' statement: " . $sth->errstr;
    my $snmpttIni = "";
    while (my ($man_id, $man_name) = $sth->fetchrow_array()) {
		my $sth2 = $dbh->prepare("SELECT t.`traps_oid`, t.`traps_status`, t.`traps_args`, t.`traps_comments`, t.`traps_name`, tv.`name` FROM `traps` t, `traps_service_relation` tsr, `traps_vendor` tv WHERE tsr.`traps_id` = t.`traps_id` AND tv.`id` = '$man_id' AND t.`manufacturer_id` = '$man_id' ORDER BY tv.name")
			or die "Couldn't prepare 'get traps information' statement: " . $dbh->errstr;
		$sth2->execute()
			or die "Couldn't execute 'get traps information' statement: " . $sth->errstr;
		if (!open(FILE, "> ".$confFiles_path."snmptt-".$man_name.".conf")) {
		    print "Cannot open ".$confFiles_path."snmptt-".$man_name.".conf in write mode - Export aborded\n";
		    exit;
		}
		$nbMan++ if ($sth2->rows);
		while (my $values = $sth2->fetchrow_hashref()) {
		    $nbTraps++;
		    my $args = "";
		    print FILE "EVENT ".$values->{'traps_name'}." ".$values->{'traps_oid'}." \"Status Event\" ".$values->{'traps_status'}."\n";
		    if (defined($values->{'traps_args'})) {
				if (exists &decode_entities) {
					$values->{'traps_args'} = decode_entities($values->{'traps_args'});
				}
				print FILE "FORMAT ".$values->{'traps_args'}."\n";
		    } else {
				$values->{'traps_args'} = "no output for trap!";
		    }
		    print FILE "EXEC ".$NAGIOS_TRAPS."centTrapHandler-2.x \$aA \$A \$o \"".$values->{'traps_args'}."\" \"\$+*\"\n";
		    if (defined($values->{'traps_comments'})) {
				print FILE "SDESC\n".$values->{'traps_comments'};
				if ($values->{'traps_comments'} =~ /\n$/) {
				    print FILE "EDESC\n\n";
				} else {
				    print FILE "\nEDESC\n\n";
				}
		    } else {
				print FILE "\n";
		    }
		}
		close FILE;
		$snmpttIni .= $confFiles_path."snmptt-".$man_name.".conf\n";
		$sth2->finish();
    }
    print "$nbTraps traps for $nbMan manufacturers are defined.\n";
    $sth->finish();
    $dbh->disconnect();
    if (!open(FILE, $confFiles_path."snmptt.ini")) {
		print "Cannot open ".$confFiles_path."snmptt.ini - Export Aborded\n";
		exit;
    }
    if (!open(TEMP, "> /tmp/snmptt.ini.tmp")) {
		print "Cannot open /tmp/snmptt.ini.tmp in write mode - Export Aborded\n";
		exit;
    }
    my $continue = 1;
    while ($continue == 1) {
		my $line = <FILE>;
		if ($line) {
		    if (!($line =~ /^snmptt\_conf\_files/)) {
				print TEMP $line;
		    } else {
				$continue = 0;
		    }
		} else {
		    $continue = -1;
		}
    }
    if (!$continue) {
		print TEMP "snmptt_conf_files = <<END\n";
		print TEMP $snmpttIni."END\n";
		my $command = "mv /tmp/snmptt.ini.tmp ".$confFiles_path."snmptt.ini";
		my $mv = `$command`;
		print "SNMPTT configuration files generated.\n";
    } else {
		print "Couldn't export ".$confFiles_path."snmptt.ini, please put these lines at the end of file snmptt.ini :\n";
		print "snmptt_conf_files = <<END\n".$snmpttIni."END\n";
    }
}

main();
