diff --git a/agent/src/main.cc b/agent/src/main.cc index 284ef6ec129..222aaf1f146 100644 --- a/agent/src/main.cc +++ b/agent/src/main.cc @@ -35,6 +35,8 @@ static std::shared_ptr _streaming_client; static std::shared_ptr _streaming_server; +static asio::signal_set _signals(*g_io_context, SIGTERM, SIGUSR1, SIGUSR2); + static void signal_handler(const boost::system::error_code& error, int signal_number) { if (!error) { @@ -58,6 +60,7 @@ static void signal_handler(const boost::system::error_code& error, } break; } + _signals.async_wait(signal_handler); } } @@ -118,18 +121,29 @@ int main(int argc, char* argv[]) { "{} or increase by kill -USR2 {}", getpid(), getpid()); - sigignore(SIGPIPE); - po::variables_map vm; - po::store(po::parse_command_line(argc, argv, desc), vm); - po::notify(vm); - if (vm.count("help")) { - std::cout << desc << "\n"; - return 1; + try { + po::store(po::parse_command_line(argc, argv, desc), vm); + po::notify(vm); + + if (vm.count("help")) { + std::cout << desc << "\n"; + return 1; + } + + if (vm.count("config-file")) { + po::store(po::parse_config_file( + vm["config-file"].as().c_str(), desc), + vm); + } + + } catch (const std::exception& e) { + SPDLOG_ERROR("fail to parse arguments {}", e.what()); + return 2; } - const std::string logger_name = "centeon-agent"; + const std::string logger_name = "centreon-agent"; auto logger_file = [&]() { if (vm.count("logger-file")) { @@ -175,13 +189,18 @@ int main(int argc, char* argv[]) { getpid(), getpid()); std::shared_ptr conf; std::string supervised_host; + try { - asio::signal_set signals(*g_io_context, SIGTERM, SIGUSR1, SIGUSR2); - signals.async_wait(signal_handler); + // ignored but mandatory because of forks + _signals.add(SIGPIPE); + + _signals.async_wait(signal_handler); if (!vm.count("endpoint")) { SPDLOG_CRITICAL( "endpoint param is mandatory (represents where to connect or where " "to listen example: 127.0.0.1:4317)"); + // in order to avoid service restart non stop + std::this_thread::sleep_for(std::chrono::seconds(10)); return -1; } std::string host_port = vm["endpoint"].as(); diff --git a/agent/src/streaming_client.cc b/agent/src/streaming_client.cc index cca96413294..5fa122c83cd 100644 --- a/agent/src/streaming_client.cc +++ b/agent/src/streaming_client.cc @@ -129,6 +129,7 @@ void streaming_client::_start() { * */ void streaming_client::_create_reactor() { + std::lock_guard l(_protect); if (_reactor) { _reactor->shutdown(); } diff --git a/clib/inc/com/centreon/process.hh b/clib/inc/com/centreon/process.hh index 475aef8e7ac..9948208a690 100644 --- a/clib/inc/com/centreon/process.hh +++ b/clib/inc/com/centreon/process.hh @@ -105,7 +105,7 @@ class process { bool in_stream = true, bool out_stream = true, bool err_stream = true); - virtual ~process() noexcept; + virtual ~process(); process(const process&) = delete; process& operator=(const process&) = delete; // void enable_stream(stream s, bool enable); @@ -129,6 +129,6 @@ class process { void set_timeout(bool timeout); }; -} +} // namespace com::centreon #endif // !CC_PROCESS_POSIX_HH diff --git a/clib/src/process.cc b/clib/src/process.cc index d5ef993b9bd..68245e2ca65 100644 --- a/clib/src/process.cc +++ b/clib/src/process.cc @@ -1,20 +1,20 @@ /** -* Copyright 2012-2013,2020-2021 Centreon -* -* 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 : contact@centreon.com -*/ + * Copyright 2012-2013,2020-2021 Centreon + * + * 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 : contact@centreon.com + */ #include @@ -63,7 +63,7 @@ process::process(process_listener* listener, /** * Destructor. */ -process::~process() noexcept { +process::~process() { std::unique_lock lock(_lock_process); _kill(SIGKILL); _cv_process_running.wait(lock, [this] { return !_is_running(); }); @@ -632,6 +632,7 @@ void process::_kill(int sig) { throw basic_error() << "could not terminate process " << _process << ": " << msg; } + _process = 0; } } diff --git a/engine/modules/opentelemetry/CMakeLists.txt b/engine/modules/opentelemetry/CMakeLists.txt index bb13dac0417..36dd0d60a76 100644 --- a/engine/modules/opentelemetry/CMakeLists.txt +++ b/engine/modules/opentelemetry/CMakeLists.txt @@ -95,7 +95,8 @@ target_link_libraries(opentelemetry add_dependencies(opentelemetry pb_open_telemetry_lib - pb_neb_lib) + pb_neb_lib + engine_rpc) target_include_directories(opentelemetry PRIVATE "${MODULE_DIR}/inc/com/centreon/engine/modules/opentelemetry" diff --git a/engine/modules/opentelemetry/src/centreon_agent/agent_config.cc b/engine/modules/opentelemetry/src/centreon_agent/agent_config.cc index dd03c8aaadf..0ad096246e4 100644 --- a/engine/modules/opentelemetry/src/centreon_agent/agent_config.cc +++ b/engine/modules/opentelemetry/src/centreon_agent/agent_config.cc @@ -75,8 +75,8 @@ agent_config::agent_config(const rapidjson::Value& json_config_v) { _export_period = file_content.get_unsigned("export_period", 60); _check_timeout = file_content.get_unsigned("_check_timeout", 30); - if (file_content.has_member("agent_reverse_server")) { - const auto& reverse_array = file_content.get_member("agent_reverse_server"); + if (file_content.has_member("reverse_connections")) { + const auto& reverse_array = file_content.get_member("reverse_connections"); for (auto conf_iter = reverse_array.Begin(); conf_iter != reverse_array.End(); ++conf_iter) { _agent_grpc_reverse_conf.insert( diff --git a/engine/modules/opentelemetry/src/centreon_agent/agent_reverse_client.cc b/engine/modules/opentelemetry/src/centreon_agent/agent_reverse_client.cc index 5f659b083e5..edcb8a99aba 100644 --- a/engine/modules/opentelemetry/src/centreon_agent/agent_reverse_client.cc +++ b/engine/modules/opentelemetry/src/centreon_agent/agent_reverse_client.cc @@ -57,6 +57,15 @@ void agent_reverse_client::update(const agent_config::pointer& new_conf) { absl::MutexLock l(&_agents_m); auto connection_iterator = _agents.begin(); + + if (!new_conf) { + while (connection_iterator != _agents.end()) { + _shutdown_connection(connection_iterator); + connection_iterator = _agents.erase(connection_iterator); + } + return; + } + auto conf_iterator = new_conf->get_agent_grpc_reverse_conf().begin(); while (connection_iterator != _agents.end() && diff --git a/engine/modules/opentelemetry/src/centreon_agent/agent_service.cc b/engine/modules/opentelemetry/src/centreon_agent/agent_service.cc index 856d002c340..23c55e60adf 100644 --- a/engine/modules/opentelemetry/src/centreon_agent/agent_service.cc +++ b/engine/modules/opentelemetry/src/centreon_agent/agent_service.cc @@ -85,7 +85,14 @@ agent_service::agent_service( : _io_context(io_context), _conf(conf), _metric_handler(handler), - _logger(logger) {} + _logger(logger) { + if (!_conf) { + _conf = std::make_shared(60, 100, 10, 30); + SPDLOG_LOGGER_INFO(logger, + "no centreon_agent configuration given => we use a " + "default configuration "); + } +} /** * @brief prefered way to construct an agent_service diff --git a/engine/modules/opentelemetry/src/host_serv_extractor.cc b/engine/modules/opentelemetry/src/host_serv_extractor.cc index bbb26cdb215..6a9ed8506ad 100644 --- a/engine/modules/opentelemetry/src/host_serv_extractor.cc +++ b/engine/modules/opentelemetry/src/host_serv_extractor.cc @@ -87,17 +87,26 @@ host_serv_attributes_extractor::host_serv_attributes_extractor( [](po::options_description& desc) { desc.add_options()( "host_path", po::value(), - "where to find host name. Example: " - "resourceMetrics.scopeMetrics.metrics.dataPoints.attributes.host"); - desc.add_options()("service_path", po::value(), - "where to find service description. Example: " - "resourceMetrics.scopeMetrics.metrics.dataPoints." - "attributes.service"); + "where to find host name. Example:\n" + "resource_metrics.scopeMetrics.metrics.dataPoints.attributes.host\n" + "or\n" + "resource_metrics.resource.attributes.host\n" + "or\n" + "resource_metrics.scope_metrics.scope.attributes.host"); + desc.add_options()( + "service_path", po::value(), + "where to find service description. Example:\n" + "resource_metrics.scope_metrics.data.data_points.attributes." + "service\n" + "or\n" + "resource_metrics.resource.attributes.service\n" + "or\n" + "resource_metrics.scope_metrics.scope.attributes.service"); }); static auto parse_path = [](const std::string& path, attribute_owner& attr, std::string& key) { - static re2::RE2 path_extractor("\\.(\\w+)\\.attributes\\.(\\w+)"); + static re2::RE2 path_extractor("(?i)\\.(\\w+)\\.attributes\\.([\\.\\w]+)"); std::string sz_attr; if (!RE2::PartialMatch(path, path_extractor, &sz_attr, &key)) { throw exceptions::msg_fmt( diff --git a/engine/modules/opentelemetry/src/otl_config.cc b/engine/modules/opentelemetry/src/otl_config.cc index 570f39ff14e..f0c62dda374 100644 --- a/engine/modules/opentelemetry/src/otl_config.cc +++ b/engine/modules/opentelemetry/src/otl_config.cc @@ -64,6 +64,10 @@ static constexpr std::string_view _grpc_config_schema(R"( "telegraf_conf_server": { "description": "http(s) telegraf config server", "type": "object" + }, + "centreon_agent": { + "description": "config of centreon_agent", + "type": "object" } }, "type" : "object" diff --git a/engine/src/configuration/applier/state.cc b/engine/src/configuration/applier/state.cc index 6c338127ab3..9df18ec97d5 100644 --- a/engine/src/configuration/applier/state.cc +++ b/engine/src/configuration/applier/state.cc @@ -460,6 +460,7 @@ void applier::state::_apply(configuration::state const& new_cfg) { config->log_level_downtimes(new_cfg.log_level_downtimes()); config->log_level_comments(new_cfg.log_level_comments()); config->log_level_macros(new_cfg.log_level_macros()); + config->log_level_otl(new_cfg.log_level_otl()); config->use_true_regexp_matching(new_cfg.use_true_regexp_matching()); config->use_send_recovery_notifications_anyways( new_cfg.use_send_recovery_notifications_anyways()); @@ -1168,10 +1169,10 @@ void applier::state::apply_log_config(configuration::state& new_cfg) { broker_sink->set_level(spdlog::level::info); log_cfg.add_custom_sink(broker_sink); - log_cfg.apply_custom_sinks({"functions", "config", "events", "checks", - "notifications", "eventbroker", - "external_command", "commands", "downtimes", - "comments", "macros", "process", "runtime"}); + log_cfg.apply_custom_sinks( + {"functions", "config", "events", "checks", "notifications", + "eventbroker", "external_command", "commands", "downtimes", "comments", + "macros", "process", "runtime", "otl"}); log_cfg.set_level("functions", new_cfg.log_level_functions()); log_cfg.set_level("config", new_cfg.log_level_config()); log_cfg.set_level("events", new_cfg.log_level_events()); @@ -1185,6 +1186,7 @@ void applier::state::apply_log_config(configuration::state& new_cfg) { log_cfg.set_level("macros", new_cfg.log_level_macros()); log_cfg.set_level("process", new_cfg.log_level_process()); log_cfg.set_level("runtime", new_cfg.log_level_runtime()); + log_cfg.set_level("otel", new_cfg.log_level_otl()); if (has_already_been_loaded) log_cfg.allow_only_atomic_changes(true); log_v2::instance().apply(log_cfg); diff --git a/engine/tests/opentelemetry/agent_to_engine_test.cc b/engine/tests/opentelemetry/agent_to_engine_test.cc index e601dea0eaf..0b5237a8f2f 100644 --- a/engine/tests/opentelemetry/agent_to_engine_test.cc +++ b/engine/tests/opentelemetry/agent_to_engine_test.cc @@ -28,6 +28,7 @@ #include "opentelemetry/proto/collector/metrics/v1/metrics_service.grpc.pb.h" #include "opentelemetry/proto/metrics/v1/metrics.pb.h" +#include "com/centreon/engine/contact.hh" #include "com/centreon/engine/host.hh" #include "com/centreon/engine/service.hh" @@ -77,6 +78,13 @@ class agent_to_engine_test : public TestEngine { spdlog::default_logger()->set_level(spdlog::level::trace); ::fmt::formatter< ::opentelemetry::proto::collector::metrics::v1:: ExportMetricsServiceRequest>::json_grpc_format = true; + timeperiod::timeperiods.clear(); + contact::contacts.clear(); + host::hosts.clear(); + host::hosts_by_id.clear(); + service::services.clear(); + service::services_by_id.clear(); + init_config_state(); configuration::applier::connector conn_aply; @@ -307,8 +315,10 @@ TEST_F(agent_to_engine_test, server_send_conf_to_agent_and_receive_metrics) { serv_2_found = true; } else if (compare_to_expected_host_metric(*to_compare)) { host_metric_found = true; - } else + } else { + SPDLOG_ERROR("bad resource metric: {}", to_compare->DebugString()); ASSERT_TRUE(false); + } } ASSERT_TRUE(host_metric_found); ASSERT_TRUE(serv_1_found); diff --git a/engine/tests/test_engine.cc b/engine/tests/test_engine.cc index ef892f82c87..c2183609ca1 100644 --- a/engine/tests/test_engine.cc +++ b/engine/tests/test_engine.cc @@ -141,7 +141,7 @@ configuration::host TestEngine::new_configuration_host( hst.parse("contacts", contacts.c_str()); configuration::command cmd("hcmd"); - cmd.parse("command_line", "echo 0"); + cmd.parse("command_line", "/bin/echo 0"); if (!connector.empty()) { cmd.parse("connector", connector.data()); } @@ -193,7 +193,9 @@ configuration::service TestEngine::new_configuration_service( svc.set_host_id(12); configuration::command cmd(fmt::format("cmd_serv_{}", svc_id)); - cmd.parse("command_line", "echo -n 'output| metric=$ARG1$;50;75 metric2=30ms;50:75;75:80;0;100'"); + cmd.parse("command_line", + "/bin/echo -n 'output| metric=$ARG1$;50;75 " + "metric2=30ms;50:75;75:80;0;100'"); if (!connector.empty()) { cmd.parse("connector", connector.data()); } diff --git a/tests/broker-engine/opentelemetry.robot b/tests/broker-engine/opentelemetry.robot index b9050880ad3..020196c8640 100644 --- a/tests/broker-engine/opentelemetry.robot +++ b/tests/broker-engine/opentelemetry.robot @@ -2,6 +2,7 @@ Documentation Engine/Broker tests on opentelemetry engine server Resource ../resources/import.resource +Library ../resources/Agent.py Suite Setup Ctn Clean Before Suite Suite Teardown Ctn Clean After Suite @@ -302,7 +303,7 @@ BEOTEL_SERVE_TELEGRAF_CONFIGURATION_CRYPTED Sleep 1 ${telegraf_conf_response} GET ... verify=${False} - ... url=https://localhost:1443/engine?host=host_1&host=host_2&host=host_3 + ... url=https://localhost:1443/engine?host=host_1 Should Be Equal As Strings ${telegraf_conf_response.reason} OK no response received or error response ${content_compare_result} Ctn Compare String With File @@ -375,7 +376,7 @@ BEOTEL_SERVE_TELEGRAF_CONFIGURATION_NO_CRYPTED Should Be True ${result} "server listen on 0.0.0.0:1443" should be available. Sleep 1 ${telegraf_conf_response} GET - ... url=http://localhost:1443/engine?host=host_1&host=host_2&host=host_3 + ... url=http://localhost:1443/engine?host=host_1 Should Be Equal As Strings ${telegraf_conf_response.reason} OK no response received or error response @@ -389,6 +390,335 @@ BEOTEL_SERVE_TELEGRAF_CONFIGURATION_NO_CRYPTED ... unexpected telegraf server response: ${telegraf_conf_response.text} +BEOTEL_CENTREON_AGENT_CHECK_HOST + [Documentation] agent check host and we expect to get it in check result + [Tags] broker engine opentelemetry MON-63843 + Ctn Config Engine ${1} ${2} ${2} + Ctn Add Otl ServerModule + ... 0 + ... {"otel_server":{"host": "0.0.0.0","port": 4317},"max_length_grpc_log":0} + Ctn Config Add Otl Connector + ... 0 + ... OTEL connector + ... opentelemetry --processor=centreon_agent --extractor=attributes --host_path=resource_metrics.resource.attributes.host.name --service_path=resource_metrics.resource.attributes.service.name + Ctn Engine Config Replace Value In Hosts ${0} host_1 check_command otel_check_icmp + Ctn Engine Config Add Command + ... ${0} + ... otel_check_icmp + ... /usr/lib/nagios/plugins/check_icmp 127.0.0.1 + ... OTEL connector + + Ctn Engine Config Set Value 0 log_level_checks trace + + Ctn Config Broker central + Ctn Config Broker module + Ctn Config Broker rrd + Ctn Config Centreon Agent + Ctn Broker Config Log central sql trace + + Ctn ConfigBBDO3 1 + Ctn Clear Retention + + ${start} Get Current Date + Ctn Start Broker + Ctn Start Engine + Ctn Start Agent + + # Let's wait for the otel server start + ${content} Create List unencrypted server listening on 0.0.0.0:4317 + ${result} Ctn Find In Log With Timeout ${engineLog0} ${start} ${content} 10 + Should Be True ${result} "unencrypted server listening on 0.0.0.0:4317" should be available. + Sleep 1 + + ${start} Ctn Get Round Current Date + Ctn Schedule Forced Host Check host_1 + + ${result} Ctn Check Host Check Status With Timeout host_1 30 ${start} 0 OK - 127.0.0.1 + Should Be True ${result} hosts table not updated + + +BEOTEL_CENTREON_AGENT_CHECK_SERVICE + [Documentation] agent check service and we expect to get it in check result + [Tags] broker engine opentelemetry MON-63843 + Ctn Config Engine ${1} ${2} ${2} + Ctn Add Otl ServerModule + ... 0 + ... {"otel_server":{"host": "0.0.0.0","port": 4317},"max_length_grpc_log":0,"centreon_agent":{"check_interval":10, "export_period":15}} + Ctn Config Add Otl Connector + ... 0 + ... OTEL connector + ... opentelemetry --processor=centreon_agent --extractor=attributes --host_path=resource_metrics.resource.attributes.host.name --service_path=resource_metrics.resource.attributes.service.name + Ctn Engine Config Replace Value In Services ${0} service_1 check_command otel_check + Ctn Engine Config Add Command + ... ${0} + ... otel_check + ... /tmp/var/lib/centreon-engine/check.pl --id 456 + ... OTEL connector + + Ctn Engine Config Set Value 0 log_level_checks trace + + #service_1 check fail CRITICAL + Ctn Set Command Status 456 ${2} + + Ctn Config Broker central + Ctn Config Broker module + Ctn Config Broker rrd + Ctn Config Centreon Agent + Ctn Broker Config Log central sql trace + + Ctn ConfigBBDO3 1 + Ctn Clear Retention + + ${start} Ctn Get Round Current Date + Ctn Start Broker + Ctn Start Engine + Ctn Start Agent + + # Let's wait for the otel server start + ${content} Create List unencrypted server listening on 0.0.0.0:4317 + ${result} Ctn Find In Log With Timeout ${engineLog0} ${start} ${content} 10 + Should Be True ${result} "unencrypted server listening on 0.0.0.0:4317" should be available. + + ${content} Create List fifos:{"host_1,service_1" + ${result} Ctn Find In Log With Timeout ${engineLog0} ${start} ${content} 30 + Should Be True ${result} fifos not found in logs + + Ctn Schedule Forced Svc Check host_1 service_1 + + ${result} Ctn Check Service Check Status With Timeout host_1 service_1 60 ${start} 2 Test check 456 + Should Be True ${result} services table not updated + + ${start} Ctn Get Round Current Date + #service_1 check ok + Ctn Set Command Status 456 ${0} + + ${content} Create List as_int: 0 + ${result} Ctn Find In Log With Timeout ${engineLog0} ${start} ${content} 30 + Should Be True ${result} status 0 not found in logs + + Ctn Schedule Forced Svc Check host_1 service_1 + + ${result} Ctn Check Service Check Status With Timeout host_1 service_1 60 ${start} 0 Test check 456 + Should Be True ${result} services table not updated + + +BEOTEL_REVERSE_CENTREON_AGENT_CHECK_HOST + [Documentation] agent check host with reversed connection and we expect to get it in check result + [Tags] broker engine opentelemetry MON-63843 + Ctn Config Engine ${1} ${2} ${2} + Ctn Add Otl ServerModule + ... 0 + ... {"max_length_grpc_log":0,"centreon_agent":{"check_interval":10, "export_period":15, "reverse_connections":[{"host": "127.0.0.1","port": 4317}]}} + Ctn Config Add Otl Connector + ... 0 + ... OTEL connector + ... opentelemetry --processor=centreon_agent --extractor=attributes --host_path=resource_metrics.resource.attributes.host.name --service_path=resource_metrics.resource.attributes.service.name + Ctn Engine Config Replace Value In Hosts ${0} host_1 check_command otel_check_icmp + Ctn Engine Config Add Command + ... ${0} + ... otel_check_icmp + ... /usr/lib/nagios/plugins/check_icmp 127.0.0.1 + ... OTEL connector + + Ctn Engine Config Set Value 0 log_level_checks trace + + Ctn Config Broker central + Ctn Config Broker module + Ctn Config Broker rrd + Ctn Config Reverse Centreon Agent + Ctn Broker Config Log central sql trace + + Ctn ConfigBBDO3 1 + Ctn Clear Retention + + ${start} Get Current Date + Ctn Start Broker + Ctn Start Engine + Ctn Start Agent + + # Let's wait for engine to connect to agent + ${content} Create List init from [.\\s]*127.0.0.1:4317 + ${result} Ctn Find Regex In Log With Timeout ${engineLog0} ${start} ${content} 10 + Should Be True ${result} "init from localhost:4317" not found in log + Sleep 1 + + ${start} Ctn Get Round Current Date + Ctn Schedule Forced Host Check host_1 + + ${result} Ctn Check Host Check Status With Timeout host_1 30 ${start} 0 OK - 127.0.0.1 + Should Be True ${result} hosts table not updated + + +BEOTEL_REVERSE_CENTREON_AGENT_CHECK_SERVICE + [Documentation] agent check service with reversed connection and we expect to get it in check result + [Tags] broker engine opentelemetry MON-63843 + Ctn Config Engine ${1} ${2} ${2} + Ctn Add Otl ServerModule + ... 0 + ... {"max_length_grpc_log":0,"centreon_agent":{"check_interval":10, "export_period":15, "reverse_connections":[{"host": "127.0.0.1","port": 4317}]}} + Ctn Config Add Otl Connector + ... 0 + ... OTEL connector + ... opentelemetry --processor=centreon_agent --extractor=attributes --host_path=resource_metrics.resource.attributes.host.name --service_path=resource_metrics.resource.attributes.service.name + Ctn Engine Config Replace Value In Services ${0} service_1 check_command otel_check + Ctn Engine Config Add Command + ... ${0} + ... otel_check + ... /tmp/var/lib/centreon-engine/check.pl --id 456 + ... OTEL connector + + Ctn Engine Config Set Value 0 log_level_checks trace + + #service_1 check fail CRITICAL + Ctn Set Command Status 456 ${2} + + Ctn Config Broker central + Ctn Config Broker module + Ctn Config Broker rrd + Ctn Config Reverse Centreon Agent + Ctn Broker Config Log central sql trace + + Ctn ConfigBBDO3 1 + Ctn Clear Retention + + ${start} Ctn Get Round Current Date + Ctn Start Broker + Ctn Start Engine + Ctn Start Agent + + # Let's wait for engine to connect to agent + ${content} Create List init from [.\\s]*127.0.0.1:4317 + ${result} Ctn Find Regex In Log With Timeout ${engineLog0} ${start} ${content} 10 + Should Be True ${result} "init from 127.0.0.1:4317" not found in log + + + ${content} Create List fifos:{"host_1,service_1" + ${result} Ctn Find In Log With Timeout ${engineLog0} ${start} ${content} 30 + Should Be True ${result} fifos not found in logs + + Ctn Schedule Forced Svc Check host_1 service_1 + + ${result} Ctn Check Service Check Status With Timeout host_1 service_1 60 ${start} 2 Test check 456 + Should Be True ${result} services table not updated + + ${start} Ctn Get Round Current Date + #service_1 check ok + Ctn Set Command Status 456 ${0} + + ${content} Create List as_int: 0 + ${result} Ctn Find In Log With Timeout ${engineLog0} ${start} ${content} 30 + Should Be True ${result} status 0 not found in logs + + Ctn Schedule Forced Svc Check host_1 service_1 + + ${result} Ctn Check Service Check Status With Timeout host_1 service_1 60 ${start} 0 Test check 456 + Should Be True ${result} services table not updated + +BEOTEL_CENTREON_AGENT_CHECK_HOST_CRYPTED + [Documentation] agent check host with encrypted connection and we expect to get it in check result + [Tags] broker engine opentelemetry MON-63843 + Ctn Config Engine ${1} ${2} ${2} + Copy File ../broker/grpc/test/grpc_test_keys/ca_1234.crt /tmp/ + Copy File ../broker/grpc/test/grpc_test_keys/server_1234.key /tmp/ + Copy File ../broker/grpc/test/grpc_test_keys/server_1234.crt /tmp/ + Ctn Add Otl ServerModule + ... 0 + ... {"otel_server":{"host": "0.0.0.0","port": 4317, "encryption": true, "public_cert": "/tmp/server_1234.crt", "private_key": "/tmp/server_1234.key", "ca_certificate": "/tmp/ca_1234.crt"},"max_length_grpc_log":0} + Ctn Config Add Otl Connector + ... 0 + ... OTEL connector + ... opentelemetry --processor=centreon_agent --extractor=attributes --host_path=resource_metrics.resource.attributes.host.name --service_path=resource_metrics.resource.attributes.service.name + Ctn Engine Config Replace Value In Hosts ${0} host_1 check_command otel_check_icmp + Ctn Engine Config Add Command + ... ${0} + ... otel_check_icmp + ... /usr/lib/nagios/plugins/check_icmp 127.0.0.1 + ... OTEL connector + + Ctn Engine Config Set Value 0 log_level_checks trace + + Ctn Config Broker central + Ctn Config Broker module + Ctn Config Broker rrd + Ctn Config Centreon Agent ${None} ${None} /tmp/ca_1234.crt + Ctn Broker Config Log central sql trace + + Ctn ConfigBBDO3 1 + Ctn Clear Retention + + ${start} Get Current Date + Ctn Start Broker + Ctn Start Engine + Ctn Start Agent + + # Let's wait for the otel server start + ${content} Create List encrypted server listening on 0.0.0.0:4317 + ${result} Ctn Find In Log With Timeout ${engineLog0} ${start} ${content} 10 + Should Be True ${result} "encrypted server listening on 0.0.0.0:4317" should be available. + Sleep 1 + + ${start} Ctn Get Round Current Date + Ctn Schedule Forced Host Check host_1 + + ${result} Ctn Check Host Check Status With Timeout host_1 30 ${start} 0 OK - 127.0.0.1 + Should Be True ${result} hosts table not updated + + + +BEOTEL_REVERSE_CENTREON_AGENT_CHECK_HOST_CRYPTED + [Documentation] agent check host with encrypted reversed connection and we expect to get it in check result + [Tags] broker engine opentelemetry MON-63843 + Ctn Config Engine ${1} ${2} ${2} + Copy File ../broker/grpc/test/grpc_test_keys/ca_1234.crt /tmp/ + Copy File ../broker/grpc/test/grpc_test_keys/server_1234.key /tmp/ + Copy File ../broker/grpc/test/grpc_test_keys/server_1234.crt /tmp/ + + Ctn Add Otl ServerModule + ... 0 + ... {"max_length_grpc_log":0,"centreon_agent":{"check_interval":10, "export_period":15, "reverse_connections":[{"host": "localhost","port": 4317, "encryption": true, "ca_certificate": "/tmp/ca_1234.crt"}]}} + + Ctn Config Add Otl Connector + ... 0 + ... OTEL connector + ... opentelemetry --processor=centreon_agent --extractor=attributes --host_path=resource_metrics.resource.attributes.host.name --service_path=resource_metrics.resource.attributes.service.name + Ctn Engine Config Replace Value In Hosts ${0} host_1 check_command otel_check_icmp + Ctn Engine Config Add Command + ... ${0} + ... otel_check_icmp + ... /usr/lib/nagios/plugins/check_icmp 127.0.0.1 + ... OTEL connector + + Ctn Engine Config Set Value 0 log_level_checks trace + + Ctn Config Broker central + Ctn Config Broker module + Ctn Config Broker rrd + Ctn Config Reverse Centreon Agent /tmp/server_1234.key /tmp/server_1234.crt /tmp/ca_1234.crt + Ctn Broker Config Log central sql trace + + Ctn ConfigBBDO3 1 + Ctn Clear Retention + + ${start} Get Current Date + Ctn Start Broker + Ctn Start Engine + Ctn Start Agent + + # Let's wait for engine to connect to agent + ${content} Create List init from localhost:4317 + ${result} Ctn Find In Log With Timeout ${engineLog0} ${start} ${content} 10 + Should Be True ${result} "init from localhost:4317" not found in log + Sleep 1 + + ${start} Ctn Get Round Current Date + Ctn Schedule Forced Host Check host_1 + + ${result} Ctn Check Host Check Status With Timeout host_1 30 ${start} 0 OK - 127.0.0.1 + Should Be True ${result} hosts table not updated + + + + *** Keywords *** Ctn Create Otl Request [Documentation] create an otl request with nagios telegraf style diff --git a/tests/resources/Agent.py b/tests/resources/Agent.py new file mode 100644 index 00000000000..9a458e871f7 --- /dev/null +++ b/tests/resources/Agent.py @@ -0,0 +1,69 @@ +#!/usr/bin/python3 +# +# Copyright 2023-2024 Centreon +# +# 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 : contact@centreon.com +# + +from os import makedirs +from robot.libraries.BuiltIn import BuiltIn + +ETC_ROOT = BuiltIn().get_variable_value("${EtcRoot}") +CONF_DIR = ETC_ROOT + "/centreon-engine" + + +agent_config=""" +log-level=trace +endpoint=localhost:4317 +host=host_1 +logger-type=file +logger-file=/tmp/var/log/centreon-engine/centreon-agent.log +""" + + +def ctn_config_centreon_agent(key_path:str = None, cert_path:str = None, ca_path:str = None): + """ctn_config_centreon_agent + Creates a default centreon agent config without encryption nor reverse connection + """ + makedirs(CONF_DIR, mode=0o777, exist_ok=True) + with open(f"{CONF_DIR}/centreon_agent.conf", "w") as ff: + ff.write(agent_config) + if key_path is not None or cert_path is not None or ca_path is not None: + ff.write("encryption=true\n") + if key_path is not None: + ff.write(f"private_key={key_path}\n") + if cert_path is not None: + ff.write(f"certificate={cert_path}\n") + if ca_path is not None: + ff.write(f"ca_certificate={ca_path}\n") + + + +def ctn_config_reverse_centreon_agent(key_path:str = None, cert_path:str = None, ca_path:str = None): + """ctn_config_centreon_agent + Creates a default reversed centreon agent config without encryption listening on 0.0.0.0:4317 + """ + makedirs(CONF_DIR, mode=0o777, exist_ok=True) + with open(f"{CONF_DIR}/centreon_agent.conf", "w") as ff: + ff.write(agent_config) + ff.write("reversed-grpc-streaming=1\n") + if key_path is not None or cert_path is not None or ca_path is not None: + ff.write("encryption=true\n") + if key_path is not None: + ff.write(f"private_key={key_path}\n") + if cert_path is not None: + ff.write(f"certificate={cert_path}\n") + if ca_path is not None: + ff.write(f"ca_certificate={ca_path}\n") diff --git a/tests/resources/Broker.py b/tests/resources/Broker.py index e9e629c335e..fe65675d4eb 100755 --- a/tests/resources/Broker.py +++ b/tests/resources/Broker.py @@ -16,9 +16,7 @@ # # For more information : contact@centreon.com # -# This script is a little tcp server working on port 5669. It can simulate -# a cbd instance. It is useful to test the validity of BBDO packets sent by -# centengine. + import signal from os import setsid from os import makedirs diff --git a/tests/resources/opentelemetry/telegraf.conf b/tests/resources/opentelemetry/telegraf.conf index 2282ce60fab..3841a19baca 100644 --- a/tests/resources/opentelemetry/telegraf.conf +++ b/tests/resources/opentelemetry/telegraf.conf @@ -34,21 +34,3 @@ host = "host_1" service = "service_2" - -[[inputs.exec]] - name_override = "otel_check_icmp_host_2" - commands = ["/usr/lib/nagios/plugins/check_icmp 127.0.0.20"] - data_format = "nagios" - [inputs.exec.tags] - host = "host_2" - service = "" - - -[[inputs.exec]] - name_override = "otel_check_icmp_serv_5" - commands = ["/usr/lib/nagios/plugins/check_icmp 127.0.0.5"] - data_format = "nagios" - [inputs.exec.tags] - host = "host_3" - service = "service_5" - diff --git a/tests/resources/resources.resource b/tests/resources/resources.resource index f53d7f6ff08..cff84f1d35d 100644 --- a/tests/resources/resources.resource +++ b/tests/resources/resources.resource @@ -233,6 +233,11 @@ Ctn Stop Engine Broker And Save Logs EXCEPT Log Can't kindly stop Broker END + TRY + Ctn Kindly Stop Agent + EXCEPT + Log Can't kindly stop Agent + END Ctn Save Logs If Failed Ctn Get Engine Pid @@ -283,7 +288,9 @@ Ctn Save Logs Copy Files ${rrdLog} ${failDir} Copy Files ${moduleLog0} ${failDir} Copy Files ${engineLog0} ${failDir} + Copy Files ${ENGINE_LOG}/*.log ${failDir} Copy Files ${EtcRoot}/centreon-engine/config0/*.cfg ${failDir}/etc/centreon-engine/config0 + Copy Files ${EtcRoot}/centreon-engine/*.conf ${failDir}/etc/centreon-engine Copy Files ${EtcRoot}/centreon-broker/*.json ${failDir}/etc/centreon-broker Move Files /tmp/lua*.log ${failDir} @@ -384,3 +391,30 @@ Ctn Wait For Engine To Be Ready ... ${result} ... A message telling check_for_external_commands() should be available in config${i}/centengine.log. END + + +Ctn Start Agent + Start Process /usr/sbin/centreon_agent --config-file ${EtcRoot}/centreon-engine/centreon_agent.conf alias=centreon_agent + +Ctn Kindly Stop Agent + #in most case centreon_agent is not started + ${centreon_agent_process} Get Process Object centreon_agent + + IF ${{$centreon_agent_process is None}} RETURN + + Send Signal To Process SIGTERM centreon_agent + ${result} Wait For Process centreon_agent timeout=60s + # In case of process not stopping + IF "${result}" == "${None}" + Log To Console "fail to stop centreon_agent" + Ctn Save Logs + Ctn Dump Process centreon_agent /usr/sbin/centreon_agent centreon_agent + Send Signal To Process SIGKILL centreon_agent + Fail centreon_agent not correctly stopped (coredump generated) + ELSE + IF ${result.rc} != 0 + Ctn Save Logs + Ctn Coredump Info centreon_agent /usr/sbin/centreon_agent centreon_agent + Should Be Equal As Integers ${result.rc} 0 centreon_agent not correctly stopped + END + END