Skip to content

Commit

Permalink
Mon 145030 cma ceip (#1950)
Browse files Browse the repository at this point in the history
feat(agent, engine, broker): CEIP collects statistics abour agent usage
fix(test): agent tests
* add os version to agent info
  • Loading branch information
jean-christophe81 authored Jan 8, 2025
1 parent 8f6f413 commit 294b720
Show file tree
Hide file tree
Showing 51 changed files with 975 additions and 147 deletions.
5 changes: 4 additions & 1 deletion agent/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ set(NATIVE_INC "${PROJECT_SOURCE_DIR}/${NATIVE_DIR}/inc/com/centreon/agent")
set(NATIVE_SRC "${PROJECT_SOURCE_DIR}/${NATIVE_DIR}/src")

set( SRC_COMMON
${NATIVE_SRC}/agent_info.cc
${NATIVE_SRC}/check_cpu.cc
${SRC_DIR}/agent.grpc.pb.cc
${SRC_DIR}/agent.pb.cc
Expand All @@ -131,6 +132,7 @@ set( SRC_WINDOWS
${NATIVE_SRC}/check_drive_size.cc
${NATIVE_SRC}/check_memory.cc
${NATIVE_SRC}/check_service.cc
${NATIVE_SRC}/ntdll.cc
)

set( SRC_LINUX
Expand Down Expand Up @@ -195,7 +197,8 @@ else()
gRPC::gpr gRPC::grpc gRPC::grpc++ gRPC::grpc++_alts
absl::any absl::log absl::base absl::bits
Boost::program_options
fmt::fmt)
fmt::fmt
pdh)

if(WITH_BUILD_AGENT_INSTALLER OR WITH_BUILD_AGENT_MODIFIER)
add_subdirectory(installer)
Expand Down
31 changes: 31 additions & 0 deletions agent/inc/com/centreon/agent/agent_info.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* 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_AGENT_INFO_HH
#define CENTREON_AGENT_AGENT_INFO_HH

#include "agent.pb.h"

namespace com::centreon::agent {

void read_os_version();

void fill_agent_info(const std::string& supervised_host,
::com::centreon::agent::AgentInfo* agent_info);
} // namespace com::centreon::agent
#endif
74 changes: 74 additions & 0 deletions agent/native_linux/src/agent_info.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* 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]
*/

#include "agent_info.hh"
#include "version.hh"

static std::string _os;
static std::string _os_version;

/**
* @brief read os version
* to call at the beginning of program
*
*/
void com::centreon::agent::read_os_version() {
std::fstream os_release("/etc/os-release", std::fstream::in);
if (os_release.is_open()) {
enum { os_found = 1, version_found = 2, all_found = 3 };
unsigned found = 0;
std::string line;
while (std::getline(os_release, line) && found != all_found) {
if (!line.compare(0, 3, "ID=")) {
line.erase(0, 3);
boost::algorithm::trim_if(line, [](const char c) {
return c == '"' || c == ' ' || c == '\'';
});
_os = line;
found |= os_found;
} else if (!line.compare(0, 11, "VERSION_ID=")) {
line.erase(0, 11);
boost::algorithm::trim_if(line, [](const char c) {
return c == '"' || c == ' ' || c == '\'';
});
_os_version = line;
found |= version_found;
}
}
}
}

/**
* @brief fill agent_info with agent and os versions
*
* @param supervised_host host configured
* @param agent_info pointer to object to fill
*/
void com::centreon::agent::fill_agent_info(
const std::string& supervised_host,
::com::centreon::agent::AgentInfo* agent_info) {
agent_info->mutable_centreon_version()->set_major(
CENTREON_AGENT_VERSION_MAJOR);
agent_info->mutable_centreon_version()->set_minor(
CENTREON_AGENT_VERSION_MINOR);
agent_info->mutable_centreon_version()->set_patch(
CENTREON_AGENT_VERSION_PATCH);
agent_info->set_host(supervised_host);
agent_info->set_os(_os);
agent_info->set_os_version(_os_version);
}
13 changes: 2 additions & 11 deletions agent/native_windows/inc/com/centreon/agent/check_cpu.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,15 @@
#ifndef CENTREON_AGENT_CHECK_CPU_HH
#define CENTREON_AGENT_CHECK_CPU_HH

#include "ntdll.hh"

#include "native_check_cpu_base.hh"

namespace com::centreon::agent {

namespace check_cpu_detail {
enum e_proc_stat_index { user = 0, system, idle, interrupt, dpc, nb_field };

/**As winternl.h may be included, we define our own
* SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION */
struct M_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION {
LARGE_INTEGER IdleTime;
LARGE_INTEGER KernelTime;
LARGE_INTEGER UserTime;
LARGE_INTEGER DpcTime;
LARGE_INTEGER InterruptTime;
ULONG InterruptCount;
};

/**
* @brief this class contains all counter for one core contained in a
* M_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION structure
Expand Down
51 changes: 51 additions & 0 deletions agent/native_windows/inc/com/centreon/agent/ntdll.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* 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_NTDLL_HH
#define CENTREON_AGENT_NTDLL_HH

namespace com::centreon::agent {

/**As winternl.h may be included, we define our own
* SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION */
struct M_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION {
LARGE_INTEGER IdleTime;
LARGE_INTEGER KernelTime;
LARGE_INTEGER UserTime;
LARGE_INTEGER DpcTime;
LARGE_INTEGER InterruptTime;
ULONG InterruptCount;
};

void load_nt_dll();

typedef LONG(WINAPI* NtQuerySystemInformationPtr)(ULONG SystemInformationClass,
PVOID SystemInformation,
ULONG SystemInformationLength,
PULONG ReturnLength);

extern NtQuerySystemInformationPtr nt_query_system_information;

typedef NTSTATUS(NTAPI* RtlGetVersionPtr)(
POSVERSIONINFOEXW lpVersionInformation);

extern RtlGetVersionPtr rtl_get_version;

} // namespace com::centreon::agent

#endif
61 changes: 61 additions & 0 deletions agent/native_windows/src/agent_info.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* 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]
*/

