Skip to content

Commit

Permalink
Mon 154129 native windows memory check (#1847) backport 24.10 (#1908)
Browse files Browse the repository at this point in the history
* Mon 147916 native windows memory check (#1847)

* implement native windows agent check

* review comments

* fix test

* fix robot test
  • Loading branch information
jean-christophe81 authored Nov 29, 2024
1 parent 5b47495 commit b8660e9
Show file tree
Hide file tree
Showing 18 changed files with 1,354 additions and 36 deletions.
6 changes: 5 additions & 1 deletion .github/scripts/agent_installer_test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ function test_args_to_registry {

for (($i = 0); $i -lt 10; $i++) {
Start-Sleep -Seconds 1
if (Get-ItemProperty -Path HKLM:\Software\Centreon\CentreonMonitoringAgent) {
try {
Get-ItemProperty -Path HKLM:\Software\Centreon\CentreonMonitoringAgent
break
}
catch {
continue
}
}

foreach ($value_name in $expected_registry_values.Keys) {
Expand Down
12 changes: 10 additions & 2 deletions .github/scripts/agent_robot_test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,23 @@ $uptime = (Get-WmiObject -Class Win32_OperatingSystem).LastBootUpTime #dtmf form
$d_uptime = [Management.ManagementDateTimeConverter]::ToDateTime($uptime) #datetime format
$ts_uptime = ([DateTimeOffset]$d_uptime).ToUnixTimeSeconds() #timestamp format

$systeminfo_data = systeminfo /FO CSV | ConvertFrom-Csv
$memory_info = @{
'total' = $systeminfo_data.'Total Physical Memory'
'free' = $systeminfo_data.'Available Physical Memory'
'virtual_max' = $systeminfo_data.'Virtual Memory: Max Size'
'virtual_free' = $systeminfo_data.'Virtual Memory: Available'
}

$test_param = @{
'host'= $my_host_name
'ip'= $my_ip
'wsl_path'= $wsl_path
'pwsh_path'= $pwsh_path
'drive' = @()
'current_dir' = $current_dir.replace('\','/')
'uptime' = $ts_uptime
}
'uptime' = $ts_uptime
'mem_info' = $memory_info}

Get-PSDrive -PSProvider FileSystem | Select Name, Used, Free | ForEach-Object -Process {$test_param.drive += $_}

Expand Down
1 change: 1 addition & 0 deletions agent/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ set( SRC_WINDOWS
${SRC_DIR}/config_win.cc
${NATIVE_SRC}/check_uptime.cc
${NATIVE_SRC}/check_drive_size.cc
${NATIVE_SRC}/check_memory.cc
)

set( SRC_LINUX
Expand Down
2 changes: 2 additions & 0 deletions agent/inc/com/centreon/agent/check.hh
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ class check : public std::enable_shared_from_this<check> {
public:
using pointer = std::shared_ptr<check>;

static const std::array<std::string_view, 4> status_label;

check(const std::shared_ptr<asio::io_context>& io_context,
const std::shared_ptr<spdlog::logger>& logger,
time_point first_start_expected,
Expand Down
160 changes: 160 additions & 0 deletions agent/inc/com/centreon/agent/native_check_memory_base.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/**
* 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_NATIVE_CHECK_MEMORY_BASE_HH
#define CENTREON_AGENT_NATIVE_CHECK_MEMORY_BASE_HH

#include "check.hh"

namespace com::centreon::agent {

namespace check_memory_detail {

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

public:
virtual ~memory_info() = default;

uint64_t get_metric(unsigned data_index) const {
return _metrics[data_index];
}

double get_proportional_value(unsigned data_index,
unsigned total_data_index) const {
const uint64_t& total = _metrics[total_data_index];
if (!total) {
return 0.0;
}
return (static_cast<double>(_metrics[data_index]) / total);
}

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

/**
* @brief this class compare a measure with threshold and returns a plugins
* status
*
* @tparam nb_metric
*/
template <unsigned nb_metric>
class mem_to_status {
e_status _status;
unsigned _data_index;
double _threshold;
unsigned _total_data_index;
bool _percent;
bool _free_threshold;

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

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;
};

/**
* @brief this struct will be used to create metrics
*
*/
struct metric_definition {
std::string_view name;
unsigned data_index;
unsigned total_data_index;
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

/**
* @brief native check base (to inherit)
*
* @tparam nb_metric
*/
template <unsigned nb_metric>
class check_memory_base : public check {
protected:
/**
* @brief key used to store mem_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;

unsigned _output_flags = 0;

public:
check_memory_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,
const std::string& serv,
const std::string& cmd_name,
const std::string& cmd_line,
const rapidjson::Value& args,
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>>(
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;

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

} // namespace com::centreon::agent

#endif
88 changes: 88 additions & 0 deletions agent/native_windows/inc/com/centreon/agent/check_memory.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* 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_NATIVE_CHECK_MEMORY_HH
#define CENTREON_AGENT_NATIVE_CHECK_MEMORY_HH

#include "native_check_memory_base.hh"

struct _PERFORMANCE_INFORMATION;

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

enum e_metric : unsigned {
phys_total,
phys_free,
phys_used,
swap_total,
swap_free,
swap_used,
virtual_total,
virtual_free,
virtual_used,
nb_metric
};

/**
* @brief this class compute a measure of memory metrics and store in _metrics
* member
*
*/
class w_memory_info
: public memory_info<check_memory_detail::e_metric::nb_metric> {
public:
enum output_flags : unsigned { dump_swap = 1, dump_virtual };

w_memory_info();
w_memory_info(const MEMORYSTATUSEX& mem_status,
const struct _PERFORMANCE_INFORMATION& perf_mem_status);
void init(const MEMORYSTATUSEX& mem_status,
const struct _PERFORMANCE_INFORMATION& perf_mem_status);

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

} // namespace check_memory_detail

/**
* @brief native final check object
*
*/
class check_memory
: public check_memory_base<check_memory_detail::e_metric::nb_metric> {
public:
check_memory(const std::shared_ptr<asio::io_context>& io_context,
const std::shared_ptr<spdlog::logger>& logger,
time_point first_start_expected,
duration check_interval,
const std::string& serv,
const std::string& cmd_name,
const std::string& cmd_line,
const rapidjson::Value& args,
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;
};

} // namespace com::centreon::agent

#endif
Loading

0 comments on commit b8660e9

Please sign in to comment.