diff --git a/broker/bam/src/timeperiod_map.cc b/broker/bam/src/timeperiod_map.cc index 5784af8ff55..2d00d2fb42a 100644 --- a/broker/bam/src/timeperiod_map.cc +++ b/broker/bam/src/timeperiod_map.cc @@ -1,22 +1,23 @@ -/* -** Copyright 2014 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 2014,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 + */ #include "com/centreon/broker/bam/timeperiod_map.hh" +#include "com/centreon/broker/log_v2.hh" #include "com/centreon/exceptions/msg_fmt.hh" using namespace com::centreon::exceptions; @@ -125,9 +126,9 @@ void timeperiod_map::add_relation(uint32_t ba_id, * @return A vector of timeperiods and optional boolean set to * true if the timeperiod is default. */ -std::vector > +std::vector> timeperiod_map::get_timeperiods_by_ba_id(uint32_t ba_id) const { - std::vector > + std::vector> res; std::pair @@ -137,11 +138,15 @@ timeperiod_map::get_timeperiods_by_ba_id(uint32_t ba_id) const { uint32_t tp_id = found.first->second.first; bool is_default = found.first->second.second; time::timeperiod::ptr tp = get_timeperiod(tp_id); - if (!tp) - throw msg_fmt("BAM-BI: could not find the timeperiod {} in cache.", - tp_id); - res.push_back(std::make_pair(tp, is_default)); + if (!tp) { + SPDLOG_LOGGER_ERROR(log_v2::bam(), + "BAM-BI: could not find the timeperiod {} in cache " + "for ba {}, check timeperiod table in conf db", + tp_id, ba_id); + } else { + res.push_back(std::make_pair(tp, is_default)); + } } - return (res); + return res; } diff --git a/broker/core/src/processing/acceptor.cc b/broker/core/src/processing/acceptor.cc index ab1370f045b..d0d37eb7bb0 100644 --- a/broker/core/src/processing/acceptor.cc +++ b/broker/core/src/processing/acceptor.cc @@ -1,20 +1,20 @@ -/* -** Copyright 2015-2022 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 2015-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 + */ #include "com/centreon/broker/processing/acceptor.hh" @@ -35,10 +35,7 @@ using namespace com::centreon::broker::processing; * @param[in] name Name of the endpoint. */ acceptor::acceptor(std::shared_ptr endp, std::string const& name) - : endpoint(true, name), - _state(stopped), - _should_exit(false), - _endp(endp) {} + : endpoint(true, name), _state(stopped), _should_exit(false), _endp(endp) {} /** * Destructor. diff --git a/broker/core/src/processing/failover.cc b/broker/core/src/processing/failover.cc index e6f01bc115c..803db2740ff 100644 --- a/broker/core/src/processing/failover.cc +++ b/broker/core/src/processing/failover.cc @@ -1,20 +1,20 @@ -/* -** Copyright 2011-2017, 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 2011-2017, 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 "com/centreon/broker/processing/failover.hh" @@ -165,6 +165,12 @@ void failover::_run() { std::lock_guard stream_lock(_stream_m); _stream = s; set_state(s ? "connected" : "connecting"); + if (s) + SPDLOG_LOGGER_DEBUG(log_v2::processing(), "{} stream connected", + _name); + else + SPDLOG_LOGGER_DEBUG(log_v2::processing(), + "{} fail to create stream", _name); } _initialized = true; set_last_connection_success(timestamp::now()); diff --git a/broker/tcp/inc/com/centreon/broker/tcp/acceptor.hh b/broker/tcp/inc/com/centreon/broker/tcp/acceptor.hh index 17edc43ad28..94914f964f9 100644 --- a/broker/tcp/inc/com/centreon/broker/tcp/acceptor.hh +++ b/broker/tcp/inc/com/centreon/broker/tcp/acceptor.hh @@ -20,12 +20,9 @@ #define CCB_TCP_ACCEPTOR_HH #include "com/centreon/broker/io/endpoint.hh" -#include "com/centreon/broker/namespace.hh" #include "com/centreon/broker/tcp/tcp_config.hh" -CCB_BEGIN() - -namespace tcp { +namespace com::centreon::broker::tcp { /** * @class acceptor acceptor.hh "com/centreon/broker/tcp/acceptor.hh" * @brief TCP acceptor. @@ -35,7 +32,7 @@ namespace tcp { class acceptor : public io::endpoint { tcp_config::pointer _conf; - std::list _children; + absl::flat_hash_set _children; std::mutex _childrenm; std::shared_ptr _acceptor; @@ -47,14 +44,11 @@ class acceptor : public io::endpoint { acceptor& operator=(const acceptor&) = delete; void add_child(std::string const& child); - void listen(); std::unique_ptr open() override; void remove_child(std::string const& child); void stats(nlohmann::json& tree) override; bool is_ready() const override; }; -} // namespace tcp - -CCB_END() +} // namespace com::centreon::broker::tcp #endif // !CCB_TCP_ACCEPTOR_HH diff --git a/broker/tcp/src/acceptor.cc b/broker/tcp/src/acceptor.cc index dcb2ccffef4..cdf80b1c13c 100644 --- a/broker/tcp/src/acceptor.cc +++ b/broker/tcp/src/acceptor.cc @@ -55,7 +55,7 @@ acceptor::~acceptor() noexcept { */ void acceptor::add_child(std::string const& child) { std::lock_guard lock(_childrenm); - _children.push_back(child); + _children.insert(child); } /** @@ -74,6 +74,7 @@ std::unique_ptr acceptor::open() { if (conn) { assert(conn->port()); log_v2::tcp()->info("acceptor gets a new connection from {}", conn->peer()); + add_child(conn->peer()); return std::make_unique(conn, _conf); } return nullptr; @@ -91,7 +92,7 @@ bool acceptor::is_ready() const { */ void acceptor::remove_child(std::string const& child) { std::lock_guard lock(_childrenm); - _children.remove(child); + _children.erase(child); } /** diff --git a/broker/tcp/test/acceptor.cc b/broker/tcp/test/acceptor.cc index 4dc252a9d67..a03ad74b9ed 100644 --- a/broker/tcp/test/acceptor.cc +++ b/broker/tcp/test/acceptor.cc @@ -939,7 +939,7 @@ TEST_F(TcpAcceptor, CloseRead) { } } -TEST_F(TcpAcceptor, ChildsAndStats) { +TEST_F(TcpAcceptor, ChildrenAndStats) { tcp::acceptor acc(test_conf); acc.add_child("child1"); @@ -949,7 +949,8 @@ TEST_F(TcpAcceptor, ChildsAndStats) { nlohmann::json obj; acc.stats(obj); - ASSERT_EQ(obj.dump(), "{\"peers\":\"2: child1, child3\"}"); + ASSERT_TRUE(obj.dump() == "{\"peers\":\"2: child1, child3\"}" || + obj.dump() == "{\"peers\":\"2: child3, child1\"}"); } TEST_F(TcpAcceptor, QuestionAnswerMultiple) { diff --git a/cmake.sh b/cmake.sh index f21774d40fd..d9942aba03c 100755 --- a/cmake.sh +++ b/cmake.sh @@ -12,6 +12,7 @@ This program build Centreon-broker -ng : C++17 standard -clang : Compilation with clang++ -mold : Link with mold instead of ld + -legacy-mold : Link with mold instead of ld but with an old gcc -h|--help : help EOF } @@ -61,6 +62,10 @@ do MOLD="-fuse-ld=mold" shift ;; + -legacy-mold) + MOLD="-B /usr/bin/mold" + shift + ;; -fcr|--force-conan-rebuild) echo "Forced conan rebuild" CONAN_REBUILD="1" diff --git a/tests/bam/bam_pb.robot b/tests/bam/bam_pb.robot index 4427b00c2f9..0200cd65874 100644 --- a/tests/bam/bam_pb.robot +++ b/tests/bam/bam_pb.robot @@ -18,7 +18,7 @@ Test Teardown Save logs If Failed *** Test Cases *** BAPBSTATUS - [Documentation] With bbdo version 3.0.1, a BA of type 'worst' with one service is configured. The BA is in critical state, because of its service. + [Documentation] With bbdo version 3.0.1, a BA of type 'worst' with one service is configured. The BA is in critical state, because of its service. We also check stats output [Tags] broker downtime engine bam Clear Commands Status Config Broker module @@ -70,6 +70,35 @@ BAPBSTATUS ${result} Grep File /tmp/output digraph Should Not Be Empty ${result} /tmp/output does not contain the word 'digraph' + # check broker stats + ${res} Get Broker Stats central 1: 127.0.0.1:[0-9]+ 10 endpoint central-broker-master-input peers + Should Be True ${res} no central-broker-master-input.peers found in broker stat output + + ${res} Get Broker Stats central listening 10 endpoint central-broker-master-input state + Should Be True ${res} central-broker-master-input not listening + + ${res} Get Broker Stats central connected 10 endpoint centreon-bam-monitoring state + Should Be True ${res} central-bam-monitoring not connected + + ${res} Get Broker Stats central connected 10 endpoint centreon-bam-reporting state + Should Be True ${res} central-bam-reporting not connected + + Reload Engine + Reload Broker + + # check broker stats + ${res} Get Broker Stats central 1: 127.0.0.1:[0-9]+ 10 endpoint central-broker-master-input peers + Should Be True ${res} no central-broker-master-input.peers found in broker stat output + + ${res} Get Broker Stats central listening 10 endpoint central-broker-master-input state + Should Be True ${res} central-broker-master-input not listening + + ${res} Get Broker Stats central connected 10 endpoint centreon-bam-monitoring state + Should Be True ${res} central-bam-monitoring not connected + + ${res} Get Broker Stats central connected 10 endpoint centreon-bam-reporting state + Should Be True ${res} central-bam-reporting not connected + [Teardown] Run Keywords Stop Engine AND Kindly Stop Broker BABEST_SERVICE_CRITICAL diff --git a/tests/resources/Broker.py b/tests/resources/Broker.py index 5272019855c..460dd601473 100755 --- a/tests/resources/Broker.py +++ b/tests/resources/Broker.py @@ -274,7 +274,7 @@ "json_fifo": "{7}/lib/centreon-broker/central-rrd-master-stats.json" }} ], - "grpc": {{ + "grpc": {{ "port": 51002 }} }} @@ -1021,6 +1021,54 @@ def get_broker_stats_size(name, key, timeout=TIMEOUT): return retval +def get_broker_stats(name: str, expected: str, timeout: int, *keys): + """! + read a value from broker stats + @param name central, module or rrd + @param expected: value expected (regexp) + @timeout delay to find key in stats + @param keys keys in json stats output + @return True if value found and matches expected + """ + + def json_get(json_dict, keys: tuple, index: int): + try: + key = keys[index] + if index == len(keys) - 1: + return json_dict[key] + else: + return json_get(json_dict[key], keys, index + 1) + except: + return None + limit = time.time() + timeout + if name == 'central': + filename = "central-broker-master-stats.json" + elif name == 'module': + filename = "central-module-master-stats.json" + else: + filename = "central-rrd-master-stats.json" + r_expected = re.compile(expected) + while time.time() < limit: + retry = True + while retry and time.time() < limit: + retry = False + with open(f"{VAR_ROOT}/lib/centreon-broker/{filename}", "r") as f: + buf = f.read() + try: + conf = json.loads(buf) + except: + retry = True + time.sleep(1) + if conf is None: + continue + value = json_get(conf, keys, 0) + if value is not None and r_expected.match(value): + return True + time.sleep(5) + logger.console(f"key:{keys} value not expected: {value}") + return False + + ## # @brief Gets count indexes that does not exist in index_data. # @@ -1960,7 +2008,7 @@ def config_broker_remove_rrd_output(name): f.write(json.dumps(conf, indent=2)) -def broker_get_ba(port:int, ba_id:int, output_file:str, timeout=TIMEOUT): +def broker_get_ba(port: int, ba_id: int, output_file:str, timeout=TIMEOUT): """ broker_get_ba calls the gRPC GetBa function provided by Broker. Args: