Skip to content

Commit

Permalink
implement agent core
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-christophe81 committed May 24, 2024
1 parent 8ef2a2f commit f764bb0
Show file tree
Hide file tree
Showing 52 changed files with 3,168 additions and 756 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/centreon-collect.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
workflow_dispatch:
pull_request:
paths:
- agent/**
- bbdo/**
- broker/**
- ccc/**
Expand All @@ -33,6 +34,7 @@ on:
- master
- "[2-9][0-9].[0-9][0-9].x"
paths:
- agent/**
- bbdo/**
- broker/**
- ccc/**
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/package-collect.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ jobs:
"build/engine/modules/bench/centengine_bench_passive"
"build/connectors/perl/centreon_connector_perl"
"build/connectors/ssh/centreon_connector_ssh"
"build/ccc/ccc")
"build/ccc/ccc"
"build/agent/centreon_agent")
for file in ${exe[@]}; do
echo "Making a debug file of $file"
objcopy --only-keep-debug $file $file.debug
Expand Down Expand Up @@ -216,7 +217,7 @@ jobs:
run: rm -rf *-debuginfo*.${{ matrix.package_extension }}

# set condition to true if artifacts are needed
- if: ${{ false }}
- if: ${{ true }}
name: Upload package artifacts
uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # v4.3.3
with:
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ add_subdirectory(bbdo)
add_subdirectory(engine)
add_subdirectory(connectors)
add_subdirectory(ccc)
add_subdirectory(agent)

if (WITH_MALLOC_TRACE)
add_subdirectory(malloc-trace)
Expand Down
147 changes: 147 additions & 0 deletions agent/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#
# 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]
#

# Global options.
project("Centreon agent" C CXX)

# Set directories.
set(INCLUDE_DIR "${PROJECT_SOURCE_DIR}/inc/com/centreon/agent")
set(SRC_DIR "${PROJECT_SOURCE_DIR}/src")


add_definitions("-D_GLIBCXX_USE_CXX11_ABI=1")
add_definitions(-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_TRACE)

option(WITH_LIBCXX "compiles and link cbd with clang++/libc++")

if(WITH_LIBCXX)
set(CMAKE_CXX_COMPILER "clang++")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")

# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -Werror -O1
# -fno-omit-frame-pointer")
endif()

#otel service
set(service_files
opentelemetry/proto/collector/metrics/v1/metrics_service
)

foreach(name IN LISTS service_files)
set(proto_file "${name}.proto")
add_custom_command(
OUTPUT "${SRC_DIR}/${name}.grpc.pb.cc"
COMMENT "Generating grpc files of the otl service file ${proto_file}"
DEPENDS opentelemetry-proto-files
COMMAND
${Protobuf_PROTOC_EXECUTABLE} ARGS
--plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN}
--proto_path=${CMAKE_SOURCE_DIR}/opentelemetry-proto
--grpc_out=${SRC_DIR} ${proto_file}
VERBATIM
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

endforeach()

set(otl_protobuf_files
opentelemetry/proto/collector/metrics/v1/metrics_service
opentelemetry/proto/metrics/v1/metrics
opentelemetry/proto/common/v1/common
opentelemetry/proto/resource/v1/resource
)
foreach(name IN LISTS otl_protobuf_files)
set(proto_file "${name}.proto")
add_custom_command(
OUTPUT "${SRC_DIR}/${name}.pb.cc"
COMMENT "Generating interface files of the otl file ${proto_file}"
DEPENDS opentelemetry-proto-files
COMMAND
${Protobuf_PROTOC_EXECUTABLE} ARGS --cpp_out=${SRC_DIR}
--proto_path=${CMAKE_SOURCE_DIR}/opentelemetry-proto ${proto_file}
VERBATIM)
endforeach()


#centreon_agent server and client
add_custom_command(
DEPENDS ${PROJECT_SOURCE_DIR}/proto/agent.proto
COMMENT "Generating interface files of the conf centreon_agent proto file (grpc)"
OUTPUT ${SRC_DIR}/agent.grpc.pb.cc
COMMAND
${Protobuf_PROTOC_EXECUTABLE} ARGS
--plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN}
--proto_path=${PROJECT_SOURCE_DIR}/proto --proto_path=${CMAKE_SOURCE_DIR}/opentelemetry-proto
--grpc_out=${SRC_DIR} ${PROJECT_SOURCE_DIR}/proto/agent.proto
DEPENDS ${PROJECT_SOURCE_DIR}/proto/agent.proto
COMMENT "Generating interface files of the conf centreon_agent proto file (protobuf)"
OUTPUT ${SRC_DIR}/agent.pb.cc
COMMAND
${Protobuf_PROTOC_EXECUTABLE} ARGS --cpp_out=${SRC_DIR}
--proto_path=${PROJECT_SOURCE_DIR}/proto --proto_path=${CMAKE_SOURCE_DIR}/opentelemetry-proto
${PROJECT_SOURCE_DIR}/proto/agent.proto
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})


add_library(centreon_agent_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}/scheduler.cc
)

include_directories(
${INCLUDE_DIR}
${SRC_DIR}
${CMAKE_SOURCE_DIR}/common/inc
)

target_precompile_headers(centreon_agent_lib PRIVATE precomp_inc/precomp.hh)

add_executable(centreon_agent ${SRC_DIR}/main.cc)

target_link_libraries(
centreon_agent PRIVATE
-L${PROTOBUF_LIB_DIR}
gRPC::gpr gRPC::grpc gRPC::grpc++ gRPC::grpc++_alts
# cerpc
# berpc
centreon_agent_lib
centreon_common
-L${Boost_LIBRARY_DIR_RELEASE}
boost_program_options
fmt::fmt)

target_precompile_headers(centreon_agent REUSE_FROM centreon_agent_lib)

target_include_directories(centreon_agent PRIVATE
${INCLUDE_DIR}
${SRC_DIR}
${CMAKE_SOURCE_DIR}/common/inc
)

install(TARGETS centreon_agent RUNTIME DESTINATION "${CMAKE_INSTALL_FULL_BINDIR}")

if(WITH_TESTING)
add_subdirectory(test)
endif()
15 changes: 15 additions & 0 deletions agent/doc/agent-doc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Centreon Agent documentation {#mainpage}

## Introduction

The goal of this program is to execute checks in both windows and linux OS
It's full asynchronous, excepted grpc layers, it's single threaded and you won't find mutex in code.

## Configuration
configuration is given by Engine by a AgentConfiguration sent over grpc

## Scheduler

We trie to spread checks over check_period.
Example: We have 10 checks to execute during one second. check1 will start at now + 0.1s, second at now + 0.2s..
In case of check duration is too long, we might exceed maximum of concurrent checks. In that case checks will b executed as soon one will be ended.
Binary file added agent/doc/pictures/logo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
134 changes: 134 additions & 0 deletions agent/inc/com/centreon/agent/check.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
* 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_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::EngineToAgent>;

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:
time_point _start_expected;
const std::string& _host;
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
engine_to_agent_request_ptr _conf;

asio::system_timer _time_out_timer;

void _start_timeout_timer(const duration& timeout);

bool _running_check = false;
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>;

template <typename handler_type>
check(const std::shared_ptr<asio::io_context>& io_context,
const std::shared_ptr<spdlog::logger>& logger,
time_point exp,
const std::string& hst,
const std::string& serv,
const std::string& command_name,
const std::string& cmd_line,
const engine_to_agent_request_ptr& cnf,
handler_type&& handler)
: _start_expected(exp),
_host(hst),
_service(serv),
_command_name(command_name),
_command_line(cmd_line),
_conf(cnf),
_io_context(io_context),
_logger(logger),
_time_out_timer(*io_context),
_completion_handler(handler) {}

virtual ~check() = default;

struct pointer_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_host() const { return _host; }

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
Loading

0 comments on commit f764bb0

Please sign in to comment.