#include "agent_info.hh"
#include "ntdll.hh"
#include "version.hh"

static std::string _os;
static std::string _os_version;

/**
* @brief read os version
* to call at the beginning of program
*
*/
void com::centreon::agent::read_os_version() {
RTL_OSVERSIONINFOEXW osvi;
ZeroMemory(&osvi, sizeof(osvi));
osvi.dwOSVersionInfoSize = sizeof(osvi);
if (rtl_get_version(&osvi) == 0) {
_os = osvi.wProductType == VER_NT_SERVER ? "windows-server" : "windows";
_os_version = std::to_string(osvi.dwMajorVersion) + "." +
std::to_string(osvi.dwMinorVersion) + "." +
std::to_string(osvi.dwBuildNumber);
}
}

/**
* @brief fill agent_info with agent and os versions
*
* @param supervised_host host configured
* @param agent_info pointer to object to fill
*/
void com::centreon::agent::fill_agent_info(
const std::string& supervised_host,
::com::centreon::agent::AgentInfo* agent_info) {
agent_info->mutable_centreon_version()->set_major(
CENTREON_AGENT_VERSION_MAJOR);
agent_info->mutable_centreon_version()->set_minor(
CENTREON_AGENT_VERSION_MINOR);
agent_info->mutable_centreon_version()->set_patch(
CENTREON_AGENT_VERSION_PATCH);
agent_info->set_host(supervised_host);
agent_info->set_os(_os);
agent_info->set_os_version(_os_version);
}
55 changes: 7 additions & 48 deletions agent/native_windows/src/check_cpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,17 @@
#include <pdhmsg.h>

#include "check_cpu.hh"
#include "com/centreon/common/rapidjson_helper.hh"
#include "com/centreon/exceptions/msg_fmt.hh"
#include "native_check_cpu_base.cc"

#pragma comment(lib, "pdh.lib")

using namespace com::centreon::agent;
using namespace com::centreon::agent::check_cpu_detail;

/**************************************************************************
Kernel measure method
***************************************************************************/

namespace com::centreon::agent::check_cpu_detail {

// ntdll.dll handle
static HMODULE _ntdll = nullptr;

typedef LONG(WINAPI* NtQuerySystemInformationPtr)(ULONG SystemInformationClass,
PVOID SystemInformation,
ULONG SystemInformationLength,
PULONG ReturnLength);

// NtQuerySystemInformation function address
static NtQuerySystemInformationPtr _nt_query_system_information = nullptr;

constexpr ULONG SystemProcessorPerformanceInformationClass = 8;

} // namespace com::centreon::agent::check_cpu_detail

/**
* @brief Construct a kernel_per_cpu_time object from a
* SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
Expand All @@ -72,29 +55,6 @@ kernel_per_cpu_time::kernel_per_cpu_time(
_total_used = _total - _metrics[e_proc_stat_index::idle];
}

/**
* @brief load ntdll.dll and get NtQuerySystemInformation address
*
*/
static void _ntdll_init() {
if (!_ntdll) {
_ntdll = LoadLibraryA("ntdll.dll");
if (!_ntdll) {
throw std::runtime_error("Failed to load ntdll.dll");
}
}

if (!_nt_query_system_information)
// Obtenir le pointeur de fonction NtQuerySystemInformation
_nt_query_system_information = (NtQuerySystemInformationPtr)GetProcAddress(
_ntdll, "NtQuerySystemInformation");
if (!_nt_query_system_information) {
FreeLibrary(_ntdll);
throw std::runtime_error(
"Failed to get address of NtQuerySystemInformation");
}
}

/**
* @brief Construct a new kernel cpu time snapshot::kernel cpu time snapshot
* object it loads alls CPUs time and compute the average
Expand All @@ -110,9 +70,10 @@ kernel_cpu_time_snapshot::kernel_cpu_time_snapshot(unsigned nb_core) {

memset(buffer.get(), 0, buffer_size);

if (_nt_query_system_information(SystemProcessorPerformanceInformationClass,
buffer.get(), buffer_size,
&return_length) != 0) {
if (nt_query_system_information(
8 /*SystemProcessorPerformanceInformationClass*/
,
buffer.get(), buffer_size, &return_length) != 0) {
throw std::runtime_error("Failed to get kernel cpu time");
}

Expand Down Expand Up @@ -447,9 +408,7 @@ check_cpu::check_cpu(const std::shared_ptr<asio::io_context>& io_context,
throw;
}

if (_use_nt_query_system_information) {
_ntdll_init();
} else {
if (!_use_nt_query_system_information) {
_pdh_counters = std::make_unique<pdh_counters>();
}
}
Expand Down
7 changes: 4 additions & 3 deletions agent/native_windows/src/check_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -546,14 +546,15 @@ check_service::check_service(
stat),
_filter(args),
_enumerator(_enumerator_constructor()) {
if (!args.IsObject()) {
return;
}
_measure_to_status.emplace(
std::make_tuple(e_service_metric::nb_service_metric,
e_service_metric::nb_service_metric, e_status::ok),
std::make_unique<w_service_info_to_status>());

if (!args.IsObject()) {
return;
}

for (auto member_iter = args.MemberBegin(); member_iter != args.MemberEnd();
++member_iter) {
std::string key = absl::AsciiStrToLower(member_iter->name.GetString());
Expand Down
Loading

0 comments on commit 294b720

Please sign in to comment.