Implemented support for external commands.

This commit is contained in:
Gunnar Beutner 2013-01-22 08:34:29 +01:00
parent 624b17be1a
commit c20ae866b7
5 changed files with 108 additions and 0 deletions

View File

@ -174,6 +174,9 @@ void CompatComponent::ProcessCommand(const String& command)
stringstream msgbuf;
msgbuf << "Received command (@" << ts << "), command: " << argv[0] << ", " << argv.size() - 1 << " arguments; raw: " << command;
Logger::Write(LogInformation, "compat", msgbuf.str());
vector<String> argvExtra(argv.begin() + 1, argv.end());
ExternalCommand::Execute(argv[0], argvExtra);
}
void CompatComponent::DumpHostStatus(ofstream& fp, const Host::Ptr& host)

View File

@ -7,6 +7,8 @@ pkglib_LTLIBRARIES = \
libicinga_la_SOURCES = \
cib.cpp \
cib.h \
externalcommand.cpp \
externalcommand.h \
host.cpp \
hostgroup.cpp \
hostgroup.h \

View File

@ -0,0 +1,55 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#include "i2-icinga.h"
using namespace icinga;
bool I2_EXPORT ExternalCommand::m_Initialized;
map<String, ExternalCommand::Callback> I2_EXPORT ExternalCommand::m_Commands;
int ExternalCommand::Execute(const String& command, const vector<String>& arguments)
{
if (!m_Initialized) {
RegisterCommand("HELLO_WORLD", &ExternalCommand::HelloWorld);
m_Initialized = true;
}
map<String, ExternalCommand::Callback>::iterator it;
it = m_Commands.find(command);
if (it == m_Commands.end())
return -1;
return it->second(arguments);
}
void ExternalCommand::RegisterCommand(const String& command, const ExternalCommand::Callback& callback)
{
m_Commands[command] = callback;
}
int ExternalCommand::HelloWorld(const vector<String>& arguments)
{
Logger::Write(LogInformation, "icinga", "HelloWorld external command called.");
return 0;
}

View File

@ -0,0 +1,46 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#ifndef EXTERNALCOMMAND_H
#define EXTERNALCOMMAND_H
namespace icinga
{
class I2_ICINGA_API ExternalCommand {
public:
static int Execute(const String& command, const vector<String>& arguments);
static int HelloWorld(const vector<String>& arguments);
private:
typedef function<int (const vector<String>& arguments)> Callback;
static bool m_Initialized;
static map<String, Callback> m_Commands;
ExternalCommand(void);
static void RegisterCommand(const String& command, const Callback& callback);
};
}
#endif /* EXTERNALCOMMAND_H */

View File

@ -40,6 +40,8 @@ using boost::algorithm::is_any_of;
# define I2_ICINGA_API I2_IMPORT
#endif /* I2_ICINGA_BUILD */
#include "externalcommand.h"
#include "endpoint.h"
#include "endpointmanager.h"
#include "icingaapplication.h"