Skip to content

Commit

Permalink
rename memory base to native base, add help on native checks (#1919)
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-christophe81 committed Dec 11, 2024
1 parent b8660e9 commit cf59e2b
Show file tree
Hide file tree
Showing 18 changed files with 666 additions and 338 deletions.
185 changes: 118 additions & 67 deletions .github/scripts/agent_installer_test.ps1

Large diffs are not rendered by default.

10 changes: 3 additions & 7 deletions agent/inc/com/centreon/agent/drive_size.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@
#ifndef CENTREON_AGENT_NATIVE_DRIVE_SIZE_BASE_HH
#define CENTREON_AGENT_NATIVE_DRIVE_SIZE_BASE_HH

#include <cstdint>
#include <memory>
#include "absl/base/thread_annotations.h"
#include "absl/container/btree_set.h"
#include "absl/synchronization/mutex.h"
#include "boost/asio/io_context.hpp"
#include "check.hh"
#include "re2/re2.h"

Expand Down Expand Up @@ -270,11 +264,13 @@ class check_drive_size : public check {
check::shared_from_this());
}

static void help(std::ostream& help_stream);

void start_check(const duration& timeout) override;

static void thread_kill();
};

} // namespace com::centreon::agent

#endif // CENTREON_AGENT_NATIVE_DRIVE_SIZE_HH
#endif // CENTREON_AGENT_NATIVE_DRIVE_SIZE_HH
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@

