Commit 94978798 authored by Matthieu Kermagoret's avatar Matthieu Kermagoret
Browse files

Connector SSH: added reporter class.

parent f002ae9d
......@@ -129,6 +129,7 @@ add_library("${CONNECTORLIB}"
"${SRC_DIR}/multiplexer.cc"
"${SRC_DIR}/orders/listener.cc"
"${SRC_DIR}/orders/parser.cc"
"${SRC_DIR}/reporter.cc"
# "${SRC_DIR}/sessions.cc"
"${SRC_DIR}/sessions/credentials.cc"
"${SRC_DIR}/sessions/listener.cc"
......@@ -143,6 +144,7 @@ add_library("${CONNECTORLIB}"
"${INC_DIR}/namespace.hh"
"${INC_DIR}/orders/listener.hh"
"${INC_DIR}/orders/parser.hh"
"${INC_DIR}/reporter.hh"
# "${INC_DIR}/sessions.hh"
"${INC_DIR}/sessions/credentials.hh"
"${INC_DIR}/sessions/listener.hh"
......
/*
** Copyright 2011 Merethis
**
** This file is part of Centreon Connector SSH.
**
** Centreon Connector SSH is free software: you can redistribute it
** and/or modify it under the terms of the GNU Affero General Public
** License as published by the Free Software Foundation, either version
** 3 of the License, or (at your option) any later version.
**
** Centreon Connector SSH is distributed in the hope that it will be
** useful, but WITHOUT ANY WARRANTY; without even the implied warranty
** of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public
** License along with Centreon Connector SSH. If not, see
** <http://www.gnu.org/licenses/>.
*/
#ifndef CCCS_REPORTER_HH
# define CCCS_REPORTER_HH
# include <string>
# include "com/centreon/connector/ssh/checks/listener.hh"
# include "com/centreon/connector/ssh/namespace.hh"
# include "com/centreon/handle_listener.hh"
CCCS_BEGIN()
/**
* @class reporter reporter.hh "com/centreon/connector/ssh/reporter.hh"
* @brief Report data back to the monitoring engine.
*
* Send replies to the monitoring engine.
*/
class reporter : public com::centreon::handle_listener,
public checks::listener {
public:
reporter();
reporter(reporter const& r);
~reporter() throw ();
reporter& operator=(reporter const& r);
void close(handle& h);
void error(handle& h);
void on_result(checks::result& r);
void send_version(unsigned int major, unsigned int minor);
bool want_write(handle& h);
void write(handle& h);
private:
void _copy(reporter const& r);
std::string _buffer;
};
CCCS_END()
#endif // !CCCS_REPORTER_HH
/*
** Copyright 2011 Merethis
**
** This file is part of Centreon Connector SSH.
**
** Centreon Connector SSH is free software: you can redistribute it
** and/or modify it under the terms of the GNU Affero General Public
** License as published by the Free Software Foundation, either version
** 3 of the License, or (at your option) any later version.
**
** Centreon Connector SSH is distributed in the hope that it will be
** useful, but WITHOUT ANY WARRANTY; without even the implied warranty
** of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public
** License along with Centreon Connector SSH. If not, see
** <http://www.gnu.org/licenses/>.
*/
#include <sstream>
#include "com/centreon/connector/ssh/checks/result.hh"
#include "com/centreon/connector/ssh/reporter.hh"
#include "com/centreon/exceptions/basic.hh"
#include "com/centreon/logging/logger.hh"
using namespace com::centreon::connector::ssh;
/**************************************
* *
* Public Methods *
* *
**************************************/
/**
* Default constructor.
*/
reporter::reporter() {}
/**
* Copy constructor.
*
* @param[in] r Object to copy.
*/
reporter::reporter(reporter const& r)
: com::centreon::handle_listener(r), checks::listener(r) {
_copy(r);
}
/**
* Destructor.
*/
reporter::~reporter() throw () {}
/**
* Assignment operator.
*
* @param[in] r Object to copy.
*
* @return This object.
*/
reporter& reporter::operator=(reporter const& r) {
if (this != &r) {
com::centreon::handle_listener::operator=(r);
checks::listener::operator=(r);
_copy(r);
}
return (*this);
}
/**
* Close event on the handle.
*
* @param[in] h Unused.
*/
void reporter::close(handle& h) {
(void)h;
throw (basic_error()
<< "handle used to report to monitoring engine is closed");
return ;
}
/**
* Error event on the handle.
*
* @param[in] h Unused.
*/
void reporter::error(handle& h) {
(void)h;
throw (basic_error()
<< "error detected on the handle used to report to the monitoring engine");
return ;
}
/**
* Check result callback.
*
* @param[in] r Check result.
*/
void reporter::on_result(checks::result& r) {
// Build packet.
std::ostringstream oss;
// Packet ID.
oss << "3";
oss.put('\0');
// Command ID.
oss << r.get_command_id();
oss.put('\0');
// Executed.
oss << (r.get_executed() ? "1" : "0");
oss.put('\0');
// Exit code.
oss << r.get_exit_code();
oss.put('\0');
// Error output.
if (r.get_error().empty())
oss.put(' ');
else
oss << r.get_error();
oss.put('\0');
// Standard output.
if (r.get_output().empty())
oss.put(' ');
else
oss << r.get_output();
// Packet boundary.
for (unsigned int i = 0; i < 4; ++i)
oss.put('\0');
// Append packet to write buffer.
_buffer.append(oss.str());
return ;
}
/**
* Send protocol version to monitoring engine.
*
* @param[in] major Major protocol version.
* @param[in] minor Minor protocol version.
*/
void reporter::send_version(unsigned int major, unsigned int minor) {
// Build packet.
logging::debug(logging::medium) << "sending protocol version "
<< major << "." << minor << " to monitoring engine";
std::ostringstream oss;
oss << "1";
oss.put('\0');
// Major.
oss << major;
oss.put('\0');
// Minor.
oss << minor;
for (unsigned int i = 0; i < 4; ++i)
oss.put('\0');
// Send packet back to monitoring engine.
_buffer.append(oss.str());
return ;
}
/**
* Do we want to send something to the monitoring engine ?
*
* @param[in] h Monitoring engine handle.
*/
bool reporter::want_write(handle& h) {
(void)h;
return (!_buffer.empty());
}
/**
* Send data to the monitoring engine.
*
* @param[in] h Handle.
*/
void reporter::write(handle& h) {
unsigned long wb(h.write(_buffer.c_str(), _buffer.size()));
_buffer.erase(0, wb);
return ;
}
/**************************************
* *
* Private Methods *
* *
**************************************/
/**
* Copy internal data members.
*
* @param[in] r Object to copy.
*/
void reporter::_copy(reporter const& r) {
_buffer = r._buffer;
return ;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment