Skip to content

Commit

Permalink
uniq
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-christophe81 committed Feb 21, 2025
1 parent 685600e commit 960ded5
Show file tree
Hide file tree
Showing 9 changed files with 487 additions and 279 deletions.
2 changes: 2 additions & 0 deletions agent/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ set( SRC_WINDOWS
${NATIVE_SRC}/check_uptime.cc
${NATIVE_SRC}/check_drive_size.cc
${NATIVE_SRC}/check_event_log.cc
${NATIVE_SRC}/check_event_log_container.cc
${NATIVE_SRC}/check_event_log_data.cc
${NATIVE_SRC}/check_event_log_uniq.cc
${NATIVE_SRC}/check_memory.cc
${NATIVE_SRC}/check_service.cc
${NATIVE_SRC}/windows_util.cc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* 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 : [email protected]
*/

#ifndef CENTREON_AGENT_CHECK_EVENT_LOG_CONTAINER_HH
#define CENTREON_AGENT_CHECK_EVENT_LOG_CONTAINER_HH

#include "check_event_log_data.hh"

namespace com::centreon::agent::check_event_log_detail {

class event_container {
public:
using event_set = boost::multi_index::multi_index_container<
event,
boost::multi_index::indexed_by<
boost::multi_index::ordered_non_unique<
BOOST_MULTI_INDEX_CONST_MEM_FUN(
event,
std::chrono::file_clock::time_point,
time)>,
boost::multi_index::ordered_non_unique<
BOOST_MULTI_INDEX_CONST_MEM_FUN(event, e_status, status)>>>;

private:
duration _scan_range;

std::wstring _file;
std::unique_ptr<event_filter> _primary_filter;
std::unique_ptr<event_filter> _warning_filter;
std::unique_ptr<event_filter> _critical_filter;

event_set _events ABSL_GUARDED_BY(_events_m);
unsigned _insertion_cpt ABSL_GUARDED_BY(_events_m);
unsigned _nb_warning ABSL_GUARDED_BY(_events_m);
unsigned _nb_critical ABSL_GUARDED_BY(_events_m);
absl::Mutex _events_m;

EVT_HANDLE _render_context;
EVT_HANDLE _subscription;

using provider_metadata = absl::flat_hash_map<std::wstring, EVT_HANDLE>;
provider_metadata _provider_metadata ABSL_GUARDED_BY(_events_m);

void* _read_event_buffer ABSL_GUARDED_BY(_events_m);
DWORD _buffer_size ABSL_GUARDED_BY(_events_m);

bool _need_to_decode_message_content;
LPWSTR _read_message_buffer ABSL_GUARDED_BY(_events_m);
DWORD _message_buffer_size ABSL_GUARDED_BY(_events_m); // size in wchar_t

std::shared_ptr<spdlog::logger> _logger;

static DWORD WINAPI _subscription_callback(EVT_SUBSCRIBE_NOTIFY_ACTION action,
PVOID p_context,
EVT_HANDLE h_event);

void _on_event(EVT_HANDLE event_handle);

LPWSTR _get_message_string(EVT_HANDLE h_metadata, EVT_HANDLE h_event);

public:
event_container(const std::string_view& file,
const std::string_view& primary_filter,
const std::string_view& warning_filter,
const std::string_view& critical_filter,
duration scan_range,
const std::shared_ptr<spdlog::logger>& logger);
void start();

~event_container();

void lock() { _events_m.Lock(); }
void unlock() { _events_m.Unlock(); }

const event_set& get_events() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(_events_m) {
return _events;
}
};

} // namespace com::centreon::agent::check_event_log_detail

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@

#include <boost/flyweight.hpp>

#include "boost/multi_index/indexed_by.hpp"
#include "boost/multi_index/ordered_index.hpp"
#include "boost/multi_index_container.hpp"
#include "check.hh"
#include "filter.hh"

Expand Down Expand Up @@ -169,75 +166,6 @@ class event {

std::ostream& operator<<(std::ostream& s, const event& evt);

class event_container {
public:
using event_set = boost::multi_index::multi_index_container<
event,
boost::multi_index::indexed_by<
boost::multi_index::ordered_non_unique<
BOOST_MULTI_INDEX_CONST_MEM_FUN(
event,
std::chrono::file_clock::time_point,
time)>,
boost::multi_index::ordered_non_unique<
BOOST_MULTI_INDEX_CONST_MEM_FUN(event, e_status, status)>>>;

private:
duration _scan_range;

std::wstring _file;
std::unique_ptr<event_filter> _primary_filter;
std::unique_ptr<event_filter> _warning_filter;
std::unique_ptr<event_filter> _critical_filter;

event_set _events ABSL_GUARDED_BY(_events_m);
unsigned _insertion_cpt ABSL_GUARDED_BY(_events_m);
unsigned _nb_warning ABSL_GUARDED_BY(_events_m);
unsigned _nb_critical ABSL_GUARDED_BY(_events_m);
absl::Mutex _events_m;

EVT_HANDLE _render_context;
EVT_HANDLE _subscription;

using provider_metadata = absl::flat_hash_map<std::wstring, EVT_HANDLE>;
provider_metadata _provider_metadata ABSL_GUARDED_BY(_events_m);

void* _read_event_buffer ABSL_GUARDED_BY(_events_m);
DWORD _buffer_size ABSL_GUARDED_BY(_events_m);

bool _need_to_decode_message_content;
LPWSTR _read_message_buffer ABSL_GUARDED_BY(_events_m);
DWORD _message_buffer_size ABSL_GUARDED_BY(_events_m); // size in wchar_t

std::shared_ptr<spdlog::logger> _logger;

static DWORD WINAPI _subscription_callback(EVT_SUBSCRIBE_NOTIFY_ACTION action,
PVOID p_context,
EVT_HANDLE h_event);

void _on_event(EVT_HANDLE event_handle);

LPWSTR _get_message_string(EVT_HANDLE h_metadata, EVT_HANDLE h_event);

public:
event_container(const std::string_view& file,
const std::string_view& primary_filter,
const std::string_view& warning_filter,
const std::string_view& critical_filter,
duration scan_range,
const std::shared_ptr<spdlog::logger>& logger);
void start();

~event_container();

void lock() { _events_m.Lock(); }
void unlock() { _events_m.Unlock(); }

const event_set& get_events() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(_events_m) {
return _events;
}
};

} // namespace com::centreon::agent::check_event_log_detail

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* 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 : [email protected]
*/

#ifndef CENTREON_AGENT_CHECK_EVENT_LOG_UNIQ_HH
#define CENTREON_AGENT_CHECK_EVENT_LOG_UNIQ_HH

#include "check_event_log_data.hh"

namespace com::centreon::agent::check_event_log_detail {

class event_comparator {
using field_event_hasher = std::function<size_t(const event&)>;
using field_event_compare = std::function<bool(const event&, const event&)>;

std::vector<field_event_hasher> _hash;
std::vector<field_event_compare> _compare;

public:
event_comparator(std::string_view fields,
const std::shared_ptr<spdlog::logger>& logger);

bool operator()(const event* left, const event* right) const;
std::size_t operator()(const event* evt) const;
};

class unique_event {
event_comparator _comparator;

public:
unique_event(const std::string_view& fields,
const std::shared_ptr<spdlog::logger>& logger);

template <class event_iter, typename out_iter>
void unique(event_iter begin,
event_iter end,
out_iter&& warning_inserter,
out_iter&& critical_inserter);
};

template <class event_iter, typename out_iter>
void unique_event::unique(event_iter begin,
event_iter end,
out_iter&& warning_inserter,
out_iter&& critical_inserter) {
absl::flat_hash_set<const event*, event_comparator, event_comparator> warning(
0, _comparator, _comparator);
absl::flat_hash_set<const event*, event_comparator, event_comparator>
critical(0, _comparator, _comparator);

for (; begin != end; ++begin) {
if (begin->status() == e_status::warning) {
if (warning.insert(&*begin).second) {
warning_inserter(*begin);
}
} else {
if (critical.insert(&*begin).second) {
critical_inserter(*begin);
}
}
}
}

} // namespace com::centreon::agent::check_event_log_detail

#endif
Loading

0 comments on commit 960ded5

Please sign in to comment.