Commit 0dc5d7dc authored by Matthieu Kermagoret's avatar Matthieu Kermagoret
Browse files

Connector SSH: should handle request but doesn't.

parent a335919a
......@@ -21,22 +21,34 @@
#ifndef CCCS_POLICY_HH
# define CCCS_POLICY_HH
# include <map>
# include "com/centreon/connector/ssh/checks/listener.hh"
# include "com/centreon/connector/ssh/orders/listener.hh"
# include "com/centreon/connector/ssh/orders/parser.hh"
# include "com/centreon/connector/ssh/policy.hh"
# include "com/centreon/connector/ssh/reporter.hh"
# include "com/centreon/connector/ssh/sessions/credentials.hh"
# include "com/centreon/io/standard_input.hh"
# include "com/centreon/io/standard_output.hh"
CCCS_BEGIN()
// Forward declarations.
namespace checks {
class check;
class result;
}
namespace sessions {
class session;
}
/**
* @class policy policy.hh "com/centreon/connector/ssh/policy.hh"
* @brief Software policy.
*
* Manage program execution.
*/
class policy : public orders::listener {
class policy : public orders::listener,
public checks::listener {
public:
policy();
~policy() throw ();
......@@ -50,6 +62,7 @@ public:
std::string const& password,
std::string const& cmd);
void on_quit();
void on_result(checks::result const& r);
void on_version();
void run();
......@@ -57,8 +70,12 @@ private:
policy(policy const& p);
policy& operator=(policy const& p);
std::map<unsigned long long, checks::check*>
_checks;
orders::parser _parser;
reporter _reporter;
std::map<sessions::credentials, sessions::session*>
_sessions;
io::standard_input _sin;
io::standard_output _sout;
};
......
......@@ -34,8 +34,7 @@ CCCS_BEGIN()
*
* Send replies to the monitoring engine.
*/
class reporter : public com::centreon::handle_listener,
public checks::listener {
class reporter : public com::centreon::handle_listener {
public:
reporter();
reporter(reporter const& r);
......@@ -43,7 +42,7 @@ public:
reporter& operator=(reporter const& r);
void close(handle& h);
void error(handle& h);
void on_result(checks::result const& r);
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);
......
......@@ -20,10 +20,12 @@
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include "com/centreon/connector/ssh/multiplexer.hh"
#include "com/centreon/connector/ssh/policy.hh"
#include "com/centreon/exceptions/basic.hh"
#include "com/centreon/logging/file.hh"
#include "com/centreon/logging/logger.hh"
using namespace com::centreon;
......@@ -61,6 +63,8 @@ int main() {
// Initializations.
logging::engine::load();
multiplexer::load();
logging::file log_file(stdout);
logging::engine::instance().add(&log_file, -1, 63);
#if LIBSSH2_VERSION_NUM >= 0x010205
// Initialize libssh2.
......
......@@ -19,9 +19,13 @@
*/
#include <assert.h>
#include <memory>
#include <stdlib.h>
#include "com/centreon/connector/ssh/checks/check.hh"
#include "com/centreon/connector/ssh/checks/result.hh"
#include "com/centreon/connector/ssh/multiplexer.hh"
#include "com/centreon/connector/ssh/policy.hh"
#include "com/centreon/connector/ssh/sessions/session.hh"
#include "com/centreon/logging/logger.hh"
using namespace com::centreon::connector::ssh;
......@@ -56,6 +60,32 @@ policy::~policy() throw () {
// Remove from multiplexer.
multiplexer::instance().handle_manager::remove(&_sin);
multiplexer::instance().handle_manager::remove(&_sout);
// Close checks.
for (std::map<unsigned long long, checks::check*>::iterator
it = _checks.begin(),
end = _checks.end();
it != end;
++it) {
try {
it->second->unlisten(this);
}
catch (...) {}
delete it->second;
}
// Close sessions.
for (std::map<sessions::credentials, sessions::session*>::iterator
it = _sessions.begin(),
end = _sessions.end();
it != end;
++it) {
try {
it->second->close();
}
catch (...) {}
delete it->second;
}
}
/**
......@@ -94,6 +124,32 @@ void policy::on_execute(
std::string const& user,
std::string const& password,
std::string const& cmd) {
// Credentials.
sessions::credentials creds;
creds.set_host(host);
creds.set_user(user);
creds.set_password(password);
// Find session.
std::map<sessions::credentials, sessions::session*>::iterator it;
it = _sessions.find(creds);
if (it == _sessions.end()) {
logging::info(logging::low) << "creating session for "
<< user << "@" << host;
std::auto_ptr<sessions::session> sess(new sessions::session(creds));
sess->connect();
_sessions[creds] = sess.get();
sess.release();
}
// Launch check.
std::auto_ptr<checks::check> chk(new checks::check);
chk->listen(this);
chk->execute(*it->second, cmd_id, cmd, timeout);
_checks[cmd_id] = chk.get();
chk.release();
return ;
}
/**
......@@ -108,11 +164,37 @@ void policy::on_quit() {
return ;
}
/**
* Check result has arrived.
*
* @param[in] r Check result.
*/
void policy::on_result(checks::result const& r) {
// Remove check from list.
std::map<unsigned long long, checks::check*>::iterator it;
it = _checks.find(r.get_command_id());
if (it != _checks.end()) {
try {
it->second->unlisten(this);
}
catch (...) {}
delete it->second;
_checks.erase(it);
}
// Send check result back to monitoring engine.
_reporter.send_result(r);
return ;
}
/**
* Version request was received.
*/
void policy::on_version() {
// Report version 1.0.
logging::info(logging::low)
<< "monitoring engine requested protocol version, sending 1.0";
_reporter.send_version(1, 0);
return ;
}
......@@ -141,7 +223,8 @@ void policy::run() {
*
* @param[in] p Unused.
*/
policy::policy(policy const& p) : orders::listener(p) {
policy::policy(policy const& p)
: orders::listener(p), checks::listener(p) {
assert(!"policy is not copyable");
abort();
}
......
......@@ -42,8 +42,7 @@ reporter::reporter() {}
*
* @param[in] r Object to copy.
*/
reporter::reporter(reporter const& r)
: com::centreon::handle_listener(r), checks::listener(r) {
reporter::reporter(reporter const& r) : com::centreon::handle_listener(r) {
_copy(r);
}
......@@ -62,7 +61,6 @@ reporter::~reporter() throw () {}
reporter& reporter::operator=(reporter const& r) {
if (this != &r) {
com::centreon::handle_listener::operator=(r);
checks::listener::operator=(r);
_copy(r);
}
return (*this);
......@@ -93,11 +91,11 @@ void reporter::error(handle& h) {
}
/**
* Check result callback.
* Report check result.
*
* @param[in] r Check result.
*/
void reporter::on_result(checks::result const& r) {
void reporter::send_result(checks::result const& r) {
// Build packet.
std::ostringstream oss;
// Packet ID.
......
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