Implement CLR-based checks.

This commit is contained in:
Gunnar Beutner 2014-04-16 10:39:13 +02:00
parent 76fa54ea70
commit 4e7738c5a5
8 changed files with 312 additions and 1 deletions

44
contrib/icinga2clr.cs Normal file
View File

@ -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);
}
}

View File

@ -35,6 +35,10 @@ template CheckCommand "agent-check-command" {
methods.execute = "AgentCheck" methods.execute = "AgentCheck"
} }
template CheckCommand "clr-check-command" {
methods.execute = "ClrCheck"
}
template NotificationCommand "plugin-notification-command" { template NotificationCommand "plugin-notification-command" {
methods.execute = "PluginNotification" methods.execute = "PluginNotification"
} }

View File

@ -15,10 +15,16 @@
# along with this program; if not, write to the Free Software Foundation # along with this program; if not, write to the Free Software Foundation
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. # 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 add_library(methods SHARED
castfuncs.cpp icingachecktask.cpp nullchecktask.cpp nulleventtask.cpp castfuncs.cpp icingachecktask.cpp nullchecktask.cpp nulleventtask.cpp
pluginchecktask.cpp plugineventtask.cpp pluginnotificationtask.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) target_link_libraries(methods ${Boost_LIBRARIES} base config icinga)

View File

@ -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);
}
}

View File

@ -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 */

View File

@ -58,6 +58,7 @@ void PluginCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes
Dictionary::Ptr env = commandObj->GetEnv(); Dictionary::Ptr env = commandObj->GetEnv();
if (env) { if (env) {
ObjectLock olock(env);
BOOST_FOREACH(const Dictionary::Pair& kv, env) { BOOST_FOREACH(const Dictionary::Pair& kv, env) {
String name = kv.second; String name = kv.second;

View File

@ -55,6 +55,7 @@ void PluginEventTask::ScriptFunc(const Checkable::Ptr& checkable)
Dictionary::Ptr env = commandObj->GetEnv(); Dictionary::Ptr env = commandObj->GetEnv();
if (env) { if (env) {
ObjectLock olock(env);
BOOST_FOREACH(const Dictionary::Pair& kv, env) { BOOST_FOREACH(const Dictionary::Pair& kv, env) {
String name = kv.second; String name = kv.second;

View File

@ -70,6 +70,7 @@ void PluginNotificationTask::ScriptFunc(const Notification::Ptr& notification, c
Dictionary::Ptr env = commandObj->GetEnv(); Dictionary::Ptr env = commandObj->GetEnv();
if (env) { if (env) {
ObjectLock olock(env);
BOOST_FOREACH(const Dictionary::Pair& kv, env) { BOOST_FOREACH(const Dictionary::Pair& kv, env) {
String name = kv.second; String name = kv.second;