]> granicus.if.org Git - icinga2/commitdiff
Implement CLR-based checks.
authorGunnar Beutner <gunnar@beutner.name>
Wed, 16 Apr 2014 08:39:13 +0000 (10:39 +0200)
committerGunnar Beutner <gunnar@beutner.name>
Wed, 16 Apr 2014 08:39:13 +0000 (10:39 +0200)
contrib/icinga2clr.cs [new file with mode: 0644]
itl/command.conf
lib/methods/CMakeLists.txt
lib/methods/clrchecktask.cpp [new file with mode: 0644]
lib/methods/clrchecktask.h [new file with mode: 0644]
lib/methods/pluginchecktask.cpp
lib/methods/plugineventtask.cpp
lib/methods/pluginnotificationtask.cpp

diff --git a/contrib/icinga2clr.cs b/contrib/icinga2clr.cs
new file mode 100644 (file)
index 0000000..5a2464d
--- /dev/null
@@ -0,0 +1,44 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012-2014 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.             *
+ ******************************************************************************/
+
+using System;
+using System.Collections;
+
+namespace Icinga
+{
+       public enum ServiceState
+       {
+               ServiceOK,
+               ServiceWarning,
+               ServiceCritical,
+               ServiceUnknown
+       }
+
+       public class CheckResult
+       {
+               public ServiceState State;
+               public String Output;
+               public String PerformanceData;
+       }
+
+       public interface ICheckPlugin
+       {
+               CheckResult Check(Hashtable args);
+       }
+}
index 972a2a1aca02a52ac3a2ded8ef5321a4e5b57b6e..c234b98814c1fed7d41e4fa3842ad3959af4fc09 100644 (file)
@@ -35,6 +35,10 @@ template CheckCommand "agent-check-command" {
        methods.execute = "AgentCheck"
 }
 
+template CheckCommand "clr-check-command" {
+       methods.execute = "ClrCheck"
+}
+
 template NotificationCommand "plugin-notification-command" {
        methods.execute = "PluginNotification"
 }
index 6f271b29238b5a5174da3c96cef21d716f3349b9..59bc28d3d946f99c6d1f78d489a005c3c74ed952 100644 (file)
 # along with this program; if not, write to the Free Software Foundation
 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 
+if(MSVC)
+  set(WindowsSources clrchecktask.cpp)
+else()
+  set(WindowsSources "")
+endif()
+
 add_library(methods SHARED
   castfuncs.cpp icingachecktask.cpp nullchecktask.cpp nulleventtask.cpp
   pluginchecktask.cpp plugineventtask.cpp pluginnotificationtask.cpp
-  randomchecktask.cpp timeperiodtask.cpp
+  randomchecktask.cpp timeperiodtask.cpp ${WindowsSources}
 )
 
 target_link_libraries(methods ${Boost_LIBRARIES} base config icinga)
