Skip to content

Commit

Permalink
enh(sonar): replace new by shared_ptr in engine downtimes (#347)
Browse files Browse the repository at this point in the history
* cleanup(engine/compatibility): embedded_perl removed
* fix(broker/tests): a regression in ut fixed.
* fix(engine/downtimes): regression on schedule()
* fix(tests/resources): when a test fails we keep configs
* fix(engine/compatibility): common.cc file missing
* fix(cmake): find package cmake

Co-authored-by: David Boucher <dboucher@centreon.com>
  • Loading branch information
denliA and bouda1 authored Sep 5, 2022
1 parent 1bf2d8f commit e46c577
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 93 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ set(USER_ENGINE centreon-engine)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

find_package(Protobuf REQUIRED)
include(GNUInstallDirs)

# var directories.
Expand Down
8 changes: 7 additions & 1 deletion broker/core/test/file/splitter/permission_denied.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <gtest/gtest.h>
#include "com/centreon/broker/file/splitter.hh"
#include "com/centreon/exceptions/msg_fmt.hh"
#include "com/centreon/broker/file/disk_accessor.hh"

using namespace com::centreon::exceptions;
using namespace com::centreon::broker;
Expand All @@ -30,7 +31,12 @@ using namespace com::centreon::broker::file;

class FileSplitterPermissionDenied : public ::testing::Test {
public:
void SetUp() override { _path = FILE_WITH_BAD_PERMISSION; }
void SetUp() override {
file::disk_accessor::load(100000);
_path = FILE_WITH_BAD_PERMISSION;
}

void TearDown() override { file::disk_accessor::unload(); }

protected:
std::string _path;
Expand Down
2 changes: 0 additions & 2 deletions engine/enginerpc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ include_directories(
)


find_package(Protobuf REQUIRED)

include_directories(${CONAN_INCLUDE_DIRS_GRPC})

add_custom_command(
Expand Down
2 changes: 1 addition & 1 deletion engine/inc/com/centreon/engine/downtimes/downtime.hh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class downtime {

type get_type() const;
virtual bool is_stale() const = 0;
//virtual void schedule() = 0;
virtual void schedule() = 0;
virtual int unschedule() = 0;
virtual int subscribe() = 0;
virtual int handle() = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class downtime_manager {
uint64_t downtime_id);
int check_pending_flex_host_downtime(host* hst);
int check_pending_flex_service_downtime(service* svc);
void add_downtime(std::shared_ptr<downtime>&& dt) noexcept;
void add_downtime(const std::shared_ptr<downtime>& dt) noexcept;
void clear_scheduled_downtimes();
int check_for_expired_downtime();
int delete_downtime_by_hostname_service_description_start_time_comment(
Expand Down
2 changes: 1 addition & 1 deletion engine/inc/com/centreon/engine/downtimes/host_downtime.hh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class host_downtime : public downtime {
virtual ~host_downtime();

virtual bool is_stale() const override;
//virtual void schedule() override;
virtual void schedule() override;
virtual int unschedule() override;
virtual int subscribe() override;
virtual int handle() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class service_downtime : public downtime {
virtual ~service_downtime();
const char* service_description() const;
virtual bool is_stale() const override;
//virtual void schedule() override;
virtual void schedule() override;
virtual int unschedule() override;
virtual int subscribe() override;
virtual int handle() override;
Expand Down
37 changes: 19 additions & 18 deletions engine/src/compatibility/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
##
## Copyright 2011-2013 Merethis
##
## This file is part of Centreon Engine.
##
## Centreon Engine is free software: you can redistribute it and/or
## modify it under the terms of the GNU General Public License version 2
## as published by the Free Software Foundation.
##
## Centreon Engine 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.
##
## You should have received a copy of the GNU General Public License
## along with Centreon Engine. If not, see
## <http://www.gnu.org/licenses/>.
##
#
# Copyright 2011-2013 Merethis
#
# This file is part of Centreon Engine.
#
# Centreon Engine is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 2
# as published by the Free Software Foundation.
#
# Centreon Engine 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.
#
# You should have received a copy of the GNU General Public License
# along with Centreon Engine. If not, see
# <http://www.gnu.org/licenses/>.
#

# Set directories.
set(SRC_DIR "${SRC_DIR}/compatibility")
Expand All @@ -31,6 +31,7 @@ set(
${FILES}

# Sources.
"${SRC_DIR}/common.cc"
"${SRC_DIR}/globals.cc"
"${SRC_DIR}/logging.cc"
"${SRC_DIR}/mmap.cc"
Expand Down
42 changes: 42 additions & 0 deletions engine/src/compatibility/common.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
** Copyright 1999-2009 Ethan Galstad
** Copyright 2011-2013 Merethis
**
** This file is part of Centreon Engine.
**
** Centreon Engine is free software: you can redistribute it and/or
** modify it under the terms of the GNU General Public License version 2
** as published by the Free Software Foundation.
**
** Centreon Engine 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.
**
** You should have received a copy of the GNU General Public License
** along with Centreon Engine. If not, see
** <http://www.gnu.org/licenses/>.
*/

#include "common.h"

/**
* @brief Get the Centreon Engine last modification date.
*
* This value is no longer maintained. The last modification date is
* set to 01-01-1970.
*
* @return 01-01-1970.
*/
char const* get_program_modification_date() {
return (PROGRAM_MODIFICATION_DATE);
}

/**
* Get the Centreon Engine version.
*
* @return Centreon Engine version.
*/
char const* get_program_version() {
return (PROGRAM_VERSION);
}
68 changes: 22 additions & 46 deletions engine/src/downtimes/downtime_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ int downtime_manager::check_pending_flex_service_downtime(service* svc) {
service_downtime& dt(
*std::static_pointer_cast<service_downtime>(it->second));

service_map::const_iterator found(service::services.find(
{dt.get_hostname(), dt.service_description()}));
service_map::const_iterator found(
service::services.find({dt.get_hostname(), dt.service_description()}));

/* this entry matches our service! */
if (found != service::services.end() && found->second.get() == svc) {
Expand Down Expand Up @@ -230,37 +230,9 @@ void downtime_manager::clear_scheduled_downtimes() {
_scheduled_downtimes.clear();
}

void downtime_manager::add_downtime(std::shared_ptr<downtime>&& dt) noexcept {
downtime* dtt = dt.get();
_scheduled_downtimes.insert({dt->get_start_time(), std::move(dt)});
/* send data to event broker */
switch (dtt->get_type()) {
case downtime::service_downtime:
log_v2::functions()->trace(
"downtime_manager::add_downtime => service_downtime");
broker_downtime_data(
NEBTYPE_DOWNTIME_LOAD, NEBATTR_NONE, downtime::service_downtime,
dtt->get_hostname().c_str(), dtt->service_description(),
dtt->get_entry_time(), dtt->get_author().c_str(),
dtt->get_comment().c_str(), dtt->get_start_time(),
dtt->get_end_time(), dtt->is_fixed(), dtt->get_triggered_by(),
dtt->get_duration(), dtt->get_downtime_id(), nullptr);
break;
case downtime::host_downtime:
log_v2::functions()->trace(
"downtime_manager::add_downtime => host_downtime");
broker_downtime_data(
NEBTYPE_DOWNTIME_LOAD, NEBATTR_NONE, downtime::host_downtime,
dtt->get_hostname().c_str(), nullptr, dtt->get_entry_time(),
dtt->get_author().c_str(), dtt->get_comment().c_str(),
dtt->get_start_time(), dtt->get_end_time(), dtt->is_fixed(),
dtt->get_triggered_by(), dtt->get_duration(), dtt->get_downtime_id(),
nullptr);
break;
default:
log_v2::functions()->error("downtime_manager::add_downtime => bad downtime type");
break;
}
void downtime_manager::add_downtime(
const std::shared_ptr<downtime>& dt) noexcept {
_scheduled_downtimes.insert({dt->get_start_time(), dt});
}

int downtime_manager::check_for_expired_downtime() {
Expand Down Expand Up @@ -349,7 +321,8 @@ int downtime_manager::
service_downtime* svc{
dynamic_cast<service_downtime*>(it->second.get())};

if (!svc || std::string(svc->service_description()) != service_description)
if (!svc ||
std::string(svc->service_description()) != service_description)
continue;
}
}
Expand Down Expand Up @@ -445,15 +418,15 @@ uint64_t downtime_manager::get_next_downtime_id() {

/* saves a host downtime entry */
void downtime_manager::add_new_host_downtime(std::string const& host_name,
time_t entry_time,
char const* author,
char const* comment_data,
time_t start_time,
time_t end_time,
bool fixed,
uint64_t triggered_by,
unsigned long duration,
uint64_t* downtime_id) {
time_t entry_time,
char const* author,
char const* comment_data,
time_t start_time,
time_t end_time,
bool fixed,
uint64_t triggered_by,
unsigned long duration,
uint64_t* downtime_id) {
if (host_name.empty())
throw engine_error()
<< "can not create a host downtime on host with empty name";
Expand All @@ -465,7 +438,8 @@ void downtime_manager::add_new_host_downtime(std::string const& host_name,
auto retval{std::make_shared<host_downtime>(
host_name, entry_time, author, comment_data, start_time, end_time, fixed,
triggered_by, duration, new_downtime_id)};
add_downtime(std::move(retval));
instance().add_downtime(retval);
retval->schedule();

/* save downtime id */
if (downtime_id)
Expand Down Expand Up @@ -502,7 +476,8 @@ void downtime_manager::add_new_service_downtime(
auto retval{std::make_shared<service_downtime>(
host_name, service_description, entry_time, author, comment_data,
start_time, end_time, fixed, triggered_by, duration, new_downtime_id)};
add_downtime(std::move(retval));
instance().add_downtime(retval);
retval->schedule();

/* save downtime id */
if (downtime_id)
Expand Down Expand Up @@ -540,7 +515,8 @@ int downtime_manager::schedule_downtime(downtime::type type,

if (start_time > 4102441200) {
engine_logger(log_verification_error, basic)
<< "SCHEDULE DOWNTIME ALERT : start time is out of range and setted to "
<< "SCHEDULE DOWNTIME ALERT : start time is out of range and setted "
"to "
"1/1/2100 00:00";
log_v2::config()->warn(
"SCHEDULE DOWNTIME ALERT : start time is out of range and setted to "
Expand Down
18 changes: 8 additions & 10 deletions engine/src/downtimes/host_downtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,11 @@ int host_downtime::handle() {
return OK;
}

//void host_downtime::schedule() {
// downtime_manager::instance().add_downtime(this);
//
// /* send data to event broker */
// broker_downtime_data(NEBTYPE_DOWNTIME_LOAD, NEBATTR_NONE,
// downtime::host_downtime, _hostname.c_str(), nullptr,
// _entry_time, _author.c_str(), _comment.c_str(),
// _start_time, _end_time, _fixed, _triggered_by, _duration,
// _downtime_id, nullptr);
//}
void host_downtime::schedule() {
/* send data to event broker */
broker_downtime_data(NEBTYPE_DOWNTIME_LOAD, NEBATTR_NONE,
downtime::host_downtime, _hostname.c_str(), nullptr,
_entry_time, _author.c_str(), _comment.c_str(),
_start_time, _end_time, _fixed, _triggered_by, _duration,
_downtime_id, nullptr);
}
22 changes: 10 additions & 12 deletions engine/src/downtimes/service_downtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -549,15 +549,13 @@ const char* service_downtime::service_description() const {
return _service_description.c_str();
}

//void service_downtime::schedule() {
// engine_logger(dbg_functions, basic) << "service_downtime::schedule()";
// log_v2::functions()->trace("service_downtime::schedule()");
// downtime_manager::instance().add_downtime(this);
//
// /* send data to event broker */
// broker_downtime_data(
// NEBTYPE_DOWNTIME_LOAD, NEBATTR_NONE, downtime::service_downtime,
// _hostname.c_str(), _service_description.c_str(), _entry_time,
// _author.c_str(), _comment.c_str(), _start_time, _end_time, _fixed,
// _triggered_by, _duration, _downtime_id, nullptr);
//}
void service_downtime::schedule() {
log_v2::functions()->trace("service_downtime::schedule()");

/* send data to event broker */
broker_downtime_data(
NEBTYPE_DOWNTIME_LOAD, NEBATTR_NONE, downtime::service_downtime,
_hostname.c_str(), _service_description.c_str(), _entry_time,
_author.c_str(), _comment.c_str(), _start_time, _end_time, _fixed,
_triggered_by, _duration, _downtime_id, nullptr);
}
2 changes: 2 additions & 0 deletions engine/src/retention/applier/downtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void applier::downtime::_add_host_downtime(
obj.start_time(), obj.end_time(), obj.fixed(), obj.triggered_by(),
obj.duration(), obj.downtime_id())};
downtimes::downtime_manager::instance().add_downtime(dt);
dt->schedule();
downtimes::downtime_manager::instance().register_downtime(
downtimes::downtime::host_downtime, obj.downtime_id());
}
Expand All @@ -71,6 +72,7 @@ void applier::downtime::_add_service_downtime(
obj.author(), obj.comment_data(), obj.start_time(), obj.end_time(),
obj.fixed(), obj.triggered_by(), obj.duration(), obj.downtime_id())};
downtimes::downtime_manager::instance().add_downtime(dt);
dt->schedule();
downtimes::downtime_manager::instance().register_downtime(
downtimes::downtime::service_downtime, obj.downtime_id());
}
2 changes: 2 additions & 0 deletions tests/resources/resources.robot
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ Save Logs
Copy files ${centralLog} ${failDir}
Copy files ${moduleLog0} ${failDir}
Copy files ${engineLog0} ${failDir}
Copy Files ${EtcRoot}/centreon-engine/config0/*.cfg ${failDir}/etc/centreon-engine/config0
Copy Files ${EtcRoot}/centreon-broker/*.json ${failDir}/etc/centreon-broker

*** Variables ***
${BROKER_LOG} ${VarRoot}/log/centreon-broker
Expand Down

0 comments on commit e46c577

Please sign in to comment.