From 8c7d92e9d566f25aed898fc9c32aa2e0d38d49e6 Mon Sep 17 00:00:00 2001 From: Jean Christophe Roques Date: Wed, 19 Jun 2024 11:19:31 +0200 Subject: [PATCH] add install scripts --- .github/workflows/package-collect.yml | 4 +- .gitignore | 5 + CMakeLists.txt | 2 + agent/CMakeLists.txt | 30 +++- agent/conf/CMakeLists.txt | 43 +++++ agent/conf/centagent.cfg.in | 121 +++++++++++++ agent/scripts/centagent.service.in | 33 ++++ agent/src/main.cc | 65 +++---- engine/CMakeLists.txt | 1 + .../agent_check_result_builder.cc | 2 + .../src/centreon_agent/agent_impl.cc | 13 +- .../opentelemetry/src/data_point_fifo.cc | 4 +- .../opentelemetry/src/open_telemetry.cc | 16 +- packaging/centagent.yaml | 47 ++++- packaging/centreon-agent-selinux.yaml | 40 +++++ .../centreon-agent-daemon-postinstall.sh | 25 +++ .../centreon-agent-daemon-postremove.sh | 8 + .../centreon-agent-daemon-preinstall.sh | 10 ++ .../centreon-agent-daemon-preremove.sh | 3 + .../centreon-agent-selinux-postinstall.sh | 25 +++ .../centreon-agent-selinux-preremove.sh | 5 + selinux/centreon-agent/centreon-agent.fc | 1 + selinux/centreon-agent/centreon-agent.if | 1 + selinux/centreon-agent/centreon-agent.te | 170 ++++++++++++++++++ tests/broker-engine/opentelemetry.robot | 105 ++++++++--- tests/resources/Agent.py | 4 +- 26 files changed, 701 insertions(+), 82 deletions(-) create mode 100644 agent/conf/CMakeLists.txt create mode 100644 agent/conf/centagent.cfg.in create mode 100644 agent/scripts/centagent.service.in create mode 100644 packaging/centreon-agent-selinux.yaml create mode 100644 packaging/scripts/centreon-agent-daemon-postinstall.sh create mode 100644 packaging/scripts/centreon-agent-daemon-postremove.sh create mode 100644 packaging/scripts/centreon-agent-daemon-preinstall.sh create mode 100644 packaging/scripts/centreon-agent-daemon-preremove.sh create mode 100644 packaging/scripts/centreon-agent-selinux-postinstall.sh create mode 100644 packaging/scripts/centreon-agent-selinux-preremove.sh create mode 100644 selinux/centreon-agent/centreon-agent.fc create mode 100644 selinux/centreon-agent/centreon-agent.if create mode 100644 selinux/centreon-agent/centreon-agent.te diff --git a/.github/workflows/package-collect.yml b/.github/workflows/package-collect.yml index 5790ea7ff0c..88c596ebc89 100644 --- a/.github/workflows/package-collect.yml +++ b/.github/workflows/package-collect.yml @@ -105,7 +105,7 @@ jobs: if: ${{ matrix.package_extension == 'rpm' }} run: | cd selinux - for MODULE in "centreon-engine" "centreon-broker"; do + for MODULE in "centreon-engine" "centreon-broker" "centreon-agent"; do cd $MODULE sed -i "s/@VERSION@/${{ inputs.version }}/g" $MODULE.te make -f /usr/share/selinux/devel/Makefile @@ -217,7 +217,7 @@ jobs: run: rm -rf *-debuginfo*.${{ matrix.package_extension }} # set condition to true if artifacts are needed - - if: ${{ true }} + - if: ${{ false }} name: Upload package artifacts uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # v4.3.3 with: diff --git a/.gitignore b/.gitignore index 8243275dd6e..77cd000405a 100644 --- a/.gitignore +++ b/.gitignore @@ -65,6 +65,10 @@ log.html output.xml report.html +# agent +agent/scripts/centagent.service +opentelemetry-proto + # bbdo bbdo/*_accessor.hh @@ -140,3 +144,4 @@ tests/bench.unqlite tests/resources/*_pb2.py tests/resources/*_pb2_grpc.py tests/resources/grpc_stream.proto +tests/resources/opentelemetry diff --git a/CMakeLists.txt b/CMakeLists.txt index e7b978be4c3..499977696df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -146,6 +146,8 @@ endif() # ########### CONSTANTS ########### set(USER_BROKER centreon-broker) set(USER_ENGINE centreon-engine) +set(USER_AGENT centreon-agent) + find_package(fmt CONFIG REQUIRED) find_package(spdlog CONFIG REQUIRED) diff --git a/agent/CMakeLists.txt b/agent/CMakeLists.txt index 79bb5ffb47b..6f5b93e5aaa 100644 --- a/agent/CMakeLists.txt +++ b/agent/CMakeLists.txt @@ -22,6 +22,7 @@ project("Centreon agent" C CXX) # Set directories. set(INCLUDE_DIR "${PROJECT_SOURCE_DIR}/inc/com/centreon/agent") set(SRC_DIR "${PROJECT_SOURCE_DIR}/src") +set(SCRIPT_DIR "${PROJECT_SOURCE_DIR}/scripts") add_definitions("-D_GLIBCXX_USE_CXX11_ABI=1") @@ -130,8 +131,6 @@ target_link_libraries( ${CENTREON_AGENT} PRIVATE -L${PROTOBUF_LIB_DIR} gRPC::gpr gRPC::grpc gRPC::grpc++ gRPC::grpc++_alts -# cerpc -# berpc centagent_lib centreon_common centreon_grpc @@ -157,7 +156,28 @@ if(WITH_TESTING) add_subdirectory(test) endif() -#if(WITH_CONF) -# add_subdirectory(conf) -#endif() +set(PREFIX_AGENT_CONF "${CMAKE_INSTALL_FULL_SYSCONFDIR}/centreon-agent") + + +if(WITH_CONF) + add_subdirectory(conf) +endif() + +# Generate Systemd script. +message(STATUS "Generating systemd startup script.") +configure_file("${SCRIPT_DIR}/centagent.service.in" + "${SCRIPT_DIR}/centagent.service") + +# Startup dir. +if(WITH_STARTUP_DIR) + set(STARTUP_DIR "${WITH_STARTUP_DIR}") +else() + set(STARTUP_DIR "/etc/systemd/system") +endif() + +# Script install rule. +install( + PROGRAMS "${SCRIPT_DIR}/centagent.service" + DESTINATION "${STARTUP_DIR}" + COMPONENT "runtime") diff --git a/agent/conf/CMakeLists.txt b/agent/conf/CMakeLists.txt new file mode 100644 index 00000000000..8387e7c2956 --- /dev/null +++ b/agent/conf/CMakeLists.txt @@ -0,0 +1,43 @@ +# +# 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 +# + +# Set directories. +set(SRC_DIR "${PROJECT_SOURCE_DIR}/conf") + +# Configure files. +configure_file("${SRC_DIR}/centagent.cfg.in" + "${SRC_DIR}/centagent.cfg") + +# Install files if necessary. +option(WITH_SAMPLE_CONFIG "Install sample configuration files." ON) +if (WITH_SAMPLE_CONFIG) + install(DIRECTORY "${SRC_DIR}/" + DESTINATION "${PREFIX_AGENT_CONF}" + COMPONENT "runtime" + FILES_MATCHING PATTERN "*.cfg") + + install(CODE " + function(my_chown user group file) + if (APPLE OR (UNIX AND NOT CYGWIN)) + execute_process(COMMAND \"chown\" \"\${user}:\${group}\" \"\${file}\") + endif () + endfunction() + + my_chown(\"${USER_AGENT}\" \"${USER_AGENT}\" \"${PREFIX_AGENT_CONF}/centagent.cfg\") + ") +endif () diff --git a/agent/conf/centagent.cfg.in b/agent/conf/centagent.cfg.in new file mode 100644 index 00000000000..9475d8f9ae1 --- /dev/null +++ b/agent/conf/centagent.cfg.in @@ -0,0 +1,121 @@ +# +# Copyright 2024 Centreon +# +# This file is part of Centreon Agent. +# +# 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 +# + +# file: centagent.cfg +# brief: Sample main config file for Centreon Agent @VERSION@ +# +# Read the documentation for more information on this configuration file. I've +# provided some comments here, but things may not be so clear without further +# explanation. +# If you want to have a list of all options, +# in a terminal, executes @CMAKE_INSTALL_FULL_BINDIR@/@CENTREON_AGENT@ --help + + +# var: log-file +# brief: in case of log-type=file, logs will be written in this file + +log-file=@AGENT_VAR_LOG_DIR@/@CENTREON_AGENT@.log + + +# var: log-level +# brief: You can choose log verbosity: +# critical, error, info, debug, trace +# You can change log verbosity during runtime with USR1 and USR2 signals +# Ex: kill -USR1 to decrease verbosity (error => info for example) +# kill -USR2 to increase verbosity + +log-level=info + + +# var: log-type +# brief: type of log output: stdout or to a file + +log-type=file + +# var: log-max-file-size +# brief: max log file size in bytes until rotate + +log-max-file-size=10000000 + + +# var: log-max-files +# brief: max number of log files (oldest will be removed) + +log-max-files=3 + + +# var: endpoint +# brief: This parameter is mandatory +# In normal case (agent connects to engine), this endpoint is the +# opentelemetry listening endpoint of engine +# His syntax is :port + +#endpoint=:4317 + + +# var: encryption +# brief: false by default +# set to true to enable encryption between engine and agent + +encryption=false + + +# var: certificate +# brief: path of the certificate file used by encryption + +#certificate=/etc/centron-agent/certif.crt + + +# var: private_key +# brief: path of the key file of the certificate file + +#private_key=/etc/centreon-agent/certif.key + + +# var: ca_certificate +# brief: path of the authority certificate file used by encryption + +#ca_certificate=/etc/centreon-agent/ca.crt + + +# var: ca_name +# brief: name of the host declared in authority certificate + +#ca_name= + + +# var: host +# brief: name of the host declared in centreon configuration +# if not given, hostname of the computer will be used + +#host= + + +# var: reversed-grpc-streaming +# brief: used when centreon agent is not allowed to connect to poller +# if this option if set to true, centreon agent become a +# grpc server listening on pair interface:port given by +# endpoint parameter where poller will have to connect to + +reversed-grpc-streaming=false + + + + diff --git a/agent/scripts/centagent.service.in b/agent/scripts/centagent.service.in new file mode 100644 index 00000000000..d95249cbb47 --- /dev/null +++ b/agent/scripts/centagent.service.in @@ -0,0 +1,33 @@ +# +# Copyright 2016 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 +# + +[Unit] +Description=Centreon Agent +PartOf=centreon.service +After=centreon.service +ReloadPropagatedFrom=centreon.service + +[Service] +ExecStart=@CMAKE_INSTALL_FULL_BINDIR@/@CENTREON_AGENT@ --config-file @PREFIX_AGENT_CONF@/@CENTREON_AGENT@.cfg +ExecReload=/bin/kill -HUP $MAINPID +Type=simple +User=centreon-agent + +[Install] +WantedBy=default.target + diff --git a/agent/src/main.cc b/agent/src/main.cc index 4ff520cbd90..07eb175a4d2 100644 --- a/agent/src/main.cc +++ b/agent/src/main.cc @@ -45,15 +45,15 @@ static void signal_handler(const boost::system::error_code& error, SPDLOG_LOGGER_INFO(g_logger, "SIGTERM received"); g_io_context->stop(); break; - case SIGUSR1: - SPDLOG_LOGGER_INFO(g_logger, "SIGUSR1 received"); + case SIGUSR2: + SPDLOG_LOGGER_INFO(g_logger, "SIGUSR2 received"); if (g_logger->level()) { g_logger->set_level( static_cast(g_logger->level() - 1)); } break; - case SIGUSR2: - SPDLOG_LOGGER_INFO(g_logger, "SIGUSR2 received"); + case SIGUSR1: + SPDLOG_LOGGER_INFO(g_logger, "SIGUSR1 received"); if (g_logger->level() < spdlog::level::off) { g_logger->set_level( static_cast(g_logger->level() + 1)); @@ -108,13 +108,13 @@ int main(int argc, char* argv[]) { "this agent connect to engine in streaming mode")( "reversed-grpc-streaming", po::value()->default_value(false), "this agent accept connection from engine in streaming mode")( - "logger-type", po::value()->default_value("stdout"), + "log-type", po::value()->default_value("stdout"), "type of logger: stdout, file")( - "logger-file", po::value(), - "log file used in case of logger_type = file")( - "logger-max-file-size", po::value(), + "log-file", po::value(), + "log file used in case of log_type = file")( + "log-max-file-size", po::value(), "max size of log file in Mo before rotate")( - "logger-max-files", po::value(), "max log files"); + "log-max-files", po::value(), "max log files"); po::variables_map vm; @@ -139,32 +139,39 @@ int main(int argc, char* argv[]) { } SPDLOG_INFO( - "centreon-agent start, you can decrease log level by kill -USR1 " + "centreon-agent start, you can decrease log verbosity by kill -USR1 " "{} or increase by kill -USR2 {}", getpid(), getpid()); const std::string logger_name = "centreon-agent"; - auto logger_file = [&]() { - if (vm.count("logger-file")) { - if (vm.count("logger-max-file-size") && vm.count("logger-max-files")) { - g_logger = spdlog::rotating_logger_mt( - logger_name, vm["logger-file"].as(), - vm["logger-max-file-size"].as(), - vm["logger-max-files"].as()); + std::string log_type = vm["log-type"].as(); + + if (log_type == "file") { + try { + if (vm.count("log-file")) { + if (vm.count("log-max-file-size") && vm.count("log-max-files")) { + g_logger = spdlog::rotating_logger_mt( + logger_name, vm["log-file"].as(), + vm["log-max-file-size"].as(), + vm["log-max-files"].as()); + } else { + SPDLOG_INFO( + "no log-max-file-size option or no log-max-files option provided " + "=> logs will not be rotated by centagent"); + g_logger = spdlog::basic_logger_mt(logger_name, + vm["log-file"].as()); + } } else { - g_logger = spdlog::basic_logger_mt(logger_name, - vm["logger-file"].as()); + SPDLOG_ERROR( + "log-type=file needs the option log-file => log to stdout"); + g_logger = spdlog::stdout_color_mt(logger_name); } - } else { - g_logger = spdlog::stdout_color_mt(logger_name); + } catch (const std::exception& e) { + SPDLOG_CRITICAL("Can't log to {}: {}", vm["log-file"].as(), + e.what()); + return 2; } - }; - - std::string log_type = vm["logger-type"].as(); - - if (log_type == "file") { - logger_file(); } else { g_logger = spdlog::stdout_color_mt(logger_name); } @@ -185,7 +192,7 @@ int main(int argc, char* argv[]) { SPDLOG_LOGGER_INFO(g_logger, "centreon-agent start, you can decrease log " - "level by kill -USR1 {} or increase by kill -USR2 {}", + "verbosity by kill -USR1 {} or increase by kill -USR2 {}", getpid(), getpid()); std::shared_ptr conf; std::string supervised_host; @@ -199,8 +206,6 @@ int main(int argc, char* argv[]) { SPDLOG_CRITICAL( "endpoint param is mandatory (represents where to connect or where " "to listen example: 127.0.0.1:4317)"); - // in order to avoid service restart non stop - std::this_thread::sleep_for(std::chrono::seconds(10)); return -1; } std::string host_port = vm["endpoint"].as(); diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index 0b9a62ebaaf..9b9255c5e3a 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -532,6 +532,7 @@ target_link_libraries( cce_core gRPC::grpc++ boost_program_options + protobuf "-Wl,--no-whole-archive" gRPC::gpr gRPC::grpc diff --git a/engine/modules/opentelemetry/src/centreon_agent/agent_check_result_builder.cc b/engine/modules/opentelemetry/src/centreon_agent/agent_check_result_builder.cc index 13bb39343e3..067826cd90d 100644 --- a/engine/modules/opentelemetry/src/centreon_agent/agent_check_result_builder.cc +++ b/engine/modules/opentelemetry/src/centreon_agent/agent_check_result_builder.cc @@ -143,6 +143,8 @@ bool agent_check_result_builder::_build_result_from_metrics( res.output.push_back('|'); for (auto& metric_to_fifo : fifos) { + if (metric_to_fifo.first == "status") + continue; auto& fifo = metric_to_fifo.second.get_fifo(); auto data_pt_search = fifo.find(last_time); if (data_pt_search != fifo.end()) { diff --git a/engine/modules/opentelemetry/src/centreon_agent/agent_impl.cc b/engine/modules/opentelemetry/src/centreon_agent/agent_impl.cc index ef4cff251b0..42c632a46ff 100644 --- a/engine/modules/opentelemetry/src/centreon_agent/agent_impl.cc +++ b/engine/modules/opentelemetry/src/centreon_agent/agent_impl.cc @@ -129,14 +129,16 @@ static bool add_command_to_agent_conf( std::string plugins_cmdline = boost::trim_copy(cmd_line); if (plugins_cmdline.empty()) { - SPDLOG_LOGGER_ERROR(logger, - "agent: {} serv: {}, no plugins cmd_line found in {}", - peer, service, cmd_line); + SPDLOG_LOGGER_ERROR( + logger, + "no add command: agent: {} serv: {}, no plugins cmd_line found in {}", + peer, service, cmd_line); return false; } - SPDLOG_LOGGER_TRACE(logger, "agent: {}, serv: {}, cmd {} plugins cmd_line {}", - peer, service, cmd_name, cmd_line); + SPDLOG_LOGGER_TRACE( + logger, "add command to agent: {}, serv: {}, cmd {} plugins cmd_line {}", + peer, service, cmd_name, cmd_line); com::centreon::agent::Service* serv = cnf->add_services(); serv->set_service_description(service); @@ -188,6 +190,7 @@ void agent_impl::_calc_and_send_config_if_needed() { _last_sent_config = new_conf; } else { new_conf.reset(); + SPDLOG_LOGGER_DEBUG(_logger, "no need to update conf to {}", get_peer()); } } if (new_conf) { diff --git a/engine/modules/opentelemetry/src/data_point_fifo.cc b/engine/modules/opentelemetry/src/data_point_fifo.cc index 3082d0644c5..00e4bec9d58 100644 --- a/engine/modules/opentelemetry/src/data_point_fifo.cc +++ b/engine/modules/opentelemetry/src/data_point_fifo.cc @@ -72,10 +72,10 @@ void data_point_fifo::clean() { /** * @brief erase oldest element * - * @param expiry data points oldest than this nano timestamp are erased + * @param expiry data points older than this nano timestamp are erased */ void data_point_fifo::clean_oldest(uint64_t expiry) { - while (!_fifo.empty() && _fifo.begin()->get_nano_timestamp() <= expiry) { + while (!_fifo.empty() && _fifo.begin()->get_nano_timestamp() < expiry) { _fifo.erase(_fifo.begin()); } } diff --git a/engine/modules/opentelemetry/src/open_telemetry.cc b/engine/modules/opentelemetry/src/open_telemetry.cc index cd9c1800567..1efaa839787 100644 --- a/engine/modules/opentelemetry/src/open_telemetry.cc +++ b/engine/modules/opentelemetry/src/open_telemetry.cc @@ -91,12 +91,6 @@ void open_telemetry::_reload() { _conf = std::move(new_conf); - // push new configuration to connected agents - centreon_agent::agent_impl<::grpc::ServerBidiReactor< - agent::MessageFromAgent, agent::MessageToAgent>>:: - all_agent_calc_and_send_config_if_needed( - _conf->get_centreon_agent_config()); - if (!_agent_reverse_client) { _agent_reverse_client = std::make_unique( @@ -108,6 +102,16 @@ void open_telemetry::_reload() { } _agent_reverse_client->update(_conf->get_centreon_agent_config()); } + // push new configuration to connected agents + centreon_agent::agent_impl<::grpc::ServerBidiReactor>:: + all_agent_calc_and_send_config_if_needed( + _conf->get_centreon_agent_config()); + + centreon_agent::agent_impl<::grpc::ClientBidiReactor< + agent::MessageToAgent, agent::MessageFromAgent>>:: + all_agent_calc_and_send_config_if_needed( + _conf->get_centreon_agent_config()); } /** diff --git a/packaging/centagent.yaml b/packaging/centagent.yaml index 4b0d8d9b65b..7a9c5d06c45 100644 --- a/packaging/centagent.yaml +++ b/packaging/centagent.yaml @@ -1,4 +1,4 @@ -name: "centagent" +name: "centreon-agent" arch: "${ARCH}" platform: "linux" version_schema: "none" @@ -15,9 +15,54 @@ homepage: "https://www.centreon.com" license: "Apache-2.0" contents: + - src: "../agent/conf/centagent.cfg" + dst: "/etc/centreon-agent/centagent.cfg" + type: config|noreplace + file_info: + mode: 0664 + owner: centreon-agent + group: centreon-agent + + - src: "../agent/scripts/centagent.service" + dst: "/usr/lib/systemd/system/centagent.service" + file_info: + mode: 0644 + packager: rpm + - src: "../agent/scripts/centagent.service" + dst: "/lib/systemd/system/centagent.service" + file_info: + mode: 0644 + packager: deb + - src: "../build/agent/centagent" dst: "/usr/bin/centagent" + - dst: "/etc/centreon-agent" + type: dir + file_info: + mode: 0775 + owner: centreon-agent + group: centreon-agent + + - dst: "/var/log/centreon-agent" + type: dir + file_info: + mode: 0755 + owner: centreon-agent + group: centreon-agent + + - src: "/usr/lib/nagios/plugins" + dst: "/usr/lib64/nagios/plugins" + type: symlink + packager: deb + +scripts: + preinstall: ./scripts/centreon-agent-daemon-preinstall.sh + postinstall: ./scripts/centreon-agent-daemon-postinstall.sh + preremove: ./scripts/centreon-agent-daemon-preremove.sh + postremove: ./scripts/centreon-agent-daemon-postremove.sh + + rpm: summary: Centreon Collect Agent. It can be used to execute remotely plugins compression: zstd diff --git a/packaging/centreon-agent-selinux.yaml b/packaging/centreon-agent-selinux.yaml new file mode 100644 index 00000000000..068b0004b24 --- /dev/null +++ b/packaging/centreon-agent-selinux.yaml @@ -0,0 +1,40 @@ +name: "centreon-agent-selinux" +arch: "${ARCH}" +platform: "linux" +version_schema: "none" +version: "${VERSION}" +release: "${RELEASE}${DIST}" +section: "default" +priority: "optional" +maintainer: "Centreon " +description: | + SELinux context for centreon-agent +vendor: "Centreon" +homepage: "https://centreon.com" +license: "Apache-2.0" + +depends: + - policycoreutils + - centreon-common-selinux +replaces: + - centreon-agent-selinux-debuginfo +conflicts: + - centreon-agent-selinux-debuginfo +provides: + - centreon-agent-selinux-debuginfo + +contents: + - src: "../selinux/centreon-agent/centreon-agent.pp" + dst: "/usr/share/selinux/packages/centreon/centreon-agent.pp" + file_info: + mode: 0655 + +scripts: + postinstall: ./scripts/centreon-agent-selinux-postinstall.sh + preremove: ./scripts/centreon-agent-selinux-preremove.sh + +rpm: + summary: SELinux context for centreon-agent + signature: + key_file: ${RPM_SIGNING_KEY_FILE} + key_id: ${RPM_SIGNING_KEY_ID} diff --git a/packaging/scripts/centreon-agent-daemon-postinstall.sh b/packaging/scripts/centreon-agent-daemon-postinstall.sh new file mode 100644 index 00000000000..87bebfa5649 --- /dev/null +++ b/packaging/scripts/centreon-agent-daemon-postinstall.sh @@ -0,0 +1,25 @@ +#!/bin/sh + +startCentagent() { + systemctl daemon-reload ||: + systemctl unmask centagent.service ||: + systemctl preset centagent.service ||: + systemctl enable centagent.service ||: + systemctl restart centagent.service ||: +} + +# on debian, it is needed to recreate centreon-agent user at each upgrade because it is removed on postrm step on versions < 23.10 +if [ "$1" = "configure" ] ; then + if [ ! "$(getent passwd centreon-agent)" ]; then + adduser --system --group --shell /bin/bash --no-create-home centreon-agent + fi + if [ "$(getent passwd nagios)" ]; then + usermod -a -G centreon-agent nagios + fi + chown -R centreon-agent:centreon-agent \ + /etc/centreon-agent \ + /var/log/centreon-agent +fi + +startCentagent + diff --git a/packaging/scripts/centreon-agent-daemon-postremove.sh b/packaging/scripts/centreon-agent-daemon-postremove.sh new file mode 100644 index 00000000000..d550f7b752f --- /dev/null +++ b/packaging/scripts/centreon-agent-daemon-postremove.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +case "$1" in + purge) + deluser centreon-agent || : + delgroup centreon-agent || : + ;; +esac diff --git a/packaging/scripts/centreon-agent-daemon-preinstall.sh b/packaging/scripts/centreon-agent-daemon-preinstall.sh new file mode 100644 index 00000000000..6a0afa62e14 --- /dev/null +++ b/packaging/scripts/centreon-agent-daemon-preinstall.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +if ! id centreon-agent > /dev/null 2>&1; then + useradd -r centreon-agent > /dev/null 2>&1 +fi + +if id -g nagios > /dev/null 2>&1; then + usermod -a -G centreon-agent nagios +fi + diff --git a/packaging/scripts/centreon-agent-daemon-preremove.sh b/packaging/scripts/centreon-agent-daemon-preremove.sh new file mode 100644 index 00000000000..e156b0c586e --- /dev/null +++ b/packaging/scripts/centreon-agent-daemon-preremove.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +systemctl stop centagent.service ||: diff --git a/packaging/scripts/centreon-agent-selinux-postinstall.sh b/packaging/scripts/centreon-agent-selinux-postinstall.sh new file mode 100644 index 00000000000..f58e756b6d5 --- /dev/null +++ b/packaging/scripts/centreon-agent-selinux-postinstall.sh @@ -0,0 +1,25 @@ +#!/bin/sh + +install() { + semodule -i /usr/share/selinux/packages/centreon/centreon-engine.pp > /dev/null 2>&1 || : +} + +upgrade() { + semodule -i /usr/share/selinux/packages/centreon/centreon-engine.pp > /dev/null 2>&1 || : +} + +action="$1" +if [ "$1" = "configure" ] && [ -z "$2" ]; then + action="install" +elif [ "$1" = "configure" ] && [ -n "$2" ]; then + action="upgrade" +fi + +case "$action" in + "1" | "install") + install + ;; + "2" | "upgrade") + upgrade + ;; +esac diff --git a/packaging/scripts/centreon-agent-selinux-preremove.sh b/packaging/scripts/centreon-agent-selinux-preremove.sh new file mode 100644 index 00000000000..0b68b4805d5 --- /dev/null +++ b/packaging/scripts/centreon-agent-selinux-preremove.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +if [ "$1" -lt "1" ]; then # Final removal + semodule -r centreon-agent > /dev/null 2>&1 || : +fi diff --git a/selinux/centreon-agent/centreon-agent.fc b/selinux/centreon-agent/centreon-agent.fc new file mode 100644 index 00000000000..7b127e8eb07 --- /dev/null +++ b/selinux/centreon-agent/centreon-agent.fc @@ -0,0 +1 @@ +/usr/bin/centagent -- gen_context(system_u:object_r:centreon_agent_exec_t,s0) diff --git a/selinux/centreon-agent/centreon-agent.if b/selinux/centreon-agent/centreon-agent.if new file mode 100644 index 00000000000..fbeed8af430 --- /dev/null +++ b/selinux/centreon-agent/centreon-agent.if @@ -0,0 +1 @@ +## Centreon Agent monitoring agent. diff --git a/selinux/centreon-agent/centreon-agent.te b/selinux/centreon-agent/centreon-agent.te new file mode 100644 index 00000000000..9bc1e0013a2 --- /dev/null +++ b/selinux/centreon-agent/centreon-agent.te @@ -0,0 +1,170 @@ +policy_module(centreon-agent, @VERSION@) + +######################################## +# +# Declarations +# +require { + type centreon_agent_t; + type centreon_etc_t; + type unconfined_t; + type unconfined_service_t; + type setroubleshootd_t; + type init_t; + type kernel_t; + type fs_t; + type bin_t; + type tmp_t; + type node_t; + type httpd_t; + type ld_so_cache_t; + type ldconfig_exec_t; + type sysfs_t; + type sysctl_net_t; + type var_log_t; + type var_lib_t; + type cert_t; + type nagios_unconfined_plugin_exec_t; + type snmpd_var_lib_t; + type mysqld_db_t; + type ssh_exec_t; + type ssh_home_t; + type setfiles_t; + type unconfined_domain_type; +} + +type centreon_agent_t; +type centreon_agent_exec_t; +init_daemon_domain(centreon_agent_t, centreon_agent_exec_t) + +######################################## +# +# Centreon local policy +# + +allow centreon_agent_t self:process { setpgid signal_perms execmem }; +allow centreon_agent_t self:fifo_file { read open getattr ioctl write rw_fifo_file_perms }; +allow centreon_agent_t self:tcp_socket { create accept listen bind setopt getopt getattr shutdown }; +allow centreon_agent_t self:udp_socket { create accept listen bind setopt getopt getattr }; +allow centreon_agent_t self:file { create read open write getattr read_file_perms relabelto unlink rename }; +allow centreon_agent_t self:dir { add_name write remove_name }; +allow centreon_agent_t self:capability { setuid net_raw }; +allow centreon_agent_t self:rawip_socket { create read write setopt }; +allow centreon_agent_t fs_t:filesystem associate; +allow centreon_agent_t ld_so_cache_t:file execute; +allow centreon_agent_t bin_t:file { execute execute_no_trans }; +allow centreon_agent_t sysfs_t:dir read; +allow centreon_agent_t sysctl_net_t:dir search; +allow centreon_agent_t sysctl_net_t:file { open read getattr }; +allow centreon_agent_t cert_t:dir search; +allow centreon_agent_t node_t:tcp_socket node_bind; +allow centreon_agent_t nagios_unconfined_plugin_exec_t:file { open read execute execute_no_trans }; +allow centreon_agent_t var_log_t:dir { write add_name remove_name }; +allow centreon_agent_t var_log_t:file { create open write read setattr unlink }; +allow centreon_agent_t snmpd_var_lib_t:dir { open read getattr search }; +allow centreon_agent_t snmpd_var_lib_t:file { open read getattr }; +allow centreon_agent_t centreon_agent_t:dir search; +allow centreon_agent_t centreon_agent_t:fifo_file { open read getattr ioctl }; +allow centreon_agent_t ldconfig_exec_t:file { open execute getattr ioctl read}; +allow centreon_agent_t tmp_t:dir { add_name search getattr setattr write unlink create open read remove_name rmdir }; +allow centreon_agent_t tmp_t:file { getattr setattr write unlink create open read }; +allow centreon_agent_t centreon_etc_t:dir { add_name search getattr setattr write unlink create open read remove_name rmdir }; +allow centreon_agent_t ssh_exec_t:file { create read open write getattr setattr read_file_perms relabelto unlink rename ioctl }; +allow centreon_agent_t ssh_home_t:dir { add_name search getattr setattr write unlink create open read remove_name rmdir }; +allow centreon_agent_t ssh_home_t:file { create read open write getattr setattr read_file_perms relabelto unlink rename ioctl }; + +#============= setroubleshootd_t ============== +allow setroubleshootd_t centreon_agent_t:file getattr; +allow setroubleshootd_t centreon_agent_t:dir { search getattr }; +allow setroubleshootd_t centreon_agent_t:fifo_file getattr; + +#============= unconfined_t ============== +allow unconfined_t centreon_agent_t:dir { getattr setattr search relabelto relabelfrom create write add_name }; +allow unconfined_t centreon_agent_t:file { create read open write getattr setattr read_file_perms relabelto unlink rename ioctl }; +allow unconfined_t centreon_agent_t:fifo_file { read open getattr ioctl write setattr }; + +#============= unconfined_service_t ============== +allow unconfined_service_t centreon_agent_t:fifo_file { open read write getattr ioctl }; +allow unconfined_service_t centreon_agent_t:dir { getattr setattr search relabelto relabelfrom create write add_name remove_name }; +allow unconfined_service_t centreon_agent_t:file { create read open write getattr setattr read_file_perms relabelto unlink rename ioctl }; + +#============= httpd_t ============== +allow httpd_t centreon_agent_t:dir { search getattr }; +allow httpd_t centreon_agent_t:fifo_file { open read write getattr }; +allow httpd_t centreon_agent_t:file { execute execute_no_trans map open read getattr setattr }; +allow httpd_t centreon_agent_exec_t:file { execute execute_no_trans map open read getattr setattr }; + +#============= setfiles_t ============== +allow setfiles_t centreon_agent_t:dir relabelto; +allow setfiles_t centreon_agent_t:fifo_file relabelto; +allow setfiles_t centreon_agent_t:file relabelto; + +#============= init_t ============== +allow init_t centreon_agent_t:dir { add_name open read remove_name write search }; +allow init_t centreon_agent_t:fifo_file { create open read write getattr unlink }; +allow init_t centreon_agent_t:file { create execute execute_no_trans getattr map open read unlink write rename }; + +#============= kernel_t ============== +allow kernel_t centreon_agent_t:dir { add_name open read remove_name write search }; +allow kernel_t centreon_agent_t:fifo_file { create open read write getattr unlink }; +allow kernel_t centreon_agent_t:file { create execute execute_no_trans getattr map open read unlink write rename }; + +#============= cluster =============== +allow daemon initrc_transition_domain:fifo_file { ioctl read write getattr lock append }; +allow centreon_agent_t domain:lnk_file { read getattr }; +allow centreon_agent_t domain:dir { ioctl read getattr lock search open }; +allow domain unconfined_domain_type:association recvfrom; +allow domain domain:key { search link }; +allow domain unconfined_domain_type:tcp_socket recvfrom; +allow centreon_agent_t domain:file { ioctl read getattr lock open }; +allow daemon initrc_domain:fd use; +allow daemon initrc_domain:process sigchld; +allow domain unconfined_domain_type:peer recv; +allow daemon initrc_transition_domain:fd use; +allow daemon initrc_domain:fifo_file { ioctl read write getattr lock append }; + +kernel_read_kernel_sysctls(centreon_agent_t) +kernel_read_net_sysctls(centreon_agent_t) +kernel_read_network_state(centreon_agent_t) +kernel_read_system_state(centreon_agent_t) +kernel_request_load_module(centreon_agent_t) + +corecmd_exec_bin(centreon_agent_t) +corecmd_exec_shell(centreon_agent_t) + +corenet_port(centreon_agent_t) +corenet_all_recvfrom_unlabeled(centreon_agent_t) +corenet_all_recvfrom_netlabel(centreon_agent_t) +corenet_tcp_sendrecv_generic_if(centreon_agent_t) +corenet_udp_sendrecv_generic_if(centreon_agent_t) +corenet_tcp_sendrecv_generic_node(centreon_agent_t) +corenet_udp_sendrecv_generic_node(centreon_agent_t) +corenet_tcp_bind_generic_node(centreon_agent_t) +corenet_udp_bind_generic_node(centreon_agent_t) +corenet_sendrecv_all_client_packets(centreon_agent_t) +corenet_tcp_connect_all_ports(centreon_agent_t) +corenet_tcp_sendrecv_all_ports(centreon_agent_t) + +corenet_sendrecv_inetd_child_server_packets(centreon_agent_t) +corenet_tcp_bind_inetd_child_port(centreon_agent_t) +corenet_tcp_sendrecv_inetd_child_port(centreon_agent_t) + +dev_read_sysfs(centreon_agent_t) +dev_read_urand(centreon_agent_t) + +domain_use_interactive_fds(centreon_agent_t) +domain_read_all_domains_state(centreon_agent_t) + +files_read_etc_runtime_files(centreon_agent_t) +files_read_usr_files(centreon_agent_t) + +fs_getattr_all_fs(centreon_agent_t) +fs_search_auto_mountpoints(centreon_agent_t) + +auth_use_nsswitch(centreon_agent_t) + +logging_send_syslog_msg(centreon_agent_t) + +miscfiles_read_localization(centreon_agent_t) + +userdom_dontaudit_use_unpriv_user_fds(centreon_agent_t) diff --git a/tests/broker-engine/opentelemetry.robot b/tests/broker-engine/opentelemetry.robot index 020196c8640..04fb6b822af 100644 --- a/tests/broker-engine/opentelemetry.robot +++ b/tests/broker-engine/opentelemetry.robot @@ -117,11 +117,24 @@ BEOTEL_TELEGRAF_CHECK_HOST ${resources_list} Ctn Create Otl Request ${0} host_1 + # check without feed + ${start} Ctn Get Round Current Date + Ctn Schedule Forced Host Check host_1 + ${result} Ctn Check Host Check Status With Timeout + ... host_1 + ... 35 + ... ${start} + ... 0 + ... (No output returned from host check) + Should Be True ${result} hosts table not updated + + Log To Console export metrics Ctn Send Otl To Engine 4317 ${resources_list} Sleep 5 + # feed and check ${start} Ctn Get Round Current Date Ctn Schedule Forced Host Check host_1 @@ -129,18 +142,6 @@ BEOTEL_TELEGRAF_CHECK_HOST ${result} Ctn Check Host Check Status With Timeout host_1 30 ${start} 0 OK Should Be True ${result} hosts table not updated - # check without feed - - ${start} Ctn Get Round Current Date - Ctn Schedule Forced Host Check host_1 - ${result} Ctn Check Host Check Status With Timeout - ... host_1 - ... 35 - ... ${start} - ... 0 - ... (No output returned from host check) - Should Be True ${result} hosts table not updated - # check then feed, three times to modify hard state ${start} Ctn Get Round Current Date Ctn Schedule Forced Host Check host_1 @@ -197,18 +198,6 @@ BEOTEL_TELEGRAF_CHECK_SERVICE ${resources_list} Ctn Create Otl Request ${0} host_1 service_1 - Log To Console export metrics - Ctn Send Otl To Engine 4317 ${resources_list} - - Sleep 5 - - # feed and check - ${start} Ctn Get Round Current Date - Ctn Schedule Forced Svc Check host_1 service_1 - - ${result} Ctn Check Service Check Status With Timeout host_1 service_1 30 ${start} 0 OK - Should Be True ${result} services table not updated - # check without feed ${start} Ctn Get Round Current Date @@ -222,6 +211,18 @@ BEOTEL_TELEGRAF_CHECK_SERVICE ... (No output returned from plugin) Should Be True ${result} services table not updated + Log To Console export metrics + Ctn Send Otl To Engine 4317 ${resources_list} + + Sleep 5 + + # feed and check + ${start} Ctn Get Round Current Date + Ctn Schedule Forced Svc Check host_1 service_1 + + ${result} Ctn Check Service Check Status With Timeout host_1 service_1 30 ${start} 0 OK + Should Be True ${result} services table not updated + # check then feed, three times to modify hard state ${start} Ctn Get Round Current Date Ctn Schedule Forced Svc Check host_1 service_1 @@ -396,7 +397,7 @@ BEOTEL_CENTREON_AGENT_CHECK_HOST Ctn Config Engine ${1} ${2} ${2} Ctn Add Otl ServerModule ... 0 - ... {"otel_server":{"host": "0.0.0.0","port": 4317},"max_length_grpc_log":0} + ... {"otel_server":{"host": "0.0.0.0","port": 4317},"max_length_grpc_log":0, "centreon_agent":{"check_interval":10, "export_period":10}} Ctn Config Add Otl Connector ... 0 ... OTEL connector @@ -405,7 +406,7 @@ BEOTEL_CENTREON_AGENT_CHECK_HOST Ctn Engine Config Add Command ... ${0} ... otel_check_icmp - ... /usr/lib/nagios/plugins/check_icmp 127.0.0.1 + ... /bin/echo "OK - 127.0.0.1: rta 0,010ms, lost 0%|rta=0,010ms;200,000;500,000;0; pl=0%;40;80;; rtmax=0,035ms;;;; rtmin=0,003ms;;;;" ... OTEL connector Ctn Engine Config Set Value 0 log_level_checks trace @@ -436,6 +437,29 @@ BEOTEL_CENTREON_AGENT_CHECK_HOST ${result} Ctn Check Host Check Status With Timeout host_1 30 ${start} 0 OK - 127.0.0.1 Should Be True ${result} hosts table not updated + Ctn Engine Config Replace Value In Hosts ${0} host_1 check_command otel_check_icmp_2 + Ctn Engine Config Add Command + ... ${0} + ... otel_check_icmp_2 + ... /bin/echo "OK check2 - 127.0.0.1: rta 0,010ms, lost 0%|rta=0,010ms;200,000;500,000;0; pl=0%;40;80;; rtmax=0,035ms;;;; rtmin=0,003ms;;;;" + ... OTEL connector + + #update conf engine, it must be taken into account by agent + Log To Console modify engine conf and reload engine + Ctn Reload Engine + + #wait for new data from agent + ${start} Ctn Get Round Current Date + ${content} Create List description: \"OK check2 + ${result} Ctn Find In Log With Timeout ${engineLog0} ${start} ${content} 22 + Should Be True ${result} "description: "OK check2" should be available. + + ${start} Ctn Get Round Current Date + Ctn Schedule Forced Host Check host_1 + + ${result} Ctn Check Host Check Status With Timeout host_1 30 ${start} 0 OK check2 - 127.0.0.1: rta 0,010ms, lost 0% + Should Be True ${result} hosts table not updated + BEOTEL_CENTREON_AGENT_CHECK_SERVICE [Documentation] agent check service and we expect to get it in check result @@ -517,7 +541,7 @@ BEOTEL_REVERSE_CENTREON_AGENT_CHECK_HOST Ctn Engine Config Add Command ... ${0} ... otel_check_icmp - ... /usr/lib/nagios/plugins/check_icmp 127.0.0.1 + ... /bin/echo "OK - 127.0.0.1: rta 0,010ms, lost 0%|rta=0,010ms;200,000;500,000;0; pl=0%;40;80;; rtmax=0,035ms;;;; rtmin=0,003ms;;;;" ... OTEL connector Ctn Engine Config Set Value 0 log_level_checks trace @@ -548,6 +572,29 @@ BEOTEL_REVERSE_CENTREON_AGENT_CHECK_HOST ${result} Ctn Check Host Check Status With Timeout host_1 30 ${start} 0 OK - 127.0.0.1 Should Be True ${result} hosts table not updated + Ctn Engine Config Replace Value In Hosts ${0} host_1 check_command otel_check_icmp_2 + Ctn Engine Config Add Command + ... ${0} + ... otel_check_icmp_2 + ... /bin/echo "OK check2 - 127.0.0.1: rta 0,010ms, lost 0%|rta=0,010ms;200,000;500,000;0; pl=0%;40;80;; rtmax=0,035ms;;;; rtmin=0,003ms;;;;" + ... OTEL connector + + #update conf engine, it must be taken into account by agent + Log To Console modify engine conf and reload engine + Ctn Reload Engine + + #wait for new data from agent + ${start} Ctn Get Round Current Date + ${content} Create List description: \"OK check2 + ${result} Ctn Find In Log With Timeout ${engineLog0} ${start} ${content} 22 + Should Be True ${result} "description: "OK check2" should be available. + + ${start} Ctn Get Round Current Date + Ctn Schedule Forced Host Check host_1 + + ${result} Ctn Check Host Check Status With Timeout host_1 30 ${start} 0 OK check2 - 127.0.0.1: rta 0,010ms, lost 0% + Should Be True ${result} hosts table not updated + BEOTEL_REVERSE_CENTREON_AGENT_CHECK_SERVICE [Documentation] agent check service with reversed connection and we expect to get it in check result @@ -632,7 +679,7 @@ BEOTEL_CENTREON_AGENT_CHECK_HOST_CRYPTED Ctn Engine Config Add Command ... ${0} ... otel_check_icmp - ... /usr/lib/nagios/plugins/check_icmp 127.0.0.1 + ... /bin/echo "OK - 127.0.0.1: rta 0,010ms, lost 0%|rta=0,010ms;200,000;500,000;0; pl=0%;40;80;; rtmax=0,035ms;;;; rtmin=0,003ms;;;;" ... OTEL connector Ctn Engine Config Set Value 0 log_level_checks trace @@ -685,7 +732,7 @@ BEOTEL_REVERSE_CENTREON_AGENT_CHECK_HOST_CRYPTED Ctn Engine Config Add Command ... ${0} ... otel_check_icmp - ... /usr/lib/nagios/plugins/check_icmp 127.0.0.1 + ... /bin/echo "OK - 127.0.0.1: rta 0,010ms, lost 0%|rta=0,010ms;200,000;500,000;0; pl=0%;40;80;; rtmax=0,035ms;;;; rtmin=0,003ms;;;;" ... OTEL connector Ctn Engine Config Set Value 0 log_level_checks trace diff --git a/tests/resources/Agent.py b/tests/resources/Agent.py index aa0e8b413f9..38345a5a160 100644 --- a/tests/resources/Agent.py +++ b/tests/resources/Agent.py @@ -28,8 +28,8 @@ log-level=trace endpoint=localhost:4317 host=host_1 -logger-type=file -logger-file=/tmp/var/log/centreon-engine/centreon-agent.log +log-type=file +log-file=/tmp/var/log/centreon-engine/centreon-agent.log """