diff --git a/lib/methods/clrchecktask.cpp b/lib/methods/clrchecktask.cpp
new file mode 100644 (file)
index 0000000..e3312a9
--- /dev/null
@@ -0,0 +1,205 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012-2014 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 "methods/clrchecktask.h"
+#include "icinga/pluginutility.h"
+#include "icinga/checkcommand.h"
+#include "icinga/macroprocessor.h"
+#include "icinga/icingaapplication.h"
+#include "base/dynamictype.h"
+#include "base/logger_fwd.h"
+#include "base/scriptfunction.h"
+#include "base/utility.h"
+#include "base/process.h"
+#include <boost/algorithm/string/classification.hpp>
+#include <boost/algorithm/string/split.hpp>
+#include <boost/foreach.hpp>
+#include <objbase.h>
+#include <mscoree.h>
+
+#import "mscorlib.tlb"
+#pragma comment(lib, "mscoree.lib")
+
+using namespace icinga;
+
+REGISTER_SCRIPTFUNCTION(ClrCheck,  &ClrCheckTask::ScriptFunc);
+
+static boost::once_flag l_OnceFlag = BOOST_ONCE_INIT;
+
+static boost::mutex l_ObjectsMutex;
+static std::map<Checkable::Ptr, variant_t> l_Objects;
+
+static mscorlib::_AppDomainPtr l_AppDomain;
+
+static void InitializeClr(void)
+{
+       ICorRuntimeHost *runtimeHost;
+
+       if (FAILED(CorBindToRuntimeEx(NULL, NULL,
+           STARTUP_LOADER_OPTIMIZATION_SINGLE_DOMAIN | STARTUP_CONCURRENT_GC,
+           CLSID_CorRuntimeHost, IID_ICorRuntimeHost, (void **)&runtimeHost))) {
+               return;
+       }
+
+       runtimeHost->Start();
+
+       IUnknownPtr punkAppDomain = NULL;
+       runtimeHost->GetDefaultDomain(&punkAppDomain);
+
+       punkAppDomain->QueryInterface(__uuidof(mscorlib::_AppDomain), (void **)&l_AppDomain);
+
+       runtimeHost->Release();
+}
+
+static variant_t CreateClrType(const String& assemblyName, const String& typeName)
+{
+       boost::call_once(l_OnceFlag, &InitializeClr);
+
+       try {
+               mscorlib::_ObjectHandlePtr pObjectHandle;
+               pObjectHandle = l_AppDomain->CreateInstanceFrom(assemblyName.CStr(), typeName.CStr());
+
+               return pObjectHandle->Unwrap();
+       } catch (_com_error& error) {
+               BOOST_THROW_EXCEPTION(std::runtime_error("Could not load .NET type: " + String(error.Description())));
+       }
+}
+
+static variant_t InvokeClrMethod(const variant_t& vtObject, const String& methodName, const Dictionary::Ptr& args)
+{
+       CLSID clsid;
+       HRESULT hr = CLSIDFromProgID(L"System.Collections.Hashtable", &clsid);
+       
+       mscorlib::IDictionaryPtr pHashtable;
+       CoCreateInstance(clsid, NULL, CLSCTX_ALL, __uuidof(mscorlib::IDictionary), (void **)&pHashtable);
+
+       ObjectLock olock(args);
+       BOOST_FOREACH(const Dictionary::Pair& kv, args) {
+               String value = kv.second;
+               pHashtable->Add(kv.first.CStr(), value.CStr());
+       }
+               
+       mscorlib::_ObjectPtr pObject;
+       vtObject.pdispVal->QueryInterface(__uuidof(mscorlib::_Object), (void**)&pObject);
+       mscorlib::_TypePtr pType = pObject->GetType();
+
+       SAFEARRAY *psa = SafeArrayCreateVector(VT_VARIANT, 0, 1);
+
+       variant_t vtHashtable = static_cast<IUnknown *>(pHashtable);
+       LONG idx = 0;
+       SafeArrayPutElement(psa, &idx, &vtHashtable);
+
+       variant_t result = pType->InvokeMember_3(methodName.CStr(),
+               mscorlib::BindingFlags_InvokeMethod,
+               NULL,
+               vtObject,
+               psa);
+
+       SafeArrayDestroy(psa);
+
+       return result;
+}
+
+static void FillCheckResult(const CheckResult::Ptr& cr, variant_t vtResult)
+{
+       mscorlib::_ObjectPtr pObject;
+       vtResult.pdispVal->QueryInterface(__uuidof(mscorlib::_Object), (void**)&pObject);
+       mscorlib::_TypePtr pType = pObject->GetType();
+
+       SAFEARRAY *psa = SafeArrayCreateVector(VT_VARIANT, 0, 0);
+       int lState = pType->InvokeMember_3("State",
+               mscorlib::BindingFlags_GetField,
+               NULL,
+               vtResult,
+               psa);
+       cr->SetState(static_cast<ServiceState>(lState));
+       bstr_t sOutput = pType->InvokeMember_3("Output",
+               mscorlib::BindingFlags_GetField,
+               NULL,
+               vtResult,
+               psa);
+       cr->SetOutput(static_cast<const char *>(sOutput));
+       bstr_t sPerformanceData = pType->InvokeMember_3("PerformanceData",
+               mscorlib::BindingFlags_GetField,
+               NULL,
+               vtResult,
+               psa);
+       SafeArrayDestroy(psa);
+       cr->SetPerformanceData(static_cast<const char *>(sPerformanceData));
+}
+
+void ClrCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
+{
+       CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
+       Value raw_command = commandObj->GetCommandLine();
+
+       Host::Ptr host;
+       Service::Ptr service;
+       tie(host, service) = GetHostService(checkable);
+
+       MacroProcessor::ResolverList resolvers;
+       if (service)
+               resolvers.push_back(std::make_pair("service", service));
+       resolvers.push_back(std::make_pair("host", host));
+       resolvers.push_back(std::make_pair("command", commandObj));
+       resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance()));
+
+       Dictionary::Ptr envMacros = make_shared<Dictionary>();
+
+       Dictionary::Ptr env = commandObj->GetEnv();
+
+       if (env) {
+               ObjectLock olock(env);
+               BOOST_FOREACH(const Dictionary::Pair& kv, env) {
+                       String name = kv.second;
+
+                       Value value = MacroProcessor::ResolveMacros(name, resolvers, checkable->GetLastCheckResult());
+
+                       envMacros->Set(kv.first, value);
+               }
+       }
+
+       variant_t vtObject;
+
+       {
+               boost::mutex::scoped_lock lock(l_ObjectsMutex);
+
+               std::map<Checkable::Ptr, variant_t>::iterator it = l_Objects.find(checkable);
+
+               if (it != l_Objects.end()) {
+                       vtObject = it->second;
+               } else {
+                       String clr_assembly = MacroProcessor::ResolveMacros("$clr_assembly$", resolvers, checkable->GetLastCheckResult());
+                       String clr_type = MacroProcessor::ResolveMacros("$clr_type$", resolvers, checkable->GetLastCheckResult());
+
+                       vtObject = CreateClrType(clr_assembly, clr_type);
+                       l_Objects[checkable] = vtObject;
+               }
+       }
+
+       try {
+               variant_t vtResult = InvokeClrMethod(vtObject, "Check", envMacros);
+               FillCheckResult(cr, vtResult);
+               checkable->ProcessCheckResult(cr);
+       } catch (_com_error& error) {
+               cr->SetOutput("Failed to invoke .NET method: " + String(error.Description()));
+               cr->SetState(ServiceUnknown);
+               checkable->ProcessCheckResult(cr);
+       }
+}
diff --git a/lib/methods/clrchecktask.h b/lib/methods/clrchecktask.h
new file mode 100644 (file)
index 0000000..28f35f5
--- /dev/null
@@ -0,0 +1,49 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012-2014 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 CLRCHECKTASK_H
+#define CLRCHECKTASK_H
+
+#include "methods/i2-methods.h"
+#include "base/process.h"
+#include "icinga/service.h"
+
+namespace icinga
+{
+
+/**
+ * Implements service checks based CLR libraries.
+ *
+ * @ingroup methods
+ */
+class I2_METHODS_API ClrCheckTask
+{
+public:
+       static void ScriptFunc(const Checkable::Ptr& service, const CheckResult::Ptr& cr);
+
+private:
+       ClrCheckTask(void);
+
+       static void ProcessFinishedHandler(const Checkable::Ptr& service, const CheckResult::Ptr& cr, const ProcessResult& pr);
+
+};
+
+}
+
+#endif /* CLRCHECKTASK_H */
index a5e4db767e6911bb9f7a78b2800120be00120403..3b87faa5f6c9316218f407a250b274c466e76a4d 100644 (file)
@@ -58,6 +58,7 @@ void PluginCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes
        Dictionary::Ptr env = commandObj->GetEnv();
 
        if (env) {
+               ObjectLock olock(env);
                BOOST_FOREACH(const Dictionary::Pair& kv, env) {
                        String name = kv.second;
 
index 355db7efd4f829f1cef74df4877f1e7959c2b62a..8522956a4156d0f788d72e8d7045619d0526deeb 100644 (file)
@@ -55,6 +55,7 @@ void PluginEventTask::ScriptFunc(const Checkable::Ptr& checkable)
        Dictionary::Ptr env = commandObj->GetEnv();
 
        if (env) {
+               ObjectLock olock(env);
                BOOST_FOREACH(const Dictionary::Pair& kv, env) {
                        String name = kv.second;
 
index 09967cf6c3695166cebcec3375c09fd908b612f9..1f69d6b1acdb6e86c58761c61a52ddfcb34c407c 100644 (file)
@@ -70,6 +70,7 @@ void PluginNotificationTask::ScriptFunc(const Notification::Ptr& notification, c
        Dictionary::Ptr env = commandObj->GetEnv();
 
        if (env) {
+               ObjectLock olock(env);
                BOOST_FOREACH(const Dictionary::Pair& kv, env) {
                        String name = kv.second;