Unverified Commit 3be72bd0 authored by syl's avatar syl Committed by GitHub
Browse files

switch ssh connector to spdlog + some minor improvments. (#18)

* enh(logger): add a common folder, for common sources.

* enh(perl): cleanup perl connector code.

* enh(ssh): cleanup code.

* enh(ssh): switch to spdlog.

* fix(chore): fix compile on centos7
parent 67caa511
......@@ -39,11 +39,11 @@ if (EMBEDDED_PERL_CXXFLAGS)
endif ()
# We will use pkg-config if available.
include_directories(${CMAKE_SOURCE_DIR}/common/inc)
add_subdirectory(perl)
add_subdirectory(ssh)
# Testing.
option(WITH_TESTING "Build unit tests." OFF)
if (WITH_TESTING)
......@@ -61,7 +61,7 @@ if (WITH_TESTING)
add_executable(ut
# Core sources
${CMAKE_SOURCE_DIR}/perl/src/embedded_perl.cc
${CMAKE_SOURCE_DIR}/perl/src/log_v2.cc
${CMAKE_SOURCE_DIR}/common/src/log.cc
${CMAKE_SOURCE_DIR}/perl/src/pipe_handle.cc
${CMAKE_SOURCE_DIR}/perl/src/script.cc
${CMAKE_SOURCE_DIR}/perl/src/xs_init.cc
......
......@@ -16,22 +16,26 @@
* For more information : contact@centreon.com
*
*/
#ifndef CONNECTORS_PERL_INC_COM_CENTREON_CONNECTOR_PERL_LOG_V2_H_
#define CONNECTORS_PERL_INC_COM_CENTREON_CONNECTOR_PERL_LOG_V2_H_
#ifndef CONNECTORS_PERL_INC_COM_CENTREON_CONNECTOR_PERL_LOG_H_
#define CONNECTORS_PERL_INC_COM_CENTREON_CONNECTOR_PERL_LOG_H_
#include <spdlog/common.h>
#include <spdlog/spdlog.h>
#include <memory>
class log_v2 {
#include "com/centreon/connector/namespace.hh"
CCC_BEGIN();
class log {
private:
std::shared_ptr<spdlog::logger> _core_log;
log_v2();
~log_v2();
log();
~log();
public:
static log_v2& instance();
static log& instance();
void set_level(spdlog::level::level_enum level);
void switch_to_stdout();
......@@ -40,4 +44,5 @@ class log_v2 {
static std::shared_ptr<spdlog::logger> core();
};
#endif // CONNECTORS_PERL_INC_COM_CENTREON_CONNECTOR_PERL_LOG_V2_H_
CCC_END();
#endif // CONNECTORS_PERL_INC_COM_CENTREON_CONNECTOR_PERL_LOG_H_
/*
** Copyright 2011-2020 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 CCC_NAMESPACE_HH
#define CCC_NAMESPACE_HH
#ifdef CCC_BEGIN
#undef CCC_BEGIN
#endif // CCC_BEGIN
#define CCC_BEGIN() \
namespace com { \
namespace centreon { \
namespace connector { \
#ifdef CCC_END
#undef CCC_END
#endif // CCC_END
#define CCC_END() \
} \
} \
}
#endif // !CCC_NAMESPACE_HH
......@@ -16,12 +16,15 @@
* For more information : contact@centreon.com
*
*/
#include "com/centreon/connector/perl/log_v2.h"
#include "com/centreon/connector/log.hh"
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/null_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
using namespace com::centreon::connector;
log_v2::log_v2() {
log::log() {
auto filesink = std::make_shared<spdlog::sinks::null_sink_mt>();
_core_log = std::make_shared<spdlog::logger>("core", filesink);
_core_log->info("log started");
......@@ -29,21 +32,21 @@ log_v2::log_v2() {
_core_log->flush_on(spdlog::level::trace);
}
log_v2::~log_v2() {
log::~log() {
}
log_v2& log_v2::instance() {
static log_v2 instance;
com::centreon::connector::log& log::instance() {
static log instance;
return instance;
}
void log_v2::set_level(spdlog::level::level_enum level) {
void log::set_level(spdlog::level::level_enum level) {
_core_log->set_level(level);
_core_log->flush_on(level);
}
void log_v2::switch_to_stdout() {
void log::switch_to_stdout() {
auto filesink = std::make_shared<spdlog::sinks::stderr_color_sink_mt>();
spdlog::level::level_enum lvl = _core_log->level();
_core_log = std::make_shared<spdlog::logger>("core", filesink);
......@@ -52,7 +55,7 @@ void log_v2::switch_to_stdout() {
_core_log->flush_on(lvl);
}
void log_v2::switch_to_file(std::string const& filename) {
void log::switch_to_file(std::string const& filename) {
auto filesink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename);
spdlog::level::level_enum lvl = _core_log->level();
_core_log = std::make_shared<spdlog::logger>("core", filesink);
......@@ -61,6 +64,6 @@ void log_v2::switch_to_file(std::string const& filename) {
_core_log->flush_on(lvl);
}
std::shared_ptr<spdlog::logger> log_v2::core() {
return log_v2::instance()._core_log;
std::shared_ptr<spdlog::logger> log::core() {
return log::instance()._core_log;
}
\ No newline at end of file
......@@ -24,13 +24,12 @@ include_directories(${CMAKE_SOURCE_DIR}/perl/inc/)
# Perl connector.
add_executable(centreon_connector_perl
# Sources.
${CMAKE_SOURCE_DIR}/common/src/log.cc
${CMAKE_SOURCE_DIR}/perl/src/main.cc
# Sources.
${CMAKE_SOURCE_DIR}/perl/src/checks/check.cc
${CMAKE_SOURCE_DIR}/perl/src/checks/result.cc
${CMAKE_SOURCE_DIR}/perl/src/checks/timeout.cc
${CMAKE_SOURCE_DIR}/perl/src/embedded_perl.cc
${CMAKE_SOURCE_DIR}/perl/src/log_v2.cc
${CMAKE_SOURCE_DIR}/perl/src/multiplexer.cc
${CMAKE_SOURCE_DIR}/perl/src/options.cc
${CMAKE_SOURCE_DIR}/perl/src/orders/parser.cc
......
......@@ -42,26 +42,27 @@ class result;
class check : public handle_listener {
public:
check();
~check() throw();
void error(handle& h);
pid_t execute(unsigned long long cmd_id,
~check() noexcept;
check(check const& c) = delete;
check& operator=(check const& c) = delete;
void error(handle& h) override;
pid_t execute(uint64_t cmd_id,
std::string const& cmd,
const timestamp& tmt);
void listen(listener* listnr);
void on_timeout(bool final = true);
void read(handle& h);
void read(handle& h) override;
void terminated(int exit_code);
void unlisten(listener* listnr);
bool want_read(handle& h);
void write(handle& h);
bool want_read(handle& h) override;
void write(handle& h) override;
private:
check(check const& c);
check& operator=(check const& c);
void _send_result_and_unregister(result const& r);
pid_t _child;
unsigned long long _cmd_id;
uint64_t _cmd_id;
pipe_handle _err;
listener* _listnr;
pipe_handle _out;
......
......@@ -34,24 +34,22 @@ namespace checks {
class result {
public:
result();
result(result const& r);
~result();
result& operator=(result const& r);
unsigned long long get_command_id() const throw();
std::string const& get_error() const throw();
bool get_executed() const throw();
int get_exit_code() const throw();
std::string const& get_output() const throw();
void set_command_id(unsigned long long cmd_id) throw();
result(result const& r) = delete;
~result() = default;
result& operator=(result const& r) = delete;
uint64_t get_command_id() const noexcept;
std::string const& get_error() const noexcept;
bool get_executed() const noexcept;
int get_exit_code() const noexcept;
std::string const& get_output() const noexcept;
void set_command_id(uint64_t cmd_id) noexcept;
void set_error(std::string const& error);
void set_executed(bool executed) throw();
void set_exit_code(int code) throw();
void set_executed(bool executed) noexcept;
void set_exit_code(int code) noexcept;
void set_output(std::string const& output);
private:
void _internal_copy(result const& r);
unsigned long long _cmd_id;
uint64_t _cmd_id;
std::string _error;
bool _executed;
int _exit_code;
......
......@@ -40,10 +40,10 @@ class timeout : public com::centreon::task {
bool _final;
public:
timeout(check* chk = NULL, bool final = false);
explicit timeout(check* chk = NULL, bool final = false);
timeout(timeout const& t) = delete;
timeout& operator=(timeout const& t) = delete;
void run();
void run() override;
};
} // namespace checks
......
......@@ -35,12 +35,12 @@ class options : public com::centreon::misc::get_options {
void _init();
public:
options();
~options() noexcept;
~options() noexcept override;
options(options const& opts) = delete;
options& operator=(options const& opts) = delete;
std::string help() const;
std::string help() const override;
void parse(int argc, char* argv[]);
std::string usage() const;
std::string usage() const override;
};
CCCP_END()
......
......@@ -38,16 +38,14 @@ namespace orders {
class parser : public handle_listener {
public:
parser();
~parser() noexcept;
~parser() override = default ;
parser(parser const& p) = delete;
parser& operator=(parser const& p) = delete;
void error(handle& h);
std::string const& get_buffer() const noexcept;
listener* get_listener() const noexcept;
void error(handle& h) override;
void listen(listener* l = nullptr) noexcept;
void read(handle& h);
bool want_read(handle& h);
bool want_write(handle& h);
void read(handle& h) override;
bool want_read(handle& h) override;
bool want_write(handle& h) override;
private:
void _parse(std::string const& cmd);
......
......@@ -36,15 +36,15 @@ class pipe_handle : public handle {
public:
pipe_handle(int fd = -1);
~pipe_handle() noexcept;
~pipe_handle() noexcept override;
pipe_handle(pipe_handle const& ph) = delete;
pipe_handle& operator=(pipe_handle const& ph) = delete;
void close() noexcept;
void close() noexcept override;
static void close_all_handles();
native_handle get_native_handle() noexcept;
unsigned long read(void* data, unsigned long size);
native_handle get_native_handle() noexcept override;
unsigned long read(void* data, unsigned long size) override;
void set_fd(int fd);
unsigned long write(void const* data, unsigned long size);
unsigned long write(void const* data, unsigned long size) override;
};
CCCP_END()
......
......@@ -20,7 +20,9 @@
#define CCCP_POLICY_HH
#include <sys/types.h>
#include <map>
#include "com/centreon/connector/perl/checks/listener.hh"
#include "com/centreon/connector/perl/namespace.hh"
#include "com/centreon/connector/perl/orders/listener.hh"
......@@ -53,20 +55,19 @@ class policy : public orders::listener, public checks::listener {
public:
policy();
~policy() noexcept;
~policy() noexcept override;
policy(policy const& p) = delete;
policy& operator=(policy const& p) = delete;
void on_eof();
void on_error();
void on_eof() override;
void on_error() override;
void on_execute(unsigned long long cmd_id,
const timestamp& timeout,
std::string const& cmd);
void on_quit();
void on_result(checks::result const& r);
void on_version();
std::string const& cmd) override;
void on_quit() override;
void on_result(checks::result const& r) override;
void on_version() override;
bool run();
};
CCCP_END()
......
......@@ -20,6 +20,7 @@
#define CCCP_REPORTER_HH
#include <string>
#include "com/centreon/connector/perl/checks/listener.hh"
#include "com/centreon/connector/perl/namespace.hh"
#include "com/centreon/handle_listener.hh"
......@@ -39,16 +40,15 @@ class reporter : public com::centreon::handle_listener {
public:
reporter();
~reporter() noexcept;
~reporter() noexcept override;
reporter(reporter const& r) = delete;
reporter& operator=(reporter const& r) = delete;
bool can_report() const noexcept;
void error(handle& h);
std::string const& get_buffer() const noexcept;
void error(handle& h) override;
void send_result(checks::result const& r);
void send_version(unsigned int major, unsigned int minor);
bool want_write(handle& h);
void write(handle& h);
bool want_write(handle& h) override;
void write(handle& h) override;
};
CCCP_END()
......
......@@ -22,14 +22,15 @@
#include <cstdlib>
#include <memory>
#include "com/centreon/connector/log.hh"
#include "com/centreon/connector/perl/checks/listener.hh"
#include "com/centreon/connector/perl/checks/result.hh"
#include "com/centreon/connector/perl/checks/timeout.hh"
#include "com/centreon/connector/perl/log_v2.h"
#include "com/centreon/connector/perl/embedded_perl.hh"
#include "com/centreon/connector/perl/multiplexer.hh"
using namespace com::centreon;
using namespace com::centreon::connector;
using namespace com::centreon::connector::perl::checks;
/**************************************
......@@ -41,12 +42,12 @@ using namespace com::centreon::connector::perl::checks;
/**
* Default constructor.
*/
check::check() : _child((pid_t)-1), _cmd_id(0), _listnr(NULL), _timeout(0) {}
check::check() : _child((pid_t)-1), _cmd_id(0), _listnr(nullptr), _timeout(0) {}
/**
* Destructor.
*/
check::~check() throw() {
check::~check() noexcept {
try {
// Send result if we haven't already done so.
result r;
......@@ -77,7 +78,7 @@ void check::error(handle& h) {
*
* @return Process ID.
*/
pid_t check::execute(unsigned long long cmd_id,
pid_t check::execute(uint64_t cmd_id,
std::string const& cmd,
const timestamp& tmt) {
// Run process.
......@@ -88,7 +89,7 @@ pid_t check::execute(unsigned long long cmd_id,
_err.set_fd(fds[2]);
// Store command ID.
log_v2::core()->debug("check {0} has ID {1}", static_cast<void*>(this),
log::core()->debug("check {0} has ID {1}", static_cast<void*>(this),
cmd_id);
_cmd_id = cmd_id;
......@@ -111,7 +112,7 @@ pid_t check::execute(unsigned long long cmd_id,
* @param[in] listnr New listener.
*/
void check::listen(listener* listnr) {
log_v2::core()->debug("check {0} is listened by {1}",
log::core()->debug("check {0} is listened by {1}",
static_cast<void*>(this), static_cast<void*>(listnr));
_listnr = listnr;
}
......@@ -123,7 +124,7 @@ void check::listen(listener* listnr) {
*/
void check::on_timeout(bool final) {
// Log message.
log_v2::core()->error("check {0} (pid={1}) reached timeout", _cmd_id, _child);
log::core()->error("check {0} (pid={1}) reached timeout", _cmd_id, _child);
// Reset timeout task ID.
_timeout = 0;
......@@ -142,7 +143,7 @@ void check::on_timeout(bool final) {
// Schedule a final timeout.
std::unique_ptr<timeout> t(new timeout(this, true));
_timeout = multiplexer::instance().com::centreon::task_manager::add(
t.get(), time(NULL) + 1, false, true);
t.get(), time(nullptr) + 1, false, true);
t.release();
}
}
......@@ -156,10 +157,10 @@ void check::read(handle& h) {
char buffer[1024];
unsigned long rb(h.read(buffer, sizeof(buffer)));
if (&h == &_err) {
log_v2::core()->debug("reading from process {}'s stdout", _child);
log::core()->debug("reading from process {}'s stdout", _child);
_stderr.append(buffer, rb);
} else {
log_v2::core()->debug("reading from process {}'s stderr", _child);
log::core()->debug("reading from process {}'s stderr", _child);
_stdout.append(buffer, rb);
}
}
......@@ -171,7 +172,7 @@ void check::read(handle& h) {
*/
void check::terminated(int exit_code) {
// Read possibly remaining data.
log_v2::core()->debug("reading remaining data from process {}", _child);
log::core()->debug("reading remaining data from process {}", _child);
try {
char buffer[1024];
unsigned long rb(_out.read(buffer, sizeof(buffer)));
......@@ -210,9 +211,9 @@ void check::terminated(int exit_code) {
* @param[in] listnr Old listener.
*/
void check::unlisten(listener* listnr) {
log_v2::core()->debug("listener {0} stops listening check {1}",
log::core()->debug("listener {0} stops listening check {1}",
static_cast<void*>(_listnr), static_cast<void*>(this));
_listnr = NULL;
_listnr = nullptr;
}
/**
......@@ -220,8 +221,7 @@ void check::unlisten(listener* listnr) {
*
* @return true.
*/
bool check::want_read(handle& h) {
(void)h;
bool check::want_read([[maybe_unused]] handle& h) {
return true;
}
......@@ -230,9 +230,8 @@ bool check::want_read(handle& h) {
*
* @param[in] h Unused.
*/
void check::write(handle& h) {
void check::write([[maybe_unused]] handle& h) {
// This is an error, we shouldn't have been called.
(void)h;
result r;
r.set_command_id(_cmd_id);
_send_result_and_unregister(r);
......
......@@ -31,39 +31,12 @@ using namespace com::centreon::connector::perl::checks;
*/
result::result() : _cmd_id(0), _executed(false), _exit_code(-1) {}
/**
* Copy constructor.
*
* @param[in] r Object to copy.
*/
result::result(result const& r) {
_internal_copy(r);
}
/**
* Destructor.
*/
result::~result() {}
/**
* Assignment operator.
*
* @param[in] r Object to copy.
*
* @return This object.
*/
result& result::operator=(result const& r) {
if (this != &r)
_internal_copy(r);
return (*this);
}
/**
* Get the command ID.
*
* @return Command ID.
*/
unsigned long long result::get_command_id() const throw() {
uint64_t result::get_command_id() const noexcept {
return (_cmd_id);
}
......@@ -72,7 +45,7 @@ unsigned long long result::get_command_id() const throw() {
*
* @return Check error string.
*/
std::string const& result::get_error() const throw() {
std::string const& result::get_error() const noexcept {
return (_error);
}
......@@ -81,7 +54,7 @@ std::string const& result::get_error() const throw() {
*
* @return true if check was executed, false otherwise.
*/
bool result::get_executed() const throw() {
bool result::get_executed() const noexcept {
return _executed;
}
......@@ -90,7 +63,7 @@ bool result::get_executed() const throw() {
*
* @return Check exit code.
*/
int result::get_exit_code() const throw() {
int result::get_exit_code() const noexcept {
return _exit_code;
}
......@@ -99,7 +72,7 @@ int result::get_exit_code() const throw() {
*
* @return Check output.
*/
std::string const& result::get_output() const throw() {
std::string const& result::get_output() const noexcept {
return (_output);
}
......@@ -108,7 +81,7 @@ std::string const& result::get_output() const throw() {
*
* @param[in] cmd_id Command ID.
*/
void result::set_command_id(unsigned long long cmd_id) throw() {
void result::set_command_id(uint64_t cmd_id) noexcept {
_cmd_id = cmd_id;
}