gestion des services templates pour les traps

git-svn-id: http://svn.centreon.com/Plugins/Dev@3043 6bcd3966-0018-0410-8128-fd23d134de7e
This commit is contained in:
Mat Sugumaran 2007-08-24 15:52:59 +00:00
parent 4db5c39a26
commit 9a0c0cfe76
1 changed files with 177 additions and 128 deletions

View File

@ -1,128 +1,177 @@
#! /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 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 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]' ");
$sth->execute(); $sth->execute();
my @host; my @host;
while (my $temp = $sth->fetchrow_array()) { while (my $temp = $sth->fetchrow_array()) {
$host[scalar(@host)] = $temp; $host[scalar(@host)] = $temp;
} }
$sth -> finish; $sth -> finish;
return @host; return @host;
} }
########################## #####################################################################
## GET SERVICE DESCRIPTION ## GET SERVICES FOR GIVEN HOST (GETTING SERVICES TEMPLATES IN ACCOUNT)
# #
sub get_servicename($$$) { sub getServicesIncludeTemplate($$$$) {
my $query_host = "SELECT host_id from host WHERE host_name ='$_[2]'"; my ($dbh, $sth_st, $host_id, $trap_id) = @_;
my $sth = $_[0]->prepare($query_host); my @service;
$sth->execute(); $sth_st->execute();
my $host_id = $sth -> fetchrow_array(); while (my @temp = $sth_st -> fetchrow_array()) {
exit if (!defined $host_id); my $tr_query = "select traps_id from traps_service_relation where service_id ='".$temp[0]."' and traps_id='".$trap_id."'";
$sth->finish(); my $sth_st3 = $dbh->prepare($tr_query);
$sth_st3->execute();
my $query_trap = "SELECT traps_id, traps_status from traps where traps_oid='$_[1]'"; my @trap = $sth_st3 -> fetchrow_array();
$sth = $_[0]->prepare($query_trap); if (defined($trap[0])) {
$sth->execute(); $service[scalar(@service)] = $temp[1];
my ($trap_id, $trap_status) = $sth->fetchrow_array(); }else {
exit if (!defined $trap_id); if (defined($temp[2])) {
my $found = 0;
my $query_services = "SELECT service_description FROM service s, host_service_relation h, traps_service_relation t"; my $service_template = $temp[2];
$query_services .= " where s.service_id = t.service_id and t.traps_id='$trap_id' and s.service_id=h.service_service_id"; while (!$found) {
$query_services .= " and h.host_host_id='$host_id'"; my $st1_query = "select service_id, service_template_model_stm_id, service_description from service s where service_id ='".$service_template."'";
$sth = $_[0]->prepare($query_services); my $sth_st1 = $dbh->prepare($st1_query);
$sth->execute(); $sth_st1 -> execute();
my @service; my @st1_result = $sth_st1 -> fetchrow_array();
while (my $temp = $sth -> fetchrow_array()) { if (defined($st1_result[0])) {
$service[scalar(@service)] = $temp; my $st2_query = "select traps_id from traps_service_relation where service_id ='".$service_template."' and traps_id='".$trap_id."'";
} my $sth_st2 = $dbh->prepare($st2_query);
$sth_st2 -> execute();
my $query_hostgroup_services = "SELECT service_description FROM hostgroup_relation hgr, traps_service_relation t, service s, host_service_relation hsr"; my @st2_result = $sth_st2 -> fetchrow_array();
$query_hostgroup_services .= " WHERE hgr.host_host_id = '".$host_id."' AND hsr.hostgroup_hg_id = hgr.hostgroup_hg_id"; if (defined($st2_result[0])) {
$query_hostgroup_services .= " AND s.service_id = hsr.service_service_id and s.service_id=t.service_id and t.traps_id='$trap_id'"; $found = 1;
$sth->finish(); $service[scalar(@service)] = $temp[1];
$sth = $_[0]->prepare($query_hostgroup_services); }else {
$sth->execute(); $found = 1;
my @new_service; if (defined($st1_result[1]) && $st1_result[1]) {
while (my $temp = $sth->fetchrow_array()){ $service_template = $st1_result[1];
$new_service[scalar(@new_service)] = $temp; $found = 0;
} }
$sth->finish(); }
return $trap_status, (@service,@new_service); $sth_st2->finish;
} }
$sth_st1->finish;
####################################### }
## GET HOSTNAME AND SERVICE DESCRIPTION }
# }
$sth_st3->finish;
sub getTrapsInfos($$$){ }
my $ip = shift; return (@service);
my $oid = shift; }
my $arguments_line = shift;
##########################
my $dsn = "dbi:mysql:$mysql_database_oreon"; ## GET SERVICE DESCRIPTION
my $dbh = DBI->connect($dsn, $mysql_user, $mysql_passwd) or die "Echec de la connexion\n"; #
my @host = get_hostinfos($dbh, $ip);
foreach(@host) { sub get_servicename($$$) {
my $this_host = $_; my $query_host = "SELECT host_id from host WHERE host_name ='$_[2]'";
my ($status, @servicename) = get_servicename($dbh, $oid, $_); my $sth = $_[0]->prepare($query_host);
foreach (@servicename) { $sth->execute();
my $this_service = $_; my $host_id = $sth -> fetchrow_array();
my $datetime=`date +%s`; exit if (!defined $host_id);
chomp($datetime); $sth->finish();
my $sth = $dbh->prepare("SELECT command_file FROM cfg_nagios WHERE nagios_activate = '1' LIMIT 1");
$sth->execute(); my $query_trap = "SELECT traps_id, traps_status from traps where traps_oid='$_[1]'";
my @conf = $sth->fetchrow_array(); $sth = $_[0]->prepare($query_trap);
$sth->finish(); $sth->execute();
my $submit = `/bin/echo "[$datetime] PROCESS_SERVICE_CHECK_RESULT;$this_host;$this_service;$status;$arguments_line" >> $conf[0]`; my ($trap_id, $trap_status) = $sth->fetchrow_array();
} exit if (!defined $trap_id);
} $sth->finish();
$dbh->disconnect();
exit; ##
} ### getting all "services by host" for given host
##
########################## my $st_query = "SELECT s.service_id, service_description, service_template_model_stm_id FROM service s, host_service_relation h";
## PARSE TRAP INFORMATIONS $st_query .= " where s.service_id=h.service_service_id and h.host_host_id='$host_id'";
# my $sth_st = $_[0]->prepare($st_query);
if (scalar(@ARGV)) { my @service = getServicesIncludeTemplate($_[0], $sth_st, $host_id, $trap_id);
my $ip = $ARGV[0]; $sth_st->finish;
my $oid = $ARGV[1];
my $arguments = $ARGV[2]; ##
getTrapsInfos($ip, $oid, $arguments); ### getting all "services by hostgroup" for given host
} ##
my $query_hostgroup_services = "SELECT s.service_id, service_description, service_template_model_stm_id FROM hostgroup_relation hgr, 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";
$sth_st = $_[0]->prepare($query_hostgroup_services);
$sth_st->execute();
@service = (@service,getServicesIncludeTemplate($_[0], $sth_st, $host_id, $trap_id));
$sth_st->finish;
return $trap_status, (@service);
}
#######################################
## GET HOSTNAME AND SERVICE DESCRIPTION
#
sub getTrapsInfos($$$){
my $ip = shift;
my $oid = shift;
my $arguments_line = shift;
my $dsn = "dbi:mysql:$mysql_database_oreon";
my $dbh = DBI->connect($dsn, $mysql_user, $mysql_passwd) or die "Echec de la connexion\n";
my @host = get_hostinfos($dbh, $ip);
foreach(@host) {
my $this_host = $_;
my ($status, @servicename) = get_servicename($dbh, $oid, $_);
foreach (@servicename) {
my $this_service = $_;
my $datetime=`date +%s`;
chomp($datetime);
my $sth = $dbh->prepare("SELECT command_file FROM cfg_nagios WHERE nagios_activate = '1' LIMIT 1");
$sth->execute();
my @conf = $sth->fetchrow_array();
$sth->finish();
my $submit = `/bin/echo "[$datetime] PROCESS_SERVICE_CHECK_RESULT;$this_host;$this_service;$status;$arguments_line" >> $conf[0]`;
}
}
$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);
}