bug fix : gestion des hosts recu par IP ou hostname

git-svn-id: http://svn.centreon.com/Plugins/Dev@3047 6bcd3966-0018-0410-8128-fd23d134de7e
This commit is contained in:
Mat Sugumaran 2007-08-24 16:15:58 +00:00
parent 1266d25472
commit 2c01040c24
2 changed files with 144 additions and 141 deletions

View File

@ -1,134 +1,135 @@
#! /usr/bin/perl -w #! /usr/bin/perl -w
################################################################### ###################################################################
# Oreon is developped with GPL Licence 2.0 # Oreon is developped with GPL Licence 2.0
# #
# GPL License: http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt # GPL License: http://www.gnu.org/licenses/gpl.txt
# #
# Developped by : Mathavarajan Sugumaran - msugumaran@merethis.com # Developped by : Mathavarajan Sugumaran - msugumaran@merethis.com
# Julien Mathis - Romain Le Merlus # Julien Mathis - Romain Le Merlus
# #
################################################################### ###################################################################
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 # as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. # of the License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# For information : contact@merethis.com # For information : contact@merethis.com
#################################################################### ####################################################################
# #
# Script init # Script init
# #
use strict; use strict;
use Getopt::Long; use Getopt::Long;
use DBI; use DBI;
use vars qw($mysql_database_oreon $mysql_database_ods $mysql_host $mysql_user $mysql_passwd); use vars qw($mysql_database_oreon $mysql_database_ods $mysql_host $mysql_user $mysql_passwd);
require "@INSTALL_DIR_OREON@/ODS/etc/conf.pm"; require "@INSTALL_DIR_OREON@/ODS/etc/conf.pm";
###################################### ######################################
## Get snmptt configuration files path ## Get snmptt configuration files path
# #
sub getPath($) { sub getPath($) {
my $dbh = shift; my $dbh = shift;
my $sth = $dbh->prepare("Select snmp_trapd_path_conf from general_opt"); my $sth = $dbh->prepare("Select snmp_trapd_path_conf from general_opt");
$sth->execute(); $sth->execute();
my $path = $sth->fetchrow_array(); my $path = $sth->fetchrow_array();
$path .= "/" if (!($path =~ /\/$/)); $path .= "/" if (!($path =~ /\/$/));
$path = "/etc/snmp/" if (!$path); $path = "/etc/snmp/" if (!$path);
return $path; return $path;
} }
sub main() { sub main() {
print "Generating SNMPTT configuration files...\n"; print "Generating SNMPTT configuration files...\n";
my ($nbMan, $nbTraps) = (0,0); my ($nbMan, $nbTraps) = (0,0);
my $dsn = "dbi:mysql:$mysql_database_oreon"; my $dsn = "dbi:mysql:$mysql_database_oreon";
my $dbh = DBI->connect($dsn, $mysql_user, $mysql_passwd) or die "Echec de la connexion\n"; my $dbh = DBI->connect($dsn, $mysql_user, $mysql_passwd) or die "Echec de la connexion\n";
my $confFiles_path = getPath($dbh); my $confFiles_path = getPath($dbh);
my $sth = $dbh->prepare("SELECT nagios_path_plugins FROM general_opt LIMIT 1"); my $sth = $dbh->prepare("SELECT nagios_path_plugins FROM general_opt LIMIT 1");
$sth->execute(); $sth->execute();
my @conf = $sth->fetchrow_array(); my @conf = $sth->fetchrow_array();
$sth->finish(); $sth->finish();
my $NAGIOS_TRAPS = $conf[0]."traps/"; my $NAGIOS_TRAPS = $conf[0]."traps/";
$sth = $dbh->prepare("SELECT id, name from traps_vendor"); $sth = $dbh->prepare("SELECT id, name from traps_vendor");
$sth->execute(); $sth->execute();
my $snmpttIni = ""; my $snmpttIni = "";
while (my ($man_id, $man_name) = $sth->fetchrow_array()) { while (my ($man_id, $man_name) = $sth->fetchrow_array()) {
my $sth2 = $dbh->prepare("SELECT traps_name, traps_oid, traps_status, traps_args, traps_comments FROM traps WHERE manufacturer_id = '$man_id'"); my $sth2 = $dbh->prepare("SELECT traps_name, traps_oid, traps_status, traps_args, traps_comments FROM traps WHERE manufacturer_id = '$man_id'");
$sth2->execute(); $sth2->execute();
if (!open(FILE, "> ".$confFiles_path."snmptt-".$man_name.".conf")) { if (!open(FILE, "> ".$confFiles_path."snmptt-".$man_name.".conf")) {
print "Cannot open ".$confFiles_path."snmptt-".$man_name.".conf in write mode - Export aborded\n"; print "Cannot open ".$confFiles_path."snmptt-".$man_name.".conf in write mode - Export aborded\n";
exit; exit;
} }
$nbMan++ if ($sth2->rows); $nbMan++ if ($sth2->rows);
while (my @values = $sth2->fetchrow_array()) { while (my @values = $sth2->fetchrow_array()) {
$nbTraps++; $nbTraps++;
my $args = ""; my $args = "";
print FILE "EVENT ".$values[0]." ".$values[1]." \"Status Event\" ".$values[2]."\n"; print FILE "EVENT ".$values[0]." ".$values[1]." \"Status Event\" ".$values[2]."\n";
if (defined($values[3])) { if (defined($values[3])) {
print FILE "FORMAT ".$values[3]."\n"; print FILE "FORMAT ".$values[3]."\n";
}else { }else {
$values[3] = "no output for trap!"; $values[3] = "no output for trap!";
} }
print FILE "EXEC ".$NAGIOS_TRAPS."trapHandler \$aA \$o \"$values[3]\"\n"; print FILE "EXEC ".$NAGIOS_TRAPS."trapHandler \$aA \$A \$o \"$values[3]\"\n";
if (defined($values[4])) { if (defined($values[4])) {
print FILE "SDESC\n".$values[4]; print FILE "SDESC\n".$values[4];
if ($values[4] =~ /\n$/) { if ($values[4] =~ /\n$/) {
print FILE "EDESC\n\n"; print FILE "EDESC\n\n";
} else { } else {
print FILE "\nEDESC\n\n"; print FILE "\nEDESC\n\n";
} }
} else { } else {
print FILE "\n"; print FILE "\n";
} }
} }
close FILE; close FILE;
$snmpttIni .= $confFiles_path."snmptt-".$man_name.".conf\n"; $snmpttIni .= $confFiles_path."snmptt-".$man_name.".conf\n";
$sth2->finish(); $sth2->finish();
} }
print "$nbTraps traps for $nbMan manufacturers are defined.\n"; print "$nbTraps traps for $nbMan manufacturers are defined.\n";
$sth->finish(); $sth->finish();
$dbh->disconnect(); $dbh->disconnect();
if (!open(FILE, $confFiles_path."snmptt.ini")) { if (!open(FILE, $confFiles_path."snmptt.ini")) {
print "Cannot open ".$confFiles_path."snmptt.ini - Export Aborded\n"; print "Cannot open ".$confFiles_path."snmptt.ini - Export Aborded\n";
exit; exit;
} }
if (!open(TEMP, "> /tmp/snmptt.ini.tmp")) { if (!open(TEMP, "> /tmp/snmptt.ini.tmp")) {
print "Cannot open /tmp/snmptt.ini.tmp in write mode - Export Aborded\n"; print "Cannot open /tmp/snmptt.ini.tmp in write mode - Export Aborded\n";
exit; exit;
} }
my $continue = 1; my $continue = 1;
while ($continue == 1) { while ($continue == 1) {
my $line = <FILE>; my $line = <FILE>;
if ($line) { if ($line) {
if (!($line =~ /^snmptt\_conf\_files/)) { if (!($line =~ /^snmptt\_conf\_files/)) {
print TEMP $line; print TEMP $line;
} else { } else {
$continue = 0; $continue = 0;
} }
} else { } else {
$continue = -1; $continue = -1;
} }
} }
if (!$continue) { if (!$continue) {
print TEMP "snmptt_conf_files = <<END\n"; print TEMP "snmptt_conf_files = <<END\n";
print TEMP $snmpttIni."END\n"; print TEMP $snmpttIni."END\n";
my $command = "mv /tmp/snmptt.ini.tmp ".$confFiles_path."snmptt.ini"; my $command = "mv /tmp/snmptt.ini.tmp ".$confFiles_path."snmptt.ini";
my $mv = `$command`; my $mv = `$command`;
print "SNMPTT configuration files generated.\n"; print "SNMPTT configuration files generated.\n";
} else { } else {
print "Couldn't export ".$confFiles_path."snmptt.ini, please put these lines at the end of file snmptt.ini :\n"; 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"; print "snmptt_conf_files = <<END\n".$snmpttIni."END\n";
} }
} }
main(); main();

