From de32d3097fa88605b07a488536c6df043a1659cb Mon Sep 17 00:00:00 2001 From: Mat Sugumaran Date: Tue, 19 Jun 2007 09:04:10 +0000 Subject: [PATCH] files renammed git-svn-id: http://svn.centreon.com/Plugins/Dev@2483 6bcd3966-0018-0410-8128-fd23d134de7e --- src/traps/fill_trapDB | 137 ++++++++++++++++++++++++++++++++++++ src/traps/genSnmpttConfFile | 129 +++++++++++++++++++++++++++++++++ 2 files changed, 266 insertions(+) create mode 100644 src/traps/fill_trapDB create mode 100644 src/traps/genSnmpttConfFile diff --git a/src/traps/fill_trapDB b/src/traps/fill_trapDB new file mode 100644 index 000000000..afb18dd2f --- /dev/null +++ b/src/traps/fill_trapDB @@ -0,0 +1,137 @@ +#! /usr/bin/perl -w +################################################################### +# Oreon is developped with GPL Licence 2.0 +# +# GPL License: http://www.gnu.org/licenses/gpl.txt +# +# Developped by : Mathavarajan Sugumaran - msugumaran@merethis.com +# Julien Mathis - Romain Le Merlus +# +################################################################### +# 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, or (at your option) any later version. +# +# 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. +# +# For information : contact@merethis.com +#################################################################### +# +# Script init +# + +use strict; +use Getopt::Long; +use DBI; + +use vars qw($mysql_database_oreon $mysql_database_ods $mysql_host $mysql_user $mysql_passwd); +require "@INSTALL_DIR_OREON@/ODS/etc/conf.pm"; + +######################################### +## TEST IF OID ALREADY EXISTS IN DATABASE +# + +sub existsInDB($$) { + my ($dbh, $oid,$manuf) = @_; + my $sth = $dbh->prepare("SELECT traps_id FROM traps WHERE traps_oid = '$oid'"); + $sth->execute(); + if (defined($sth->fetchrow_array)) { + $sth->finish(); + return 1; + } + $sth->finish(); + return 0; +} + +##################################### +## RETURN ENUM FROM STRING FOR STATUS +# + +sub getStatus($$) { + my ($val, $type) = @_; + if ($val =~ /[I|i][N|n][F|f][O|o][R|r][M|m][A|a][T|t][I|i][O|o][N|n][A|a][L|l]|[N|n][O|o][R|r][M|m][A|a][L|l]/) { + return 0; + } elsif ($val =~ /^[W|w][A|a][R|r][N|n][I|i][N|n][G|g]|[M|m][I|i][N|n][O|o][R|r]$/) { + return 1; + } elsif ($val =~ /^[C|c][R|r][I|i][T|t][I|i][C|c][A|a][L|l]|[M|m][A|a][J|j][O|o][R|r]$/) { + return 2; + } + return 3; +} + +################ +## MAIN FUNCTION +# + +sub main($$) { + my $manuf = $_[1]; + + my $dsn = "dbi:mysql:$mysql_database_oreon"; + my $dbh = DBI->connect($dsn, $mysql_user, $mysql_passwd) or die "Echec de la connexion\n"; + if (!open(FILE, $_[0])) { + print "Cannot open configuration file : $_[0]\n"; + exit; + } + my $last_oid = ""; + while () { + if ($_ =~ /^EVENT\ ([a-zA-Z0-9]+)\ ([0-9\.]+)\ (\"[A-Za-z\ ]+\")\ ([a-zA-Z]+)/) { + my ($name,$oid,$type,$val) = ($1,$2,$3,$4); + if (existsInDB($dbh, $oid)) { + print "Trap oid : $name => $oid already exists in database\n"; + $last_oid = $oid; + } else { + $val = getStatus($val,$type); + my $sth = $dbh->prepare("INSERT INTO `traps` (traps_name, traps_oid, traps_status, manufacturer_id) values ('$name', '$oid', '$val', '$manuf')"); + $sth->execute(); + $sth->finish(); + $last_oid = $oid; + } + } elsif ($_ =~/^FORMAT\ (.*)/ && $last_oid ne "") { + my $sth = $dbh->prepare("UPDATE `traps` set traps_args='$1' WHERE traps_oid = '$last_oid'"); + $sth->execute(); + $sth->finish(); + } elsif ($_ =~ /^SDESC(.*)/ && $last_oid ne "") { + my $temp_val = $1; + my $desc = ""; + if (! ($temp_val =~ /\s+/)){ + $temp_val =~ s/\"/\\\"/g; + $temp_val =~ s/\'/\\\'/g; + $desc .= $temp_val; + } + my $found = 0; + while (!$found) { + my $line = ; + if ($line =~ /^EDESC/) { + $found = 1; + } else { + $line =~ s/\"/\\\"/g; + $line =~ s/\'/\\\'/g; + $desc .= $line; + } + } + if ($desc ne "") { + my $sth = $dbh->prepare("UPDATE `traps` set traps_comments='$desc' where traps_oid='$last_oid'"); + $sth->execute(); + $sth->finish(); + } + } + } + $dbh->disconnect(); +} + + Getopt::Long::Configure('bundling'); + my ($opt_f, $opt_m); + GetOptions("f|file=s" => \$opt_f, "m|man=s" => \$opt_m); + +if (!$opt_f || !$opt_m) { + print "fill_trapDB.pl : Usage : ./fill_trapDB -f configuration_file -m manufacturer_id\n"; + exit; +} + +main($opt_f,$opt_m); + +exit(); diff --git a/src/traps/genSnmpttConfFile b/src/traps/genSnmpttConfFile new file mode 100644 index 000000000..4f40dbd35 --- /dev/null +++ b/src/traps/genSnmpttConfFile @@ -0,0 +1,129 @@ +#! /usr/bin/perl -w +################################################################### +# Oreon is developped with GPL Licence 2.0 +# +# GPL License: http://www.gnu.org/licenses/gpl.txt +# +# Developped by : Mathavarajan Sugumaran - msugumaran@merethis.com +# Julien Mathis - Romain Le Merlus +# +################################################################### +# 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, or (at your option) any later version. +# +# 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. +# +# For information : contact@merethis.com +#################################################################### +# +# Script init +# + +use strict; +use Getopt::Long; +use DBI; + +use vars qw($mysql_database_oreon $mysql_database_ods $mysql_host $mysql_user $mysql_passwd); +require "@INSTALL_DIR_OREON@/ODS/etc/conf.pm"; + +###################################### +## Get snmptt configuration files path +# + +sub getPath($) { + my $dbh = shift; + my $sth = $dbh->prepare("Select snmp_trapd_path_conf from general_opt"); + $sth->execute(); + my $path = $sth->fetchrow_array(); + $path .= "/" if (!($path =~ /\/$/)); + $path = "/etc/snmp/" if (!$path); + return $path; +} + +sub main() { + print "Generating SNMPTT configuration files...\n"; + my ($nbMan, $nbTraps) = (0,0); + + my $dsn = "dbi:mysql:$mysql_database_oreon"; + my $dbh = DBI->connect($dsn, $mysql_user, $mysql_passwd) or die "Echec de la connexion\n"; + my $confFiles_path = getPath($dbh); + + my $sth = $dbh->prepare("SELECT nagios_path_plugins FROM general_opt LIMIT 1"); + $sth->execute(); + my @conf = $sth->fetchrow_array(); + $sth->finish(); + my $NAGIOS_TRAPS = $conf[0]."traps/"; + + $sth = $dbh->prepare("SELECT id, name from inventory_manufacturer"); + $sth->execute(); + my $snmpttIni = ""; + 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'"); + $sth2->execute(); + 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_array()) { + $nbTraps++; + print FILE "EVENT ".$values[0]." ".$values[1]." \"Status Event\" ".$values[2]."\n"; + print FILE "FORMAT ".$values[3]."\n" if (defined($values[3])); + print FILE "EXEC ".$NAGIOS_TRAPS."/trapHandler \$aA \$o \"\$*\"\n"; + if (defined($values[4])) { + print FILE "SDESC\n".$values[4]; + if ($values[4] =~ /\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 = ; + if ($line) { + if (!($line =~ /^snmptt\_conf\_files/)) { + print TEMP $line; + } else { + $continue = 0; + } + } else { + $continue = -1; + } + } + if (!$continue) { + print TEMP "snmptt_conf_files = <