Skip to content

Commit

Permalink
telegraf conf accepts only one host
Browse files Browse the repository at this point in the history
add scheduler, check and check_exec class to agent
  • Loading branch information
jean-christophe81 committed Jun 25, 2024
1 parent 7d96154 commit 40a0b6c
Show file tree
Hide file tree
Showing 21 changed files with 2,223 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ report.html

# agent
agent/scripts/centagent.service
agent/conf/centagent.json
opentelemetry-proto

# bbdo
Expand Down
6 changes: 6 additions & 0 deletions agent/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,17 @@ add_custom_command(


add_library(centagent_lib STATIC
${SRC_DIR}/agent.grpc.pb.cc
${SRC_DIR}/agent.pb.cc
${SRC_DIR}/check.cc
${SRC_DIR}/check_exec.cc
${SRC_DIR}/opentelemetry/proto/collector/metrics/v1/metrics_service.grpc.pb.cc
${SRC_DIR}/opentelemetry/proto/collector/metrics/v1/metrics_service.pb.cc
${SRC_DIR}/opentelemetry/proto/metrics/v1/metrics.pb.cc
${SRC_DIR}/opentelemetry/proto/common/v1/common.pb.cc
${SRC_DIR}/opentelemetry/proto/resource/v1/resource.pb.cc
${SRC_DIR}/config.cc
${SRC_DIR}/scheduler.cc
)

include_directories(
Expand All @@ -127,6 +132,7 @@ target_link_libraries(
centagent_lib
centreon_common
centreon_grpc
centreon_process
-L${Boost_LIBRARY_DIR_RELEASE}
boost_program_options
fmt::fmt)
Expand Down
3 changes: 2 additions & 1 deletion agent/doc/agent-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ The first service check will start right now, the second one at 12:00:06, third
We don't care about the duration of tests, we work with time points.
In the previous example, the second check for the first service will be scheduled at 12:00:10 even if all other checks has not been yet started.

In case of check duration is too long, we might exceed maximum of concurrent checks. In that case checks will be executed as soon one will be ended.
In case of check duration is too long, we might exceed maximum of concurrent checks. In that case checks will be executed as soon one will be ended. But next calls will occur at check start + check_period.
This means that the second check may start later than the scheduled time point (12:00:10) if the other first checks are too long. The order of checks is always respected even in case of a bottleneck.
For example, a check lambda has a start_expected to 12:00, because of bottleneck, it starts at 12:15. Next start_expected of check lambda will then be 12:15 + check_period.
125 changes: 125 additions & 0 deletions agent/inc/com/centreon/agent/check.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/**
* 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 : contact@centreon.com
*/

#ifndef CENTREON_AGENT_CHECK_HH
#define CENTREON_AGENT_CHECK_HH

#include "agent.pb.h"
#include "com/centreon/common/perfdata.hh"

namespace com::centreon::agent {

using engine_to_agent_request_ptr =
std::shared_ptr<com::centreon::agent::MessageToAgent>;

using time_point = std::chrono::system_clock::time_point;
using duration = std::chrono::system_clock::duration;

/**
* @brief base class for check
* start_expected is set by scheduler and increased by check_period on each
* check
*
*/
class check : public std::enable_shared_from_this<check> {
public:
using completion_handler = std::function<void(
const std::shared_ptr<check>& caller,
int status,
const std::list<com::centreon::common::perfdata>& perfdata,
const std::list<std::string>& outputs)>;

private:
//_start_expected is set on construction on config receive
// it's updated on check_start and added of check_period on check completion
time_point _start_expected;
const std::string& _service;
const std::string& _command_name;
const std::string& _command_line;
// by owning a reference to the original request, we can get only reference to
// host, service and command_line
// on completion, this pointer is compared to the current config pointer.
// if not equal result is not processed
engine_to_agent_request_ptr _conf;

asio::system_timer _time_out_timer;

void _start_timeout_timer(const duration& timeout);

bool _running_check = false;
// this index is used and incremented by on_completion to insure that
// async on_completion is called by the actual asynchronous check
unsigned _running_check_index = 0;
completion_handler _completion_handler;

protected:
std::shared_ptr<asio::io_context> _io_context;
std::shared_ptr<spdlog::logger> _logger;

unsigned _get_running_check_index() const { return _running_check_index; }
const completion_handler& _get_completion_handler() const {
return _completion_handler;
}

virtual void _timeout_timer_handler(const boost::system::error_code& err,
unsigned start_check_index);

public:
using pointer = std::shared_ptr<check>;

check(const std::shared_ptr<asio::io_context>& io_context,
const std::shared_ptr<spdlog::logger>& logger,
time_point exp,
const std::string& serv,
const std::string& command_name,
const std::string& cmd_line,
const engine_to_agent_request_ptr& cnf,
completion_handler&& handler);

virtual ~check() = default;

struct pointer_start_compare {
bool operator()(const check::pointer& left,
const check::pointer& right) const {
return left->_start_expected < right->_start_expected;
}
};

void add_duration_to_start_expected(const duration& to_add);

time_point get_start_expected() const { return _start_expected; }

const std::string& get_service() const { return _service; }

const std::string& get_command_name() const { return _command_name; }

const std::string& get_command_line() const { return _command_line; }

const engine_to_agent_request_ptr& get_conf() const { return _conf; }

void on_completion(unsigned start_check_index,
unsigned status,
const std::list<com::centreon::common::perfdata>& perfdata,
const std::list<std::string>& outputs);

virtual void start_check(const duration& timeout);
};

} // namespace com::centreon::agent

#endif
117 changes: 117 additions & 0 deletions agent/inc/com/centreon/agent/check_exec.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* 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 : contact@centreon.com
*/

#ifndef CENTREON_AGENT_CHECK_EXEC_HH
#define CENTREON_AGENT_CHECK_EXEC_HH

#include "check.hh"
#include "com/centreon/common/process.hh"

namespace com::centreon::agent {

class check_exec;

namespace detail {

/**
* @brief This class is used by check_exec class to execute plugins
* It calls check_exec::on_completion once process is ended AND we have received
* an eof on stdout pipe
* stderr pipe is not read as plugins should not use it
* As we are in asynchronous world, running index is carried until completion to
* ensure that completion is called for the right process and not for the
* previous one
*/
class process : public common::process {
bool _process_ended;
bool _stdout_eof;
std::string _stdout;
unsigned _running_index;
std::weak_ptr<check_exec> _parent;

void _on_completion();

public:
process(const std::shared_ptr<asio::io_context>& io_context,
const std::shared_ptr<spdlog::logger>& logger,
const std::string& cmd_line,
const std::shared_ptr<check_exec>& parent);

void start(unsigned running_index);

void kill() { common::process::kill(); }

int get_exit_status() const { return common::process::get_exit_status(); }

const std::string& get_stdout() const { return _stdout; }

protected:
void on_stdout_read(const boost::system::error_code& err,
size_t nb_read) override;
void on_stderr_read(const boost::system::error_code& err,
size_t nb_read) override;

void on_process_end(const boost::system::error_code& err,
int raw_exit_status) override;
};

} // namespace detail

/**
* @brief check that executes a process (plugins)
*
*/
class check_exec : public check {
std::shared_ptr<detail::process> _process;

protected:
using check::completion_handler;

void _timeout_timer_handler(const boost::system::error_code& err,
unsigned start_check_index) override;

void _init();

public:
check_exec(const std::shared_ptr<asio::io_context>& io_context,
const std::shared_ptr<spdlog::logger>& logger,
time_point exp,
const std::string& serv,
const std::string& cmd_name,
const std::string& cmd_line,
const engine_to_agent_request_ptr& cnf,
check::completion_handler&& handler);

static std::shared_ptr<check_exec> load(
const std::shared_ptr<asio::io_context>& io_context,
const std::shared_ptr<spdlog::logger>& logger,
time_point exp,
const std::string& serv,
const std::string& cmd_name,
const std::string& cmd_line,
const engine_to_agent_request_ptr& cnf,
check::completion_handler&& handler);

void start_check(const duration& timeout) override;

void on_completion(unsigned running_index);
};

} // namespace com::centreon::agent

#endif
6 changes: 4 additions & 2 deletions agent/inc/com/centreon/agent/config.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class config {
public:
enum log_type { to_stdout, to_file };

static const std::string_view config_schema;

private:
std::string _endpoint;
spdlog::level::level_enum _log_level;
Expand All @@ -35,7 +37,7 @@ class config {
unsigned _log_files_max_number;

bool _encryption;
std::string _certificate_file;
std::string _public_cert_file;
std::string _private_key_file;
std::string _ca_certificate_file;
std::string _ca_name;
Expand All @@ -53,7 +55,7 @@ class config {
unsigned get_log_files_max_number() const { return _log_files_max_number; }

bool use_encryption() const { return _encryption; }
const std::string& get_certificate_file() const { return _certificate_file; }
const std::string& get_public_cert_file() const { return _public_cert_file; }
const std::string& get_private_key_file() const { return _private_key_file; }
const std::string& get_ca_certificate_file() const {
return _ca_certificate_file;
Expand Down
Loading

0 comments on commit 40a0b6c

Please sign in to comment.