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

Mon 35115 acceptor stats information 23.04 #1122

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
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
53 changes: 29 additions & 24 deletions broker/bam/src/timeperiod_map.cc
Original file line number Diff line number Diff line change
@@ -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 : [email protected]
*/
/**
* 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 : [email protected]
*/

#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;
Expand Down Expand Up @@ -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::pair<com::centreon::broker::time::timeperiod::ptr, bool> >
std::vector<std::pair<com::centreon::broker::time::timeperiod::ptr, bool>>
timeperiod_map::get_timeperiods_by_ba_id(uint32_t ba_id) const {
std::vector<std::pair<com::centreon::broker::time::timeperiod::ptr, bool> >
std::vector<std::pair<com::centreon::broker::time::timeperiod::ptr, bool>>
res;
std::pair<timeperiod_relation_map::const_iterator,
timeperiod_relation_map::const_iterator>
Expand All @@ -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;
}
39 changes: 18 additions & 21 deletions broker/core/src/processing/acceptor.cc
Original file line number Diff line number Diff line change
@@ -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 : [email protected]
*/
/**
* 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 : [email protected]
*/

#include "com/centreon/broker/processing/acceptor.hh"

Expand All @@ -35,10 +35,7 @@ using namespace com::centreon::broker::processing;
* @param[in] name Name of the endpoint.
*/
acceptor::acceptor(std::shared_ptr<io::endpoint> 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.
Expand Down
40 changes: 23 additions & 17 deletions broker/core/src/processing/failover.cc
Original file line number Diff line number Diff line change
@@ -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 : [email protected]
*/
/**
* 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 : [email protected]
*/

#include "com/centreon/broker/processing/failover.hh"

Expand Down Expand Up @@ -165,6 +165,12 @@ void failover::_run() {
std::lock_guard<std::timed_mutex> 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());
Expand Down
12 changes: 3 additions & 9 deletions broker/tcp/inc/com/centreon/broker/tcp/acceptor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -35,7 +32,7 @@ namespace tcp {
class acceptor : public io::endpoint {
tcp_config::pointer _conf;

std::list<std::string> _children;
absl::flat_hash_set<std::string> _children;
std::mutex _childrenm;
std::shared_ptr<asio::ip::tcp::acceptor> _acceptor;

Expand All @@ -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<io::stream> 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
5 changes: 3 additions & 2 deletions broker/tcp/src/acceptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ acceptor::~acceptor() noexcept {
*/
void acceptor::add_child(std::string const& child) {
std::lock_guard<std::mutex> lock(_childrenm);
_children.push_back(child);
_children.insert(child);
}

/**
Expand All @@ -74,6 +74,7 @@ std::unique_ptr<io::stream> 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<stream>(conn, _conf);
}
return nullptr;
Expand All @@ -91,7 +92,7 @@ bool acceptor::is_ready() const {
*/
void acceptor::remove_child(std::string const& child) {
std::lock_guard<std::mutex> lock(_childrenm);
_children.remove(child);
_children.erase(child);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions broker/tcp/test/acceptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions cmake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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"
Expand Down
31 changes: 30 additions & 1 deletion tests/bam/bam_pb.robot
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
52 changes: 50 additions & 2 deletions tests/resources/Broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@
"json_fifo": "{7}/lib/centreon-broker/central-rrd-master-stats.json"
}}
],
"grpc": {{
"grpc": {{
"port": 51002
}}
}}
Expand Down Expand Up @@ -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.
#
Expand Down Expand Up @@ -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:
Expand Down
Loading