Skip to content

Commit

Permalink
Mon 155376 native windows service (#1899) (#1935)
Browse files Browse the repository at this point in the history
* rename memory base to native base, add help on native checks (#1919)

* Mon 155376 native windows service (#1899)

* native_check_service

add robot test

* fix robot tests
  • Loading branch information
jean-christophe81 authored Dec 19, 2024
1 parent 296c2d1 commit 58ffb92
Show file tree
Hide file tree
Showing 35 changed files with 2,601 additions and 365 deletions.
185 changes: 118 additions & 67 deletions .github/scripts/agent_installer_test.ps1

Large diffs are not rendered by default.

45 changes: 27 additions & 18 deletions .github/scripts/agent_robot_test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
Write-Host "Work in" $pwd.ToString()

$current_dir = (pwd).Path
$wsl_path = "/mnt/" + $current_dir.SubString(0,1).ToLower() + "/" + $current_dir.SubString(3).replace('\','/')
$wsl_path = "/mnt/" + $current_dir.SubString(0, 1).ToLower() + "/" + $current_dir.SubString(3).replace('\', '/')

mkdir reports

Expand Down Expand Up @@ -102,29 +102,38 @@ Start-Process -FilePath build_windows\agent\Release\centagent.exe -ArgumentList

$uptime = (Get-WmiObject -Class Win32_OperatingSystem).LastBootUpTime #dtmf format
$d_uptime = [Management.ManagementDateTimeConverter]::ToDateTime($uptime) #datetime format
$ts_uptime = ([DateTimeOffset]$d_uptime).ToUnixTimeSeconds() #timestamp 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'
$snapshot = @{
'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'
}

$serv_list = Get-Service

$serv_stat = @{
'services.running.count' = ($serv_list | Where-Object { $_.Status -eq "Running" } | measure).Count
'services.stopped.count' = ($serv_list | Where-Object { $_.Status -eq "stopped" } | measure).Count
}

$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
'mem_info' = $memory_info}

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

$json_test_param = $test_param | ConvertTo-Json -Compress
'host' = $my_host_name
'ip' = $my_ip
'wsl_path' = $wsl_path
'pwsh_path' = $pwsh_path
'drive' = @()
'current_dir' = $current_dir.replace('\', '/')
'uptime' = $ts_uptime
'mem_info' = $snapshot
'serv_stat' = $serv_stat
}

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

$json_test_param = $test_param | ConvertTo-Json -Compress

Write-Host "json_test_param" $json_test_param
$quoted_json_test_param = "'" + $json_test_param + "'"
Expand Down
1 change: 1 addition & 0 deletions agent/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ set( SRC_WINDOWS
${NATIVE_SRC}/check_uptime.cc
${NATIVE_SRC}/check_drive_size.cc
${NATIVE_SRC}/check_memory.cc
${NATIVE_SRC}/check_service.cc
)

set( SRC_LINUX
Expand Down
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
Loading

0 comments on commit 58ffb92

Please sign in to comment.