diff --git a/broker/unified_sql/inc/com/centreon/broker/unified_sql/stream.hh b/broker/unified_sql/inc/com/centreon/broker/unified_sql/stream.hh index b73dbae9620..fb48927c7d4 100644 --- a/broker/unified_sql/inc/com/centreon/broker/unified_sql/stream.hh +++ b/broker/unified_sql/inc/com/centreon/broker/unified_sql/stream.hh @@ -293,8 +293,7 @@ class stream : public io::stream { database::mysql_stmt _severity_insert; database::mysql_stmt _severity_update; - database::mysql_stmt _tag_insert; - database::mysql_stmt _tag_update; + database::mysql_stmt _tag_insert_update; database::mysql_stmt _tag_delete; database::mysql_stmt _resources_tags_insert; database::mysql_stmt _resources_host_insert; @@ -359,9 +358,11 @@ class stream : public io::stream { void _process_responsive_instance(const std::shared_ptr& d); void _process_pb_host(const std::shared_ptr& d); + uint64_t _process_pb_host_in_resources(const Host& h, int32_t conn); void _process_pb_host_status(const std::shared_ptr& d); void _process_pb_adaptive_host(const std::shared_ptr& d); void _process_pb_service(const std::shared_ptr& d); + uint64_t _process_pb_service_in_resources(const Service& s, int32_t conn); void _process_pb_adaptive_service(const std::shared_ptr& d); void _process_pb_service_status(const std::shared_ptr& d); void _process_severity(const std::shared_ptr& d); diff --git a/broker/unified_sql/src/stream_sql.cc b/broker/unified_sql/src/stream_sql.cc index f4e06eb7c46..ece0254519c 100644 --- a/broker/unified_sql/src/stream_sql.cc +++ b/broker/unified_sql/src/stream_sql.cc @@ -36,6 +36,9 @@ using namespace com::centreon::broker::unified_sql; static bool time_is_undefined(uint64_t t) { return t == 0 || t == static_cast(-1); } +static const std::string _insert_or_update_tags = + "INSERT INTO tags (id,type,name) VALUES(?,?,?) ON DUPLICATE " + "KEY UPDATE tag_id=LAST_INSERT_ID(tag_id), name=VALUES(name)"; /** * @brief Clean tables with data associated to the instance. @@ -1610,254 +1613,7 @@ void stream::_process_pb_host(const std::shared_ptr& d) { _cache_host_instance.erase(h.host_id()); if (_store_in_resources) { - uint64_t res_id = 0; - auto found = _resource_cache.find({h.host_id(), 0}); - - if (h.enabled()) { - uint64_t sid = 0; - fmt::string_view name{misc::string::truncate( - h.name(), get_resources_col_size(resources_name))}; - fmt::string_view address{misc::string::truncate( - h.address(), get_resources_col_size(resources_address))}; - fmt::string_view alias{misc::string::truncate( - h.alias(), get_resources_col_size(resources_alias))}; - fmt::string_view parent_name{misc::string::truncate( - h.name(), get_resources_col_size(resources_parent_name))}; - fmt::string_view notes_url{misc::string::truncate( - h.notes_url(), get_resources_col_size(resources_notes_url))}; - fmt::string_view notes{misc::string::truncate( - h.notes(), get_resources_col_size(resources_notes))}; - fmt::string_view action_url{misc::string::truncate( - h.action_url(), get_resources_col_size(resources_action_url))}; - - // INSERT - if (found == _resource_cache.end()) { - _resources_host_insert.bind_value_as_u64(0, h.host_id()); - _resources_host_insert.bind_value_as_u32(1, h.state()); - _resources_host_insert.bind_value_as_u32( - 2, hst_ordered_status[h.state()]); - _resources_host_insert.bind_value_as_u64_ext( - 3u, h.last_state_change(), mapping::entry::invalid_on_zero); - _resources_host_insert.bind_value_as_bool( - 4, h.scheduled_downtime_depth() > 0); - _resources_host_insert.bind_value_as_bool( - 5, h.acknowledgement_type() != AckType::NONE); - _resources_host_insert.bind_value_as_bool( - 6, h.state_type() == Host_StateType_HARD); - _resources_host_insert.bind_value_as_u32(7, h.check_attempt()); - _resources_host_insert.bind_value_as_u32(8, h.max_check_attempts()); - _resources_host_insert.bind_value_as_u64( - 9, _cache_host_instance[h.host_id()]); - if (h.severity_id()) { - sid = _severity_cache[{h.severity_id(), 1}]; - SPDLOG_LOGGER_DEBUG(log_v2::sql(), - "host {} with severity_id {} => uid = {}", - h.host_id(), h.severity_id(), sid); - } else - SPDLOG_LOGGER_INFO(log_v2::sql(), - "no host severity found in cache for host {}", - h.host_id()); - if (sid) - _resources_host_insert.bind_value_as_u64(10, sid); - else - _resources_host_insert.bind_null_u64(10); - _resources_host_insert.bind_value_as_str(11, name); - _resources_host_insert.bind_value_as_str(12, address); - _resources_host_insert.bind_value_as_str(13, alias); - _resources_host_insert.bind_value_as_str(14, parent_name); - _resources_host_insert.bind_value_as_str(15, notes_url); - _resources_host_insert.bind_value_as_str(16, notes); - _resources_host_insert.bind_value_as_str(17, action_url); - _resources_host_insert.bind_value_as_bool(18, h.notify()); - _resources_host_insert.bind_value_as_bool(19, h.passive_checks()); - _resources_host_insert.bind_value_as_bool(20, h.active_checks()); - _resources_host_insert.bind_value_as_u64(21, h.icon_id()); - - std::promise p; - std::future future = p.get_future(); - _mysql.run_statement_and_get_int( - _resources_host_insert, std::move(p), - database::mysql_task::LAST_INSERT_ID, conn); - _add_action(conn, actions::resources); - try { - res_id = future.get(); - _resource_cache.insert({{h.host_id(), 0}, res_id}); - } catch (const std::exception& e) { - SPDLOG_LOGGER_CRITICAL( - log_v2::sql(), - "SQL: unable to insert new host resource {}: {}", h.host_id(), - e.what()); - - std::promise promise_resource; - std::future future_resource = - promise_resource.get_future(); - _mysql.run_query_and_get_result( - fmt::format("SELECT resource_id FROM resources WHERE " - "parent_id=0 AND id={}", - h.host_id()), - std::move(promise_resource)); - try { - mysql_result res{future_resource.get()}; - if (_mysql.fetch_row(res)) { - auto r = _resource_cache.insert( - {{h.host_id(), 0}, res.value_as_u64(0)}); - found = r.first; - SPDLOG_LOGGER_DEBUG( - log_v2::sql(), - "Host resource (host {}) found in database with id {}", - h.host_id(), found->second); - } else { - SPDLOG_LOGGER_CRITICAL( - log_v2::sql(), - "Could not insert host resource in database and no host " - "resource in database with id {}: {}", - h.host_id(), e.what()); - return; - } - } catch (const std::exception& e) { - SPDLOG_LOGGER_CRITICAL( - log_v2::sql(), - "No host resource in database with id {}: {}", h.host_id(), - e.what()); - return; - } - } - SPDLOG_LOGGER_DEBUG(log_v2::sql(), "insert resource {} for host{}", - res_id, h.host_id()); - } - if (res_id == 0) { - res_id = found->second; - SPDLOG_LOGGER_DEBUG(log_v2::sql(), "update resource {} for host{}", - res_id, h.host_id()); - // UPDATE - _resources_host_update.bind_value_as_u32(0, h.state()); - _resources_host_update.bind_value_as_u32( - 1, hst_ordered_status[h.state()]); - _resources_host_update.bind_value_as_u64_ext( - 2, h.last_state_change(), mapping::entry::invalid_on_zero); - _resources_host_update.bind_value_as_bool( - 3, h.scheduled_downtime_depth() > 0); - _resources_host_update.bind_value_as_bool( - 4, h.acknowledgement_type() != AckType::NONE); - _resources_host_update.bind_value_as_bool( - 5, h.state_type() == Host_StateType_HARD); - _resources_host_update.bind_value_as_u32(6, h.check_attempt()); - _resources_host_update.bind_value_as_u32(7, h.max_check_attempts()); - _resources_host_update.bind_value_as_u64( - 8, _cache_host_instance[h.host_id()]); - if (h.severity_id()) { - sid = _severity_cache[{h.severity_id(), 1}]; - SPDLOG_LOGGER_DEBUG(log_v2::sql(), - "host {} with severity_id {} => uid = {}", - h.host_id(), h.severity_id(), sid); - } else - SPDLOG_LOGGER_INFO(log_v2::sql(), - "no host severity found in cache for host {}", - h.host_id()); - if (sid) - _resources_host_update.bind_value_as_u64(9, sid); - else - _resources_host_update.bind_null_u64(9); - _resources_host_update.bind_value_as_str(10, name); - _resources_host_update.bind_value_as_str(11, address); - _resources_host_update.bind_value_as_str(12, alias); - _resources_host_update.bind_value_as_str(13, parent_name); - _resources_host_update.bind_value_as_str(14, notes_url); - _resources_host_update.bind_value_as_str(15, notes); - _resources_host_update.bind_value_as_str(16, action_url); - _resources_host_update.bind_value_as_bool(17, h.notify()); - _resources_host_update.bind_value_as_bool(18, h.passive_checks()); - _resources_host_update.bind_value_as_bool(19, h.active_checks()); - _resources_host_update.bind_value_as_u64(20, h.icon_id()); - _resources_host_update.bind_value_as_u64(21, res_id); - - _mysql.run_statement(_resources_host_update, - database::mysql_error::store_host_resources, - conn); - _add_action(conn, actions::resources); - } - - if (!_resources_tags_insert.prepared()) { - _resources_tags_insert = _mysql.prepare_query( - "INSERT INTO resources_tags (tag_id,resource_id) " - "VALUES(?,?)"); - } - if (!_resources_tags_remove.prepared()) - _resources_tags_remove = _mysql.prepare_query( - "DELETE FROM resources_tags WHERE resource_id=?"); - _finish_action(-1, actions::tags); - _resources_tags_remove.bind_value_as_u64(0, res_id); - _mysql.run_statement(_resources_tags_remove, - database::mysql_error::delete_resources_tags, - conn); - for (auto& tag : h.tags()) { - SPDLOG_LOGGER_DEBUG(log_v2::sql(), - "add tag ({}, {}) for resource {} for host{}", - tag.id(), tag.type(), res_id, h.host_id()); - - auto it_tags_cache = _tags_cache.find({tag.id(), tag.type()}); - - if (it_tags_cache == _tags_cache.end()) { - SPDLOG_LOGGER_ERROR( - log_v2::sql(), - "SQL: could not find in cache the tag ({}, {}) for host " - "'{}': " - "trying to add it.", - tag.id(), tag.type(), h.host_id()); - if (!_tag_insert.prepared()) - _tag_insert = _mysql.prepare_query( - "INSERT INTO tags (id,type,name) VALUES(?,?,?)"); - _tag_insert.bind_value_as_u64(0, tag.id()); - _tag_insert.bind_value_as_u32(1, tag.type()); - _tag_insert.bind_value_as_str(2, "(unknown)"); - std::promise p; - std::future future = p.get_future(); - - _mysql.run_statement_and_get_int( - _tag_insert, std::move(p), - database::mysql_task::LAST_INSERT_ID, conn); - try { - uint64_t tag_id = future.get(); - it_tags_cache = - _tags_cache.insert({{tag.id(), tag.type()}, tag_id}).first; - } catch (const std::exception& e) { - SPDLOG_LOGGER_ERROR(log_v2::sql(), - "SQL: unable to insert new tag ({},{}): {}", - tag.id(), tag.type(), e.what()); - } - } - - if (it_tags_cache != _tags_cache.end()) { - _resources_tags_insert.bind_value_as_u64(0, - it_tags_cache->second); - _resources_tags_insert.bind_value_as_u64(1, res_id); - SPDLOG_LOGGER_DEBUG( - log_v2::sql(), - "SQL: new relation between host (resource_id: {}, host_id: " - "{}) " - "and tag ({},{})", - res_id, h.host_id(), tag.id(), tag.type()); - _mysql.run_statement( - _resources_tags_insert, - database::mysql_error::store_tags_resources_tags, conn); - _add_action(conn, actions::resources_tags); - } - } - } else { - if (found != _resource_cache.end()) { - _resources_disable.bind_value_as_u64(0, found->second); - - _mysql.run_statement(_resources_disable, - database::mysql_error::clean_resources, conn); - _resource_cache.erase(found); - _add_action(conn, actions::resources); - } else { - SPDLOG_LOGGER_INFO( - log_v2::sql(), - "SQL: no need to remove host {}, it is not in database", - h.host_id()); - } - } + _process_pb_host_in_resources(h, conn); } } else SPDLOG_LOGGER_TRACE( @@ -1868,6 +1624,251 @@ void stream::_process_pb_host(const std::shared_ptr& d) { } } +uint64_t stream::_process_pb_host_in_resources(const Host& h, int32_t conn) { + auto found = _resource_cache.find({h.host_id(), 0}); + + uint64_t res_id = 0; + if (h.enabled()) { + uint64_t sid = 0; + fmt::string_view name{misc::string::truncate( + h.name(), get_resources_col_size(resources_name))}; + fmt::string_view address{misc::string::truncate( + h.address(), get_resources_col_size(resources_address))}; + fmt::string_view alias{misc::string::truncate( + h.alias(), get_resources_col_size(resources_alias))}; + fmt::string_view parent_name{misc::string::truncate( + h.name(), get_resources_col_size(resources_parent_name))}; + fmt::string_view notes_url{misc::string::truncate( + h.notes_url(), get_resources_col_size(resources_notes_url))}; + fmt::string_view notes{misc::string::truncate( + h.notes(), get_resources_col_size(resources_notes))}; + fmt::string_view action_url{misc::string::truncate( + h.action_url(), get_resources_col_size(resources_action_url))}; + + // INSERT + if (found == _resource_cache.end()) { + _resources_host_insert.bind_value_as_u64(0, h.host_id()); + _resources_host_insert.bind_value_as_u32(1, h.state()); + _resources_host_insert.bind_value_as_u32(2, + hst_ordered_status[h.state()]); + _resources_host_insert.bind_value_as_u64_ext( + 3u, h.last_state_change(), mapping::entry::invalid_on_zero); + _resources_host_insert.bind_value_as_bool( + 4, h.scheduled_downtime_depth() > 0); + _resources_host_insert.bind_value_as_bool( + 5, h.acknowledgement_type() != AckType::NONE); + _resources_host_insert.bind_value_as_bool( + 6, h.state_type() == Host_StateType_HARD); + _resources_host_insert.bind_value_as_u32(7, h.check_attempt()); + _resources_host_insert.bind_value_as_u32(8, h.max_check_attempts()); + _resources_host_insert.bind_value_as_u64( + 9, _cache_host_instance[h.host_id()]); + if (h.severity_id()) { + sid = _severity_cache[{h.severity_id(), 1}]; + SPDLOG_LOGGER_DEBUG(log_v2::sql(), + "host {} with severity_id {} => uid = {}", + h.host_id(), h.severity_id(), sid); + } else + SPDLOG_LOGGER_INFO(log_v2::sql(), + "no host severity found in cache for host {}", + h.host_id()); + if (sid) + _resources_host_insert.bind_value_as_u64(10, sid); + else + _resources_host_insert.bind_null_u64(10); + _resources_host_insert.bind_value_as_str(11, name); + _resources_host_insert.bind_value_as_str(12, address); + _resources_host_insert.bind_value_as_str(13, alias); + _resources_host_insert.bind_value_as_str(14, parent_name); + _resources_host_insert.bind_value_as_str(15, notes_url); + _resources_host_insert.bind_value_as_str(16, notes); + _resources_host_insert.bind_value_as_str(17, action_url); + _resources_host_insert.bind_value_as_bool(18, h.notify()); + _resources_host_insert.bind_value_as_bool(19, h.passive_checks()); + _resources_host_insert.bind_value_as_bool(20, h.active_checks()); + _resources_host_insert.bind_value_as_u64(21, h.icon_id()); + + std::promise p; + std::future future = p.get_future(); + _mysql.run_statement_and_get_int( + _resources_host_insert, std::move(p), + database::mysql_task::LAST_INSERT_ID, conn); + _add_action(conn, actions::resources); + try { + res_id = future.get(); + _resource_cache.insert({{h.host_id(), 0}, res_id}); + } catch (const std::exception& e) { + SPDLOG_LOGGER_CRITICAL(log_v2::sql(), + "SQL: unable to insert new host resource {}: {}", + h.host_id(), e.what()); + + std::promise promise_resource; + std::future future_resource = + promise_resource.get_future(); + _mysql.run_query_and_get_result( + fmt::format("SELECT resource_id FROM resources WHERE " + "parent_id=0 AND id={}", + h.host_id()), + std::move(promise_resource)); + try { + mysql_result res{future_resource.get()}; + if (_mysql.fetch_row(res)) { + auto r = + _resource_cache.insert({{h.host_id(), 0}, res.value_as_u64(0)}); + found = r.first; + SPDLOG_LOGGER_DEBUG( + log_v2::sql(), + "Host resource (host {}) found in database with id {}", + h.host_id(), found->second); + } else { + SPDLOG_LOGGER_CRITICAL( + log_v2::sql(), + "Could not insert host resource in database and no host " + "resource in database with id {}: {}", + h.host_id(), e.what()); + return 0; + } + } catch (const std::exception& e) { + SPDLOG_LOGGER_CRITICAL(log_v2::sql(), + "No host resource in database with id {}: {}", + h.host_id(), e.what()); + return 0; + } + } + SPDLOG_LOGGER_DEBUG(log_v2::sql(), "insert resource {} for host{}", + res_id, h.host_id()); + } + if (res_id == 0) { + res_id = found->second; + SPDLOG_LOGGER_DEBUG(log_v2::sql(), "update resource {} for host{}", + res_id, h.host_id()); + // UPDATE + _resources_host_update.bind_value_as_u32(0, h.state()); + _resources_host_update.bind_value_as_u32(1, + hst_ordered_status[h.state()]); + _resources_host_update.bind_value_as_u64_ext( + 2, h.last_state_change(), mapping::entry::invalid_on_zero); + _resources_host_update.bind_value_as_bool( + 3, h.scheduled_downtime_depth() > 0); + _resources_host_update.bind_value_as_bool( + 4, h.acknowledgement_type() != AckType::NONE); + _resources_host_update.bind_value_as_bool( + 5, h.state_type() == Host_StateType_HARD); + _resources_host_update.bind_value_as_u32(6, h.check_attempt()); + _resources_host_update.bind_value_as_u32(7, h.max_check_attempts()); + _resources_host_update.bind_value_as_u64( + 8, _cache_host_instance[h.host_id()]); + if (h.severity_id()) { + sid = _severity_cache[{h.severity_id(), 1}]; + SPDLOG_LOGGER_DEBUG(log_v2::sql(), + "host {} with severity_id {} => uid = {}", + h.host_id(), h.severity_id(), sid); + } else + SPDLOG_LOGGER_INFO(log_v2::sql(), + "no host severity found in cache for host {}", + h.host_id()); + if (sid) + _resources_host_update.bind_value_as_u64(9, sid); + else + _resources_host_update.bind_null_u64(9); + _resources_host_update.bind_value_as_str(10, name); + _resources_host_update.bind_value_as_str(11, address); + _resources_host_update.bind_value_as_str(12, alias); + _resources_host_update.bind_value_as_str(13, parent_name); + _resources_host_update.bind_value_as_str(14, notes_url); + _resources_host_update.bind_value_as_str(15, notes); + _resources_host_update.bind_value_as_str(16, action_url); + _resources_host_update.bind_value_as_bool(17, h.notify()); + _resources_host_update.bind_value_as_bool(18, h.passive_checks()); + _resources_host_update.bind_value_as_bool(19, h.active_checks()); + _resources_host_update.bind_value_as_u64(20, h.icon_id()); + _resources_host_update.bind_value_as_u64(21, res_id); + + _mysql.run_statement(_resources_host_update, + database::mysql_error::store_host_resources, conn); + _add_action(conn, actions::resources); + } + + if (!_resources_tags_insert.prepared()) { + _resources_tags_insert = _mysql.prepare_query( + "INSERT INTO resources_tags (tag_id,resource_id) " + "VALUES(?,?)"); + } + if (!_resources_tags_remove.prepared()) + _resources_tags_remove = _mysql.prepare_query( + "DELETE FROM resources_tags WHERE resource_id=?"); + _finish_action(-1, actions::tags); + _resources_tags_remove.bind_value_as_u64(0, res_id); + _mysql.run_statement(_resources_tags_remove, + database::mysql_error::delete_resources_tags, conn); + for (auto& tag : h.tags()) { + SPDLOG_LOGGER_DEBUG(log_v2::sql(), + "add tag ({}, {}) for resource {} for host{}", + tag.id(), tag.type(), res_id, h.host_id()); + + auto it_tags_cache = _tags_cache.find({tag.id(), tag.type()}); + + if (it_tags_cache == _tags_cache.end()) { + SPDLOG_LOGGER_ERROR( + log_v2::sql(), + "SQL: could not find in cache the tag ({}, {}) for host " + "'{}': " + "trying to add it.", + tag.id(), tag.type(), h.host_id()); + if (!_tag_insert_update.prepared()) + _tag_insert_update = _mysql.prepare_query(_insert_or_update_tags); + _tag_insert_update.bind_value_as_u64(0, tag.id()); + _tag_insert_update.bind_value_as_u32(1, tag.type()); + _tag_insert_update.bind_value_as_str(2, "(unknown)"); + std::promise p; + std::future future = p.get_future(); + + _mysql.run_statement_and_get_int( + _tag_insert_update, std::move(p), + database::mysql_task::LAST_INSERT_ID, conn); + try { + uint64_t tag_id = future.get(); + it_tags_cache = + _tags_cache.insert({{tag.id(), tag.type()}, tag_id}).first; + } catch (const std::exception& e) { + SPDLOG_LOGGER_ERROR(log_v2::sql(), + "SQL: unable to insert new tag ({},{}): {}", + tag.id(), tag.type(), e.what()); + } + } + + if (it_tags_cache != _tags_cache.end()) { + _resources_tags_insert.bind_value_as_u64(0, it_tags_cache->second); + _resources_tags_insert.bind_value_as_u64(1, res_id); + SPDLOG_LOGGER_DEBUG( + log_v2::sql(), + "SQL: new relation between host (resource_id: {}, host_id: " + "{}) " + "and tag ({},{},{})", + res_id, h.host_id(), it_tags_cache->second, tag.id(), tag.type()); + _mysql.run_statement(_resources_tags_insert, + database::mysql_error::store_tags_resources_tags, + conn); + _add_action(conn, actions::resources_tags); + } + } + } else { + if (found != _resource_cache.end()) { + _resources_disable.bind_value_as_u64(0, found->second); + + _mysql.run_statement(_resources_disable, + database::mysql_error::clean_resources, conn); + _resource_cache.erase(found); + _add_action(conn, actions::resources); + } else { + SPDLOG_LOGGER_INFO( + log_v2::sql(), + "SQL: no need to remove host {}, it is not in database", h.host_id()); + } + } + return res_id; +} + /** * Process an adaptive host event. * @@ -3061,257 +3062,257 @@ void stream::_process_pb_service(const std::shared_ptr& d) { _check_and_update_index_cache(s); if (_store_in_resources) { - uint64_t res_id = 0; - auto found = _resource_cache.find({s.service_id(), s.host_id()}); - - if (s.enabled()) { - uint64_t sid = 0; - fmt::string_view name{misc::string::truncate( - s.display_name(), get_resources_col_size(resources_name))}; - fmt::string_view parent_name{misc::string::truncate( - s.host_name(), get_resources_col_size(resources_parent_name))}; - fmt::string_view notes_url{misc::string::truncate( - s.notes_url(), get_resources_col_size(resources_notes_url))}; - fmt::string_view notes{misc::string::truncate( - s.notes(), get_resources_col_size(resources_notes))}; - fmt::string_view action_url{misc::string::truncate( - s.action_url(), get_resources_col_size(resources_action_url))}; - - // INSERT - if (found == _resource_cache.end()) { - _resources_service_insert.bind_value_as_u64(0, s.service_id()); - _resources_service_insert.bind_value_as_u64(1, s.host_id()); - _resources_service_insert.bind_value_as_u32(2, s.type()); - if (s.internal_id()) - _resources_service_insert.bind_value_as_u64(3, s.internal_id()); - else - _resources_service_insert.bind_null_u64(3); - _resources_service_insert.bind_value_as_u32(4, s.state()); - _resources_service_insert.bind_value_as_u32( - 5, svc_ordered_status[s.state()]); - _resources_service_insert.bind_value_as_u64_ext( - 6, s.last_state_change(), mapping::entry::invalid_on_zero); - _resources_service_insert.bind_value_as_bool( - 7, s.scheduled_downtime_depth() > 0); - _resources_service_insert.bind_value_as_bool( - 8, s.acknowledgement_type() != AckType::NONE); - _resources_service_insert.bind_value_as_bool( - 9, s.state_type() == Service_StateType_HARD); - _resources_service_insert.bind_value_as_u32(10, s.check_attempt()); - _resources_service_insert.bind_value_as_u32(11, - s.max_check_attempts()); - _resources_service_insert.bind_value_as_u64( - 12, _cache_host_instance[s.host_id()]); - if (s.severity_id() > 0) { - sid = _severity_cache[{s.severity_id(), 0}]; + _process_pb_service_in_resources(s, conn); + } + } else + SPDLOG_LOGGER_TRACE( + log_v2::sql(), + "SQL: service '{}' has no host ID, service ID nor hostname, probably " + "bam fake service", + s.description()); +} + +uint64_t stream::_process_pb_service_in_resources(const Service& s, + int32_t conn) { + uint64_t res_id = 0; + + auto found = _resource_cache.find({s.service_id(), s.host_id()}); + + if (s.enabled()) { + uint64_t sid = 0; + fmt::string_view name{misc::string::truncate( + s.display_name(), get_resources_col_size(resources_name))}; + fmt::string_view parent_name{misc::string::truncate( + s.host_name(), get_resources_col_size(resources_parent_name))}; + fmt::string_view notes_url{misc::string::truncate( + s.notes_url(), get_resources_col_size(resources_notes_url))}; + fmt::string_view notes{misc::string::truncate( + s.notes(), get_resources_col_size(resources_notes))}; + fmt::string_view action_url{misc::string::truncate( + s.action_url(), get_resources_col_size(resources_action_url))}; + + // INSERT + if (found == _resource_cache.end()) { + _resources_service_insert.bind_value_as_u64(0, s.service_id()); + _resources_service_insert.bind_value_as_u64(1, s.host_id()); + _resources_service_insert.bind_value_as_u32(2, s.type()); + if (s.internal_id()) + _resources_service_insert.bind_value_as_u64(3, s.internal_id()); + else + _resources_service_insert.bind_null_u64(3); + _resources_service_insert.bind_value_as_u32(4, s.state()); + _resources_service_insert.bind_value_as_u32( + 5, svc_ordered_status[s.state()]); + _resources_service_insert.bind_value_as_u64_ext( + 6, s.last_state_change(), mapping::entry::invalid_on_zero); + _resources_service_insert.bind_value_as_bool( + 7, s.scheduled_downtime_depth() > 0); + _resources_service_insert.bind_value_as_bool( + 8, s.acknowledgement_type() != AckType::NONE); + _resources_service_insert.bind_value_as_bool( + 9, s.state_type() == Service_StateType_HARD); + _resources_service_insert.bind_value_as_u32(10, s.check_attempt()); + _resources_service_insert.bind_value_as_u32(11, s.max_check_attempts()); + _resources_service_insert.bind_value_as_u64( + 12, _cache_host_instance[s.host_id()]); + if (s.severity_id() > 0) { + sid = _severity_cache[{s.severity_id(), 0}]; + SPDLOG_LOGGER_DEBUG(log_v2::sql(), + "service ({}, {}) with severity_id {} => uid = {}", + s.host_id(), s.service_id(), s.severity_id(), sid); + } + if (sid) + _resources_service_insert.bind_value_as_u64(13, sid); + else + _resources_service_insert.bind_null_u64(13); + _resources_service_insert.bind_value_as_str(14, name); + _resources_service_insert.bind_value_as_str(15, parent_name); + _resources_service_insert.bind_value_as_str(16, notes_url); + _resources_service_insert.bind_value_as_str(17, notes); + _resources_service_insert.bind_value_as_str(18, action_url); + _resources_service_insert.bind_value_as_bool(19, s.notify()); + _resources_service_insert.bind_value_as_bool(20, s.passive_checks()); + _resources_service_insert.bind_value_as_bool(21, s.active_checks()); + _resources_service_insert.bind_value_as_u64(22, s.icon_id()); + + std::promise p; + std::future future = p.get_future(); + _mysql.run_statement_and_get_int( + _resources_service_insert, std::move(p), + database::mysql_task::LAST_INSERT_ID, conn); + _add_action(conn, actions::resources); + try { + res_id = future.get(); + _resource_cache.insert({{s.service_id(), s.host_id()}, res_id}); + } catch (const std::exception& e) { + SPDLOG_LOGGER_CRITICAL( + log_v2::sql(), + "SQL: unable to insert new service resource ({}, {}): {}", + s.host_id(), s.service_id(), e.what()); + + std::promise promise_resource; + std::future future_resource = + promise_resource.get_future(); + _mysql.run_query_and_get_result( + fmt::format("SELECT resource_id FROM resources WHERE " + "parent_id={} AND id={}", + s.host_id(), s.service_id()), + std::move(promise_resource)); + try { + mysql_result res{future_resource.get()}; + if (_mysql.fetch_row(res)) { + auto r = _resource_cache.insert( + {{s.service_id(), s.host_id()}, res.value_as_u64(0)}); + found = r.first; SPDLOG_LOGGER_DEBUG( log_v2::sql(), - "service ({}, {}) with severity_id {} => uid = {}", s.host_id(), - s.service_id(), s.severity_id(), sid); - } - if (sid) - _resources_service_insert.bind_value_as_u64(13, sid); - else - _resources_service_insert.bind_null_u64(13); - _resources_service_insert.bind_value_as_str(14, name); - _resources_service_insert.bind_value_as_str(15, parent_name); - _resources_service_insert.bind_value_as_str(16, notes_url); - _resources_service_insert.bind_value_as_str(17, notes); - _resources_service_insert.bind_value_as_str(18, action_url); - _resources_service_insert.bind_value_as_bool(19, s.notify()); - _resources_service_insert.bind_value_as_bool(20, s.passive_checks()); - _resources_service_insert.bind_value_as_bool(21, s.active_checks()); - _resources_service_insert.bind_value_as_u64(22, s.icon_id()); - - std::promise p; - std::future future = p.get_future(); - _mysql.run_statement_and_get_int( - _resources_service_insert, std::move(p), - database::mysql_task::LAST_INSERT_ID, conn); - _add_action(conn, actions::resources); - try { - res_id = future.get(); - _resource_cache.insert({{s.service_id(), s.host_id()}, res_id}); - } catch (const std::exception& e) { + "Service resource ({}, {}) found in database with id {}", + s.host_id(), s.service_id(), found->second); + } else { SPDLOG_LOGGER_CRITICAL( log_v2::sql(), - "SQL: unable to insert new service resource ({}, {}): {}", + "Could not insert service resource in database and no " + "service resource in database with id ({},{}): {}", s.host_id(), s.service_id(), e.what()); - - std::promise promise_resource; - std::future future_resource = - promise_resource.get_future(); - _mysql.run_query_and_get_result( - fmt::format("SELECT resource_id FROM resources WHERE " - "parent_id={} AND id={}", - s.host_id(), s.service_id()), - std::move(promise_resource)); - try { - mysql_result res{future_resource.get()}; - if (_mysql.fetch_row(res)) { - auto r = _resource_cache.insert( - {{s.service_id(), s.host_id()}, res.value_as_u64(0)}); - found = r.first; - SPDLOG_LOGGER_DEBUG( - log_v2::sql(), - "Service resource ({}, {}) found in database with id {}", - s.host_id(), s.service_id(), found->second); - } else { - SPDLOG_LOGGER_CRITICAL( - log_v2::sql(), - "Could not insert service resource in database and no " - "service resource in database with id ({},{}): {}", - s.host_id(), s.service_id(), e.what()); - return; - } - } catch (const std::exception& e) { - SPDLOG_LOGGER_CRITICAL( - log_v2::sql(), - "No service resource in database with id ({}, {}): {}", - s.host_id(), s.service_id(), e.what()); - return; - } + return 0; } + } catch (const std::exception& e) { + SPDLOG_LOGGER_CRITICAL( + log_v2::sql(), + "No service resource in database with id ({}, {}): {}", + s.host_id(), s.service_id(), e.what()); + return 0; } - if (res_id == 0) { - res_id = found->second; - // UPDATE - _resources_service_update.bind_value_as_u32(0, s.type()); - if (s.internal_id()) - _resources_service_update.bind_value_as_u64(1, s.internal_id()); - else - _resources_service_update.bind_null_u64(1); - _resources_service_update.bind_value_as_u32(2, s.state()); - _resources_service_update.bind_value_as_u32( - 3, svc_ordered_status[s.state()]); - _resources_service_update.bind_value_as_u64_ext( - 4, s.last_state_change(), mapping::entry::invalid_on_zero); - _resources_service_update.bind_value_as_bool( - 5, s.scheduled_downtime_depth() > 0); - _resources_service_update.bind_value_as_bool( - 6, s.acknowledgement_type() != AckType::NONE); - _resources_service_update.bind_value_as_bool( - 7, s.state_type() == Service_StateType_HARD); - _resources_service_update.bind_value_as_u32(8, s.check_attempt()); - _resources_service_update.bind_value_as_u32(9, - s.max_check_attempts()); - _resources_service_update.bind_value_as_u64( - 10, _cache_host_instance[s.host_id()]); - if (s.severity_id() > 0) { - sid = _severity_cache[{s.severity_id(), 0}]; - SPDLOG_LOGGER_DEBUG( - log_v2::sql(), - "service ({}, {}) with severity_id {} => uid = {}", s.host_id(), - s.service_id(), s.severity_id(), sid); - } - if (sid) - _resources_service_update.bind_value_as_u64(11, sid); - else - _resources_service_update.bind_null_u64(11); - _resources_service_update.bind_value_as_str(12, name); - _resources_service_update.bind_value_as_str(13, parent_name); - _resources_service_update.bind_value_as_str(14, notes_url); - _resources_service_update.bind_value_as_str(15, notes); - _resources_service_update.bind_value_as_str(16, action_url); - _resources_service_update.bind_value_as_bool(17, s.notify()); - _resources_service_update.bind_value_as_bool(18, s.passive_checks()); - _resources_service_update.bind_value_as_bool(19, s.active_checks()); - _resources_service_update.bind_value_as_u64(20, s.icon_id()); - _resources_service_update.bind_value_as_u64(21, res_id); - - _mysql.run_statement(_resources_service_update, - database::mysql_error::store_service, conn); - _add_action(conn, actions::resources); - } + } + } + if (res_id == 0) { + res_id = found->second; + // UPDATE + _resources_service_update.bind_value_as_u32(0, s.type()); + if (s.internal_id()) + _resources_service_update.bind_value_as_u64(1, s.internal_id()); + else + _resources_service_update.bind_null_u64(1); + _resources_service_update.bind_value_as_u32(2, s.state()); + _resources_service_update.bind_value_as_u32( + 3, svc_ordered_status[s.state()]); + _resources_service_update.bind_value_as_u64_ext( + 4, s.last_state_change(), mapping::entry::invalid_on_zero); + _resources_service_update.bind_value_as_bool( + 5, s.scheduled_downtime_depth() > 0); + _resources_service_update.bind_value_as_bool( + 6, s.acknowledgement_type() != AckType::NONE); + _resources_service_update.bind_value_as_bool( + 7, s.state_type() == Service_StateType_HARD); + _resources_service_update.bind_value_as_u32(8, s.check_attempt()); + _resources_service_update.bind_value_as_u32(9, s.max_check_attempts()); + _resources_service_update.bind_value_as_u64( + 10, _cache_host_instance[s.host_id()]); + if (s.severity_id() > 0) { + sid = _severity_cache[{s.severity_id(), 0}]; + SPDLOG_LOGGER_DEBUG(log_v2::sql(), + "service ({}, {}) with severity_id {} => uid = {}", + s.host_id(), s.service_id(), s.severity_id(), sid); + } + if (sid) + _resources_service_update.bind_value_as_u64(11, sid); + else + _resources_service_update.bind_null_u64(11); + _resources_service_update.bind_value_as_str(12, name); + _resources_service_update.bind_value_as_str(13, parent_name); + _resources_service_update.bind_value_as_str(14, notes_url); + _resources_service_update.bind_value_as_str(15, notes); + _resources_service_update.bind_value_as_str(16, action_url); + _resources_service_update.bind_value_as_bool(17, s.notify()); + _resources_service_update.bind_value_as_bool(18, s.passive_checks()); + _resources_service_update.bind_value_as_bool(19, s.active_checks()); + _resources_service_update.bind_value_as_u64(20, s.icon_id()); + _resources_service_update.bind_value_as_u64(21, res_id); + + _mysql.run_statement(_resources_service_update, + database::mysql_error::store_service, conn); + _add_action(conn, actions::resources); + } - if (!_resources_tags_insert.prepared()) { - _resources_tags_insert = _mysql.prepare_query( - "INSERT INTO resources_tags (tag_id,resource_id) " - "VALUES(?,?)"); + if (!_resources_tags_insert.prepared()) { + _resources_tags_insert = _mysql.prepare_query( + "INSERT INTO resources_tags (tag_id,resource_id) " + "VALUES(?,?)"); + } + if (!_resources_tags_remove.prepared()) + _resources_tags_remove = _mysql.prepare_query( + "DELETE FROM resources_tags WHERE resource_id=?"); + _finish_action(-1, actions::tags); + _resources_tags_remove.bind_value_as_u64(0, res_id); + _mysql.run_statement(_resources_tags_remove, + database::mysql_error::delete_resources_tags, conn); + for (auto& tag : s.tags()) { + auto it_tags_cache = _tags_cache.find({tag.id(), tag.type()}); + + if (it_tags_cache == _tags_cache.end()) { + SPDLOG_LOGGER_ERROR( + log_v2::sql(), + "SQL: could not find in cache the tag ({}, {}) for service " + "({},{}): trying to add it.", + tag.id(), tag.type(), s.host_id(), s.service_id()); + if (!_tag_insert_update.prepared()) + _tag_insert_update = _mysql.prepare_query(_insert_or_update_tags); + _tag_insert_update.bind_value_as_u64(0, tag.id()); + _tag_insert_update.bind_value_as_u32(1, tag.type()); + _tag_insert_update.bind_value_as_str(2, "(unknown)"); + std::promise p; + std::future future = p.get_future(); + _mysql.run_statement_and_get_int( + _tag_insert_update, std::move(p), + database::mysql_task::LAST_INSERT_ID, conn); + try { + uint64_t tag_id = future.get(); + it_tags_cache = + _tags_cache.insert({{tag.id(), tag.type()}, tag_id}).first; + } catch (const std::exception& e) { + SPDLOG_LOGGER_ERROR(log_v2::sql(), + "SQL: unable to insert new tag ({},{}): {}", + tag.id(), tag.type(), e.what()); } - if (!_resources_tags_remove.prepared()) - _resources_tags_remove = _mysql.prepare_query( - "DELETE FROM resources_tags WHERE resource_id=?"); - _finish_action(-1, actions::tags); - _resources_tags_remove.bind_value_as_u64(0, res_id); - _mysql.run_statement(_resources_tags_remove, - database::mysql_error::delete_resources_tags, - conn); - for (auto& tag : s.tags()) { - auto it_tags_cache = _tags_cache.find({tag.id(), tag.type()}); - - if (it_tags_cache == _tags_cache.end()) { - SPDLOG_LOGGER_ERROR( - log_v2::sql(), - "SQL: could not find in cache the tag ({}, {}) for service " - "({},{}): trying to add it.", - tag.id(), tag.type(), s.host_id(), s.service_id()); - if (!_tag_insert.prepared()) - _tag_insert = _mysql.prepare_query( - "INSERT INTO tags (id,type,name) VALUES(?,?,?)"); - _tag_insert.bind_value_as_u64(0, tag.id()); - _tag_insert.bind_value_as_u32(1, tag.type()); - _tag_insert.bind_value_as_str(2, "(unknown)"); - std::promise p; - std::future future = p.get_future(); - _mysql.run_statement_and_get_int( - _tag_insert, std::move(p), database::mysql_task::LAST_INSERT_ID, - conn); - try { - uint64_t tag_id = future.get(); - it_tags_cache = - _tags_cache.insert({{tag.id(), tag.type()}, tag_id}).first; - } catch (const std::exception& e) { - SPDLOG_LOGGER_ERROR(log_v2::sql(), - "SQL: unable to insert new tag ({},{}): {}", - tag.id(), tag.type(), e.what()); - } - } + } - if (it_tags_cache != _tags_cache.end()) { - _resources_tags_insert.bind_value_as_u64(0, it_tags_cache->second); - _resources_tags_insert.bind_value_as_u64(1, res_id); - SPDLOG_LOGGER_DEBUG( - log_v2::sql(), - "SQL: new relation between service (resource_id: {}, ({}, " - "{})) and tag ({},{})", - res_id, s.host_id(), s.service_id(), tag.id(), tag.type()); - _mysql.run_statement( - _resources_tags_insert, - database::mysql_error::store_tags_resources_tags, conn); - _add_action(conn, actions::resources_tags); - } else { - SPDLOG_LOGGER_ERROR( - log_v2::sql(), - "SQL: could not find the tag ({}, {}) in cache for host '{}'", - tag.id(), tag.type(), s.service_id()); - } - } + if (it_tags_cache != _tags_cache.end()) { + _resources_tags_insert.bind_value_as_u64(0, it_tags_cache->second); + _resources_tags_insert.bind_value_as_u64(1, res_id); + SPDLOG_LOGGER_DEBUG( + log_v2::sql(), + "SQL: new relation between service (resource_id: {}, ({}, " + "{})) and tag ({},{})", + res_id, s.host_id(), s.service_id(), tag.id(), tag.type()); + _mysql.run_statement(_resources_tags_insert, + database::mysql_error::store_tags_resources_tags, + conn); + _add_action(conn, actions::resources_tags); } else { - if (found != _resource_cache.end()) { - _resources_disable.bind_value_as_u64(0, found->second); - - _mysql.run_statement(_resources_disable, - database::mysql_error::clean_resources, conn); - _resource_cache.erase(found); - _add_action(conn, actions::resources); - } else { - SPDLOG_LOGGER_INFO( - log_v2::sql(), - "SQL: no need to remove service ({}, {}), it is not in " - "database", - s.host_id(), s.service_id()); - } + SPDLOG_LOGGER_ERROR( + log_v2::sql(), + "SQL: could not find the tag ({}, {}) in cache for host '{}'", + tag.id(), tag.type(), s.service_id()); } } - } else - SPDLOG_LOGGER_TRACE( - log_v2::sql(), - "SQL: service '{}' has no host ID, service ID nor hostname, probably " - "bam fake service", - s.description()); + } else { + if (found != _resource_cache.end()) { + _resources_disable.bind_value_as_u64(0, found->second); + + _mysql.run_statement(_resources_disable, + database::mysql_error::clean_resources, conn); + _resource_cache.erase(found); + _add_action(conn, actions::resources); + } else { + SPDLOG_LOGGER_INFO( + log_v2::sql(), + "SQL: no need to remove service ({}, {}), it is not in " + "database", + s.host_id(), s.service_id()); + } + } + return res_id; } - /** * Process an adaptive service event. * @@ -3952,14 +3953,8 @@ void stream::_process_tag(const std::shared_ptr& d) { _finish_action(-1, actions::tags); // Prepare queries. - if (!_tag_update.prepared()) - _tag_update = _mysql.prepare_query( - "UPDATE tags SET id=?,type=?,name=? WHERE " - "tag_id=?"); - if (!_tag_insert.prepared()) - _tag_insert = _mysql.prepare_query( - "INSERT INTO tags (id,type,name) " - "VALUES(?,?,?)"); + if (!_tag_insert_update.prepared()) + _tag_insert_update = _mysql.prepare_query(_insert_or_update_tags); if (!_tag_delete.prepared()) _tag_delete = _mysql.prepare_query("DELETE FROM resources_tags WHERE tag_id=?"); @@ -3967,57 +3962,36 @@ void stream::_process_tag(const std::shared_ptr& d) { // Processed object. auto s{static_cast(d.get())}; auto& tg = s->obj(); - uint64_t tag_id = _tags_cache[{tg.id(), tg.type()}]; int32_t conn = special_conn::tag % _mysql.connections_count(); switch (tg.action()) { case Tag_Action_ADD: - if (tag_id) { - SPDLOG_LOGGER_TRACE(log_v2::sql(), "SQL: add already existing tag {}", - tg.id()); - _tag_update.bind_value_as_u64(0, tg.id()); - _tag_update.bind_value_as_u32(1, tg.type()); - _tag_update.bind_value_as_str(2, tg.name()); - _tag_update.bind_value_as_u64(3, tag_id); - _mysql.run_statement(_tag_update, database::mysql_error::store_tag, - conn); - } else { - SPDLOG_LOGGER_TRACE(log_v2::sql(), "SQL: add tag {}", tg.id()); - _tag_insert.bind_value_as_u64(0, tg.id()); - _tag_insert.bind_value_as_u32(1, tg.type()); - _tag_insert.bind_value_as_str(2, tg.name()); - std::promise p; - std::future future = p.get_future(); - _mysql.run_statement_and_get_int( - _tag_insert, std::move(p), database::mysql_task::LAST_INSERT_ID, - conn); - try { - tag_id = future.get(); - _tags_cache[{tg.id(), tg.type()}] = tag_id; - } catch (const std::exception& e) { - SPDLOG_LOGGER_ERROR( - log_v2::sql(), - "unified sql: unable to insert new tag ({},{}): {}", tg.id(), - tg.type(), e.what()); - } + case Tag_Action_MODIFY: { + const char* debug_action = + tg.action() == Tag_Action_ADD ? "insert" : "update"; + SPDLOG_LOGGER_TRACE(log_v2::sql(), "SQL: {} tag {}", debug_action, + tg.id()); + _tag_insert_update.bind_value_as_u64(0, tg.id()); + _tag_insert_update.bind_value_as_u32(1, tg.type()); + _tag_insert_update.bind_value_as_str(2, tg.name()); + std::promise p; + std::future future = p.get_future(); + _mysql.run_statement_and_get_int( + _tag_insert_update, std::move(p), + database::mysql_task::LAST_INSERT_ID, conn); + try { + uint64_t tag_id = future.get(); + _tags_cache[{tg.id(), tg.type()}] = tag_id; + SPDLOG_LOGGER_TRACE(log_v2::sql(), "new tag ({}, {}, {}) {}", tag_id, + tg.id(), tg.type(), tg.name()); + + } catch (const std::exception& e) { + SPDLOG_LOGGER_ERROR(log_v2::sql(), + "unified sql: unable to {} tag ({},{}): {}", + debug_action, tg.id(), tg.type(), e.what()); } _add_action(conn, actions::tags); break; - case Tag_Action_MODIFY: - SPDLOG_LOGGER_TRACE(log_v2::sql(), "SQL: modify tag {}", tg.id()); - _tag_update.bind_value_as_u64(0, tg.id()); - _tag_update.bind_value_as_u32(1, tg.type()); - _tag_update.bind_value_as_str(2, tg.name()); - if (tag_id) { - _tag_update.bind_value_as_u64(3, tag_id); - _mysql.run_statement(_tag_update, database::mysql_error::store_tag, - conn); - _add_action(conn, actions::tags); - } else - SPDLOG_LOGGER_ERROR( - log_v2::sql(), - "unified sql: unable to modify tag ({}, {}): not in cache", tg.id(), - tg.type()); - break; + } case Tag_Action_DELETE: { auto it = _tags_cache.find({tg.id(), tg.type()}); if (it != _tags_cache.end()) { diff --git a/tests/broker-engine/tags.robot b/tests/broker-engine/tags.robot index 430857f9b98..75df9459d5a 100644 --- a/tests/broker-engine/tags.robot +++ b/tests/broker-engine/tags.robot @@ -1,24 +1,25 @@ *** Settings *** -Documentation Engine/Broker tests on tags. - -Resource ../resources/resources.robot -Suite Setup Clean Before Suite -Suite Teardown Clean After Suite -Test Setup Init Test -Test Teardown Save logs If Failed - -Library Process -Library DateTime -Library OperatingSystem -Library ../resources/Engine.py -Library ../resources/Broker.py -Library ../resources/Common.py -Library ../resources/specific-duplication.py +Documentation Engine/Broker tests on tags. + +Resource ../resources/resources.robot +Library Process +Library DateTime +Library OperatingSystem +Library ../resources/Engine.py +Library ../resources/Broker.py +Library ../resources/Common.py +Library ../resources/specific-duplication.py + +Suite Setup Clean Before Suite +Suite Teardown Clean After Suite +Test Setup Init Test +Test Teardown Stop Engine Broker And Save Logs + *** Test Cases *** BETAG1 [Documentation] Engine is configured with some tags. When broker receives them, it stores them in the centreon_storage.tags table. Broker is started before. - [Tags] Broker Engine protobuf bbdo tags + [Tags] broker engine protobuf bbdo tags Config Engine ${1} Create Tags File ${0} ${20} Config Engine Add Cfg File ${0} tags.cfg @@ -29,24 +30,22 @@ BETAG1 Broker Config Log central sql debug Clear Retention Start Broker - ${start}= Get Current Date + ${start} Get Current Date Start Engine # Let's wait for the external command check start - ${content}= Create List check_for_external_commands() - ${result}= Find In Log with Timeout ${engineLog0} ${start} ${content} 60 - Should Be True ${result} msg=A message telling check_for_external_commands() should be available. - - ${result}= check tag With Timeout tag20 3 30 - Should Be True ${result} msg=tag20 should be of type 3 - ${result}= check tag With Timeout tag1 0 30 - Should Be True ${result} msg=tag1 should be of type 0 - Stop Engine - Kindly Stop Broker + ${content} Create List check_for_external_commands() + ${result} Find In Log With Timeout ${engineLog0} ${start} ${content} 60 + Should Be True ${result} A message telling check_for_external_commands() should be available. + + ${result} Check Tag With Timeout tag20 3 30 + Should Be True ${result} tag20 should be of type 3 + ${result} Check Tag With Timeout tag1 0 30 + Should Be True ${result} tag1 should be of type 0 BETAG2 [Documentation] Engine is configured with some tags. When broker receives them, it stores them in the centreon_storage.tags table. Engine is started before. - [Tags] Broker Engine protobuf bbdo tags + [Tags] broker engine protobuf bbdo tags Config Engine ${1} Create Tags File ${0} ${20} Config Engine Add Cfg File ${0} tags.cfg @@ -57,25 +56,23 @@ BETAG2 Broker Config Log central sql debug Clear Retention Sleep 1s - ${start}= Get Current Date + ${start} Get Current Date Start Engine Start Broker # Let's wait for the external command check start - ${content}= Create List check_for_external_commands() - ${result}= Find In Log with Timeout ${engineLog0} ${start} ${content} 60 - Should Be True ${result} msg=A message telling check_for_external_commands() should be available. - - ${result}= check tag With Timeout tag20 3 30 - Should Be True ${result} msg=tag20 should be of type 3 - ${result}= check tag With Timeout tag1 0 30 - Should Be True ${result} msg=tag1 should be of type 0 - Stop Engine - Kindly Stop Broker + ${content} Create List check_for_external_commands() + ${result} Find In Log With Timeout ${engineLog0} ${start} ${content} 60 + Should Be True ${result} A message telling check_for_external_commands() should be available. + + ${result} Check Tag With Timeout tag20 3 30 + Should Be True ${result} tag20 should be of type 3 + ${result} Check Tag With Timeout tag1 0 30 + Should Be True ${result} tag1 should be of type 0 BEUTAG1 [Documentation] Engine is configured with some tags. When broker receives them through unified_sql stream, it stores them in the centreon_storage.tags table. Broker is started before. - [Tags] Broker Engine protobuf bbdo tags unified_sql + [Tags] broker engine protobuf bbdo tags unified_sql Config Engine ${1} Create Tags File ${0} ${20} Config Engine Add Cfg File ${0} tags.cfg @@ -83,32 +80,27 @@ BEUTAG1 Config Broker rrd Config Broker module Config Broker Sql Output central unified_sql - Broker Config Add Item module0 bbdo_version 3.0.0 - Broker Config Add Item central bbdo_version 3.0.0 - Broker Config Add Item rrd bbdo_version 3.0.0 + Config BBDO3 1 Broker Config Log module0 neb debug Broker Config Log central sql debug Clear Retention Start Broker - ${start}= Get Current Date + ${start} Get Current Date Start Engine # Let's wait for the external command check start - ${content}= Create List check_for_external_commands() - ${result}= Find In Log with Timeout ${engineLog0} ${start} ${content} 60 - Should Be True ${result} msg=A message telling check_for_external_commands() should be available. - - ${result}= check tag With Timeout tag20 3 30 - Should Be True ${result} msg=tag20 should be of type 3 - ${result}= check tag With Timeout tag1 0 30 - Should Be True ${result} msg=tag1 should be of type 0 - Stop Engine - Kindly Stop Broker + ${content} Create List check_for_external_commands() + ${result} Find In Log With Timeout ${engineLog0} ${start} ${content} 60 + Should Be True ${result} A message telling check_for_external_commands() should be available. + + ${result} Check Tag With Timeout tag20 3 30 + Should Be True ${result} tag20 should be of type 3 + ${result} Check Tag With Timeout tag1 0 30 + Should Be True ${result} tag1 should be of type 0 BEUTAG2 [Documentation] Engine is configured with some tags. A new service is added with a tag. Broker should make the relations. - [Tags] Broker Engine protobuf bbdo tags unified_sql - Clear Db resources + [Tags] broker engine protobuf bbdo tags unified_sql Config Engine ${1} Create Tags File ${0} ${20} Config Engine Add Cfg File ${0} tags.cfg @@ -116,46 +108,42 @@ BEUTAG2 Config Broker rrd Config Broker module Config Broker Sql Output central unified_sql - Broker Config Add Item module0 bbdo_version 3.0.0 - Broker Config Add Item central bbdo_version 3.0.0 - Broker Config Add Item rrd bbdo_version 3.0.0 - Broker Config Output Set central central-broker-unified-sql connections_count 1 - Broker Config Output Set central central-broker-unified-sql queries_per_transaction 1 - Broker Config Output Set central central-broker-unified-sql read_timeout 1 - Broker Config Output Set central central-broker-unified-sql retry_interval 5 + Config BBDO3 1 + Broker Config Output Set central central-broker-unified-sql connections_count 1 + Broker Config Output Set central central-broker-unified-sql queries_per_transaction 1 + Broker Config Output Set central central-broker-unified-sql read_timeout 1 + Broker Config Output Set central central-broker-unified-sql retry_interval 5 Broker Config Log module0 neb debug Broker Config Log central sql error Clear Retention Start Broker - ${start}= Get Current Date + ${start} Get Current Date Start Engine # Let's wait for the external command check start - ${content}= Create List check_for_external_commands() - ${result}= Find In Log with Timeout ${engineLog0} ${start} ${content} 60 - Should Be True ${result} msg=A message telling check_for_external_commands() should be available. + ${content} Create List check_for_external_commands() + ${result} Find In Log With Timeout ${engineLog0} ${start} ${content} 60 + Should Be True ${result} A message telling check_for_external_commands() should be available. - ${svc}= Create Service ${0} 1 1 + ${svc} Create Service ${0} 1 1 Add Tags To Services ${0} group_tags 4 [${svc}] Stop Engine - ${start}= Get Current Date + ${start} Get Current Date Start Engine Reload Broker # Let's wait for the external command check start - ${content}= Create List check_for_external_commands() - ${result}= Find In Log with Timeout ${engineLog0} ${start} ${content} 60 - Should Be True ${result} msg=A message telling check_for_external_commands() should be available. + ${content} Create List check_for_external_commands() + ${result} Find In Log With Timeout ${engineLog0} ${start} ${content} 60 + Should Be True ${result} A message telling check_for_external_commands() should be available. - ${result}= check resources tags With Timeout 1 ${svc} servicegroup [4] 60 - Should Be True ${result} msg=New service should have a service group tag of id 4. - Stop Engine - Kindly Stop Broker + ${result} Check Resources Tags With Timeout 1 ${svc} servicegroup [4] 60 + Should Be True ${result} New service should have a service group tag of id 4. BEUTAG3 [Documentation] Engine is configured with some tags. When broker receives them, it stores them in the centreon_storage.tags table. Engine is started before. - [Tags] Broker Engine protobuf bbdo tags unified_sql + [Tags] broker engine protobuf bbdo tags unified_sql Config Engine ${1} Create Tags File ${0} ${20} Config Engine Add Cfg File ${0} tags.cfg @@ -163,33 +151,29 @@ BEUTAG3 Config Broker rrd Config Broker module Config Broker Sql Output central unified_sql - Broker Config Add Item module0 bbdo_version 3.0.0 - Broker Config Add Item central bbdo_version 3.0.0 - Broker Config Add Item rrd bbdo_version 3.0.0 + Config BBDO3 1 Broker Config Log module0 neb debug Broker Config Log central sql debug Clear Retention Sleep 1s - ${start}= Get Current Date + ${start} Get Current Date Start Engine Start Broker # Let's wait for the external command check start - ${content}= Create List check_for_external_commands() - ${result}= Find In Log with Timeout ${engineLog0} ${start} ${content} 60 - Should Be True ${result} msg=A message telling check_for_external_commands() should be available. - - ${result}= check tag With Timeout tag20 3 30 - Should Be True ${result} msg=tag20 should be of type 3 - ${result}= check tag With Timeout tag1 0 30 - Should Be True ${result} msg=tag1 should be of type 0 - Stop Engine - Kindly Stop Broker + ${content} Create List check_for_external_commands() + ${result} Find In Log With Timeout ${engineLog0} ${start} ${content} 60 + Should Be True ${result} A message telling check_for_external_commands() should be available. + + ${result} Check Tag With Timeout tag20 3 30 + Should Be True ${result} tag20 should be of type 3 + ${result} Check Tag With Timeout tag1 0 30 + Should Be True ${result} tag1 should be of type 0 BEUTAG4 [Documentation] Engine is configured with some tags. Group tags tag9, tag13 are set to services 1 and 3. Category tags tag3 and tag11 are added to services 1, 3, 5 and 6. The centreon_storage.resources and resources_tags tables are well filled. - [Tags] Broker Engine protobuf bbdo tags unified_sql - #Clear DB tags + [Tags] broker engine protobuf bbdo tags unified_sql + # Clear Db tags Config Engine ${1} Create Tags File ${0} ${20} Config Engine Add Cfg File ${0} tags.cfg @@ -199,37 +183,33 @@ BEUTAG4 Config Broker rrd Config Broker module Config Broker Sql Output central unified_sql - Broker Config Add Item module0 bbdo_version 3.0.0 - Broker Config Add Item central bbdo_version 3.0.0 - Broker Config Add Item rrd bbdo_version 3.0.0 + Config BBDO3 1 Broker Config Log module0 neb debug Broker Config Log central sql debug Clear Retention - ${start}= Get Current Date + ${start} Get Current Date Start Engine Sleep 1s Start Broker # Let's wait for the external command check start - ${content}= Create List check_for_external_commands() - ${result}= Find In Log with Timeout ${engineLog0} ${start} ${content} 60 - Should Be True ${result} msg=A message telling check_for_external_commands() should be available. - - ${result}= check resources tags With Timeout 1 1 servicegroup [4, 5] 60 - Should Be True ${result} msg=Service (1, 1) should have servicegroup tag ids 4 and 5 - ${result}= check resources tags With Timeout 1 3 servicegroup [4, 5] 60 - Should Be True ${result} msg=Service (1, 3) should have servicegroup tag ids 4, 5 - ${result}= check resources tags With Timeout 1 3 servicecategory [2, 4] 60 - Should Be True ${result} msg=Service (1, 3) should have servicecategory tag ids 2, 4 - ${result}= check resources tags With Timeout 1 5 servicecategory [2, 4] 60 - Should Be True ${result} msg=Service (1, 5) should have servicecategory tag ids 2, 4 - Stop Engine - Kindly Stop Broker + ${content} Create List check_for_external_commands() + ${result} Find In Log With Timeout ${engineLog0} ${start} ${content} 60 + Should Be True ${result} A message telling check_for_external_commands() should be available. + + ${result} Check Resources Tags With Timeout 1 1 servicegroup [4, 5] 60 + Should Be True ${result} Service (1, 1) should have servicegroup tag ids 4 and 5 + ${result} Check Resources Tags With Timeout 1 3 servicegroup [4, 5] 60 + Should Be True ${result} Service (1, 3) should have servicegroup tag ids 4, 5 + ${result} Check Resources Tags With Timeout 1 3 servicecategory [2, 4] 60 + Should Be True ${result} Service (1, 3) should have servicecategory tag ids 2, 4 + ${result} Check Resources Tags With Timeout 1 5 servicecategory [2, 4] 60 + Should Be True ${result} Service (1, 5) should have servicecategory tag ids 2, 4 BEUTAG5 [Documentation] Engine is configured with some tags. Group tags tag2, tag6 are set to hosts 1 and 2. Category tags tag4 and tag8 are added to hosts 2, 3, 4. The resources and resources_tags tables are well filled. - [Tags] Broker Engine protobuf bbdo tags - #Clear DB tags + [Tags] broker engine protobuf bbdo tags + # Clear Db tags Config Engine ${1} Create Tags File ${0} ${20} Config Engine Add Cfg File ${0} tags.cfg @@ -239,37 +219,33 @@ BEUTAG5 Config Broker rrd Config Broker module Config Broker Sql Output central unified_sql - Broker Config Add Item module0 bbdo_version 3.0.0 - Broker Config Add Item central bbdo_version 3.0.0 - Broker Config Add Item rrd bbdo_version 3.0.0 + Config BBDO3 1 Broker Config Log module0 neb debug Broker Config Log central sql debug Clear Retention Sleep 1s - ${start}= Get Current Date + ${start} Get Current Date Start Engine Start Broker # Let's wait for the external command check start - ${content}= Create List check_for_external_commands() - ${result}= Find In Log with Timeout ${engineLog0} ${start} ${content} 60 - Should Be True ${result} msg=A message telling check_for_external_commands() should be available. - - ${result}= check resources tags With Timeout 0 1 hostgroup [2,3] 60 - Should Be True ${result} msg=Host 1 should have hostgroup tags 2 and 3 - ${result}= check resources tags With Timeout 0 2 hostgroup [2,3] 60 - Should Be True ${result} msg=Host 2 should have hostgroup tags 2 and 3 - ${result}= check resources tags With Timeout 0 2 hostcategory [2, 3] 60 - Should Be True ${result} msg=Host 2 should have hostcategory tags 2 and 3 - ${result}= check resources tags With Timeout 0 3 hostcategory [2, 3] 60 - Should Be True ${result} msg=Host 3 should have hostcategory tags 2 and 3 - Stop Engine - Kindly Stop Broker + ${content} Create List check_for_external_commands() + ${result} Find In Log With Timeout ${engineLog0} ${start} ${content} 60 + Should Be True ${result} A message telling check_for_external_commands() should be available. + + ${result} Check Resources Tags With Timeout 0 1 hostgroup [2,3] 60 + Should Be True ${result} Host 1 should have hostgroup tags 2 and 3 + ${result} Check Resources Tags With Timeout 0 2 hostgroup [2,3] 60 + Should Be True ${result} Host 2 should have hostgroup tags 2 and 3 + ${result} Check Resources Tags With Timeout 0 2 hostcategory [2, 3] 60 + Should Be True ${result} Host 2 should have hostcategory tags 2 and 3 + ${result} Check Resources Tags With Timeout 0 3 hostcategory [2, 3] 60 + Should Be True ${result} Host 3 should have hostcategory tags 2 and 3 BEUTAG6 [Documentation] Engine is configured with some tags. When broker receives them, it stores them in the centreon_storage.resources_tags table. Engine is started before. - [Tags] Broker Engine protobuf bbdo tags - #Clear DB tags + [Tags] broker engine protobuf bbdo tags + # Clear Db tags Config Engine ${1} Create Tags File ${0} ${20} Config Engine Add Cfg File ${0} tags.cfg @@ -281,36 +257,32 @@ BEUTAG6 Config Broker rrd Config Broker module ${1} Config Broker Sql Output central unified_sql - Broker Config Add Item module0 bbdo_version 3.0.0 - Broker Config Add Item central bbdo_version 3.0.0 - Broker Config Add Item rrd bbdo_version 3.0.0 + Config BBDO3 1 Broker Config Log module0 neb debug Broker Config Log central sql debug Clear Retention Sleep 1s - ${start}= Get Current Date + ${start} Get Current Date Start Engine Start Broker # Let's wait for the external command check start - ${content}= Create List check_for_external_commands() - ${result}= Find In Log with Timeout ${engineLog0} ${start} ${content} 60 - Should Be True ${result} msg=A message telling check_for_external_commands() should be available. - - ${result}= check resources tags With Timeout 0 1 hostgroup [2,4] 60 - Should Be True ${result} msg=Host 1 should have hostgroup tag_id 2 and 4 - ${result}= check resources tags With Timeout 0 1 hostcategory [1,5] 60 - Should Be True ${result} msg=Host 1 should have hostcategory tag_id 1 and 5 - ${result}= check resources tags With Timeout 1 1 servicegroup [2,4] 60 - Should Be True ${result} msg=Service (1, 1) should have servicegroup tag_id 2 and 4. - ${result}= check resources tags With Timeout 1 1 servicecategory [3,5] 60 - Should Be True ${result} msg=Service (1, 1) should have servicecategory tag_id 3 and 5. - Stop Engine - Kindly Stop Broker + ${content} Create List check_for_external_commands() + ${result} Find In Log With Timeout ${engineLog0} ${start} ${content} 60 + Should Be True ${result} A message telling check_for_external_commands() should be available. + + ${result} Check Resources Tags With Timeout 0 1 hostgroup [2,4] 60 + Should Be True ${result} Host 1 should have hostgroup tag_id 2 and 4 + ${result} Check Resources Tags With Timeout 0 1 hostcategory [1,5] 60 + Should Be True ${result} Host 1 should have hostcategory tag_id 1 and 5 + ${result} Check Resources Tags With Timeout 1 1 servicegroup [2,4] 60 + Should Be True ${result} Service (1, 1) should have servicegroup tag_id 2 and 4. + ${result} Check Resources Tags With Timeout 1 1 servicecategory [3,5] 60 + Should Be True ${result} Service (1, 1) should have servicecategory tag_id 3 and 5. BEUTAG7 - [Documentation] some services are configured and deleted with tags on two pollers. - [Tags] Broker Engine protobuf bbdo tags + [Documentation] Some services are configured with tags on two pollers. Then tags configuration is modified. + [Tags] broker engine protobuf bbdo tags unstable Config Engine ${2} Create Tags File ${0} ${20} Create Tags File ${1} ${20} @@ -326,31 +298,28 @@ BEUTAG7 Config Broker rrd Config Broker module ${2} Config Broker Sql Output central unified_sql - Broker Config Add Item module0 bbdo_version 3.0.0 - Broker Config Add Item module1 bbdo_version 3.0.0 - Broker Config Add Item central bbdo_version 3.0.0 - Broker Config Add Item rrd bbdo_version 3.0.0 + Config BBDO3 2 Broker Config Log module0 neb debug Broker Config Log module1 neb debug Broker Config Log central sql trace Clear Retention - ${start}= Get Current Date + ${start} Get Current Date Start Engine Start Broker # Let's wait for the external command check start - ${content}= Create List check_for_external_commands() - ${result}= Find In Log with Timeout ${engineLog0} ${start} ${content} 60 - Should Be True ${result} msg=A message telling check_for_external_commands() should be available. + ${content} Create List check_for_external_commands() + ${result} Find In Log With Timeout ${engineLog0} ${start} ${content} 60 + Should Be True ${result} A message telling check_for_external_commands() should be available. - # We need to wait a little before reloading Engine - ${result}= check resources tags With Timeout 1 1 servicegroup [2,4] 60 - Should Be True ${result} msg=First step: Service (1, 1) should have servicegroup tags 2 and 4 + # We check in the DB if the service (1,1) has well its servicegroup tags configured. + ${result} Check Resources Tags With Timeout 1 1 servicegroup [2,4] 60 + Should Be True ${result} First step: Service (1, 1) should have servicegroup tags 2 and 4 - ${result}= check resources tags With Timeout 26 502 servicecategory [2,4] 60 - Should Be True ${result} msg=First step: Service (26, 502) should have servicecategory tags 13, 9, 3 and 11. - ${result}= check resources tags With Timeout 26 502 servicegroup [3,5] 60 - Should Be True ${result} msg=First step: Service (26, 502) should have servicegroup tags 3 and 5. + ${result} Check Resources Tags With Timeout 26 502 servicecategory [2,4] 60 + Should Be True ${result} First step: Service (26, 502) should have servicecategory tags 13, 9, 3 and 11. + ${result} Check Resources Tags With Timeout 26 502 servicegroup [3,5] 60 + Should Be True ${result} First step: Service (26, 502) should have servicegroup tags 3 and 5. Remove Tags From Services ${0} group_tags Remove Tags From Services ${0} category_tags @@ -359,21 +328,23 @@ BEUTAG7 Create Tags File ${0} ${18} Create Tags File ${1} ${18} Add Tags To Services ${1} group_tags 3,5 [505, 506, 507, 508] + ${start} Get Round Current Date Reload Engine Reload Broker - Sleep 3s - ${result}= check resources tags With Timeout 26 507 servicegroup [3,5] 60 - Should Be True ${result} msg=Second step: Service (26, 507) should have servicegroup tags 3 and 5 + # Let's wait for the external command check start + ${content} Create List check_for_external_commands() + ${result} Find In Log With Timeout ${engineLog0} ${start} ${content} 60 + Should Be True ${result} A message telling check_for_external_commands() should be available. - ${result}= check resources tags With Timeout 26 508 servicegroup [3,5] 60 - Should Be True ${result} msg=Second step: Service (26, 508) should have servicegroup tags 3 and 5 + ${result} Check Resources Tags With Timeout 26 507 servicegroup [3,5] 60 + Should Be True ${result} Second step: Service (26, 507) should have servicegroup tags 3 and 5 - Stop Engine - Kindly Stop Broker + ${result} Check Resources Tags With Timeout 26 508 servicegroup [3,5] 60 + Should Be True ${result} Second step: Service (26, 508) should have servicegroup tags 3 and 5 BEUTAG8 [Documentation] Services have tags provided by templates. - [Tags] Broker Engine protobuf bbdo tags + [Tags] broker engine protobuf bbdo tags Config Engine ${2} Create Tags File ${0} ${40} Create Tags File ${1} ${40} @@ -397,19 +368,20 @@ BEUTAG8 Config Broker central Config Broker rrd Config Broker module ${2} + Config Broker Sql Output central unified_sql Config BBDO3 2 Broker Config Log module0 neb debug Broker Config Log module1 neb debug Broker Config Log central sql trace Clear Retention - ${start}= Get Current Date + ${start} Get Current Date Start Engine Start Broker # Let's wait for the external command check start - ${content}= Create List check_for_external_commands() - ${result}= Find In Log with Timeout ${engineLog0} ${start} ${content} 60 - Should Be True ${result} msg=A message telling check_for_external_commands() should be available. + ${content} Create List check_for_external_commands() + ${result} Find In Log With Timeout ${engineLog0} ${start} ${content} 60 + Should Be True ${result} A message telling check_for_external_commands() should be available. # We need to wait a little before reloading Engine ${result} Check Resources Tags With Timeout 1 2 servicecategory [3,5] 60 @@ -426,12 +398,9 @@ BEUTAG8 ${result} Check Resources Tags With Timeout 26 503 servicegroup [7] 60 Should Be True ${result} First step: Service (26, 503) should have servicegroup tag 7 - Stop Engine - Kindly Stop Broker - BEUTAG9 [Documentation] hosts have tags provided by templates. - [Tags] Broker Engine protobuf bbdo tags + [Tags] broker engine protobuf bbdo tags Config Engine ${2} Create Tags File ${0} ${40} Create Tags File ${1} ${40} @@ -452,55 +421,49 @@ BEUTAG9 Config Broker rrd Config Broker module ${2} Config Broker Sql Output central unified_sql - Broker Config Add Item module0 bbdo_version 3.0.0 - Broker Config Add Item module1 bbdo_version 3.0.0 - Broker Config Add Item central bbdo_version 3.0.0 - Broker Config Add Item rrd bbdo_version 3.0.0 + Config BBDO3 2 Broker Config Log module0 neb debug Broker Config Log module1 neb debug Broker Config Log central sql trace Clear Retention Sleep 1s - ${start}= Get Current Date + ${start} Get Current Date Start Engine Start Broker # Let's wait for the external command check start - ${content}= Create List check_for_external_commands() - ${result}= Find In Log with Timeout ${engineLog0} ${start} ${content} 60 - Should Be True ${result} msg=A message telling check_for_external_commands() should be available. + ${content} Create List check_for_external_commands() + ${result} Find In Log With Timeout ${engineLog0} ${start} ${content} 60 + Should Be True ${result} A message telling check_for_external_commands() should be available. # We need to wait a little before reloading Engine - ${result}= check resources tags With Timeout 0 9 hostgroup [2] 60 - Should Be True ${result} msg=First step: resource 9 should have hostgroup tag with id=2 - - ${result}= check resources tags With Timeout 0 10 hostgroup [2] 60 - Should Be True ${result} msg=First step: resource 10 should have hostgroup tag with id=2 + ${result} Check Resources Tags With Timeout 0 9 hostgroup [2] 60 + Should Be True ${result} First step: resource 9 should have hostgroup tag with id=2 - ${result}= check resources tags With Timeout 0 11 hostgroup [6] 60 - Should Be True ${result} msg=First step: resource 11 should have hostgroup tag with id=6 + ${result} Check Resources Tags With Timeout 0 10 hostgroup [2] 60 + Should Be True ${result} First step: resource 10 should have hostgroup tag with id=2 - ${result}= check resources tags With Timeout 0 12 hostgroup [6] 60 - Should Be True ${result} msg=First step: resource 12 should have hostgroup tag with id=6 + ${result} Check Resources Tags With Timeout 0 11 hostgroup [6] 60 + Should Be True ${result} First step: resource 11 should have hostgroup tag with id=6 - ${result}= check resources tags With Timeout 0 30 hostgroup [8] 60 - Should Be True ${result} msg=First step: resource 30 should have hostgroup tag with id=10 + ${result} Check Resources Tags With Timeout 0 12 hostgroup [6] 60 + Should Be True ${result} First step: resource 12 should have hostgroup tag with id=6 - ${result}= check resources tags With Timeout 0 31 hostgroup [8] 60 - Should Be True ${result} msg=First step: resource 31 should have hostgroup tag with id=10 + ${result} Check Resources Tags With Timeout 0 30 hostgroup [8] 60 + Should Be True ${result} First step: resource 30 should have hostgroup tag with id=10 - ${result}= check resources tags With Timeout 0 32 hostgroup [9] 60 - Should Be True ${result} msg=First step: resource 32 should have hostgroup tag with id=14 + ${result} Check Resources Tags With Timeout 0 31 hostgroup [8] 60 + Should Be True ${result} First step: resource 31 should have hostgroup tag with id=10 - ${result}= check resources tags With Timeout 0 33 hostgroup [9] 60 - Should Be True ${result} msg=First step: host 33 should have hostgroup tag with id=14 + ${result} Check Resources Tags With Timeout 0 32 hostgroup [9] 60 + Should Be True ${result} First step: resource 32 should have hostgroup tag with id=14 - Stop Engine - Kindly Stop Broker + ${result} Check Resources Tags With Timeout 0 33 hostgroup [9] 60 + Should Be True ${result} First step: host 33 should have hostgroup tag with id=14 BEUTAG10 [Documentation] some services are configured with tags on two pollers. Then tags are removed from some of them and in centreon_storage, we can observe resources_tags table updated. - [Tags] Broker Engine protobuf bbdo tags + [Tags] broker engine protobuf bbdo tags Config Engine ${2} Create Tags File ${0} ${20} Create Tags File ${1} ${20} @@ -516,32 +479,29 @@ BEUTAG10 Config Broker rrd Config Broker module ${2} Config Broker Sql Output central unified_sql - Broker Config Add Item module0 bbdo_version 3.0.0 - Broker Config Add Item module1 bbdo_version 3.0.0 - Broker Config Add Item central bbdo_version 3.0.0 - Broker Config Add Item rrd bbdo_version 3.0.0 + Config BBDO3 2 Broker Config Log module0 neb debug Broker Config Log module1 neb debug Broker Config Log central sql trace Clear Retention - ${start}= Get Current Date + ${start} Get Current Date Start Engine Start Broker # Let's wait for the external command check start - ${content}= Create List check_for_external_commands() - ${result}= Find In Log with Timeout ${engineLog0} ${start} ${content} 60 - Should Be True ${result} msg=A message telling check_for_external_commands() should be available. + ${content} Create List check_for_external_commands() + ${result} Find In Log With Timeout ${engineLog0} ${start} ${content} 60 + Should Be True ${result} A message telling check_for_external_commands() should be available. - ${result}= check resources tags With Timeout 1 4 servicegroup [2,4] 60 - Should Be True ${result} msg=First step: Service (1, 4) should have servicegroup tags 2 and 4 - ${result}= check resources tags With Timeout 1 3 servicecategory [3,5] 60 - Should Be True ${result} msg=First step: Service (1, 3) should have servicecategory tags 3 and 5 + ${result} Check Resources Tags With Timeout 1 4 servicegroup [2,4] 60 + Should Be True ${result} First step: Service (1, 4) should have servicegroup tags 2 and 4 + ${result} Check Resources Tags With Timeout 1 3 servicecategory [3,5] 60 + Should Be True ${result} First step: Service (1, 3) should have servicecategory tags 3 and 5 - ${result}= check resources tags With Timeout 26 504 servicegroup [3,5] 60 - Should Be True ${result} msg=First step: Service (26, 504) should have servicegroup tags 3 and 5. - ${result}= check resources tags With Timeout 26 503 servicecategory [2,4] 60 - Should Be True ${result} msg=First step: Service (26, 503) should have servicecategory tags 2 and 4. + ${result} Check Resources Tags With Timeout 26 504 servicegroup [3,5] 60 + Should Be True ${result} First step: Service (26, 504) should have servicegroup tags 3 and 5. + ${result} Check Resources Tags With Timeout 26 503 servicecategory [2,4] 60 + Should Be True ${result} First step: Service (26, 503) should have servicecategory tags 2 and 4. Remove Tags From Services ${0} group_tags Remove Tags From Services ${0} category_tags @@ -555,24 +515,21 @@ BEUTAG10 Add Tags To Services ${1} category_tags 2,4 [501, 502, 504] Reload Engine Reload Broker - ${result}= check resources tags With Timeout 1 4 servicegroup [2,4] 60 False - Should Be True ${result} msg=Second step: Service (1, 4) should not have servicegroup tags 2 and 4 + ${result} Check Resources Tags With Timeout 1 4 servicegroup [2,4] 60 False + Should Be True ${result} Second step: Service (1, 4) should not have servicegroup tags 2 and 4 - ${result}= check resources tags With Timeout 1 3 servicecategory [3,5] 60 False - Should Be True ${result} msg=Second step: Service (1, 3) should not have servicecategory tags 3 and 5 + ${result} Check Resources Tags With Timeout 1 3 servicecategory [3,5] 60 False + Should Be True ${result} Second step: Service (1, 3) should not have servicecategory tags 3 and 5 - ${result}= check resources tags With Timeout 26 504 servicegroup [3,5] 60 False - Should Be True ${result} msg=Second step: Service (26, 504) should not have servicegroup tags 3 and 5 + ${result} Check Resources Tags With Timeout 26 504 servicegroup [3,5] 60 False + Should Be True ${result} Second step: Service (26, 504) should not have servicegroup tags 3 and 5 - ${result}= check resources tags With Timeout 26 503 servicecategory [3,5] 60 False - Should Be True ${result} msg=Second step: Service (26, 503) should not have servicecategory tags 3 and 5 - - Stop Engine - Kindly Stop Broker + ${result} Check Resources Tags With Timeout 26 503 servicecategory [3,5] 60 False + Should Be True ${result} Second step: Service (26, 503) should not have servicecategory tags 3 and 5 BEUTAG11 [Documentation] some services are configured with tags on two pollers. Then several tags are removed, and we can observe resources_tags table updated. - [Tags] Broker Engine protobuf bbdo tags + [Tags] broker engine protobuf bbdo tags Config Engine ${2} Create Tags File ${0} ${20} Create Tags File ${1} ${20} @@ -588,32 +545,29 @@ BEUTAG11 Config Broker rrd Config Broker module ${2} Config Broker Sql Output central unified_sql - Broker Config Add Item module0 bbdo_version 3.0.0 - Broker Config Add Item module1 bbdo_version 3.0.0 - Broker Config Add Item central bbdo_version 3.0.0 - Broker Config Add Item rrd bbdo_version 3.0.0 + Config BBDO3 2 Broker Config Log module0 neb debug Broker Config Log module1 neb debug Broker Config Log central sql trace Clear Retention - ${start}= Get Current Date + ${start} Get Current Date Start Engine Start Broker # Let's wait for the external command check start - ${content}= Create List check_for_external_commands() - ${result}= Find In Log with Timeout ${engineLog0} ${start} ${content} 60 - Should Be True ${result} msg=A message telling check_for_external_commands() should be available. + ${content} Create List check_for_external_commands() + ${result} Find In Log With Timeout ${engineLog0} ${start} ${content} 60 + Should Be True ${result} A message telling check_for_external_commands() should be available. - ${result}= check resources tags With Timeout 1 4 servicegroup [2,4] 60 - Should Be True ${result} msg=First step: Service (1, 4) should have servicegroup tags 2 and 4 - ${result}= check resources tags With Timeout 1 3 servicecategory [3,5] 60 - Should Be True ${result} msg=First step: Service (1, 3) should have servicecategory tags 3 and 5 + ${result} Check Resources Tags With Timeout 1 4 servicegroup [2,4] 60 + Should Be True ${result} First step: Service (1, 4) should have servicegroup tags 2 and 4 + ${result} Check Resources Tags With Timeout 1 3 servicecategory [3,5] 60 + Should Be True ${result} First step: Service (1, 3) should have servicecategory tags 3 and 5 - ${result}= check resources tags With Timeout 26 504 servicegroup [3,5] 60 - Should Be True ${result} msg=First step: Service (26, 504) should have servicegroup tags 3 and 5. - ${result}= check resources tags With Timeout 26 503 servicecategory [2,4] 60 - Should Be True ${result} msg=First step: Service (26, 503) should have servicecategory tags 2 and 4. + ${result} Check Resources Tags With Timeout 26 504 servicegroup [3,5] 60 + Should Be True ${result} First step: Service (26, 504) should have servicegroup tags 3 and 5. + ${result} Check Resources Tags With Timeout 26 503 servicecategory [2,4] 60 + Should Be True ${result} First step: Service (26, 503) should have servicecategory tags 2 and 4. Remove Tags From Services ${0} group_tags Remove Tags From Services ${0} category_tags @@ -627,25 +581,22 @@ BEUTAG11 Add Tags To Services ${1} category_tags 2,4 [501, 502, 504] Reload Engine Reload Broker - ${result}= check resources tags With Timeout 1 4 servicegroup [2,4] 60 - Should Be True ${result} msg=Second step: Service (1, 4) should not have servicegroup tags 2 and 4 + ${result} Check Resources Tags With Timeout 1 4 servicegroup [2,4] 60 + Should Be True ${result} Second step: Service (1, 4) should not have servicegroup tags 2 and 4 - ${result}= check resources tags With Timeout 1 3 servicecategory [5] 60 False - Should Be True ${result} msg=Second step: Service (1, 3) should not have servicecategory tags 5 + ${result} Check Resources Tags With Timeout 1 3 servicecategory [5] 60 False + Should Be True ${result} Second step: Service (1, 3) should not have servicecategory tags 5 - ${result}= check resources tags With Timeout 26 504 servicegroup [3,5] 60 False - Should Be True ${result} msg=Second step: Service (26, 504) should not have servicegroup tags 3 and 5 + ${result} Check Resources Tags With Timeout 26 504 servicegroup [3,5] 60 False + Should Be True ${result} Second step: Service (26, 504) should not have servicegroup tags 3 and 5 - ${result}= check resources tags With Timeout 26 503 servicecategory [3,5] 60 - Should Be True ${result} msg=Second step: Service (26, 503) should not have servicecategory tags 3 and 5 - - Stop Engine - Kindly Stop Broker + ${result} Check Resources Tags With Timeout 26 503 servicecategory [3,5] 60 + Should Be True ${result} Second step: Service (26, 503) should not have servicecategory tags 3 and 5 BEUTAG12 [Documentation] Engine is configured with some tags. Group tags tag2, tag6 are set to hosts 1 and 2. Category tags tag4 and tag8 are added to hosts 2, 3, 4. The resources and resources_tags tables are well filled. The tag6 and tag8 are removed and resources_tags is also well updated. - [Tags] Broker Engine protobuf bbdo tags - #Clear DB tags + [Tags] broker engine protobuf bbdo tags + # Clear Db tags Config Engine ${1} Create Tags File ${0} ${20} Config Engine Add Cfg File ${0} tags.cfg @@ -655,30 +606,28 @@ BEUTAG12 Config Broker rrd Config Broker module Config Broker Sql Output central unified_sql - Broker Config Add Item module0 bbdo_version 3.0.0 - Broker Config Add Item central bbdo_version 3.0.0 - Broker Config Add Item rrd bbdo_version 3.0.0 + Config BBDO3 1 Broker Config Log module0 neb debug Broker Config Log central sql debug Clear Retention Sleep 1s - ${start}= Get Current Date + ${start} Get Current Date Start Engine Start Broker # Let's wait for the external command check start - ${content}= Create List check_for_external_commands() - ${result}= Find In Log with Timeout ${engineLog0} ${start} ${content} 60 - Should Be True ${result} msg=A message telling check_for_external_commands() should be available. - - ${result}= check resources tags With Timeout 0 1 hostgroup [2,3] 60 - Should Be True ${result} msg=Host 1 should have hostgroup tags 2 and 3 - ${result}= check resources tags With Timeout 0 2 hostgroup [2,3] 60 - Should Be True ${result} msg=Host 2 should have hostgroup tags 2 and 3 - ${result}= check resources tags With Timeout 0 2 hostcategory [2, 3] 60 - Should Be True ${result} msg=Host 2 should have hostcategory tags 2 and 3 - ${result}= check resources tags With Timeout 0 3 hostcategory [2, 3] 60 - Should Be True ${result} msg=Host 3 should have hostcategory tags 2 and 3 + ${content} Create List check_for_external_commands() + ${result} Find In Log With Timeout ${engineLog0} ${start} ${content} 60 + Should Be True ${result} A message telling check_for_external_commands() should be available. + + ${result} Check Resources Tags With Timeout 0 1 hostgroup [2,3] 60 + Should Be True ${result} Host 1 should have hostgroup tags 2 and 3 + ${result} Check Resources Tags With Timeout 0 2 hostgroup [2,3] 60 + Should Be True ${result} Host 2 should have hostgroup tags 2 and 3 + ${result} Check Resources Tags With Timeout 0 2 hostcategory [2, 3] 60 + Should Be True ${result} Host 2 should have hostcategory tags 2 and 3 + ${result} Check Resources Tags With Timeout 0 3 hostcategory [2, 3] 60 + Should Be True ${result} Host 3 should have hostcategory tags 2 and 3 Remove Tags From Hosts ${0} group_tags Remove Tags From Hosts ${0} category_tags @@ -688,22 +637,80 @@ BEUTAG12 Reload Engine Reload Broker - ${result}= check resources tags With Timeout 0 1 hostgroup [2,3] 60 False - Should Be True ${result} msg=Host 1 should not have hostgroup tags 2 nor 3 - ${result}= check resources tags With Timeout 0 2 hostgroup [2,3] 60 False - Should Be True ${result} msg=Host 2 should not have hostgroup tags 2 nor 3 - ${result}= check resources tags With Timeout 0 2 hostcategory [2,3] 60 False - Should Be True ${result} msg=Host 2 should not have hostgroup tags 2 nor 3 - ${result}= check resources tags With Timeout 0 3 hostcategory [2,3] 60 False - Should Be True ${result} msg=Host 3 should not have hostgroup tags 2 nor 3 - ${result}= check resources tags With Timeout 0 4 hostcategory [2,3] 60 False - Should Be True ${result} msg=Host 4 should not have hostgroup tags 2 nor 3 + ${result} Check Resources Tags With Timeout 0 1 hostgroup [2,3] 60 False + Should Be True ${result} Host 1 should not have hostgroup tags 2 nor 3 + ${result} Check Resources Tags With Timeout 0 2 hostgroup [2,3] 60 False + Should Be True ${result} Host 2 should not have hostgroup tags 2 nor 3 + ${result} Check Resources Tags With Timeout 0 2 hostcategory [2,3] 60 False + Should Be True ${result} Host 2 should not have hostgroup tags 2 nor 3 + ${result} Check Resources Tags With Timeout 0 3 hostcategory [2,3] 60 False + Should Be True ${result} Host 3 should not have hostgroup tags 2 nor 3 + ${result} Check Resources Tags With Timeout 0 4 hostcategory [2,3] 60 False + Should Be True ${result} Host 4 should not have hostgroup tags 2 nor 3 + +BEUTAG_REMOVE_HOST_FROM_HOSTGROUP + [Documentation] remove a host from hostgroup, reload, insert 2 host in the hostgroup must not make sql error + [Tags] broker engine tags + Clear Db tags + Config Engine ${1} + Create Tags File ${0} ${3} ${0} hostgroup + Config Engine Add Cfg File ${0} tags.cfg + Add Tags To Hosts ${0} group_tags 2 1 + Add Tags To Hosts ${0} group_tags 1 4 + Config Broker central + Config Broker rrd + Config Broker module + Config Broker Sql Output central unified_sql + Config BBDO3 1 + Broker Config Log module0 neb debug + Broker Config Log central sql trace + Broker Config Log central perfdata trace + Clear Retention + Sleep 1s + ${start} Get Current Date + Start Engine + Start Broker + + # Let's wait for the external command check start + ${content} Create List check_for_external_commands() + ${result} Find In Log With Timeout ${engineLog0} ${start} ${content} 60 + Should Be True ${result} A message telling check_for_external_commands() should be available. + + ${result} Check Resources Tags With Timeout 0 1 hostgroup [2] 60 True + Should Be True ${result} Host 1 should not have hostgroup tags 2 + + ${content} Create List unified_sql: end check_queue + ${result} Find In Log With Timeout ${centralLog} ${start} ${content} 60 + Should Be True ${result} A message unified_sql: end check_queue should be available. + + Engine Config Remove Service Host ${0} host_1 + Engine Config Remove Host 0 host_1 + Engine Config Remove Tag 0 2 + Reload Engine + + ${result} Check Resources Tags With Timeout 0 1 hostgroup [2] 60 False + Should Be True ${result} Host 1 should not have hostgroup tags 2 + + # wait for commits + ${start} Get Current Date + ${content} Create List unified_sql: end check_queue + ${result} Find In Log With Timeout ${centralLog} ${start} ${content} 60 + Should Be True ${result} A message unified_sql: end check_queue should be available. + + Sleep 5 + + Create Tags File ${0} ${3} ${0} hostgroup + Add Tags To Hosts ${0} group_tags 2 [2,3] + Reload Engine + + ${result} Check Resources Tags With Timeout 0 2 hostgroup [2] 60 True + Should Be True ${result} Host 2 should have hostgroup tags 2 + + ${result} Check Resources Tags With Timeout 0 3 hostgroup [2] 60 True + Should Be True ${result} Host 3 should have hostgroup tags 2 - Stop Engine - Kindly Stop Broker *** Keywords *** Init Test Stop Processes - truncate_resource_host_service - + Truncate Resource Host Service diff --git a/tests/resources/Engine.py b/tests/resources/Engine.py index 3eaed2f61ad..c24d1488b99 100755 --- a/tests/resources/Engine.py +++ b/tests/resources/Engine.py @@ -405,7 +405,7 @@ def create_template_file(poller: int, typ: str, what: str, ids): ff.close() @staticmethod - def create_tags(poller: int, nb: int, offset: int): + def create_tags(poller: int, nb: int, offset: int, tag_type: str): tt = ["servicegroup", "hostgroup", "servicecategory", "hostcategory"] config_file = "{}/config{}/tags.cfg".format(CONF_DIR, poller) @@ -413,9 +413,13 @@ def create_tags(poller: int, nb: int, offset: int): content = "" tid = 0 for i in range(nb): - if i % 4 == 0: - tid += 1 - typ = tt[i % 4] + if not tag_type: + if i % 4 == 0: + tid += 1 + typ = tt[i % 4] + else: + typ = tag_type + tid += 1 content += """define tag {{ id {0} name tag{2} @@ -899,10 +903,9 @@ def engine_config_remove_service_host(idx: int, host: str): def engine_config_remove_host(idx: int, host: str): - filename = ETC_ROOT + "/centreon-engine/config{}/services.cfg".format(idx) - f = open(filename, "r") - lines = f.readlines() - f.close() + filename = f"{ETC_ROOT}/centreon-engine/config{idx}/hosts.cfg" + with open(filename, "r") as f: + lines = f.readlines() host_name = re.compile(r"^\s*host_name\s+" + host + "\s*$") host_begin = re.compile(r"^define host {$") @@ -1615,8 +1618,42 @@ def create_template_file(poller: int, typ: str, what: str, ids: list): engine.create_template_file(poller, typ, what, ids) -def create_tags_file(poller: int, nb: int, offset: int = 1): - engine.create_tags(poller, nb, offset) +def create_tags_file(poller: int, nb: int, offset: int = 1, tag_type: str = ""): + engine.create_tags(poller, nb, offset, tag_type) + + +def engine_config_remove_tag(poller: int, tag_id: int): + """! remove tags from tags.cfg where tag id = tag_id + @param poller poller index + @param tag_id id of the tag to remove + """ + filename = f"{CONF_DIR}/config{poller}/tags.cfg" + with open(filename, "r") as ff: + lines = ff.readlines() + + tag_name = re.compile(f"^\s*id\s+{tag_id}\s*$") + tag_begin = re.compile(r"^define tag {$") + tag_end = re.compile(r"^}$") + tag_begin_idx = 0 + while tag_begin_idx < len(lines): + if (tag_begin.match(lines[tag_begin_idx])): + for tag_line_idx in range(tag_begin_idx, len(lines)): + if (tag_name.match(lines[tag_line_idx])): + for end_tag_line in range(tag_line_idx, len(lines)): + if tag_end.match(lines[end_tag_line]): + del lines[tag_begin_idx:end_tag_line + 1] + break + break + elif tag_end.match(lines[tag_line_idx]): + tag_begin_idx = tag_line_idx + break + else: + tag_begin_idx = tag_begin_idx + 1 + + f = open(filename, "w") + f.writelines(lines) + f.close() + def config_engine_add_cfg_file(poller: int, cfg: str):