Skip to content

Commit

Permalink
add robot test
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-christophe81 committed Nov 18, 2024
1 parent 7dbbd77 commit 827fcd9
Show file tree
Hide file tree
Showing 7 changed files with 369 additions and 129 deletions.
188 changes: 97 additions & 91 deletions agent/native_windows/src/check_cpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -410,111 +410,117 @@ check_cpu::check_cpu(const std::shared_ptr<asio::io_context>& io_context,
std::move(handler))

{
com::centreon::common::rapidjson_helper arg(args);
if (args.IsObject()) {
for (auto member_iter = args.MemberBegin(); member_iter != args.MemberEnd();
++member_iter) {
auto cpu_to_status_search = _label_to_cpu_to_status.find(
absl::AsciiStrToLower(member_iter->name.GetString()));
if (cpu_to_status_search != _label_to_cpu_to_status.end()) {
const rapidjson::Value& val = member_iter->value;
if (val.IsFloat() || val.IsInt() || val.IsUint() || val.IsInt64() ||
val.IsUint64()) {
check_cpu_detail::cpu_to_status cpu_checker =
cpu_to_status_search->second(member_iter->value.GetDouble() /
100);
_cpu_to_status.emplace(
std::make_tuple(cpu_checker.get_proc_stat_index(),
cpu_checker.is_average(),
cpu_checker.get_status()),
cpu_checker);
} else if (val.IsString()) {
auto to_conv = val.GetString();
double dval;
if (absl::SimpleAtod(to_conv, &dval)) {
try {
com::centreon::common::rapidjson_helper arg(args);
if (args.IsObject()) {
for (auto member_iter = args.MemberBegin();
member_iter != args.MemberEnd(); ++member_iter) {
auto cpu_to_status_search = _label_to_cpu_to_status.find(
absl::AsciiStrToLower(member_iter->name.GetString()));
if (cpu_to_status_search != _label_to_cpu_to_status.end()) {
const rapidjson::Value& val = member_iter->value;
if (val.IsFloat() || val.IsInt() || val.IsUint() || val.IsInt64() ||
val.IsUint64()) {
check_cpu_detail::cpu_to_status cpu_checker =
cpu_to_status_search->second(dval / 100);
cpu_to_status_search->second(member_iter->value.GetDouble() /
100);
_cpu_to_status.emplace(
std::make_tuple(cpu_checker.get_proc_stat_index(),
cpu_checker.is_average(),
cpu_checker.get_status()),
cpu_checker);
} else if (val.IsString()) {
auto to_conv = val.GetString();
double dval;
if (absl::SimpleAtod(to_conv, &dval)) {
check_cpu_detail::cpu_to_status cpu_checker =
cpu_to_status_search->second(dval / 100);
_cpu_to_status.emplace(
std::make_tuple(cpu_checker.get_proc_stat_index(),
cpu_checker.is_average(),
cpu_checker.get_status()),
cpu_checker);
} else {
SPDLOG_LOGGER_ERROR(
logger,
"command: {}, value is not a number for parameter {}: {}",
cmd_name, member_iter->name, val);
}

} else {
SPDLOG_LOGGER_ERROR(logger,
"command: {}, bad value for parameter {}: {}",
cmd_name, member_iter->name, val);
}
} else if (member_iter->name == "use-nt-query-system-information") {
const rapidjson::Value& val = member_iter->value;
if (val.IsBool()) {
_use_nt_query_system_information = val.GetBool();
} else {
SPDLOG_LOGGER_ERROR(
logger,
"command: {}, value is not a number for parameter {}: {}",
cmd_name, member_iter->name, val);
"command: {}, use-nt-query-system-information is not a boolean",
cmd_name);
}

} else {
SPDLOG_LOGGER_ERROR(logger,
"command: {}, bad value for parameter {}: {}",
cmd_name, member_iter->name, val);
}
} else if (member_iter->name == "use-nt-query-system-information") {
const rapidjson::Value& val = member_iter->value;
if (val.IsBool()) {
_use_nt_query_system_information = val.GetBool();
} else {
SPDLOG_LOGGER_ERROR(
logger,
"command: {}, use-nt-query-system-information is not a boolean",
cmd_name);
} else if (member_iter->name != "cpu-detailed") {
SPDLOG_LOGGER_ERROR(logger, "command: {}, unknown parameter: {}",
cmd_name, member_iter->name);
}
} else if (member_iter->name != "cpu-detailed") {
SPDLOG_LOGGER_ERROR(logger, "command: {}, unknown parameter: {}",
cmd_name, member_iter->name);
}
}
}
if (_use_nt_query_system_information) {
_ntdll_init();
} else {
_pdh_counters = std::make_unique<pdh_counters>();
}
}
catch (const std::exception& e) {
SPDLOG_LOGGER_ERROR(_logger, "check_cpu fail to parse check params: {}",
e.what());
throw;
}

check_cpu::~check_cpu() {}

std::unique_ptr<
check_cpu_detail::cpu_time_snapshot<e_proc_stat_index::nb_field>>
check_cpu::get_cpu_time_snapshot(bool first_measure) {
if (_use_nt_query_system_information) {
return std::make_unique<check_cpu_detail::kernel_cpu_time_snapshot>(
_nb_core);
} else {
return std::make_unique<check_cpu_detail::pdh_cpu_time_snapshot>(
_nb_core, *_pdh_counters, first_measure);
if (_use_nt_query_system_information) {
_ntdll_init();
} else {
_pdh_counters = std::make_unique<pdh_counters>();
}
}
}

constexpr std::array<std::string_view, e_proc_stat_index::nb_field>
_sz_summary_labels = {", User ", ", System ", ", Idle ", ", Interrupt ",
", Dpc Interrupt "};

constexpr std::array<std::string_view, e_proc_stat_index::nb_field>
_sz_perfdata_name = {"user", "system", "idle", "interrupt",
"dpc_interrupt"};
check_cpu::~check_cpu() {}

std::unique_ptr<
check_cpu_detail::cpu_time_snapshot<e_proc_stat_index::nb_field>>
check_cpu::get_cpu_time_snapshot(bool first_measure) {
if (_use_nt_query_system_information) {
return std::make_unique<check_cpu_detail::kernel_cpu_time_snapshot>(
_nb_core);
} else {
return std::make_unique<check_cpu_detail::pdh_cpu_time_snapshot>(
_nb_core, *_pdh_counters, first_measure);
}
}

/**
* @brief compute the difference between second_measure and first_measure and
* generate status, output and perfdatas
*
* @param first_measure first snapshot of /proc/stat
* @param second_measure second snapshot of /proc/stat
* @param output out plugin output
* @param perfs perfdatas
* @return e_status plugin out status
*/
e_status check_cpu::compute(
const check_cpu_detail::cpu_time_snapshot<
check_cpu_detail::e_proc_stat_index::nb_field>& first_measure,
const check_cpu_detail::cpu_time_snapshot<
check_cpu_detail::e_proc_stat_index::nb_field>& second_measure,
std::string* output,
std::list<common::perfdata>* perfs) {
output->reserve(256 * _nb_core);

return _compute(first_measure, second_measure, _sz_summary_labels.data(),
_sz_perfdata_name.data(), output, perfs);
}
constexpr std::array<std::string_view, e_proc_stat_index::nb_field>
_sz_summary_labels = {", User ", ", System ", ", Idle ", ", Interrupt ",
", Dpc Interrupt "};

constexpr std::array<std::string_view, e_proc_stat_index::nb_field>
_sz_perfdata_name = {"user", "system", "idle", "interrupt",
"dpc_interrupt"};

/**
* @brief compute the difference between second_measure and first_measure and
* generate status, output and perfdatas
*
* @param first_measure first snapshot of /proc/stat
* @param second_measure second snapshot of /proc/stat
* @param output out plugin output
* @param perfs perfdatas
* @return e_status plugin out status
*/
e_status check_cpu::compute(
const check_cpu_detail::cpu_time_snapshot<
check_cpu_detail::e_proc_stat_index::nb_field>& first_measure,
const check_cpu_detail::cpu_time_snapshot<
check_cpu_detail::e_proc_stat_index::nb_field>& second_measure,
std::string* output, std::list<common::perfdata>* perfs) {
output->reserve(256 * _nb_core);

return _compute(first_measure, second_measure, _sz_summary_labels.data(),
_sz_perfdata_name.data(), output, perfs);
}
25 changes: 16 additions & 9 deletions agent/native_windows/src/check_uptime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <windows.h>
#include <chrono>
#include <exception>

#include "absl/container/flat_hash_map.h"
#include "check_uptime.hh"
Expand Down Expand Up @@ -66,16 +67,22 @@ check_uptime::check_uptime(const std::shared_ptr<asio::io_context>& io_context,
_second_warning_threshold(0),
_second_critical_threshold(0) {
com::centreon::common::rapidjson_helper arg(args);
if (args.IsObject()) {
_second_warning_threshold = arg.get_unsigned("warning-uptime", 0);
_second_critical_threshold = arg.get_unsigned("critical-uptime", 0);
std::string unit = arg.get_string("unit", "s");
boost::to_lower(unit);
auto multiplier = _unit_multiplier.find(unit);
if (multiplier != _unit_multiplier.end()) {
_second_warning_threshold *= multiplier->second;
_second_critical_threshold *= multiplier->second;
try {
if (args.IsObject()) {
_second_warning_threshold = arg.get_unsigned("warning-uptime", 0);
_second_critical_threshold = arg.get_unsigned("critical-uptime", 0);
std::string unit = arg.get_string("unit", "s");
boost::to_lower(unit);
auto multiplier = _unit_multiplier.find(unit);
if (multiplier != _unit_multiplier.end()) {
_second_warning_threshold *= multiplier->second;
_second_critical_threshold *= multiplier->second;
}
}
} catch (const std::exception& e) {
SPDLOG_LOGGER_ERROR(_logger, "check_uptime, fail to parse arguments: {}",
e.what());
throw;
}
}

Expand Down
6 changes: 4 additions & 2 deletions agent/src/check.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ void check::on_completion(
const std::list<com::centreon::common::perfdata>& perfdata,
const std::list<std::string>& outputs) {
if (start_check_index == _running_check_index) {
SPDLOG_LOGGER_TRACE(_logger, "end check for service {} cmd: {}", _service,
_command_name);
SPDLOG_LOGGER_TRACE(_logger,
"end check for service {} cmd: {} status:{} output: {}",
_service, _command_name, status,
outputs.empty() ? "" : outputs.front());
_time_out_timer.cancel();
_running_check = false;
++_running_check_index;
Expand Down
48 changes: 27 additions & 21 deletions agent/src/drive_size.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,31 +342,37 @@ check_drive_size::check_drive_size(
_critical(0),
_fs_test(&check_drive_size::_no_test) {
using namespace std::literals;
if (args.IsObject()) {
common::rapidjson_helper helper(args);

if (args.HasMember("unit")) {
_prct_threshold = helper.get_string("unit", "%") == "%"sv;
} else {
_prct_threshold = helper.get_string("units", "%") == "%"sv;
}
_free_threshold = helper.get_bool("free", false);
try {
if (args.IsObject()) {
common::rapidjson_helper helper(args);

_warning = helper.get_uint64_t("warning", 0);
_critical = helper.get_uint64_t("critical", 0);
if (_prct_threshold) {
if (_warning || _critical) {
_warning *= 100;
_critical *= 100;
_fs_test = _free_threshold ? &check_drive_size::_prct_free_test
: &check_drive_size::_prct_used_test;
if (args.HasMember("unit")) {
_prct_threshold = helper.get_string("unit", "%") == "%"sv;
} else {
_prct_threshold = helper.get_string("units", "%") == "%"sv;
}
} else {
if (_warning || _critical) {
_fs_test = _free_threshold ? &check_drive_size::_free_test
: &check_drive_size::_used_test;
_free_threshold = helper.get_bool("free", false);

_warning = helper.get_uint64_t("warning", 0);
_critical = helper.get_uint64_t("critical", 0);
if (_prct_threshold) {
if (_warning || _critical) {
_warning *= 100;
_critical *= 100;
_fs_test = _free_threshold ? &check_drive_size::_prct_free_test
: &check_drive_size::_prct_used_test;
}
} else {
if (_warning || _critical) {
_fs_test = _free_threshold ? &check_drive_size::_free_test
: &check_drive_size::_used_test;
}
}
}
} catch (const std::exception& e) {
SPDLOG_LOGGER_ERROR(
_logger, "check_drive_size fail to parse check params: {}", e.what());
throw;
}
}

Expand Down
Loading

0 comments on commit 827fcd9

Please sign in to comment.