-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add scheduler, check and check_exec class to agent
- Loading branch information
1 parent
7d96154
commit 40a0b6c
Showing
21 changed files
with
2,223 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.