Skip to content

Commit

Permalink
use json configuration instead
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-christophe81 committed Jun 21, 2024
1 parent 2655b43 commit b9244c2
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 110 deletions.
3 changes: 2 additions & 1 deletion agent/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#

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

# Set directories.
set(INCLUDE_DIR "${PROJECT_SOURCE_DIR}/inc/com/centreon/agent")
Expand Down Expand Up @@ -104,6 +104,7 @@ add_library(centagent_lib STATIC
${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
)

include_directories(
Expand Down
7 changes: 3 additions & 4 deletions agent/doc/agent-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

## 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 non grpc code.
This is why when we receive request, we post it to asio in order to process it in the main thread.
The purpose of this program is to run checks in Windows and Linux operating systems. It is entirely asynchronous, with the exception of the gRPC layers. It is also single-threaded and therefore needs no mutexes, except in the gRPC part.
This is why, when a request is received, it is posted to ASIO for processing in the main thread.

## Configuration
configuration is given by Engine by a AgentConfiguration sent over grpc
The configuration object is embedded in MessageToAgent::config

## 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..
Example: We have 10 checks to execute during one second. check1 will start at now, second at now + 0.1s..

When Agent receives configuration, all checks are recreated.
For example, we have 100 checks to execute in 10 minute, at it is 12:00:00.
Expand Down
67 changes: 67 additions & 0 deletions agent/inc/com/centreon/agent/config.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

/**
* 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_CONFIG_HH
#define CENTREON_AGENT_CONFIG_HH

#include "com/centreon/common/grpc/grpc_config.hh"

namespace com::centreon::agent {

class config {
public:
enum log_type { stdout, file };

private:
std::string _endpoint;
spdlog::level::level_enum _log_level;
log_type _log_type;
std::string _log_file;
unsigned _log_max_file_size;
unsigned _log_max_files;

bool _encryption;
std::string _certificate_file;
std::string _private_key_file;
std::string _ca_certificate_file;
std::string _ca_name;
std::string _host;
bool _reverse_connection;

public:
config(const std::string& path);

const std::string& get_endpoint() const { return _endpoint; }
spdlog::level::level_enum get_log_level() const { return _log_level; };
log_type get_log_type() const { return _log_type; }
const std::string& get_log_file() const { return _log_file; }
unsigned get_log_max_file_size() const { return _log_max_file_size; }
unsigned get_log_max_files() const { return _log_max_files; }

bool use_encryption() const { return _encryption; }
const std::string& get_certificate_file() const { return _certificate_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;
}
const std::string& get_ca_name() const { return _ca_name; }
const std::string& get_host() const { return _host; }
bool use_reverse_connection() const { return _reverse_connection; }
};
}; // namespace com::centreon::agent

#endif
147 changes: 147 additions & 0 deletions agent/src/config.cc
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]
*/

#include <rapidjson/document.h>
#include <re2/re2.h>

#include "com/centreon/common/rapidjson_helper.hh"
#include "com/centreon/exceptions/msg_fmt.hh"
#include "config.hh"

using namespace com::centreon::agent;
using com::centreon::common::rapidjson_helper;

static constexpr std::string_view _config_schema(R"(
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "agent config",
"properties": {
"host": {
"description": "IP or dns name, if not given, hostname will be used",
"type": "string",
"minLength": 5
},
"endpoint": {
"description": "endpoint of poller where agent have to connect or listen endpoint in case of reverse_connection",
"type": "string",
"pattern": "[\\w\\.:]+:\\w+"
},
"encryption": {
"description": "true if https, default: false",
"type": "boolean"
},
"public_cert": {
"description": "path of certificate file .crt",
"type": "string"
},
"private_key": {
"description": "path of certificate file .key",
"type": "string"
},
"ca_certificate": {
"description": "path of authority certificate file .crt",
"type": "string"
},
"ca_name": {
"description": "name of authority certificate",
"type": "string"
},
"reverse_connection": {
"description": "if true, centagent is a server and centengine will connect to, default:false",
"type": "boolean"
},
"log_level": {
"description": "log level, may be critical, error, info, debug, trace",
"type": "string",
"pattern": "critical|error|info|debug|trace"
},
"log_type": {
"description": "stdout or file if log to a file (path must be given by log_file), default: stdout",
"type": "string",
"pattern": "stdout|file"
},
"log_file": {
"description": "path of the log file",
"type": "string",
"minLength": 5
},
"log_max_file_size": {
"description:": "max size in Mo of the log file before rotate, to be valid log_max_files must be also be specified",
"type": "integer",
"min": 1
},
"log_max_files": {
"description:": "max of log files before remove, to be valid log_max_file_size must be also be specified",
"type": "integer",
"min": 1
}
},
"required": [
"endpoint"
],
"type": "object"
}
)");

config::config(const std::string& path) {
static common::json_validator validator(_config_schema);
rapidjson::Document file_content_d;
try {
file_content_d = rapidjson_helper::read_from_file(path);
} catch (const std::exception& e) {
SPDLOG_ERROR("incorrect json file{}: {} ", path, e.what());
throw;
}

common::rapidjson_helper json_config(file_content_d);

try {
json_config.validate(validator);
} catch (const std::exception& e) {
SPDLOG_ERROR("forbidden values in agent config: {}", e.what());
throw;
}

_endpoint = json_config.get_string("endpoint");

// pattern schema doesn't work so we do it ourselves
if (!RE2::FullMatch(_endpoint, "[\\w\\.:]+:\\w+")) {
throw exceptions::msg_fmt(
"bad format for endpoint {}, it must match to the regex: "
"[\\w\\.:]+:\\w+",
_endpoint);
}
_log_level =
spdlog::level::from_str(json_config.get_string("log_level", "info"));
_log_type =
json_config.get_string("log_type", "stdout") == "file" ? file : stdout;
_log_file = json_config.get_string("log_file", "");
_log_max_file_size = json_config.get_unsigned("log_max_file_size", 0);
_log_max_files = json_config.get_unsigned("log_max_files", 0);
_encryption = json_config.get_bool("encryption", false);
_certificate_file = json_config.get_string("certificate_file", "");
_private_key_file = json_config.get_string("private_key_file", "");
_ca_certificate_file = json_config.get_string("ca_certificate_file", "");
_ca_name = json_config.get_string("ca_name", "");
_host = json_config.get_string("host", "");
if (_host.empty()) {
_host = boost::asio::ip::host_name();
}
_reverse_connection = json_config.get_bool("reverse_connection", false);
}
Loading

0 comments on commit b9244c2

Please sign in to comment.