namespace com::centreon::agent {

namespace check_memory_detail {
namespace native_check_detail {

/**
* @brief we store the result of a measure in this struct
*
* @tparam nb_metric
*/
template <unsigned nb_metric>
class memory_info {
class snapshot {
protected:
std::array<uint64_t, nb_metric> _metrics;

public:
virtual ~memory_info() = default;
virtual ~snapshot() = default;

uint64_t get_metric(unsigned data_index) const {
return _metrics[data_index];
Expand All @@ -51,7 +51,7 @@ class memory_info {
return (static_cast<double>(_metrics[data_index]) / total);
}

virtual void dump_to_output(std::string* output, unsigned flags) const = 0;
virtual void dump_to_output(std::string* output) const = 0;
};

/**
Expand All @@ -61,7 +61,7 @@ class memory_info {
* @tparam nb_metric
*/
template <unsigned nb_metric>
class mem_to_status {
class measure_to_status {
e_status _status;
unsigned _data_index;
double _threshold;
Expand All @@ -70,20 +70,22 @@ class mem_to_status {
bool _free_threshold;

public:
mem_to_status(e_status status,
unsigned data_index,
double threshold,
unsigned total_data_index,
bool _percent,
bool free_threshold);
measure_to_status(e_status status,
unsigned data_index,
double threshold,
unsigned total_data_index,
bool _percent,
bool free_threshold);

virtual ~measure_to_status() = default;

unsigned get_data_index() const { return _data_index; }
unsigned get_total_data_index() const { return _total_data_index; }
e_status get_status() const { return _status; }
double get_threshold() const { return _threshold; }

void compute_status(const memory_info<nb_metric>& to_test,
e_status* status) const;
virtual void compute_status(const snapshot<nb_metric>& to_test,
e_status* status) const;
};

/**
Expand All @@ -97,39 +99,34 @@ struct metric_definition {
bool percent;
};

/**
* @brief this must be defined and filled in final OS implementation
*
*/
extern const std::vector<metric_definition> metric_definitions;

} // namespace check_memory_detail
} // namespace native_check_detail

/**
* @brief native check base (to inherit)
*
* @tparam nb_metric
*/
template <unsigned nb_metric>
class check_memory_base : public check {
class native_check_base : public check {
protected:
/**
* @brief key used to store mem_to_status
* @brief key used to store measure_to_status
* @tparam 1 index (phys, virtual..)
* @tparam 2 total index (phys, virtual..)
* @tparam 3 e_status warning or critical
*
*/
using mem_to_status_key = std::tuple<unsigned, unsigned, e_status>;

boost::container::flat_map<mem_to_status_key,
check_memory_detail::mem_to_status<nb_metric>>
_mem_to_status;
boost::container::flat_map<
mem_to_status_key,
std::unique_ptr<native_check_detail::measure_to_status<nb_metric>>>
_measure_to_status;

unsigned _output_flags = 0;
const char* _no_percent_unit = nullptr;

public:
check_memory_base(const std::shared_ptr<asio::io_context>& io_context,
native_check_base(const std::shared_ptr<asio::io_context>& io_context,
const std::shared_ptr<spdlog::logger>& logger,
time_point first_start_expected,
duration check_interval,
Expand All @@ -140,19 +137,22 @@ class check_memory_base : public check {
const engine_to_agent_request_ptr& cnf,
check::completion_handler&& handler);

std::shared_ptr<check_memory_base<nb_metric>> shared_from_this() {
return std::static_pointer_cast<check_memory_base<nb_metric>>(
std::shared_ptr<native_check_base<nb_metric>> shared_from_this() {
return std::static_pointer_cast<native_check_base<nb_metric>>(
check::shared_from_this());
}

void start_check(const duration& timeout) override;

virtual std::shared_ptr<check_memory_detail::memory_info<nb_metric>> measure()
const = 0;
virtual std::shared_ptr<native_check_detail::snapshot<nb_metric>>
measure() = 0;

e_status compute(const check_memory_detail::memory_info<nb_metric>& data,
e_status compute(const native_check_detail::snapshot<nb_metric>& data,
std::string* output,
std::list<common::perfdata>* perfs) const;

virtual const std::vector<native_check_detail::metric_definition>&
get_metric_definitions() const = 0;
};

} // namespace com::centreon::agent
Expand Down
5 changes: 5 additions & 0 deletions agent/installer/silent.nsi
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ Function cmd_line_to_registry
Call silent_fatal_error
${EndIf}
WriteRegStr HKLM ${CMA_REG_KEY} "host" "$0"
${If} ${Errors}
StrCpy $1 "Failed to write registry key for host"
Call silent_fatal_error
${EndIf}

ClearErrors
${GetOptions} $cmdline_parameters "--endpoint" $0
${If} ${Errors}
Expand Down
30 changes: 30 additions & 0 deletions agent/native_linux/src/check_cpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,33 @@ e_status check_cpu::compute(
return _compute(first_measure, second_measure, _sz_summary_labels.data(),
_sz_perfdata_name.data(), output, perfs);
}

void check_cpu::help(std::ostream& help_stream) {
help_stream << R"(
- cpu params:
warning-core: threshold for warning status on core usage in percentage
critical-core: threshold for critical status on core usage in percentage
warning-average: threshold for warning status on average usage in percentage
critical-average: threshold for critical status on average usage in percentage
warning-core-user: threshold for warning status on core user usage in percentage
critical-core-user: threshold for critical status on core user usage in percentage
warning-average-user: threshold for warning status on average user usage in percentage
critical-average-user: threshold for critical status on average user usage in percentage
warning-core-nice: threshold for warning status on core nice usage in percentage
critical-core-nice: threshold for critical status on core nice usage in percentage
warning-average-nice: threshold for warning status on average nice usage in percentage
critical-average-nice: threshold for critical status on average nice usage in percentage
warning-core-system: threshold for warning status on core system usage in percentage
critical-core-system: threshold for critical status on core system usage in percentage
warning-average-system: threshold for warning status on average system usage in percentage
critical-average-system: threshold for critical status on average system usage in percentage
warning-core-iowait: threshold for warning status on core iowait usage in percentage
critical-core-iowait: threshold for critical status on core iowait usage in percentage
warning-average-iowait: threshold for warning status on average iowait usage in percentage
critical-average-iowait: threshold for critical status on average iowait usage in percentage
warning-core-guest: threshold for warning status on core guest usage in percentage
critical-core-guest: threshold for critical status on core guest usage in percentage
warning-average-guest: threshold for warning status on average guest usage in percentage
critical-average-guest: threshold for critical status on average guest usage in percentage
)";
}
37 changes: 24 additions & 13 deletions agent/native_windows/inc/com/centreon/agent/check_memory.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
#ifndef CENTREON_AGENT_NATIVE_CHECK_MEMORY_HH
#define CENTREON_AGENT_NATIVE_CHECK_MEMORY_HH

#include "native_check_memory_base.hh"
#include "native_check_base.hh"

struct _PERFORMANCE_INFORMATION;

namespace com::centreon::agent {
namespace check_memory_detail {
namespace native_check_detail {

enum e_metric : unsigned {
enum e_memory_metric : unsigned {
phys_total,
phys_free,
phys_used,
Expand All @@ -45,27 +45,33 @@ enum e_metric : unsigned {
*
*/
class w_memory_info
: public memory_info<check_memory_detail::e_metric::nb_metric> {
: public snapshot<native_check_detail::e_memory_metric::nb_metric> {
unsigned _output_flags = 0;

public:
enum output_flags : unsigned { dump_swap = 1, dump_virtual };

w_memory_info();
w_memory_info(unsigned flags);
w_memory_info(const MEMORYSTATUSEX& mem_status,
const struct _PERFORMANCE_INFORMATION& perf_mem_status);
const struct _PERFORMANCE_INFORMATION& perf_mem_status,
unsigned flags = 0);
void init(const MEMORYSTATUSEX& mem_status,
const struct _PERFORMANCE_INFORMATION& perf_mem_status);

void dump_to_output(std::string* output, unsigned flags) const override;
void dump_to_output(std::string* output) const override;
};

} // namespace check_memory_detail
} // namespace native_check_detail

/**
* @brief native final check object
*
*/
class check_memory
: public check_memory_base<check_memory_detail::e_metric::nb_metric> {
class check_memory : public native_check_base<
native_check_detail::e_memory_metric::nb_metric> {
protected:
unsigned _output_flags = 0;

public:
check_memory(const std::shared_ptr<asio::io_context>& io_context,
const std::shared_ptr<spdlog::logger>& logger,
Expand All @@ -78,9 +84,14 @@ class check_memory
const engine_to_agent_request_ptr& cnf,
check::completion_handler&& handler);

std::shared_ptr<check_memory_detail::memory_info<
check_memory_detail::e_metric::nb_metric>>
measure() const override;
std::shared_ptr<native_check_detail::snapshot<
native_check_detail::e_memory_metric::nb_metric>>
measure() override;

static void help(std::ostream& help_stream);

const std::vector<native_check_detail::metric_definition>&
get_metric_definitions() const override;
};

} // namespace com::centreon::agent
Expand Down
50 changes: 50 additions & 0 deletions agent/native_windows/src/check_cpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -525,3 +525,53 @@ e_status check_cpu::compute(
return _compute(first_measure, second_measure, _sz_summary_labels.data(),
_sz_perfdata_name.data(), output, perfs);
}

void check_cpu::help(std::ostream& help_stream) {
help_stream << R"(
- cpu params:
use-nt-query-system-information (default true): true: use NtQuerySystemInformation instead of performance counters
cpu-detailed (default false): true: add detailed cpu usage metrics
warning-core: threshold for warning status on core usage in percentage
critical-core: threshold for critical status on core usage in percentage
warning-average: threshold for warning status on average usage in percentage
critical-average: threshold for critical status on average usage in percentage
warning-core-user: threshold for warning status on core user usage in percentage
critical-core-user: threshold for critical status on core user usage in percentage
warning-average-user: threshold for warning status on average user usage in percentage
critical-average-user: threshold for critical status on average user usage in percentage
warning-core-system: threshold for warning status on core system usage in percentage
critical-core-system: threshold for critical status on core system usage in percentage
warning-average-system: threshold for warning status on average system usage in percentage
critical-average-system: threshold for critical status on average system usage in percentage
An example of configuration:
{
"check": "cpu_percentage",
"args": {
"cpu-detailed": true,
"warning-core": 80,
"critical-core": 90,
"warning-average": 60,
"critical-average": 70
}
}
Examples of output:
OK: CPU(s) average usage is 50.00%
WARNING: CPU'0' Usage: 40.00%, User 25.00%, System 10.00%, Idle 60.00%, Interrupt 5.00%, Dpc Interrupt 1.00% CRITICAL: CPU'1' Usage: 60.00%, User 45.00%, System 10.00%, Idle 40.00%, Interrupt 5.00%, Dpc Interrupt 0.00% WARNING: CPU(s) average Usage: 50.00%, User 35.00%, System 10.00%, Idle 50.00%, Interrupt 5.00%, Dpc Interrupt 0.50%
Metrics:
Normal mode:
<core index>#core.cpu.utilization.percentage
cpu.utilization.percentage
cpu-detailed mode:
<core index>~user#core.cpu.utilization.percentage
<core index>~system#core.cpu.utilization.percentage
<core index>~idle#core.cpu.utilization.percentage
<core index>~interrupt#core.cpu.utilization.percentage
<core index>~dpc_interrupt#core.cpu.utilization.percentage
<code index>~used#core.cpu.utilization.percentage
user#cpu.utilization.percentage
system#cpu.utilization.percentage
idle#cpu.utilization.percentage
interrupt#cpu.utilization.percentage
dpc_interrupt#cpu.utilization.percentage
)";
}
6 changes: 4 additions & 2 deletions agent/native_windows/src/check_drive_size.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ static e_drive_fs_type get_fs_type(
fs_type = e_drive_fs_type::hr_storage_ram_disk;
break;
default:
fs_type = e_drive_fs_type::hr_unknown;
SPDLOG_LOGGER_ERROR(logger, "{} unknown drive type {}", fs_root,
drive_type);
break;
Expand All @@ -78,6 +79,7 @@ static e_drive_fs_type get_fs_type(
if (fs_search != _sz_filesystem_map.end()) {
fs_type |= fs_search->second;
} else {
fs_type |= e_drive_fs_type::hr_fs_unknown;
SPDLOG_LOGGER_ERROR(logger, "{} unknown file system type {}", fs_root,
file_system_name);
}
Expand Down Expand Up @@ -130,8 +132,8 @@ std::list<fs_stat> os_fs_stats(filter& filt,

if (success) {
SPDLOG_LOGGER_TRACE(logger, "{} total: {}, free {}", fs_to_test,
total_number_of_bytes.QuadPart ,
total_number_of_free_bytes.QuadPart);
total_number_of_bytes.QuadPart,
total_number_of_free_bytes.QuadPart);

result.emplace_back(std::move(fs_to_test),
total_number_of_bytes.QuadPart -
Expand Down
Loading

0 comments on commit cf59e2b

Please sign in to comment.