From: Gunnar Beutner Date: Tue, 22 Jan 2013 07:34:29 +0000 (+0100) Subject: Implemented support for external commands. X-Git-Tag: v0.0.2~697 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c20ae866b7ddcb8ce84d94601151df30e72d184e;p=icinga2 Implemented support for external commands. --- diff --git a/components/compat/compatcomponent.cpp b/components/compat/compatcomponent.cpp index 5f933866c..207a19e18 100644 --- a/components/compat/compatcomponent.cpp +++ b/components/compat/compatcomponent.cpp @@ -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 argvExtra(argv.begin() + 1, argv.end()); + ExternalCommand::Execute(argv[0], argvExtra); } void CompatComponent::DumpHostStatus(ofstream& fp, const Host::Ptr& host) diff --git a/lib/icinga/Makefile.am b/lib/icinga/Makefile.am index 5e4095319..e6c9a6dd7 100644 --- a/lib/icinga/Makefile.am +++ b/lib/icinga/Makefile.am @@ -7,6 +7,8 @@ pkglib_LTLIBRARIES = \ libicinga_la_SOURCES = \ cib.cpp \ cib.h \ + externalcommand.cpp \ + externalcommand.h \ host.cpp \ hostgroup.cpp \ hostgroup.h \ diff --git a/lib/icinga/externalcommand.cpp b/lib/icinga/externalcommand.cpp new file mode 100644 index 000000000..3988049e8 --- /dev/null +++ b/lib/icinga/externalcommand.cpp @@ -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 I2_EXPORT ExternalCommand::m_Commands; + +int ExternalCommand::Execute(const String& command, const vector& arguments) +{ + if (!m_Initialized) { + RegisterCommand("HELLO_WORLD", &ExternalCommand::HelloWorld); + + m_Initialized = true; + } + + map::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& arguments) +{ + Logger::Write(LogInformation, "icinga", "HelloWorld external command called."); + + return 0; +} + diff --git a/lib/icinga/externalcommand.h b/lib/icinga/externalcommand.h new file mode 100644 index 000000000..408cdcf09 --- /dev/null +++ b/lib/icinga/externalcommand.h @@ -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& arguments); + + static int HelloWorld(const vector& arguments); + +private: + typedef function& arguments)> Callback; + + static bool m_Initialized; + static map m_Commands; + + ExternalCommand(void); + + static void RegisterCommand(const String& command, const Callback& callback); +}; + +} + +#endif /* EXTERNALCOMMAND_H */ diff --git a/lib/icinga/i2-icinga.h b/lib/icinga/i2-icinga.h index f221e68f6..62a952d2e 100644 --- a/lib/icinga/i2-icinga.h +++ b/lib/icinga/i2-icinga.h @@ -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"