Skip to content

Commit

Permalink
enh(engine/conf): customvariable has its conf object now
Browse files Browse the repository at this point in the history
  • Loading branch information
bouda1 committed May 23, 2024
1 parent 843a91c commit 2e0e617
Show file tree
Hide file tree
Showing 28 changed files with 305 additions and 201 deletions.
4 changes: 2 additions & 2 deletions broker/neb/src/initial.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static void send_custom_variables_list(
nscvd.type = NEBTYPE_HOSTCUSTOMVARIABLE_ADD;
nscvd.timestamp.tv_sec = time(nullptr);
nscvd.var_name = const_cast<char*>(name.c_str());
nscvd.var_value = const_cast<char*>(cit->second.get_value().c_str());
nscvd.var_value = const_cast<char*>(cit->second.value().c_str());
nscvd.object_ptr = it->second.get();

// Callback.
Expand All @@ -103,7 +103,7 @@ static void send_custom_variables_list(
nscvd.type = NEBTYPE_SERVICECUSTOMVARIABLE_ADD;
nscvd.timestamp.tv_sec = time(nullptr);
nscvd.var_name = const_cast<char*>(name.c_str());
nscvd.var_value = const_cast<char*>(cit->second.get_value().c_str());
nscvd.var_value = const_cast<char*>(cit->second.value().c_str());
nscvd.object_ptr = it->second.get();

// Callback.
Expand Down
11 changes: 6 additions & 5 deletions engine/inc/com/centreon/engine/configuration/anomalydetection.hh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** Copyright 2011-2013,2015-2017, 2022 Centreon
** Copyright 2011-2013,2015-2017, 2022-2023 Centreon
**
** This file is part of Centreon Engine.
**
Expand All @@ -21,9 +21,9 @@
#define CCE_CONFIGURATION_ANOMALYDETECTION_HH

#include "com/centreon/engine/common.hh"
#include "com/centreon/engine/configuration/customvariable.hh"
#include "com/centreon/engine/configuration/group.hh"
#include "com/centreon/engine/configuration/object.hh"
#include "com/centreon/engine/customvariable.hh"
#include "com/centreon/engine/opt.hh"

namespace com::centreon::engine {
Expand Down Expand Up @@ -69,8 +69,9 @@ class anomalydetection : public object {
set_string& contacts() noexcept;
set_string const& contacts() const noexcept;
bool contacts_defined() const noexcept;
map_customvar const& customvariables() const noexcept;
map_customvar& customvariables() noexcept;
const std::unordered_map<std::string, customvariable>& customvariables()
const noexcept;
std::unordered_map<std::string, customvariable>& customvariables() noexcept;
std::string const& display_name() const noexcept;
std::string const& event_handler() const noexcept;
bool event_handler_enabled() const noexcept;
Expand Down Expand Up @@ -190,7 +191,7 @@ class anomalydetection : public object {
opt<unsigned int> _check_interval;
group<set_string> _contactgroups;
group<set_string> _contacts;
map_customvar _customvariables;
std::unordered_map<std::string, customvariable> _customvariables;
std::string _display_name;
std::string _event_handler;
opt<bool> _event_handler_enabled;
Expand Down
2 changes: 1 addition & 1 deletion engine/inc/com/centreon/engine/configuration/contact.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

#include <absl/container/flat_hash_map.h>

#include "com/centreon/engine/configuration/customvariable.hh"
#include "com/centreon/engine/configuration/group.hh"
#include "com/centreon/engine/configuration/object.hh"
#include "com/centreon/engine/customvariable.hh"
#include "com/centreon/engine/opt.hh"

typedef std::vector<std::string> tab_string;
Expand Down
48 changes: 48 additions & 0 deletions engine/inc/com/centreon/engine/configuration/customvariable.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2023 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : [email protected]
*
*/
#ifndef CCE_CONFIGURATION_CUSTOMVARIABLE_HH
#define CCE_CONFIGURATION_CUSTOMVARIABLE_HH
#include <string>

namespace com::centreon::engine::configuration {
/**
* @class customvariable customvariable.hh
* "com/centreon/engine/configuration/customvariable.hh"
* @brief This class represents a customvariable configuration.
*
* It is lighter than the engine::customvariable class and also it is
* separated from the engine core because the configuration module must be
* loadable from cbd.
*/
class customvariable {
std::string _value;
bool _is_sent;

public:
customvariable(std::string const& value = "", bool is_sent = false);
bool operator==(const customvariable& other) const;
~customvariable() noexcept = default;
void set_sent(bool sent);
bool is_sent() const;
void set_value(const std::string& value);
const std::string& value() const;
};
} // namespace com::centreon::engine::configuration

#endif /* !CCE_CONFIGURATION_CUSTOMVARIABLE_HH */
5 changes: 3 additions & 2 deletions engine/inc/com/centreon/engine/configuration/host.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
#define CCE_CONFIGURATION_HOST_HH

#include "com/centreon/engine/common.hh"
#include "com/centreon/engine/configuration/customvariable.hh"
#include "com/centreon/engine/configuration/group.hh"
#include "com/centreon/engine/configuration/object.hh"
#include "com/centreon/engine/configuration/point_2d.hh"
#include "com/centreon/engine/configuration/point_3d.hh"
#include "com/centreon/engine/customvariable.hh"
#include "com/centreon/engine/opt.hh"

namespace com::centreon::engine {
Expand Down Expand Up @@ -70,7 +70,8 @@ class host : public object {
point_3d const& coords_3d() const noexcept;
const std::unordered_map<std::string, customvariable>& customvariables()
const noexcept;
std::unordered_map<std::string, customvariable>& customvariables() noexcept;
std::unordered_map<std::string, customvariable>&
mut_customvariables() noexcept;
std::string const& display_name() const noexcept;
std::string const& event_handler() const noexcept;
bool event_handler_enabled() const noexcept;
Expand Down
53 changes: 26 additions & 27 deletions engine/inc/com/centreon/engine/configuration/service.hh
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
/*
** Copyright 2011-2013,2015-2017-2022 Centreon
**
** 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,2015-2017-2022 Centreon
*
* 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/>.
*/

#ifndef CCE_CONFIGURATION_SERVICE_HH
#define CCE_CONFIGURATION_SERVICE_HH

#include "com/centreon/engine/common.hh"
#include "com/centreon/engine/configuration/customvariable.hh"
#include "com/centreon/engine/configuration/group.hh"
#include "com/centreon/engine/configuration/object.hh"
#include "com/centreon/engine/customvariable.hh"
#include "com/centreon/engine/opt.hh"

namespace com::centreon::engine {

namespace configuration {
namespace com::centreon::engine::configuration {

class service : public object {
public:
Expand Down Expand Up @@ -68,8 +66,10 @@ class service : public object {
set_string& contacts() noexcept;
set_string const& contacts() const noexcept;
bool contacts_defined() const noexcept;
map_customvar const& customvariables() const noexcept;
map_customvar& customvariables() noexcept;
const std::unordered_map<std::string, customvariable>& customvariables()
const noexcept;
std::unordered_map<std::string, customvariable>&
mut_customvariables() noexcept;
std::string const& display_name() const noexcept;
std::string const& event_handler() const noexcept;
bool event_handler_enabled() const noexcept;
Expand Down Expand Up @@ -185,7 +185,7 @@ class service : public object {
std::string _check_period;
group<set_string> _contactgroups;
group<set_string> _contacts;
map_customvar _customvariables;
std::unordered_map<std::string, customvariable> _customvariables;
std::string _display_name;
std::string _event_handler;
opt<bool> _event_handler_enabled;
Expand Down Expand Up @@ -231,8 +231,7 @@ typedef std::list<service_ptr> list_service;
typedef std::set<service> set_service;
typedef std::unordered_map<std::pair<std::string, std::string>, service_ptr>
map_service;
} // namespace configuration

} // namespace com::centreon::engine
} // namespace com::centreon::engine::configuration

#endif // !CCE_CONFIGURATION_SERVICE_HH
4 changes: 2 additions & 2 deletions engine/inc/com/centreon/engine/customvariable.hh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class customvariable {
void set_sent(bool sent);
bool is_sent() const;
void set_value(std::string const& value);
std::string const& get_value() const;
const std::string& value() const;
bool has_been_modified() const;
void update(std::string const& value);

Expand All @@ -55,6 +55,6 @@ class customvariable {

typedef std::unordered_map<std::string, customvariable> map_customvar;

}
} // namespace com::centreon::engine

#endif // !CCE_OBJECTS_CUSTOMVARIABLE_HH
2 changes: 0 additions & 2 deletions engine/src/anomalydetection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1095,8 +1095,6 @@ bool anomalydetection::parse_perfdata(std::string const& perfdata,
/* We should master this string, so no need to check if it is utf-8 */
calculated_result.set_output(oss.str());

timestamp now(timestamp::now());

// Update check result.
timeval tv;
gettimeofday(&tv, nullptr);
Expand Down
15 changes: 6 additions & 9 deletions engine/src/commands/raw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,8 @@ void raw::_build_custom_contact_macro_environment(nagios_macros& macros,
// Set custom contact variable into the environement
for (auto const& cv : macros.custom_contact_vars) {
if (!cv.first.empty()) {
std::string value(
clean_macro_chars(cv.second.get_value(),
STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS));
std::string value(clean_macro_chars(
cv.second.value(), STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS));
std::string line;
line.append(MACRO_ENV_VAR_PREFIX);
line.append(cv.first);
Expand Down Expand Up @@ -429,9 +428,8 @@ void raw::_build_custom_host_macro_environment(nagios_macros& macros,
// Set custom host variable into the environement
for (auto const& cv : macros.custom_host_vars) {
if (!cv.first.empty()) {
std::string value(
clean_macro_chars(cv.second.get_value(),
STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS));
std::string value(clean_macro_chars(
cv.second.value(), STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS));
std::string line;
line.append(MACRO_ENV_VAR_PREFIX);
line.append(cv.first);
Expand Down Expand Up @@ -464,9 +462,8 @@ void raw::_build_custom_service_macro_environment(nagios_macros& macros,
// Set custom service variable into the environement
for (auto const& cv : macros.custom_service_vars) {
if (!cv.first.empty()) {
std::string value(
clean_macro_chars(cv.second.get_value(),
STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS));
std::string value(clean_macro_chars(
cv.second.value(), STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS));
std::string line;
line.append(MACRO_ENV_VAR_PREFIX);
line.append(cv.first);
Expand Down
1 change: 1 addition & 0 deletions engine/src/configuration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ set(FILES
"${SRC_DIR}/connector.cc"
"${SRC_DIR}/contact.cc"
"${SRC_DIR}/contactgroup.cc"
"${SRC_DIR}/customvariable.cc"
"${SRC_DIR}/extended_conf.cc"
"${SRC_DIR}/group.cc"
"${SRC_DIR}/host.cc"
Expand Down
6 changes: 3 additions & 3 deletions engine/src/configuration/anomalydetection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1012,8 +1012,8 @@ bool anomalydetection::contacts_defined() const noexcept {
*
* @return The customvariables.
*/
com::centreon::engine::map_customvar const& anomalydetection::customvariables()
const noexcept {
const std::unordered_map<std::string, customvariable>&
anomalydetection::customvariables() const noexcept {
return _customvariables;
}

Expand All @@ -1022,7 +1022,7 @@ com::centreon::engine::map_customvar const& anomalydetection::customvariables()
*
* @return The customvariables.
*/
com::centreon::engine::map_customvar&
std::unordered_map<std::string, customvariable>&
anomalydetection::customvariables() noexcept {
return _customvariables;
}
Expand Down
14 changes: 7 additions & 7 deletions engine/src/configuration/applier/anomalydetection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,13 @@ void applier::anomalydetection::add_object(
for (auto it = obj.customvariables().begin(),
end = obj.customvariables().end();
it != end; ++it) {
ad->custom_variables[it->first] = it->second;
ad->custom_variables[it->first] =
engine::customvariable(it->second.value(), it->second.is_sent());

if (it->second.is_sent()) {
timeval tv(get_broker_timestamp(nullptr));
broker_custom_variable(NEBTYPE_SERVICECUSTOMVARIABLE_ADD, ad,
it->first.c_str(), it->second.get_value().c_str(),
it->first.c_str(), it->second.value().c_str(),
&tv);
}
}
Expand Down Expand Up @@ -368,20 +369,19 @@ void applier::anomalydetection::modify_object(
if (c.second.is_sent()) {
timeval tv(get_broker_timestamp(nullptr));
broker_custom_variable(NEBTYPE_SERVICECUSTOMVARIABLE_DELETE, s.get(),
c.first.c_str(), c.second.get_value().c_str(),
&tv);
c.first.c_str(), c.second.value().c_str(), &tv);
}
}
s->custom_variables.clear();

for (auto& c : obj.customvariables()) {
s->custom_variables[c.first] = c.second;
s->custom_variables[c.first] =
engine::customvariable(c.second.value(), c.second.is_sent());

if (c.second.is_sent()) {
timeval tv(get_broker_timestamp(nullptr));
broker_custom_variable(NEBTYPE_SERVICECUSTOMVARIABLE_ADD, s.get(),
c.first.c_str(), c.second.get_value().c_str(),
&tv);
c.first.c_str(), c.second.value().c_str(), &tv);
}
}
}
Expand Down
Loading

0 comments on commit 2e0e617

Please sign in to comment.