From 6f986f8ce08f3a136ad26ef04e0edce25607497e Mon Sep 17 00:00:00 2001 From: Julien Mathis Date: Tue, 5 Jun 2007 18:07:34 +0000 Subject: [PATCH] add trap handler git-svn-id: http://svn.centreon.com/Plugins/Dev@2437 6bcd3966-0018-0410-8128-fd23d134de7e --- src/traps/trapHandler | 134 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 src/traps/trapHandler diff --git a/src/traps/trapHandler b/src/traps/trapHandler new file mode 100644 index 000000000..93b305a9b --- /dev/null +++ b/src/traps/trapHandler @@ -0,0 +1,134 @@ +#! /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 +# +################################################################### +# 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 DBI; + +############################# +## SET DATABASE CONFIGURATION +# +sub set_db { + require "@OREON_PATH@/ODS/etc/conf.pm" + my $dsn = "dbi:mysql:$mysql_database_oreon"; + return $dsn, $mysql_user, $mysql_passwd; +} + +############################### +## GET HOSTNAME FROM IP ADDRESS +# + +sub get_hostinfos($$){ + my $requete = "SELECT host_name FROM host WHERE host_address='$_[1]' "; + my $sth = $_[0]->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; + exit if (!defined $host_id); + $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; + exit if (!defined $trap_id); + + 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); + $sth = $_[0]->prepare("SELECT command_file FROM cfg_nagios WHERE nagios_activate = '1' LIMIT 1"); + $sth->execute(); + my $conf = $sth->fetchrow_array(); + $sth->finish(); + my $submit = `/usr/bin/printf "[$datetime] PROCESS_SERVICE_CHECK_RESULT;$this_host;$this_service;$status;$argument" >> $conf->{'command_file'}`; + } + } + $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); +}