Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid template loops (causing infinite loop) while calculating dimension #1516

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion gorgone/gorgone/modules/centreon/mbi/libs/centreon/Service.pm
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,55 @@ sub getServicesWithHostAndCategory {
return (\@results);
}

sub IstemplateLooping {
my $self = shift;
my $db = $self->{"centreon"};
my $logger = $self->{"logger"};
my $currentTemplate = shift;

my $query = "SELECT service_id, service_description, service_template_model_stm_id FROM service WHERE service_id = $currentTemplate";
my $sth = $db->query({ query => $query });
my $row = $sth->fetchrow_hashref();

my $parentId = $row->{"service_template_model_stm_id"};

my $isLooping = 0;
my $hasParent = 0;

my @parent_templates = (); # already used template

if (defined($parentId)) {
$hasParent = 1;
push(@parent_templates, $parentId);# add used template to array
}

while($hasParent){
my $query = "SELECT service_id, service_description, service_template_model_stm_id FROM service WHERE service_id = $parentId LIMIT 1";
my $sth = $db->query({ query => $query });
my $row = $sth->fetchrow_hashref();
my $grandParentId = $row->{"service_template_model_stm_id"};

if (!defined($grandParentId)) {
$hasParent = 0;
last;
}

for (my $i = 0; $i <= $#parent_templates; $i++)
{
if($parent_templates[$i] == $grandParentId)
{
$logger->writeLog("ERROR", "The service template with id = $currentTemplate inherits the template ID = $grandParentId more than once, please check relations of the template with ID = $currentTemplate !");
$logger->close();
$hasParent = 0;
$isLooping = 1;
last;
}
}
$parentId = $grandParentId;
}
return $isLooping;
}

sub getServicesTemplatesCategories {
my $self = shift;
my $db = $self->{"centreon"};
Expand All @@ -157,7 +206,7 @@ sub getServicesTemplatesCategories {
my $currentTemplate = $row->{"service_id"};
my $categories = $self->getServiceCategories($row->{"service_id"});
my $parentId = $row->{"service_template_model_stm_id"};
if (defined($parentId)) {
if (defined($parentId) && !IstemplateLooping($self,$currentTemplate)) {
my $hasParent = 1;
# getting all parent templates category relations
while ($hasParent) {
Expand Down
Loading