From f172341c9b2dd65bfb23ce5f621b9aae96858fbc Mon Sep 17 00:00:00 2001 From: David Boucher Date: Tue, 27 Feb 2024 15:09:22 +0100 Subject: [PATCH] fix(broker/bam): ba initialization REFS: MON-35581 --- .gitignore | 3 + bbdo/CMakeLists.txt | 23 +- bbdo/bam.proto | 204 ++++++++-------- bbdo/bam_state.proto | 46 ++++ bbdo/events.hh | 3 +- .../broker/bam/availability_thread.hh | 34 +-- broker/bam/inc/com/centreon/broker/bam/ba.hh | 8 +- .../inc/com/centreon/broker/bam/ba_best.hh | 2 + .../inc/com/centreon/broker/bam/ba_impact.hh | 8 + .../centreon/broker/bam/ba_ratio_number.hh | 2 + .../centreon/broker/bam/ba_ratio_percent.hh | 2 + .../inc/com/centreon/broker/bam/ba_worst.hh | 2 + .../centreon/broker/bam/bool_expression.hh | 7 +- .../inc/com/centreon/broker/bam/bool_or.hh | 34 +-- .../com/centreon/broker/bam/bool_service.hh | 6 +- .../inc/com/centreon/broker/bam/computable.hh | 1 - .../broker/bam/configuration/applier/ba.hh | 7 +- .../broker/bam/configuration/applier/state.hh | 8 +- .../inc/com/centreon/broker/bam/internal.hh | 5 +- broker/bam/inc/com/centreon/broker/bam/kpi.hh | 1 - .../bam/inc/com/centreon/broker/bam/kpi_ba.hh | 3 +- .../com/centreon/broker/bam/kpi_boolexp.hh | 2 +- .../com/centreon/broker/bam/kpi_service.hh | 6 +- .../com/centreon/broker/bam/service_book.hh | 52 ++-- .../centreon/broker/bam/service_listener.hh | 7 +- .../com/centreon/broker/bam/service_state.hh | 62 +++++ broker/bam/src/availability_thread.cc | 34 +-- broker/bam/src/ba.cc | 23 -- broker/bam/src/ba_best.cc | 20 ++ broker/bam/src/ba_impact.cc | 25 ++ broker/bam/src/ba_ratio_number.cc | 22 ++ broker/bam/src/ba_ratio_percent.cc | 22 ++ broker/bam/src/ba_svc_mapping.cc | 2 +- broker/bam/src/ba_worst.cc | 20 ++ broker/bam/src/bool_and.cc | 2 +- broker/bam/src/bool_call.cc | 2 +- broker/bam/src/bool_constant.cc | 4 +- broker/bam/src/bool_equal.cc | 4 +- broker/bam/src/bool_expression.cc | 2 +- broker/bam/src/bool_less_than.cc | 2 +- broker/bam/src/bool_more_than.cc | 2 +- broker/bam/src/bool_not.cc | 2 +- broker/bam/src/bool_not_equal.cc | 2 +- broker/bam/src/bool_operation.cc | 7 +- broker/bam/src/bool_or.cc | 2 +- broker/bam/src/bool_service.cc | 34 ++- broker/bam/src/bool_value.cc | 34 +-- broker/bam/src/bool_xor.cc | 2 +- broker/bam/src/configuration/applier/ba.cc | 93 ++++--- .../configuration/applier/bool_expression.cc | 34 +-- broker/bam/src/configuration/applier/kpi.cc | 34 +-- broker/bam/src/configuration/applier/state.cc | 49 ++-- broker/bam/src/configuration/ba.cc | 34 +-- broker/bam/src/configuration/state.cc | 34 +-- broker/bam/src/connector.cc | 34 +-- broker/bam/src/event_cache_visitor.cc | 34 +-- broker/bam/src/exp_builder.cc | 34 +-- broker/bam/src/exp_parser.cc | 34 +-- broker/bam/src/exp_tokenizer.cc | 34 +-- broker/bam/src/factory.cc | 34 +-- broker/bam/src/hst_svc_mapping.cc | 34 +-- broker/bam/src/impact_values.cc | 34 +-- broker/bam/src/kpi.cc | 36 +-- broker/bam/src/kpi_service.cc | 56 ++++- broker/bam/src/main.cc | 2 + broker/bam/src/monitoring_stream.cc | 5 +- broker/bam/src/reporting_stream.cc | 34 +-- broker/bam/src/service_book.cc | 231 +++++++++++------- broker/bam/src/service_listener.cc | 43 ++-- tests/bam/bam_pb.robot | 4 +- tests/resources/Common.py | 40 ++- tests/resources/resources.robot | 5 + 72 files changed, 1111 insertions(+), 702 deletions(-) create mode 100644 bbdo/bam_state.proto create mode 100644 broker/bam/inc/com/centreon/broker/bam/service_state.hh diff --git a/.gitignore b/.gitignore index e19c09d631c..c8f77542641 100644 --- a/.gitignore +++ b/.gitignore @@ -84,6 +84,9 @@ broker/libroker.a broker/librokerbase.a broker/test/python/*.crt broker/test/python/*.key +broker/compile_commands.json +broker/grpc/grpc_stream.proto +broker/grpc/src/grpc_bridge.cc # clib clib/inc/com/centreon/clib/version.hh diff --git a/bbdo/CMakeLists.txt b/bbdo/CMakeLists.txt index a5170b3873d..6163c0bbc0e 100644 --- a/bbdo/CMakeLists.txt +++ b/bbdo/CMakeLists.txt @@ -17,15 +17,16 @@ # set(protobuf_files - header - rebuild_message - remove_graph_message - severity - tag - bbdo - neb - storage - bam) + header + rebuild_message + remove_graph_message + severity + tag + bbdo + neb + storage + bam + bam_state) foreach(name IN LISTS protobuf_files) set(proto_file "${name}.proto") @@ -69,8 +70,8 @@ add_library(pb_tag_lib STATIC tag.pb.cc tag.pb.h) add_dependencies(pb_tag_lib target_neb) set_target_properties(pb_tag_lib PROPERTIES POSITION_INDEPENDENT_CODE ON) -add_library(pb_bam_lib STATIC bam.pb.cc bam.pb.h) -add_dependencies(pb_bam_lib target_bam target_header) +add_library(pb_bam_lib STATIC bam.pb.cc bam.pb.h bam_state.pb.cc bam_state.pb.h) +add_dependencies(pb_bam_lib target_bam target_bam_state target_header) set_target_properties(pb_bam_lib PROPERTIES POSITION_INDEPENDENT_CODE ON) macro(get_protobuf_files name) diff --git a/bbdo/bam.proto b/bbdo/bam.proto index bdd45567846..e41bf092204 100644 --- a/bbdo/bam.proto +++ b/bbdo/bam.proto @@ -1,20 +1,20 @@ -/* -** Copyright 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 2022-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 + */ syntax = "proto3"; @@ -23,16 +23,16 @@ import "header.proto"; package com.centreon.broker; enum State { - OK = 0; - WARNING = 1; - CRITICAL = 2; - UNKNOWN = 3; + OK = 0; + WARNING = 1; + CRITICAL = 2; + UNKNOWN = 3; } message InheritedDowntime { - BBDOHeader header = 1; - uint32 ba_id = 2; - bool in_downtime = 3; + BBDOHeader header = 1; + uint32 ba_id = 2; + bool in_downtime = 3; } message BaStatus { @@ -47,111 +47,111 @@ message BaStatus { } message BaEvent { - uint32 ba_id = 1; - double first_level = 2; - int64 end_time = 3; - bool in_downtime = 4; - uint64 start_time = 5; - State status = 6; + uint32 ba_id = 1; + double first_level = 2; + int64 end_time = 3; + bool in_downtime = 4; + uint64 start_time = 5; + State status = 6; } message KpiEvent { - uint32 ba_id = 1; - uint64 start_time = 2; - int64 end_time = 3; - uint32 kpi_id = 4; - int32 impact_level = 5; - bool in_downtime = 6; - string output = 7; - string perfdata = 8; - State status = 9; + uint32 ba_id = 1; + uint64 start_time = 2; + int64 end_time = 3; + uint32 kpi_id = 4; + int32 impact_level = 5; + bool in_downtime = 6; + string output = 7; + string perfdata = 8; + State status = 9; } message DimensionBvEvent { - uint32 bv_id = 1; - string bv_name = 2; - string bv_description = 3; + uint32 bv_id = 1; + string bv_name = 2; + string bv_description = 3; } message DimensionBaBvRelationEvent { - uint32 ba_id = 1; - uint32 bv_id = 2; + uint32 ba_id = 1; + uint32 bv_id = 2; } message DimensionTimeperiod { - uint32 id = 1; - string name = 2; - string monday = 3; - string tuesday = 4; - string wednesday = 5; - string thursday = 6; - string friday = 7; - string saturday = 8; - string sunday = 9; + uint32 id = 1; + string name = 2; + string monday = 3; + string tuesday = 4; + string wednesday = 5; + string thursday = 6; + string friday = 7; + string saturday = 8; + string sunday = 9; } message DimensionBaEvent { - uint32 ba_id = 1; - string ba_name = 2; - string ba_description = 3; - double sla_month_percent_crit = 4; - double sla_month_percent_warn = 5; - uint32 sla_duration_crit = 6; - uint32 sla_duration_warn = 7; + uint32 ba_id = 1; + string ba_name = 2; + string ba_description = 3; + double sla_month_percent_crit = 4; + double sla_month_percent_warn = 5; + uint32 sla_duration_crit = 6; + uint32 sla_duration_warn = 7; } message DimensionKpiEvent { - uint32 kpi_id = 1; - uint32 ba_id = 2; - string ba_name = 3; - uint32 host_id = 4; - string host_name = 5; - uint32 service_id = 6; - string service_description = 7; - uint32 kpi_ba_id = 8; - string kpi_ba_name = 9; - uint32 meta_service_id = 10; - string meta_service_name = 11; - uint32 boolean_id = 12; - string boolean_name = 13; - double impact_warning = 14; - double impact_critical = 15; - double impact_unknown = 16; + uint32 kpi_id = 1; + uint32 ba_id = 2; + string ba_name = 3; + uint32 host_id = 4; + string host_name = 5; + uint32 service_id = 6; + string service_description = 7; + uint32 kpi_ba_id = 8; + string kpi_ba_name = 9; + uint32 meta_service_id = 10; + string meta_service_name = 11; + uint32 boolean_id = 12; + string boolean_name = 13; + double impact_warning = 14; + double impact_critical = 15; + double impact_unknown = 16; } message KpiStatus { - uint32 kpi_id = 1; - bool in_downtime = 2; - double level_acknowledgement_hard = 3; - double level_acknowledgement_soft = 4; - double level_downtime_hard = 5; - double level_downtime_soft = 6; - double level_nominal_hard = 7; - double level_nominal_soft = 8; - State state_hard = 9; - State state_soft = 10; - int64 last_state_change = 11; - double last_impact = 12; - bool valid = 13; + uint32 kpi_id = 1; + bool in_downtime = 2; + double level_acknowledgement_hard = 3; + double level_acknowledgement_soft = 4; + double level_downtime_hard = 5; + double level_downtime_soft = 6; + double level_nominal_hard = 7; + double level_nominal_soft = 8; + State state_hard = 9; + State state_soft = 10; + int64 last_state_change = 11; + double last_impact = 12; + bool valid = 13; } message BaDurationEvent { - uint32 ba_id = 1; - int64 real_start_time = 2; - int64 end_time = 3; - int64 start_time = 4; - uint32 duration = 5; - uint32 sla_duration = 6; - uint32 timeperiod_id = 7; - bool timeperiod_is_default = 8; + uint32 ba_id = 1; + int64 real_start_time = 2; + int64 end_time = 3; + int64 start_time = 4; + uint32 duration = 5; + uint32 sla_duration = 6; + uint32 timeperiod_id = 7; + bool timeperiod_is_default = 8; } message DimensionBaTimeperiodRelation { - uint32 ba_id = 1; - uint32 timeperiod_id = 2; - bool is_default = 3; + uint32 ba_id = 1; + uint32 timeperiod_id = 2; + bool is_default = 3; } message DimensionTruncateTableSignal { - bool update_started = 1; -} \ No newline at end of file + bool update_started = 1; +} diff --git a/bbdo/bam_state.proto b/bbdo/bam_state.proto new file mode 100644 index 00000000000..19a567c4f58 --- /dev/null +++ b/bbdo/bam_state.proto @@ -0,0 +1,46 @@ +/** + * Copyright 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 + */ + +syntax = "proto3"; + +import "bam.proto"; + +package com.centreon.broker; + +/** + * @brief Stores needed information for bool_service or kpi_service. Very useful + * when broker is stopped to save the BA's states. + */ +message ServiceState { + uint64 host_id = 1; + uint64 service_id = 2; + State current_state = 3; + State last_hard_state = 4; + uint64 last_check = 5; + bool state_type = 6; + bool acknowledged = 7; +} + +/** + * @brief This message contains the living informations of the current BA's. + * Thanks to them we can recompute their states when Broker is restarted. + */ +/*io::bam, bam::de_pb_services_book_state*/ +message ServicesBookState { + repeated ServiceState service = 1; +} diff --git a/bbdo/events.hh b/bbdo/events.hh index 56a785b1e7f..94beb56973e 100644 --- a/bbdo/events.hh +++ b/bbdo/events.hh @@ -190,7 +190,8 @@ enum data_element { de_pb_kpi_status = 27, de_pb_ba_duration_event = 28, de_pb_dimension_ba_timeperiod_relation = 29, - de_pb_dimension_truncate_table_signal = 30 + de_pb_dimension_truncate_table_signal = 30, + de_pb_services_book_state = 31, }; } diff --git a/broker/bam/inc/com/centreon/broker/bam/availability_thread.hh b/broker/bam/inc/com/centreon/broker/bam/availability_thread.hh index 519e56b952c..bcb10b6b2e6 100644 --- a/broker/bam/inc/com/centreon/broker/bam/availability_thread.hh +++ b/broker/bam/inc/com/centreon/broker/bam/availability_thread.hh @@ -1,20 +1,20 @@ -/* -** Copyright 2014 - 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 2014 - 2021-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 + */ #ifndef CCB_BAM_AVAILABILITY_THREAD_HH #define CCB_BAM_AVAILABILITY_THREAD_HH diff --git a/broker/bam/inc/com/centreon/broker/bam/ba.hh b/broker/bam/inc/com/centreon/broker/bam/ba.hh index b24d151860c..01146e38e0b 100644 --- a/broker/bam/inc/com/centreon/broker/bam/ba.hh +++ b/broker/bam/inc/com/centreon/broker/bam/ba.hh @@ -91,11 +91,6 @@ class ba : public computable, public service_listener { double _level_hard{100.0}; double _level_soft{100.0}; - double _downtime_hard{0.0}; - double _downtime_soft{0.0}; - double _acknowledgement_hard{0.0}; - double _acknowledgement_soft{0.0}; - std::unordered_map _impacts; bool _valid{true}; configuration::ba::downtime_behaviour _dt_behaviour{ @@ -109,7 +104,8 @@ class ba : public computable, public service_listener { const impact_values& new_hard_impact, const impact_values& new_soft_impact, bool in_downtime) = 0; - std::shared_ptr _generate_ba_status(bool state_changed) const; + virtual std::shared_ptr _generate_ba_status( + bool state_changed) const = 0; std::shared_ptr _generate_virtual_service_status() const; public: diff --git a/broker/bam/inc/com/centreon/broker/bam/ba_best.hh b/broker/bam/inc/com/centreon/broker/bam/ba_best.hh index 5449898cb4d..eb67be88e93 100644 --- a/broker/bam/inc/com/centreon/broker/bam/ba_best.hh +++ b/broker/bam/inc/com/centreon/broker/bam/ba_best.hh @@ -50,6 +50,8 @@ class ba_best : public ba { const impact_values& new_hard_impact, const impact_values& new_soft_impact, bool in_downtime) override; + std::shared_ptr _generate_ba_status( + bool state_changed) const override; public: ba_best(uint32_t id, diff --git a/broker/bam/inc/com/centreon/broker/bam/ba_impact.hh b/broker/bam/inc/com/centreon/broker/bam/ba_impact.hh index a174197c819..919cde42054 100644 --- a/broker/bam/inc/com/centreon/broker/bam/ba_impact.hh +++ b/broker/bam/inc/com/centreon/broker/bam/ba_impact.hh @@ -35,6 +35,12 @@ class kpi; * of value. */ class ba_impact : public ba { + double _downtime_hard = 0.0; + double _downtime_soft = 0.0; + + double _acknowledgement_hard = 0.0; + double _acknowledgement_soft = 0.0; + void _recompute(); protected: @@ -44,6 +50,8 @@ class ba_impact : public ba { const impact_values& new_hard_impact, const impact_values& new_soft_impact, bool in_downtime) override; + std::shared_ptr _generate_ba_status( + bool state_changed) const override; public: ba_impact(uint32_t id, diff --git a/broker/bam/inc/com/centreon/broker/bam/ba_ratio_number.hh b/broker/bam/inc/com/centreon/broker/bam/ba_ratio_number.hh index 25be94cc278..b7e17d69814 100644 --- a/broker/bam/inc/com/centreon/broker/bam/ba_ratio_number.hh +++ b/broker/bam/inc/com/centreon/broker/bam/ba_ratio_number.hh @@ -41,6 +41,8 @@ class ba_ratio_number : public ba { const impact_values& new_hard_impact, const impact_values& new_soft_impact, bool in_downtime) override; + std::shared_ptr _generate_ba_status( + bool state_changed) const override; public: ba_ratio_number(uint32_t id, diff --git a/broker/bam/inc/com/centreon/broker/bam/ba_ratio_percent.hh b/broker/bam/inc/com/centreon/broker/bam/ba_ratio_percent.hh index 7837c75891e..a37c5469cfd 100644 --- a/broker/bam/inc/com/centreon/broker/bam/ba_ratio_percent.hh +++ b/broker/bam/inc/com/centreon/broker/bam/ba_ratio_percent.hh @@ -41,6 +41,8 @@ class ba_ratio_percent : public ba { const impact_values& new_hard_impact, const impact_values& new_soft_impact, bool in_downtime) override; + std::shared_ptr _generate_ba_status( + bool state_changed) const override; public: ba_ratio_percent(uint32_t id, diff --git a/broker/bam/inc/com/centreon/broker/bam/ba_worst.hh b/broker/bam/inc/com/centreon/broker/bam/ba_worst.hh index 06206c1b35c..ba77f57fc3e 100644 --- a/broker/bam/inc/com/centreon/broker/bam/ba_worst.hh +++ b/broker/bam/inc/com/centreon/broker/bam/ba_worst.hh @@ -50,6 +50,8 @@ class ba_worst : public ba { const impact_values& new_hard_impact, const impact_values& new_soft_impact, bool in_downtime) override; + std::shared_ptr _generate_ba_status( + bool state_changed) const override; public: ba_worst(uint32_t id, diff --git a/broker/bam/inc/com/centreon/broker/bam/bool_expression.hh b/broker/bam/inc/com/centreon/broker/bam/bool_expression.hh index 0cba39906a9..985ac2d872f 100644 --- a/broker/bam/inc/com/centreon/broker/bam/bool_expression.hh +++ b/broker/bam/inc/com/centreon/broker/bam/bool_expression.hh @@ -1,5 +1,5 @@ -/* - * Copyright 2014, 2021-2023 Centreon +/** + * Copyright 2014, 2021-2024 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,9 +20,8 @@ #define CCB_BAM_BOOL_EXPRESSION_HH #include "bbdo/bam/state.hh" +#include "bbdo/bam_state.pb.h" #include "com/centreon/broker/bam/computable.hh" -#include "com/centreon/broker/io/stream.hh" -#include "com/centreon/broker/namespace.hh" #include "impact_values.hh" CCB_BEGIN() diff --git a/broker/bam/inc/com/centreon/broker/bam/bool_or.hh b/broker/bam/inc/com/centreon/broker/bam/bool_or.hh index 9462ca65c80..3c8a4579dfa 100644 --- a/broker/bam/inc/com/centreon/broker/bam/bool_or.hh +++ b/broker/bam/inc/com/centreon/broker/bam/bool_or.hh @@ -1,20 +1,20 @@ -/* -** Copyright 2014, 2021-2023 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, 2021-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 + */ #ifndef CCB_BAM_BOOL_OR_HH #define CCB_BAM_BOOL_OR_HH diff --git a/broker/bam/inc/com/centreon/broker/bam/bool_service.hh b/broker/bam/inc/com/centreon/broker/bam/bool_service.hh index f876837a277..8652102c2fb 100644 --- a/broker/bam/inc/com/centreon/broker/bam/bool_service.hh +++ b/broker/bam/inc/com/centreon/broker/bam/bool_service.hh @@ -1,5 +1,5 @@ -/* - * Copyright 2014, 2021-2023 Centreon +/** + * Copyright 2014, 2021-2024 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,7 @@ #include "com/centreon/broker/bam/bool_value.hh" #include "com/centreon/broker/bam/service_listener.hh" +#include "com/centreon/broker/bam/service_state.hh" #include "com/centreon/broker/io/stream.hh" #include "com/centreon/broker/namespace.hh" #include "com/centreon/broker/neb/internal.hh" @@ -52,6 +53,7 @@ class bool_service : public bool_value, public service_listener { bool_service& operator=(const bool_service&) = delete; uint32_t get_host_id() const; uint32_t get_service_id() const; + void service_update(const service_state& s) override; void service_update(const std::shared_ptr& status, io::stream* visitor = nullptr) override; void service_update(const std::shared_ptr& status, diff --git a/broker/bam/inc/com/centreon/broker/bam/computable.hh b/broker/bam/inc/com/centreon/broker/bam/computable.hh index bf4dcf54a20..64419b69ab0 100644 --- a/broker/bam/inc/com/centreon/broker/bam/computable.hh +++ b/broker/bam/inc/com/centreon/broker/bam/computable.hh @@ -19,7 +19,6 @@ #ifndef CCB_BAM_COMPUTABLE_HH #define CCB_BAM_COMPUTABLE_HH -#include "com/centreon/broker/io/stream.hh" #include "com/centreon/broker/namespace.hh" #include "com/centreon/broker/persistent_cache.hh" diff --git a/broker/bam/inc/com/centreon/broker/bam/configuration/applier/ba.hh b/broker/bam/inc/com/centreon/broker/bam/configuration/applier/ba.hh index 4b86a76dd10..4beb2c0a382 100644 --- a/broker/bam/inc/com/centreon/broker/bam/configuration/applier/ba.hh +++ b/broker/bam/inc/com/centreon/broker/bam/configuration/applier/ba.hh @@ -1,5 +1,5 @@ -/* - * Copyright 2014-2015 Centreon +/** + * 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. @@ -74,7 +74,8 @@ class ba { std::shared_ptr find_ba(uint32_t id) const; void visit(io::stream* visitor); void save_to_cache(persistent_cache& cache); - void load_from_cache(persistent_cache& cache); + void apply_inherited_downtime(const inherited_downtime& dwn); + void apply_inherited_downtime(const pb_inherited_downtime& dwn); }; } // namespace applier } // namespace configuration diff --git a/broker/bam/inc/com/centreon/broker/bam/configuration/applier/state.hh b/broker/bam/inc/com/centreon/broker/bam/configuration/applier/state.hh index 7b83a7641a0..8f20a7fbb85 100644 --- a/broker/bam/inc/com/centreon/broker/bam/configuration/applier/state.hh +++ b/broker/bam/inc/com/centreon/broker/bam/configuration/applier/state.hh @@ -1,5 +1,5 @@ -/* - * Copyright 2014-2015 Centreon +/** + * 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. @@ -63,8 +63,8 @@ class state { std::unordered_map _nodes; public: - state(); - ~state(); + state() = default; + ~state() noexcept = default; state(const state&) = delete; state& operator=(state const& other) = delete; void apply(configuration::state const& my_state); diff --git a/broker/bam/inc/com/centreon/broker/bam/internal.hh b/broker/bam/inc/com/centreon/broker/bam/internal.hh index d91b3a39267..64f391eeead 100644 --- a/broker/bam/inc/com/centreon/broker/bam/internal.hh +++ b/broker/bam/inc/com/centreon/broker/bam/internal.hh @@ -20,10 +20,10 @@ #define CCB_BAM_INTERNAL_HH #include "bbdo/bam.pb.h" +#include "bbdo/bam_state.pb.h" #include "broker.pb.h" #include "com/centreon/broker/io/events.hh" #include "com/centreon/broker/io/protobuf.hh" -#include "com/centreon/broker/namespace.hh" CCB_BEGIN() @@ -76,6 +76,9 @@ using pb_dimension_truncate_table_signal = io::protobuf; +using pb_services_book_state = + io::protobuf; } // namespace bam /* We have to declare the pb_ba_info also here because we don't control the diff --git a/broker/bam/inc/com/centreon/broker/bam/kpi.hh b/broker/bam/inc/com/centreon/broker/bam/kpi.hh index ac1f2f315ad..9e18fb1f02f 100644 --- a/broker/bam/inc/com/centreon/broker/bam/kpi.hh +++ b/broker/bam/inc/com/centreon/broker/bam/kpi.hh @@ -22,7 +22,6 @@ #include "com/centreon/broker/bam/computable.hh" #include "com/centreon/broker/bam/internal.hh" #include "com/centreon/broker/io/stream.hh" -#include "com/centreon/broker/namespace.hh" #include "com/centreon/broker/timestamp.hh" CCB_BEGIN() diff --git a/broker/bam/inc/com/centreon/broker/bam/kpi_ba.hh b/broker/bam/inc/com/centreon/broker/bam/kpi_ba.hh index a515fc45434..a29a5c62546 100644 --- a/broker/bam/inc/com/centreon/broker/bam/kpi_ba.hh +++ b/broker/bam/inc/com/centreon/broker/bam/kpi_ba.hh @@ -1,5 +1,5 @@ /* - * Copyright 2014, 2021-2023 Centreon + * Copyright 2014, 2021-2024 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,7 +24,6 @@ #include "com/centreon/broker/bam/internal.hh" #include "com/centreon/broker/bam/kpi.hh" #include "com/centreon/broker/io/stream.hh" -#include "com/centreon/broker/namespace.hh" CCB_BEGIN() diff --git a/broker/bam/inc/com/centreon/broker/bam/kpi_boolexp.hh b/broker/bam/inc/com/centreon/broker/bam/kpi_boolexp.hh index c9b424c7c36..4402dcc1100 100644 --- a/broker/bam/inc/com/centreon/broker/bam/kpi_boolexp.hh +++ b/broker/bam/inc/com/centreon/broker/bam/kpi_boolexp.hh @@ -1,5 +1,5 @@ /* - * Copyright 2014, 202122023 Centreon + * Copyright 2014, 2021-2024 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/broker/bam/inc/com/centreon/broker/bam/kpi_service.hh b/broker/bam/inc/com/centreon/broker/bam/kpi_service.hh index 84006c1cce1..c43abbf201b 100644 --- a/broker/bam/inc/com/centreon/broker/bam/kpi_service.hh +++ b/broker/bam/inc/com/centreon/broker/bam/kpi_service.hh @@ -1,5 +1,5 @@ -/* - * Copyright 2014-2015, 2021-2023 Centreon +/** + * Copyright 2014-2015, 2021-2024 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,7 +25,6 @@ #include "com/centreon/broker/bam/kpi.hh" #include "com/centreon/broker/bam/service_listener.hh" #include "com/centreon/broker/io/stream.hh" -#include "com/centreon/broker/namespace.hh" #include "com/centreon/broker/timestamp.hh" CCB_BEGIN() @@ -77,6 +76,7 @@ class kpi_service : public service_listener, public kpi { void impact_soft(impact_values& impact) override; bool in_downtime() const override; bool is_acknowledged() const; + void service_update(const service_state& state) override; void service_update(std::shared_ptr const& status, io::stream* visitor = nullptr) override; void service_update(const std::shared_ptr& status, diff --git a/broker/bam/inc/com/centreon/broker/bam/service_book.hh b/broker/bam/inc/com/centreon/broker/bam/service_book.hh index 1b8686ab249..0c2ea5dd0d5 100644 --- a/broker/bam/inc/com/centreon/broker/bam/service_book.hh +++ b/broker/bam/inc/com/centreon/broker/bam/service_book.hh @@ -1,26 +1,27 @@ -/* -** Copyright 2014-2015 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-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 + */ #ifndef CCB_BAM_SERVICE_BOOK_HH #define CCB_BAM_SERVICE_BOOK_HH -#include "com/centreon/broker/bam/service_listener.hh" -#include "com/centreon/broker/io/stream.hh" +#include +#include "com/centreon/broker/bam/service_state.hh" +#include "com/centreon/broker/persistent_cache.hh" CCB_BEGIN() @@ -43,9 +44,14 @@ class service_listener; * Propagate updates of services to service listeners. */ class service_book { - using multimap = - std::multimap, service_listener*>; - multimap _book; + struct service_state_listeners { + std::list listeners; + service_state state; + }; + std::unordered_map, + service_state_listeners, + absl::Hash>> + _book; public: service_book() = default; @@ -70,6 +76,8 @@ class service_book { io::stream* visitor = nullptr); void update(const std::shared_ptr& t, io::stream* visitor = nullptr); + void save_to_cache(persistent_cache& cache) const; + void apply_services_state(const ServicesBookState& state); }; } // namespace bam diff --git a/broker/bam/inc/com/centreon/broker/bam/service_listener.hh b/broker/bam/inc/com/centreon/broker/bam/service_listener.hh index 1971e2c07fb..5509bcbb6df 100644 --- a/broker/bam/inc/com/centreon/broker/bam/service_listener.hh +++ b/broker/bam/inc/com/centreon/broker/bam/service_listener.hh @@ -1,5 +1,5 @@ -/* - * Copyright 2014-2015, 2021-2023 Centreon +/** + * Copyright 2014-2015, 2021-2024 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,6 +33,8 @@ class service_status; } // namespace neb namespace bam { +struct service_state; + /** * @class service_listener service_listener.hh * "com/centreon/broker/bam/service_listener.hh" @@ -47,6 +49,7 @@ class service_listener { service_listener(const service_listener&) = delete; virtual ~service_listener() noexcept = default; service_listener& operator=(const service_listener&) = delete; + virtual void service_update(const service_state& s); virtual void service_update(std::shared_ptr const& status, io::stream* visitor = nullptr); virtual void service_update( diff --git a/broker/bam/inc/com/centreon/broker/bam/service_state.hh b/broker/bam/inc/com/centreon/broker/bam/service_state.hh new file mode 100644 index 00000000000..7695ad0b87d --- /dev/null +++ b/broker/bam/inc/com/centreon/broker/bam/service_state.hh @@ -0,0 +1,62 @@ +/** + * Copyright 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 + */ + +#ifndef CCB_BAM_SERVICE_STATE_HH +#define CCB_BAM_SERVICE_STATE_HH + +#include "bbdo/bam_state.pb.h" +#include "com/centreon/broker/bam/service_listener.hh" +#include "com/centreon/broker/neb/acknowledgement.hh" + +namespace com::centreon::broker::bam { +struct service_state { + uint64_t host_id = 0; + uint64_t service_id = 0; + State current_state = State::UNKNOWN; + State last_hard_state = State::UNKNOWN; + time_t last_check = 0; + bool state_type = false; + bool acknowledged = false; +}; +} // namespace com::centreon::broker::bam + +namespace fmt { +template <> +struct formatter { + constexpr auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) { + return ctx.begin(); + } + + // Formats the point p using the parsed format specification (presentation) + // stored in this formatter. + template + auto format(const com::centreon::broker::bam::service_state& state, + FormatContext& ctx) const -> decltype(ctx.out()) { + return format_to(ctx.out(), + "service state on service ({},{}): current state: {}, " + "last hard state: {}, last check: {}, state type: {}, " + "acknowledged: {}", + state.host_id, state.service_id, + State_Name(state.current_state), + State_Name(state.last_hard_state), state.last_check, + state.state_type, state.acknowledged); + } +}; +} // namespace fmt + +#endif /* !CCB_BAM_SERVICE_STATE_HH */ diff --git a/broker/bam/src/availability_thread.cc b/broker/bam/src/availability_thread.cc index 44f0c5a6bf8..76fa3659188 100644 --- a/broker/bam/src/availability_thread.cc +++ b/broker/bam/src/availability_thread.cc @@ -1,20 +1,20 @@ -/* -** Copyright 2014, 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 2014, 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/bam/availability_thread.hh" diff --git a/broker/bam/src/ba.cc b/broker/bam/src/ba.cc index 1c973f6450d..88f80ca4b0b 100644 --- a/broker/bam/src/ba.cc +++ b/broker/bam/src/ba.cc @@ -503,29 +503,6 @@ void ba::_compute_inherited_downtime(io::stream* visitor) { _in_downtime); } -std::shared_ptr ba::_generate_ba_status( - bool state_changed) const { - auto ret{std::make_shared()}; - BaStatus& status = ret->mut_obj(); - status.set_ba_id(get_id()); - status.set_in_downtime(in_downtime()); - if (_event) - status.set_last_state_change(_event->obj().start_time()); - else - status.set_last_state_change(get_last_kpi_update()); - status.set_level_acknowledgement(_normalize(_acknowledgement_hard)); - status.set_level_downtime(_normalize(_downtime_hard)); - status.set_level_nominal(_normalize(_level_hard)); - status.set_state(com::centreon::broker::State(get_state_hard())); - status.set_state_changed(state_changed); - SPDLOG_LOGGER_DEBUG( - log_v2::bam(), - "BAM: generating status of BA {} '{}' (state {}, in downtime {}, " - "level {})", - _id, _name, status.state(), status.in_downtime(), status.level_nominal()); - return ret; -} - std::shared_ptr ba::_generate_virtual_service_status() const { auto bbdo = config::applier::state::instance().get_bbdo_version(); if (bbdo.major_v < 3) { diff --git a/broker/bam/src/ba_best.cc b/broker/bam/src/ba_best.cc index ef52a7aa8de..0f75c6523b0 100644 --- a/broker/bam/src/ba_best.cc +++ b/broker/bam/src/ba_best.cc @@ -200,3 +200,23 @@ std::string ba_best::get_output() const { std::string ba_best::get_perfdata() const { return {}; } + +std::shared_ptr ba_best::_generate_ba_status( + bool state_changed) const { + auto ret{std::make_shared()}; + BaStatus& status = ret->mut_obj(); + status.set_ba_id(get_id()); + status.set_in_downtime(in_downtime()); + if (_event) + status.set_last_state_change(_event->obj().start_time()); + else + status.set_last_state_change(get_last_kpi_update()); + status.set_state(com::centreon::broker::State(get_state_hard())); + status.set_state_changed(state_changed); + + SPDLOG_LOGGER_DEBUG( + log_v2::bam(), + "BAM: generating status of best BA {} '{}' (state {}, in downtime {})", + get_id(), _name, status.state(), status.in_downtime()); + return ret; +} diff --git a/broker/bam/src/ba_impact.cc b/broker/bam/src/ba_impact.cc index c7defce961b..d916c234cde 100644 --- a/broker/bam/src/ba_impact.cc +++ b/broker/bam/src/ba_impact.cc @@ -265,3 +265,28 @@ void ba_impact::_recompute() { _apply_impact(it->first, it->second); _recompute_count = 0; } + +std::shared_ptr ba_impact::_generate_ba_status( + bool state_changed) const { + auto ret{std::make_shared()}; + BaStatus& status = ret->mut_obj(); + status.set_ba_id(get_id()); + status.set_in_downtime(in_downtime()); + if (_event) + status.set_last_state_change(_event->obj().start_time()); + else + status.set_last_state_change(get_last_kpi_update()); + status.set_level_acknowledgement(_normalize(_acknowledgement_hard)); + status.set_level_downtime(_normalize(_downtime_hard)); + status.set_level_nominal(_normalize(_level_hard)); + status.set_state(com::centreon::broker::State(get_state_hard())); + status.set_state_changed(state_changed); + + SPDLOG_LOGGER_DEBUG( + log_v2::bam(), + "BAM: generating status of impact BA {} '{}' (state {}, in downtime {}, " + "level {})", + get_id(), _name, status.state(), status.in_downtime(), + status.level_nominal()); + return ret; +} diff --git a/broker/bam/src/ba_ratio_number.cc b/broker/bam/src/ba_ratio_number.cc index 4977e563a29..dc124b71df3 100644 --- a/broker/bam/src/ba_ratio_number.cc +++ b/broker/bam/src/ba_ratio_number.cc @@ -188,3 +188,25 @@ std::string ba_ratio_number::get_perfdata() const { static_cast(_level_warning), static_cast(_level_critical), _impacts.size()); } + +std::shared_ptr ba_ratio_number::_generate_ba_status( + bool state_changed) const { + auto ret = std::make_shared(); + BaStatus& status = ret->mut_obj(); + status.set_ba_id(get_id()); + status.set_in_downtime(in_downtime()); + if (_event) + status.set_last_state_change(_event->obj().start_time()); + else + status.set_last_state_change(get_last_kpi_update()); + status.set_level_nominal(_normalize(_level_hard)); + status.set_state(com::centreon::broker::State(get_state_hard())); + status.set_state_changed(state_changed); + + SPDLOG_LOGGER_DEBUG(log_v2::bam(), + "BAM: generating status of ratio number BA {} '{}' " + "(state {}, in downtime {}, level {})", + get_id(), _name, status.state(), status.in_downtime(), + status.level_nominal()); + return ret; +} diff --git a/broker/bam/src/ba_ratio_percent.cc b/broker/bam/src/ba_ratio_percent.cc index 4c4b2e0fb37..8950cfa2b55 100644 --- a/broker/bam/src/ba_ratio_percent.cc +++ b/broker/bam/src/ba_ratio_percent.cc @@ -191,3 +191,25 @@ std::string ba_ratio_percent::get_perfdata() const { static_cast(_level_warning), static_cast(_level_critical)); } + +std::shared_ptr ba_ratio_percent::_generate_ba_status( + bool state_changed) const { + auto ret{std::make_shared()}; + BaStatus& status = ret->mut_obj(); + status.set_ba_id(get_id()); + status.set_in_downtime(in_downtime()); + if (_event) + status.set_last_state_change(_event->obj().start_time()); + else + status.set_last_state_change(get_last_kpi_update()); + status.set_level_nominal(_normalize(_level_hard)); + status.set_state(com::centreon::broker::State(get_state_hard())); + status.set_state_changed(state_changed); + + SPDLOG_LOGGER_DEBUG(log_v2::bam(), + "BAM: generating status of ratio percent BA {} '{}' " + "(state {}, in downtime {}, level {})", + get_id(), _name, status.state(), status.in_downtime(), + status.level_nominal()); + return ret; +} diff --git a/broker/bam/src/ba_svc_mapping.cc b/broker/bam/src/ba_svc_mapping.cc index 4aff684dac8..88a70e5691f 100644 --- a/broker/bam/src/ba_svc_mapping.cc +++ b/broker/bam/src/ba_svc_mapping.cc @@ -1,4 +1,4 @@ -/* +/** * Copyright 2014 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/broker/bam/src/ba_worst.cc b/broker/bam/src/ba_worst.cc index 8c9e79ce25e..7403f26cfb9 100644 --- a/broker/bam/src/ba_worst.cc +++ b/broker/bam/src/ba_worst.cc @@ -220,3 +220,23 @@ std::string ba_worst::get_output() const { std::string ba_worst::get_perfdata() const { return {}; } + +std::shared_ptr ba_worst::_generate_ba_status( + bool state_changed) const { + auto ret = std::make_shared(); + BaStatus& status = ret->mut_obj(); + status.set_ba_id(get_id()); + status.set_in_downtime(in_downtime()); + if (_event) + status.set_last_state_change(_event->obj().start_time()); + else + status.set_last_state_change(get_last_kpi_update()); + status.set_state(com::centreon::broker::State(get_state_hard())); + status.set_state_changed(state_changed); + + SPDLOG_LOGGER_DEBUG( + log_v2::bam(), + "BAM: generating status of worst BA {} '{}' (state {}, in downtime {})", + get_id(), _name, status.state(), status.in_downtime()); + return ret; +} diff --git a/broker/bam/src/bool_and.cc b/broker/bam/src/bool_and.cc index b7827e41946..b4bfd08c393 100644 --- a/broker/bam/src/bool_and.cc +++ b/broker/bam/src/bool_and.cc @@ -1,4 +1,4 @@ -/* +/** * Copyright 2014, 2023 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/broker/bam/src/bool_call.cc b/broker/bam/src/bool_call.cc index 61d7e1fc30f..d9ef598ede9 100644 --- a/broker/bam/src/bool_call.cc +++ b/broker/bam/src/bool_call.cc @@ -1,4 +1,4 @@ -/* +/** * Copyright 2014, 2023 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/broker/bam/src/bool_constant.cc b/broker/bam/src/bool_constant.cc index fd68c36aa20..5db05c3c153 100644 --- a/broker/bam/src/bool_constant.cc +++ b/broker/bam/src/bool_constant.cc @@ -1,5 +1,5 @@ -/* - * Copyright 2016, 2023 Centreon +/** + * Copyright 2014, 2023 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/broker/bam/src/bool_equal.cc b/broker/bam/src/bool_equal.cc index 471fcb3d9a9..fb60c27e62d 100644 --- a/broker/bam/src/bool_equal.cc +++ b/broker/bam/src/bool_equal.cc @@ -1,4 +1,4 @@ -/* +/** * Copyright 2014-2023 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -33,7 +33,6 @@ double bool_equal::value_hard() const { if (state_known()) retval = std::fabs(_left_hard - _right_hard) < COMPARE_EPSILON; - log_v2::bam()->trace("BAM: bool_equal: value as double: {}", retval); return retval; } @@ -47,7 +46,6 @@ bool bool_equal::boolean_value() const { if (state_known()) retval = std::fabs(_left_hard - _right_hard) < COMPARE_EPSILON; - log_v2::bam()->trace("BAM: bool_equal: value: {}", retval); return retval; } diff --git a/broker/bam/src/bool_expression.cc b/broker/bam/src/bool_expression.cc index ddae66dbd62..91786490f2f 100644 --- a/broker/bam/src/bool_expression.cc +++ b/broker/bam/src/bool_expression.cc @@ -1,4 +1,4 @@ -/* +/** * Copyright 2014, 2021-2023 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/broker/bam/src/bool_less_than.cc b/broker/bam/src/bool_less_than.cc index e2c6ecac6b6..ec6090d4285 100644 --- a/broker/bam/src/bool_less_than.cc +++ b/broker/bam/src/bool_less_than.cc @@ -1,4 +1,4 @@ -/* +/** * Copyright 2014, 2021-2023 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/broker/bam/src/bool_more_than.cc b/broker/bam/src/bool_more_than.cc index 7b1a65bce18..2f6b25e340a 100644 --- a/broker/bam/src/bool_more_than.cc +++ b/broker/bam/src/bool_more_than.cc @@ -1,4 +1,4 @@ -/* +/** * Copyright 2014, 2023 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/broker/bam/src/bool_not.cc b/broker/bam/src/bool_not.cc index f012089888a..f0b8be0f03c 100644 --- a/broker/bam/src/bool_not.cc +++ b/broker/bam/src/bool_not.cc @@ -1,4 +1,4 @@ -/* +/** * Copyright 2014, 2022-2023 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/broker/bam/src/bool_not_equal.cc b/broker/bam/src/bool_not_equal.cc index 4879a96d3de..77de82ca2a2 100644 --- a/broker/bam/src/bool_not_equal.cc +++ b/broker/bam/src/bool_not_equal.cc @@ -1,4 +1,4 @@ -/* +/** * Copyright 2016-2023 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/broker/bam/src/bool_operation.cc b/broker/bam/src/bool_operation.cc index f9744832078..12bef11453b 100644 --- a/broker/bam/src/bool_operation.cc +++ b/broker/bam/src/bool_operation.cc @@ -1,4 +1,4 @@ -/* +/** * Copyright 2014-2023 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -110,14 +110,19 @@ std::string bool_operation::object_info() const { switch (_type) { case addition: op = "PLUS"; + break; case substraction: op = "MINUS"; + break; case multiplication: op = "MUL"; + break; case division: op = "DIV"; + break; case modulo: op = "MODULO"; + break; default: return "unknown operation"; } diff --git a/broker/bam/src/bool_or.cc b/broker/bam/src/bool_or.cc index 0c15a6cbc72..f85d11d3a3e 100644 --- a/broker/bam/src/bool_or.cc +++ b/broker/bam/src/bool_or.cc @@ -1,4 +1,4 @@ -/* +/** * Copyright 2014, 2021, 2023 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/broker/bam/src/bool_service.cc b/broker/bam/src/bool_service.cc index 406f29cc4fd..fd6fd94d4f2 100644 --- a/broker/bam/src/bool_service.cc +++ b/broker/bam/src/bool_service.cc @@ -1,5 +1,5 @@ -/* - * Copyright 2014, 2021-2023 Centreon +/** + * Copyright 2014, 2021-2024 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,10 +16,9 @@ * For more information : contact@centreon.com */ -#include - #include "com/centreon/broker/bam/bool_service.hh" - +#include "bbdo/bam/state.hh" +#include "com/centreon/broker/bam/service_state.hh" #include "com/centreon/broker/log_v2.hh" #include "com/centreon/broker/neb/service_status.hh" @@ -54,6 +53,30 @@ uint32_t bool_service::get_service_id() const { return _service_id; } +/** + * @brief When the cache is restored, this method is used to update services in + * BAs. It works as others service_update() methods, except no visitor is + * needed here. + * + * @param s The service_state to apply. + */ +void bool_service::service_update(const service_state& s) { + // Update information. + log_v2::bam()->debug("BAM: bool_service updated with service state {}", s); + bool changed = + _state_hard != static_cast(s.last_hard_state) || !_state_known; + + _state_hard = static_cast(s.last_hard_state); + log_v2::bam()->debug("BAM: bool_service ({},{}) state hard set to {}", + s.host_id, s.service_id, _state_hard); + if (_state_hard != state_unknown) + _state_known = true; + + // Propagate change. + if (changed) + notify_parents_of_change(nullptr); +} + /** * Notify of service update. * @@ -159,7 +182,6 @@ bool bool_service::boolean_value() const { * @return True if the state is known. */ bool bool_service::state_known() const { - log_v2::bam()->trace("BAM: bool_service::state_known: {}", _state_known); return _state_known; } diff --git a/broker/bam/src/bool_value.cc b/broker/bam/src/bool_value.cc index 8979e7a3fd6..e2bcb28de06 100644 --- a/broker/bam/src/bool_value.cc +++ b/broker/bam/src/bool_value.cc @@ -1,20 +1,20 @@ -/* -** 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/bool_value.hh" diff --git a/broker/bam/src/bool_xor.cc b/broker/bam/src/bool_xor.cc index 258b52bbcb3..69bf05d54d3 100644 --- a/broker/bam/src/bool_xor.cc +++ b/broker/bam/src/bool_xor.cc @@ -1,4 +1,4 @@ -/* +/** * Copyright 2014, 2022-2023 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/broker/bam/src/configuration/applier/ba.cc b/broker/bam/src/configuration/applier/ba.cc index f17e72bbb48..84c3dacccd6 100644 --- a/broker/bam/src/configuration/applier/ba.cc +++ b/broker/bam/src/configuration/applier/ba.cc @@ -1,5 +1,5 @@ -/* - * Copyright 2014-2017, 2021-2023 Centreon +/** + * Copyright 2014-2017, 2021-2024 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,11 +18,13 @@ #include "com/centreon/broker/bam/configuration/applier/ba.hh" #include +#include "bbdo/bam/inherited_downtime.hh" #include "com/centreon/broker/bam/ba_best.hh" #include "com/centreon/broker/bam/ba_impact.hh" #include "com/centreon/broker/bam/ba_ratio_number.hh" #include "com/centreon/broker/bam/ba_ratio_percent.hh" #include "com/centreon/broker/bam/ba_worst.hh" +#include "com/centreon/broker/bam/internal.hh" #include "com/centreon/broker/config/applier/state.hh" #include "com/centreon/broker/log_v2.hh" #include "com/centreon/broker/multiplexing/publisher.hh" @@ -70,7 +72,7 @@ applier::ba& applier::ba::operator=(applier::ba const& other) { * @param[in] my_bas BAs to apply. * @param[in] book The service book. */ -void applier::ba::apply(bam::configuration::state::bas const& my_bas, +void applier::ba::apply(const bam::configuration::state::bas& my_bas, service_book& book) { // // DIFF @@ -371,13 +373,11 @@ std::shared_ptr applier::ba::_new_ba(configuration::ba const& cfg, * @param[in] cache The cache. */ void applier::ba::save_to_cache(persistent_cache& cache) { - cache.transaction(); for (std::map::const_iterator it = _applied.begin(), end = _applied.end(); it != end; ++it) { it->second.obj->save_inherited_downtime(cache); } - cache.commit(); } /** @@ -385,53 +385,44 @@ void applier::ba::save_to_cache(persistent_cache& cache) { * * @param[in] cache The cache. */ -void applier::ba::load_from_cache(persistent_cache& cache) { - log_v2::bam()->trace("BAM: loading inherited downtimes from cache"); +void applier::ba::apply_inherited_downtime(const inherited_downtime& dwn) { auto bbdo = config::applier::state::instance().get_bbdo_version(); bool bbdo3_enabled = bbdo.major_v >= 3; - std::shared_ptr d; - cache.get(d); - while (d) { - if (d->type() == inherited_downtime::static_type()) { - inherited_downtime const& dwn = - *std::static_pointer_cast(d); - std::map::iterator found = _applied.find(dwn.ba_id); - if (found != _applied.end()) { - log_v2::bam()->debug("BAM: found an inherited downtime for BA {}", - found->first); - found->second.obj->set_inherited_downtime(dwn); - std::shared_ptr s; - if (bbdo3_enabled) - s = _ba_pb_service(found->first, found->second.cfg.get_host_id(), - found->second.cfg.get_service_id(), - dwn.in_downtime); - else - s = _ba_service(found->first, found->second.cfg.get_host_id(), - found->second.cfg.get_service_id(), dwn.in_downtime); - multiplexing::publisher().write(s); - } - cache.get(d); - } else if (d->type() == pb_inherited_downtime::static_type()) { - pb_inherited_downtime const& dwn = - *std::static_pointer_cast(d); - std::map::iterator found = - _applied.find(dwn.obj().ba_id()); - if (found != _applied.end()) { - log_v2::bam()->debug("BAM: found an inherited downtime for BA {}", - found->first); - found->second.obj->set_inherited_downtime(dwn); - std::shared_ptr s; - if (bbdo3_enabled) - s = _ba_pb_service(found->first, found->second.cfg.get_host_id(), - found->second.cfg.get_service_id(), - dwn.obj().in_downtime()); - else - s = _ba_service(found->first, found->second.cfg.get_host_id(), - found->second.cfg.get_service_id(), - dwn.obj().in_downtime()); - multiplexing::publisher().write(s); - } - cache.get(d); - } + + std::map::iterator found = _applied.find(dwn.ba_id); + if (found != _applied.end()) { + log_v2::bam()->debug("BAM: found an inherited downtime for BA {}", + found->first); + found->second.obj->set_inherited_downtime(dwn); + std::shared_ptr s; + if (bbdo3_enabled) + s = _ba_pb_service(found->first, found->second.cfg.get_host_id(), + found->second.cfg.get_service_id(), dwn.in_downtime); + else + s = _ba_service(found->first, found->second.cfg.get_host_id(), + found->second.cfg.get_service_id(), dwn.in_downtime); + multiplexing::publisher().write(s); + } +} + +void applier::ba::apply_inherited_downtime(const pb_inherited_downtime& dwn) { + auto bbdo = config::applier::state::instance().get_bbdo_version(); + bool bbdo3_enabled = bbdo.major_v >= 3; + std::map::iterator found = + _applied.find(dwn.obj().ba_id()); + if (found != _applied.end()) { + log_v2::bam()->debug("BAM: found an inherited downtime for BA {}", + found->first); + found->second.obj->set_inherited_downtime(dwn); + std::shared_ptr s; + if (bbdo3_enabled) + s = _ba_pb_service(found->first, found->second.cfg.get_host_id(), + found->second.cfg.get_service_id(), + dwn.obj().in_downtime()); + else + s = _ba_service(found->first, found->second.cfg.get_host_id(), + found->second.cfg.get_service_id(), + dwn.obj().in_downtime()); + multiplexing::publisher().write(s); } } diff --git a/broker/bam/src/configuration/applier/bool_expression.cc b/broker/bam/src/configuration/applier/bool_expression.cc index 6c6d938961a..8aefc3b3d31 100644 --- a/broker/bam/src/configuration/applier/bool_expression.cc +++ b/broker/bam/src/configuration/applier/bool_expression.cc @@ -1,20 +1,20 @@ -/* -** Copyright 2014-2016 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/bool_expression.hh" #include "com/centreon/broker/bam/ba.hh" diff --git a/broker/bam/src/configuration/applier/kpi.cc b/broker/bam/src/configuration/applier/kpi.cc index 83fb3edd414..73c4ad51356 100644 --- a/broker/bam/src/configuration/applier/kpi.cc +++ b/broker/bam/src/configuration/applier/kpi.cc @@ -1,20 +1,20 @@ -/* -** Copyright 2014-2015, 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 2014-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/bam/configuration/applier/kpi.hh" #include "com/centreon/broker/bam/bool_expression.hh" diff --git a/broker/bam/src/configuration/applier/state.cc b/broker/bam/src/configuration/applier/state.cc index b269b173c87..9473f05190d 100644 --- a/broker/bam/src/configuration/applier/state.cc +++ b/broker/bam/src/configuration/applier/state.cc @@ -1,4 +1,4 @@ -/* +/** * Copyright 2014-2023 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,10 +17,9 @@ */ #include "com/centreon/broker/bam/configuration/applier/state.hh" -#include #include "com/centreon/broker/bam/exp_builder.hh" -#include "com/centreon/broker/bam/exp_parser.hh" -#include "com/centreon/exceptions/msg_fmt.hh" +#include "com/centreon/broker/bam/internal.hh" +#include "com/centreon/broker/log_v2.hh" using namespace com::centreon::exceptions; using namespace com::centreon::broker; @@ -71,16 +70,6 @@ static std::string service_node_id(uint32_t host_id, uint32_t service_id) { return fmt::format("service ({}, {})", host_id, service_id); } -/** - * Default constructor. - */ -applier::state::state() {} - -/** - * Destructor. - */ -applier::state::~state() {} - /** * Apply configuration. * @@ -233,7 +222,12 @@ void applier::state::_circular_check(applier::state::circular_check_node& n) { * @param[in] cache The cache. */ void applier::state::save_to_cache(persistent_cache& cache) { + log_v2::bam()->trace("BAM: Saving states to cache"); + cache.transaction(); + _book_service.save_to_cache(cache); _ba_applier.save_to_cache(cache); + cache.commit(); + log_v2::bam()->trace("BAM: States correctly saved"); } /** @@ -242,7 +236,32 @@ void applier::state::save_to_cache(persistent_cache& cache) { * @param[in] cache the cache. */ void applier::state::load_from_cache(persistent_cache& cache) { - _ba_applier.load_from_cache(cache); + log_v2::bam()->debug( + "BAM: Loading restoring inherited downtimes and BA states"); + + std::shared_ptr d; + cache.get(d); + while (d) { + switch (d->type()) { + case inherited_downtime::static_type(): { + const inherited_downtime& dwn = + *std::static_pointer_cast(d); + _ba_applier.apply_inherited_downtime(dwn); + } break; + case pb_inherited_downtime::static_type(): { + const pb_inherited_downtime& dwn = + *std::static_pointer_cast(d); + _ba_applier.apply_inherited_downtime(dwn); + } break; + case pb_services_book_state::static_type(): { + const ServicesBookState& state = + std::static_pointer_cast(d)->obj(); + _book_service.apply_services_state(state); + } break; + } + cache.get(d); + } + log_v2::bam()->debug("BAM: Inherited downtimes and BA states restored"); } /** diff --git a/broker/bam/src/configuration/ba.cc b/broker/bam/src/configuration/ba.cc index 9d70aa50ac7..b255a5d2b9b 100644 --- a/broker/bam/src/configuration/ba.cc +++ b/broker/bam/src/configuration/ba.cc @@ -1,20 +1,20 @@ -/* -** 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 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/configuration/ba.hh" #include "com/centreon/broker/bam/configuration/reader_exception.hh" diff --git a/broker/bam/src/configuration/state.cc b/broker/bam/src/configuration/state.cc index 764cf881714..69eb2f00053 100644 --- a/broker/bam/src/configuration/state.cc +++ b/broker/bam/src/configuration/state.cc @@ -1,20 +1,20 @@ -/* -** Copyright 2014-2015 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-2015 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/configuration/state.hh" diff --git a/broker/bam/src/connector.cc b/broker/bam/src/connector.cc index 37a3d36a319..15749645106 100644 --- a/broker/bam/src/connector.cc +++ b/broker/bam/src/connector.cc @@ -1,20 +1,20 @@ -/* -** Copyright 2014-2015, 2021, 2023 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-2015, 2021, 2023 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/connector.hh" diff --git a/broker/bam/src/event_cache_visitor.cc b/broker/bam/src/event_cache_visitor.cc index 6242ad6bc4f..73f897b141c 100644 --- a/broker/bam/src/event_cache_visitor.cc +++ b/broker/bam/src/event_cache_visitor.cc @@ -1,20 +1,20 @@ -/* -** Copyright 2014-2015, 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 2014-2015, 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/bam/event_cache_visitor.hh" diff --git a/broker/bam/src/exp_builder.cc b/broker/bam/src/exp_builder.cc index df218096b99..14dcc4f650f 100644 --- a/broker/bam/src/exp_builder.cc +++ b/broker/bam/src/exp_builder.cc @@ -1,20 +1,20 @@ -/* -** Copyright 2016 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 2016 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/exp_builder.hh" diff --git a/broker/bam/src/exp_parser.cc b/broker/bam/src/exp_parser.cc index 81bbaa3bc25..5326289e5e6 100644 --- a/broker/bam/src/exp_parser.cc +++ b/broker/bam/src/exp_parser.cc @@ -1,20 +1,20 @@ -/* -** Copyright 2016 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 2016 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/exp_parser.hh" diff --git a/broker/bam/src/exp_tokenizer.cc b/broker/bam/src/exp_tokenizer.cc index c7536aba439..5b714231e46 100644 --- a/broker/bam/src/exp_tokenizer.cc +++ b/broker/bam/src/exp_tokenizer.cc @@ -1,20 +1,20 @@ -/* -** Copyright 2016 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 2016 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/exp_tokenizer.hh" diff --git a/broker/bam/src/factory.cc b/broker/bam/src/factory.cc index 6f0c5ba0f73..e484e884dd4 100644 --- a/broker/bam/src/factory.cc +++ b/broker/bam/src/factory.cc @@ -1,20 +1,20 @@ -/* -** Copyright 2014-2016, 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 2014-2016, 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/bam/factory.hh" diff --git a/broker/bam/src/hst_svc_mapping.cc b/broker/bam/src/hst_svc_mapping.cc index 40ac81ff8ac..9de8b69627c 100644 --- a/broker/bam/src/hst_svc_mapping.cc +++ b/broker/bam/src/hst_svc_mapping.cc @@ -1,20 +1,20 @@ -/* -** Copyright 2014, 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 2014, 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/bam/hst_svc_mapping.hh" #include "com/centreon/broker/log_v2.hh" diff --git a/broker/bam/src/impact_values.cc b/broker/bam/src/impact_values.cc index d576fc623fc..88659136476 100644 --- a/broker/bam/src/impact_values.cc +++ b/broker/bam/src/impact_values.cc @@ -1,20 +1,20 @@ -/* -** 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 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/impact_values.hh" diff --git a/broker/bam/src/kpi.cc b/broker/bam/src/kpi.cc index 02ad2d7d725..cc637c59227 100644 --- a/broker/bam/src/kpi.cc +++ b/broker/bam/src/kpi.cc @@ -1,20 +1,20 @@ -/* -** Copyright 2014-2015, 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 2014-2015, 2021-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/kpi.hh" @@ -124,4 +124,4 @@ void kpi::_event_init() { _event->set_kpi_id(_id); _event->set_ba_id(_ba_id); } -} \ No newline at end of file +} diff --git a/broker/bam/src/kpi_service.cc b/broker/bam/src/kpi_service.cc index 2ebc7f89c13..7e2819a1c67 100644 --- a/broker/bam/src/kpi_service.cc +++ b/broker/bam/src/kpi_service.cc @@ -1,5 +1,5 @@ -/* - * Copyright 2014-2015, 2021-2023 Centreon +/** + * Copyright 2014-2015, 2021-2024 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ */ #include "com/centreon/broker/bam/kpi_service.hh" +#include "com/centreon/broker/bam/service_state.hh" #include @@ -32,7 +33,7 @@ using namespace com::centreon::exceptions; using namespace com::centreon::broker; using namespace com::centreon::broker::bam; -static bool time_is_undefined(uint64_t t) { +static constexpr bool time_is_undefined(uint64_t t) { return t == 0 || t == static_cast(-1); } @@ -164,6 +165,32 @@ bool kpi_service::is_acknowledged() const { return _acknowledged; } +void kpi_service::service_update(const service_state& s) { + // Log message. + log_v2::bam()->debug("BAM: Service KPI {} is restored from persistent cache", + _id); + + // Update information. + if (!time_is_undefined(s.last_check)) { + _last_check = s.last_check; + log_v2::bam()->trace( + "service kpi {} last check updated with status last check {}", _id, + s.last_check); + } + bool changed = _state_hard != static_cast(s.last_hard_state) || + _state_soft != static_cast(s.current_state) || + _state_type != s.state_type || _acknowledged != s.acknowledged; + + _state_hard = static_cast(s.last_hard_state); + _state_soft = static_cast(s.current_state); + _state_type = s.state_type; + _acknowledged = s.acknowledged; + + // Propagate change. + if (changed) + notify_parents_of_change(nullptr); +} + /** * Service got updated ! * @@ -381,15 +408,13 @@ void kpi_service::service_update( */ void kpi_service::service_update(const std::shared_ptr& dt, io::stream* visitor) { - assert(dt && dt->host_id == _host_id && dt->service_id == _service_id); + log_v2::bam()->info( + "kpi_service:service_update on downtime {}: was started {} ; actual end " + "time {}", + dt->internal_id, dt->was_started, dt->actual_end_time.get_time_t()); // Update information. bool downtimed = dt->was_started && dt->actual_end_time.is_null(); - bool changed = true; - - if (!_downtimed && downtimed) { - _downtimed = true; - changed = true; - } + bool changed = false; if (_downtime_ids.contains(dt->internal_id) && dt->deletion_time.is_null()) { log_v2::bam()->trace("Downtime {} already handled in this kpi service", @@ -397,17 +422,26 @@ void kpi_service::service_update(const std::shared_ptr& dt, return; } + log_v2::bam()->info( + "kpi_service:service_update on downtime {}: was started {} ; actual end " + "time {} ; downtimed {}", + dt->internal_id, dt->was_started, dt->actual_end_time.get_time_t(), + downtimed); if (downtimed) { log_v2::bam()->trace("adding in kpi service the impacting downtime {}", dt->internal_id); _downtime_ids.insert(dt->internal_id); + if (!_downtimed) { + _downtimed = true; + changed = true; + } } else { log_v2::bam()->trace("removing from kpi service the impacting downtime {}", dt->internal_id); _downtime_ids.erase(dt->internal_id); bool new_downtimed = !_downtime_ids.empty(); if (new_downtimed != _downtimed) { - _downtimed = !_downtime_ids.empty(); + _downtimed = new_downtimed; changed = true; } } diff --git a/broker/bam/src/main.cc b/broker/bam/src/main.cc index 10b0025bced..45f5190ee1b 100644 --- a/broker/bam/src/main.cc +++ b/broker/bam/src/main.cc @@ -170,6 +170,8 @@ void broker_module_init(void const* arg) { e.register_event(bam::pb_dimension_truncate_table_signal::static_type(), "DimensionTruncateTableSignal", &bam::pb_dimension_truncate_table_signal::operations); + e.register_event(bam::pb_services_book_state::static_type(), "BaState", + &bam::pb_services_book_state::operations); /* Let's register the ba_info event to be sure it is declared in case * brokerrpc is not already instanciated. */ e.register_event(make_type(io::extcmd, extcmd::de_ba_info), "ba_info", diff --git a/broker/bam/src/monitoring_stream.cc b/broker/bam/src/monitoring_stream.cc index 9ba1a58c922..efc97970a6c 100644 --- a/broker/bam/src/monitoring_stream.cc +++ b/broker/bam/src/monitoring_stream.cc @@ -75,8 +75,6 @@ monitoring_stream::monitoring_stream(const std::string& ext_cmd_file, // Let's update BAs then we will be able to load the cache with inherited // downtimes. update(); - // Read cache. - _read_cache(); } /** @@ -181,6 +179,8 @@ void monitoring_stream::update() { _ba_mapping = s.get_ba_svc_mapping(); _rebuild(); initialize(); + // Read cache. + _read_cache(); } catch (std::exception const& e) { throw msg_fmt("BAM: could not process configuration update: {}", e.what()); } @@ -500,6 +500,7 @@ int monitoring_stream::write(std::shared_ptr const& data) { _write_external_command(cmd); } break; case extcmd::pb_ba_info::static_type(): { + log_v2::bam()->info("BAM: dump BA"); extcmd::pb_ba_info const& e = *std::static_pointer_cast(data); auto& obj = e.obj(); diff --git a/broker/bam/src/reporting_stream.cc b/broker/bam/src/reporting_stream.cc index afc421d0ea9..0d76622bd93 100644 --- a/broker/bam/src/reporting_stream.cc +++ b/broker/bam/src/reporting_stream.cc @@ -1,20 +1,20 @@ -/* -** Copyright 2014-2015,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 2014-2015,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 diff --git a/broker/bam/src/service_book.cc b/broker/bam/src/service_book.cc index 49162bdbeef..5780351485e 100644 --- a/broker/bam/src/service_book.cc +++ b/broker/bam/src/service_book.cc @@ -1,31 +1,34 @@ -/* -** Copyright 2014-2015, 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 2014-2015, 2021-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/service_book.hh" -#include "com/centreon/broker/bam/service_listener.hh" +#include "com/centreon/broker/bam/internal.hh" #include "com/centreon/broker/log_v2.hh" -#include "com/centreon/broker/neb/acknowledgement.hh" #include "com/centreon/broker/neb/downtime.hh" #include "com/centreon/broker/neb/service_status.hh" using namespace com::centreon::broker::bam; +static constexpr bool time_is_undefined(uint64_t t) { + return t == 0 || t == static_cast(-1); +} + /** * Make a service listener listen to service updates. * @@ -38,7 +41,13 @@ void service_book::listen(uint32_t host_id, service_listener* listnr) { log_v2::bam()->trace("BAM: service ({}, {}) added to service book", host_id, service_id); - _book.insert(std::make_pair(std::make_pair(host_id, service_id), listnr)); + auto found = _book.find(std::make_pair(host_id, service_id)); + if (found == _book.end()) { + service_state_listeners sl{{listnr}, + {.host_id = host_id, .service_id = service_id}}; + _book[std::make_pair(host_id, service_id)] = sl; + } else + _book[std::make_pair(host_id, service_id)].listeners.push_back(listnr); } /** @@ -51,14 +60,12 @@ void service_book::listen(uint32_t host_id, void service_book::unlisten(uint32_t host_id, uint32_t service_id, service_listener* listnr) { - std::pair range( - _book.equal_range(std::make_pair(host_id, service_id))); - while (range.first != range.second) { - if (range.first->second == listnr) { - _book.erase(range.first); - break; - } - ++range.first; + auto found = _book.find(std::make_pair(host_id, service_id)); + if (found != _book.end()) { + found->second.listeners.remove_if( + [listnr](service_listener* item) { return item == listnr; }); + if (found->second.listeners.empty()) + _book.erase(found); } } @@ -71,12 +78,14 @@ void service_book::unlisten(uint32_t host_id, */ void service_book::update(const std::shared_ptr& t, io::stream* visitor) { - std::pair range{_book.equal_range( - std::make_pair(t->obj().host_id(), t->obj().service_id()))}; - while (range.first != range.second) { - range.first->second->service_update(t, visitor); - ++range.first; - } + auto found = + _book.find(std::make_pair(t->obj().host_id(), t->obj().service_id())); + if (found == _book.end()) + return; + auto& svc_state = found->second.state; + svc_state.acknowledged = time_is_undefined(t->obj().deletion_time()); + for (auto l : found->second.listeners) + l->service_update(t, visitor); } /** @@ -88,12 +97,13 @@ void service_book::update(const std::shared_ptr& t, */ void service_book::update(const std::shared_ptr& t, io::stream* visitor) { - std::pair range{ - _book.equal_range(std::make_pair(t->host_id, t->service_id))}; - while (range.first != range.second) { - range.first->second->service_update(t, visitor); - ++range.first; - } + auto found = _book.find(std::make_pair(t->host_id, t->service_id)); + if (found == _book.end()) + return; + auto& svc_state = found->second.state; + svc_state.acknowledged = t->deletion_time.is_null(); + for (auto l : found->second.listeners) + l->service_update(t, visitor); } /** @@ -105,12 +115,11 @@ void service_book::update(const std::shared_ptr& t, */ void service_book::update(const std::shared_ptr& t, io::stream* visitor) { - std::pair range{ - _book.equal_range(std::make_pair(t->host_id, t->service_id))}; - while (range.first != range.second) { - range.first->second->service_update(t, visitor); - ++range.first; - } + auto found = _book.find(std::make_pair(t->host_id, t->service_id)); + if (found == _book.end()) + return; + for (auto l : found->second.listeners) + l->service_update(t, visitor); } /** @@ -122,13 +131,12 @@ void service_book::update(const std::shared_ptr& t, */ void service_book::update(const std::shared_ptr& t, io::stream* visitor) { - auto& downtime = t->obj(); - std::pair range{_book.equal_range( - std::make_pair(downtime.host_id(), downtime.service_id()))}; - while (range.first != range.second) { - range.first->second->service_update(t, visitor); - ++range.first; - } + auto found = + _book.find(std::make_pair(t->obj().host_id(), t->obj().service_id())); + if (found == _book.end()) + return; + for (auto l : found->second.listeners) + l->service_update(t, visitor); } /** @@ -140,17 +148,17 @@ void service_book::update(const std::shared_ptr& t, */ void service_book::update(const std::shared_ptr& t, io::stream* visitor) { - std::pair range{ - _book.equal_range(std::make_pair(t->host_id, t->service_id))}; - size_t count = 0; - while (range.first != range.second) { - count++; - range.first->second->service_update(t, visitor); - ++range.first; - } - log_v2::bam()->trace( - "service_book: {} listeners notified of service ({},{}) status", count, - t->host_id, t->service_id); + auto found = _book.find(std::make_pair(t->host_id, t->service_id)); + if (found == _book.end()) + return; + auto& svc_state = found->second.state; + svc_state.current_state = static_cast(t->current_state); + svc_state.last_hard_state = static_cast(t->last_hard_state); + svc_state.last_check = t->last_check; + svc_state.state_type = t->state_type; + + for (auto l : found->second.listeners) + l->service_update(t, visitor); } /** @@ -162,17 +170,20 @@ void service_book::update(const std::shared_ptr& t, */ void service_book::update(const std::shared_ptr& t, io::stream* visitor) { - std::pair range{_book.equal_range( - std::make_pair(t->obj().host_id(), t->obj().service_id()))}; - size_t count = 0; - while (range.first != range.second) { - ++count; - range.first->second->service_update(t, visitor); - ++range.first; - } - log_v2::bam()->trace( - "service_book: {} listeners notified of pb service ({},{})", count, - t->obj().host_id(), t->obj().service_id()); + auto found = + _book.find(std::make_pair(t->obj().host_id(), t->obj().service_id())); + if (found == _book.end()) + return; + auto& svc_state = found->second.state; + auto& o = t->obj(); + svc_state.last_check = + time_is_undefined(o.last_check()) ? std::time(nullptr) : o.last_check(); + svc_state.last_hard_state = static_cast(o.last_hard_state()); + svc_state.current_state = static_cast(o.state()); + svc_state.state_type = o.state_type(); + + for (auto l : found->second.listeners) + l->service_update(t, visitor); } /** @@ -184,15 +195,65 @@ void service_book::update(const std::shared_ptr& t, */ void service_book::update(const std::shared_ptr& t, io::stream* visitor) { - std::pair range{_book.equal_range( - std::make_pair(t->obj().host_id(), t->obj().service_id()))}; - size_t count = 0; - while (range.first != range.second) { - ++count; - range.first->second->service_update(t, visitor); - ++range.first; + auto found = + _book.find(std::make_pair(t->obj().host_id(), t->obj().service_id())); + if (found == _book.end()) + return; + auto& svc_state = found->second.state; + auto& o = t->obj(); + svc_state.last_check = + time_is_undefined(o.last_check()) ? std::time(nullptr) : o.last_check(); + svc_state.last_hard_state = static_cast(o.last_hard_state()); + svc_state.current_state = static_cast(o.state()); + svc_state.state_type = o.state_type(); + + for (auto l : found->second.listeners) + l->service_update(t, visitor); +} + +/** + * @brief Save the services states from the service_book to the cache. The cache + * must be open with write mode, otherwise, this function throws an exception. + * + * @param cache The persistent cache to receive data. + */ +void service_book::save_to_cache(persistent_cache& cache) const { + auto to_save_ptr = std::make_shared(); + ServicesBookState& to_save = to_save_ptr->mut_obj(); + for (auto it = _book.begin(); it != _book.end(); ++it) { + auto& state = it->second.state; + auto* svc = to_save.add_service(); + svc->set_host_id(state.host_id); + svc->set_service_id(state.service_id); + svc->set_current_state(state.current_state); + svc->set_last_hard_state(state.last_hard_state); + svc->set_last_check(state.last_check); + svc->set_state_type(state.state_type); + svc->set_acknowledged(state.acknowledged); + } + cache.add(to_save_ptr); +} + +/** + * @brief Restore from the ServicesBookState the states of all the services + * involved in BAs. + * + * @param state A ServicesBookState get from the cache. + */ +void service_book::apply_services_state(const ServicesBookState& state) { + log_v2::bam()->trace("BAM: applying services state from cache"); + for (auto& svc : state.service()) { + auto found = _book.find(std::make_pair(svc.host_id(), svc.service_id())); + if (found == _book.end()) + continue; + auto& svc_state = found->second.state; + svc_state.current_state = svc.current_state(); + svc_state.last_hard_state = svc.last_hard_state(); + svc_state.last_check = svc.last_check(); + svc_state.state_type = svc.state_type(); + svc_state.acknowledged = svc.acknowledged(); + for (auto l : found->second.listeners) + l->service_update(svc_state); } - log_v2::bam()->trace( - "service_book: {} listeners notified of pb service ({},{}) status", count, - t->obj().host_id(), t->obj().service_id()); + log_v2::bam()->trace("BAM: Services state applied from cache"); } diff --git a/broker/bam/src/service_listener.cc b/broker/bam/src/service_listener.cc index 91238966b59..46aa9ccce93 100644 --- a/broker/bam/src/service_listener.cc +++ b/broker/bam/src/service_listener.cc @@ -1,25 +1,34 @@ -/* -** Copyright 2014-2015 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-2015 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/service_listener.hh" using namespace com::centreon::broker::bam; +/** + * Notify of a service status update. + * + * @param[in] status Service status. + * @param[out] visitor Visitor. + */ +void service_listener::service_update(const service_state& state + [[maybe_unused]]) {} + /** * Notify of a service status update. * diff --git a/tests/bam/bam_pb.robot b/tests/bam/bam_pb.robot index 0200cd65874..e3d9e9f6518 100644 --- a/tests/bam/bam_pb.robot +++ b/tests/bam/bam_pb.robot @@ -59,8 +59,8 @@ BAPBSTATUS Connect To Database pymysql ${DBNameConf} ${DBUser} ${DBPass} ${DBHost} ${DBPort} ${output} Query - ... SELECT current_level, acknowledged, downtime, in_downtime, current_status FROM mod_bam WHERE name='test' - Should Be Equal As Strings ${output} ((100.0, 0.0, 0.0, 0, 2),) + ... SELECT acknowledged, downtime, in_downtime, current_status FROM mod_bam WHERE name='test' + Should Be Equal As Strings ${output} ((0.0, 0.0, 0, 2),) # Little check of the GetBa gRPC command ${result} Run Keyword And Return Status File Should Exist /tmp/output diff --git a/tests/resources/Common.py b/tests/resources/Common.py index 6cfd477e66b..9824ee8175b 100644 --- a/tests/resources/Common.py +++ b/tests/resources/Common.py @@ -1342,9 +1342,47 @@ def has_file_permissions(path: str, permission: int): @permission mask to test file permission @return True if the file has the requested permissions """ - stat_res= os.stat(path) + stat_res = os.stat(path) if stat_res is None: logger.console(f"fail to get permission of {path}") return False masked = stat_res.st_mode & permission return masked == permission + + +def compare_dot_files(file1: str, file2: str): + """ + Compare two dot files file1 and file2 after removing the pointer addresses + that clearly are not the same. + + Args: + file1: The first file to compare. + file2: The second file to compare. + + Returns: True if they have the same content, False otherwise. + """ + + with open(file1, "r") as f1: + content1 = f1.readlines() + with open(file2, "r") as f2: + content2 = f2.readlines() + r = re.compile(r"(.*) 0x[0-9a-f]+") + + def replace_ptr(line): + m = r.match(line) + if m: + return m.group(1) + else: + return line + + content1 = list(map(replace_ptr, content1)) + content2 = list(map(replace_ptr, content2)) + + if len(content1) != len(content2): + return False + for i in range(len(content1)): + if content1[i] != content2[i]: + logger.console( + f"Files are different at line {i + 1}: first => << {content1[i].strip()} >> and second => << {content2[i].strip()} >>") + return False + return True diff --git a/tests/resources/resources.robot b/tests/resources/resources.robot index a9c9c52b0d0..2254c0eea38 100644 --- a/tests/resources/resources.robot +++ b/tests/resources/resources.robot @@ -54,6 +54,11 @@ Start Broker Start Process /usr/sbin/cbd ${EtcRoot}/centreon-broker/central-rrd.json alias=b2 END +Restart Broker + [Arguments] ${only_central}=False + Kindly Stop Broker ${only_central} + Start Broker ${only_central} + Reload Broker Send Signal To Process SIGHUP b1 Send Signal To Process SIGHUP b2