View File

@ -34,8 +34,8 @@ require "@INSTALL_DIR_OREON@/ODS/etc/conf.pm";
## GET HOSTNAME FROM IP ADDRESS ## GET HOSTNAME FROM IP ADDRESS
# #
sub get_hostinfos($$){ sub get_hostinfos($$$){
my $sth = $_[0]->prepare("SELECT host_name FROM host WHERE host_address='$_[1]' "); my $sth = $_[0]->prepare("SELECT host_name FROM host WHERE host_address='$_[1]' OR host_address='$_[2]'");
$sth->execute(); $sth->execute();
my @host; my @host;
while (my $temp = $sth->fetchrow_array()) { while (my $temp = $sth->fetchrow_array()) {
@ -140,14 +140,15 @@ sub get_servicename($$$) {
## GET HOSTNAME AND SERVICE DESCRIPTION ## GET HOSTNAME AND SERVICE DESCRIPTION
# #
sub getTrapsInfos($$$){ sub getTrapsInfos($$$$){
my $ip = shift; my $ip = shift;
my $hostname = shift;
my $oid = shift; my $oid = shift;
my $arguments_line = shift; my $arguments_line = shift;
my $dsn = "dbi:mysql:$mysql_database_oreon"; my $dsn = "dbi:mysql:$mysql_database_oreon";
my $dbh = DBI->connect($dsn, $mysql_user, $mysql_passwd) or die "Echec de la connexion\n"; my $dbh = DBI->connect($dsn, $mysql_user, $mysql_passwd) or die "Echec de la connexion\n";
my @host = get_hostinfos($dbh, $ip); my @host = get_hostinfos($dbh, $ip, $hostname);
foreach(@host) { foreach(@host) {
my $this_host = $_; my $this_host = $_;
my ($status, @servicename) = get_servicename($dbh, $oid, $_); my ($status, @servicename) = get_servicename($dbh, $oid, $_);
@ -171,7 +172,8 @@ sub getTrapsInfos($$$){
# #
if (scalar(@ARGV)) { if (scalar(@ARGV)) {
my $ip = $ARGV[0]; my $ip = $ARGV[0];
my $oid = $ARGV[1]; my $hostname = $ARGV[1];
my $arguments = $ARGV[2]; my $oid = $ARGV[2];
getTrapsInfos($ip, $oid, $arguments); my $arguments = $ARGV[3];
getTrapsInfos($ip, $hostname, $oid, $arguments);
} }