]> granicus.if.org Git - icinga2/commitdiff
Implemented support for external commands.
authorGunnar Beutner <gunnar.beutner@netways.de>
Tue, 22 Jan 2013 07:34:29 +0000 (08:34 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Tue, 22 Jan 2013 07:34:29 +0000 (08:34 +0100)
components/compat/compatcomponent.cpp
lib/icinga/Makefile.am
lib/icinga/externalcommand.cpp [new file with mode: 0644]
lib/icinga/externalcommand.h [new file with mode: 0644]
lib/icinga/i2-icinga.h

index 5f933866ce829bc92002706f0cc496af9859b240..207a19e18f9cb70bab4c8c84d791689057306369 100644 (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)
index 5e40953198aed0e06c98752bdfb8f93e69314caf..e6c9a6dd7558b03b17de1a3e423ae7393985418d 100644 (file)
@@ -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 (file)
index 0000000..3988049
--- /dev/null
@@ -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;
+}
+
diff --git a/lib/icinga/externalcommand.h b/lib/icinga/externalcommand.h
new file mode 100644 (file)
index 0000000..408cdcf
--- /dev/null
@@ -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 */
index f221e68f6001b22f3271a5ac6a0c7ed766dc4ab9..62a952d2eaf6c70191e848f23a81ce0a98a82e67 100644 (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"