From a46f343a7398126ea2cf1fc18da3ad6abd37482f Mon Sep 17 00:00:00 2001 From: Mat Sugumaran Date: Tue, 5 Jun 2007 17:09:31 +0000 Subject: [PATCH] git-svn-id: http://svn.centreon.com/Plugins/Dev@2436 6bcd3966-0018-0410-8128-fd23d134de7e --- centreon-plugins/src/traps/fill_trapDB.pl | 139 ++++++++++++++++++ .../src/traps/genSnmpttConfFile.pl | 134 +++++++++++++++++ centreon-plugins/src/traps/trapHandler.pl | 127 ++++++++++++++++ 3 files changed, 400 insertions(+) create mode 100644 centreon-plugins/src/traps/fill_trapDB.pl create mode 100644 centreon-plugins/src/traps/genSnmpttConfFile.pl create mode 100644 centreon-plugins/src/traps/trapHandler.pl diff --git a/centreon-plugins/src/traps/fill_trapDB.pl b/centreon-plugins/src/traps/fill_trapDB.pl new file mode 100644 index 000000000..d8f670e47 --- /dev/null +++ b/centreon-plugins/src/traps/fill_trapDB.pl @@ -0,0 +1,139 @@ +#! /usr/bin/perl -w +# +# $Id: genSnmpttConfFile.pl,v 1.0 2007/05/15 17:21:49 Sugumaran Mat $ +# +# Oreon's plugins are developped with GPL Licence : +# http://www.fsf.org/licenses/gpl.txt +# Developped by : Sugumaran Mathavarajan +# +# The Software is provided to you AS IS and WITH ALL FAULTS. +# OREON makes no representation and gives no warranty whatsoever, +# whether express or implied, and without limitation, with regard to the quality, +# safety, contents, performance, merchantability, non-infringement or suitability for +# any particular or intended purpose of the Software found on the OREON web site. +# In no event will OREON be liable for any direct, indirect, punitive, special, +# incidental or consequential damages however they may arise and even if OREON has +# been previously advised of the possibility of such damages. + +use strict; +use Getopt::Long; +use DBI; + +############################# +## SET DATABASE CONFIGURATION +# +sub set_db +{ + my $db_name = "oreon"; ## name of your database for oreon + my $login = "root"; ## user of your database + my $mdp = "mysql-password"; ## password for this user + my $dsn = "dbi:mysql:$db_name"; + return $dsn, $login, $mdp; +} + +######################################### +## TEST IF OID ALREADY EXISTS IN DATABASE +# +sub existsInDB($$) { + my ($dbh, $oid,$manuf) = @_; + my $query = "SELECT traps_id from traps where traps_oid='$oid'"; + my $sth = $dbh->prepare($query); + $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; +} + +########################## +## INSERT TRAP IN DATABASE +# + +################ +## MAIN FUNCTION +# +sub main($$) { + my $manuf = $_[1]; + my @db = set_db(); + my $dbh = DBI->connect($db[0], $db[1], $db[2]) or die "Echec de la connexion mysql\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 $query = "INSERT INTO `traps` (traps_name, traps_oid, traps_status, manufacturer_id) values ('$name', '$oid', '$val', '$manuf')"; + my $sth = $dbh->prepare($query); + $sth->execute; + $sth->finish; + $last_oid = $oid; + } + }elsif ($_ =~/^FORMAT\ (.*)/ && $last_oid ne "") { + my $query = "UPDATE `traps` set traps_args='$1' where traps_oid='$last_oid'"; + my $sth = $dbh->prepare($query); + $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 $query = "UPDATE `traps` set traps_comments='$desc' where traps_oid='$last_oid'"; + my $sth = $dbh->prepare($query); + $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.pl -f configuration_file -m manufacturer_id\n"; + exit; +} + main($opt_f,$opt_m); diff --git a/centreon-plugins/src/traps/genSnmpttConfFile.pl b/centreon-plugins/src/traps/genSnmpttConfFile.pl new file mode 100644 index 000000000..8059d3934 --- /dev/null +++ b/centreon-plugins/src/traps/genSnmpttConfFile.pl @@ -0,0 +1,134 @@ +#! /usr/bin/perl -w +# +# $Id: genSnmpttConfFile.pl,v 1.0 2007/05/15 17:21:49 Sugumaran Mat $ +# +# Oreon's plugins are developped with GPL Licence : +# http://www.fsf.org/licenses/gpl.txt +# Developped by : Sugumaran Mathavarajan +# +# The Software is provided to you AS IS and WITH ALL FAULTS. +# OREON makes no representation and gives no warranty whatsoever, +# whether express or implied, and without limitation, with regard to the quality, +# safety, contents, performance, merchantability, non-infringement or suitability for +# any particular or intended purpose of the Software found on the OREON web site. +# In no event will OREON be liable for any direct, indirect, punitive, special, +# incidental or consequential damages however they may arise and even if OREON has +# been previously advised of the possibility of such damages. + +use strict; +use Getopt::Long; +use DBI; +my $NAGIOS_TRAPS = "/srv/nagios/libexec/traps"; + +############################# +## SET DATABASE CONFIGURATION +# +sub set_db +{ + my $db_name = "oreon"; ## name of your database for oreon + my $login = "root"; ## user of your database + my $mdp = "mysql-password"; ## password for this user + my $dsn = "dbi:mysql:$db_name"; + return $dsn, $login, $mdp; +} + +###################################### +## Get snmptt configuration files path +# +sub getPath($) { + my $dbh = shift; + my $query = "Select snmp_trapd_path_conf from general_opt"; + my $sth = $dbh->prepare($query); + $sth->execute; + my $path = $sth->fetchrow_array; + if (!($path =~ /\/$/)) { + $path .= "/"; + } + if (!$path) { + $path = "/etc/snmp/"; + } + return $path; +} + + +sub main() { + print "Generating SNMPTT configuration files...\n"; + my ($nbMan, $nbTraps) = (0,0); + my @db = set_db(); + my $dbh = DBI->connect($db[0], $db[1], $db[2]) or die "Echec de la connexion mysql\n"; + my $confFiles_path = getPath($dbh); + my $query = "SELECT id, name from inventory_manufacturer"; + my $sth = $dbh->prepare($query); + $sth->execute; + my $snmpttIni = ""; + while (my ($man_id, $man_name) = $sth->fetchrow_array) { + my $query2 = "SELECT traps_name, traps_oid, traps_status, traps_args, traps_comments"; + $query2 .= " from traps where manufacturer_id='$man_id'"; + my $sth2 = $dbh->prepare($query2); + $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; + } + if ($sth2->rows) { + $nbMan++; + } + while (my @values = $sth2->fetchrow_array) { + $nbTraps++; + print FILE "EVENT ".$values[0]." ".$values[1]." \"Status Event\" ".$values[2]."\n"; + if (defined($values[3])) { + print FILE "FORMAT ".$values[3]."\n"; + } + print FILE "EXEC ".$NAGIOS_TRAPS."/trapHandler.pl \$aA \$o \"\$*\"\n"; + if (defined($values[4])) { + print FILE "SDESC\n"; + print FILE $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 = <prepare($requete); + $sth->execute(); + my @host; + while (my $temp = $sth -> fetchrow_array) { + $host[scalar(@host)] = $temp; + } + $sth -> finish; + return @host; +} + +########################## +## GET SERVICE DESCRIPTION +# +sub get_servicename($$$) +{ + my $query_host = "SELECT host_id from host WHERE host_name ='$_[2]'"; + my $sth = $_[0]->prepare($query_host); + $sth->execute(); + my $host_id = $sth -> fetchrow_array; + if (!defined $host_id) { + exit; + } + $sth->finish; + my $query_trap = "SELECT traps_id, traps_args, traps_status from traps where traps_oid='$_[1]'"; + $sth = $_[0]->prepare($query_trap); + $sth->execute(); + my ($trap_id,$argument, $trap_status) = $sth -> fetchrow_array; + if (!defined $trap_id) { + exit; + } + my $query_services = "SELECT service_description FROM service s, host_service_relation h, traps_service_relation t"; + $query_services .= " where s.service_id = t.service_id and t.traps_id='$trap_id' and s.service_id=h.service_service_id"; + $query_services .= " and h.host_host_id='$host_id'"; + $sth = $_[0]->prepare($query_services); + $sth->execute(); + my @service; + while (my $temp = $sth -> fetchrow_array) { + $service[scalar(@service)] = $temp; + } + my $query_hostgroup_services = "SELECT service_description FROM hostgroup_relation hgr, traps_service_relation t, service s, host_service_relation hsr"; + $query_hostgroup_services .= " WHERE hgr.host_host_id = '".$host_id."' AND hsr.hostgroup_hg_id = hgr.hostgroup_hg_id"; + $query_hostgroup_services .= " AND s.service_id = hsr.service_service_id and s.service_id=t.service_id and t.traps_id='$trap_id'"; + $sth -> finish; + $sth = $_[0]->prepare($query_hostgroup_services); + $sth->execute(); + my @new_service; + while (my $temp = $sth -> fetchrow_array){ + $new_service[scalar(@new_service)] = $temp; + } + $sth -> finish; + return $trap_status, $argument, (@service,@new_service); +} + +####################################### +## GET HOSTNAME AND SERVICE DESCRIPTION +# +sub getTrapsInfos($$$) +{ + my $ip = shift; + my $oid = shift; + my $arguments_line = shift; + my @db = set_db(); + my $dbh = DBI->connect($db[0], $db[1], $db[2]) or die "Echec de la connexion\n"; + my @host = get_hostinfos($dbh, $ip); + foreach(@host) { + my $this_host = $_; + my ($status, $argument, @servicename) = get_servicename($dbh, $oid, $_); + foreach (@servicename) { + my $this_service = $_; + my $datetime=`date +%s`; + my @vars = split /\ /,$arguments_line; + $argument =~ s/\$([0-9]+)/$vars[$1-1]/g; + chomp($datetime); + my $submit = `/usr/bin/printf "[$datetime] PROCESS_SERVICE_CHECK_RESULT;$this_host;$this_service;$status;$argument" >> /srv/nagios/var/rw/nagios.cmd`; + } + } + $dbh -> disconnect; + exit; +} + +########################## +## PARSE TRAP INFORMATIONS +# +if (scalar(@ARGV)) { + my $ip = $ARGV[0]; + my $oid = $ARGV[1]; + my $arguments = $ARGV[2]; + getTrapsInfos($ip, $oid, $arguments); +}