1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "methods/clrchecktask.hpp"
21 #include "icinga/pluginutility.hpp"
22 #include "icinga/checkcommand.hpp"
23 #include "icinga/macroprocessor.hpp"
24 #include "icinga/icingaapplication.hpp"
25 #include "base/dynamictype.hpp"
26 #include "base/logger.hpp"
27 #include "base/scriptfunction.hpp"
28 #include "base/utility.hpp"
29 #include "base/process.hpp"
30 #include <boost/algorithm/string/classification.hpp>
31 #include <boost/algorithm/string/split.hpp>
32 #include <boost/foreach.hpp>
33 #include <boost/thread/once.hpp>
37 #import "mscorlib.tlb"
38 #pragma comment(lib, "mscoree.lib")
40 using namespace icinga;
42 REGISTER_SCRIPTFUNCTION(ClrCheck, &ClrCheckTask::ScriptFunc);
44 static boost::once_flag l_OnceFlag = BOOST_ONCE_INIT;
46 static boost::mutex l_ObjectsMutex;
47 static std::map<Checkable::Ptr, variant_t> l_Objects;
49 static mscorlib::_AppDomainPtr l_AppDomain;
51 static void InitializeClr(void)
53 ICorRuntimeHost *runtimeHost;
55 if (FAILED(CorBindToRuntimeEx(NULL, NULL,
56 STARTUP_LOADER_OPTIMIZATION_SINGLE_DOMAIN | STARTUP_CONCURRENT_GC,
57 CLSID_CorRuntimeHost, IID_ICorRuntimeHost, (void **)&runtimeHost))) {
63 IUnknownPtr punkAppDomain = NULL;
64 runtimeHost->GetDefaultDomain(&punkAppDomain);
66 punkAppDomain->QueryInterface(__uuidof(mscorlib::_AppDomain), (void **)&l_AppDomain);
68 runtimeHost->Release();
71 static variant_t CreateClrType(const String& assemblyName, const String& typeName)
73 boost::call_once(l_OnceFlag, &InitializeClr);
76 mscorlib::_ObjectHandlePtr pObjectHandle;
77 pObjectHandle = l_AppDomain->CreateInstanceFrom(assemblyName.CStr(), typeName.CStr());
79 return pObjectHandle->Unwrap();
80 } catch (_com_error& error) {
81 BOOST_THROW_EXCEPTION(std::runtime_error("Could not load .NET type: " + String(error.Description())));
85 static variant_t InvokeClrMethod(const variant_t& vtObject, const String& methodName, const Dictionary::Ptr& args)
88 HRESULT hr = CLSIDFromProgID(L"System.Collections.Hashtable", &clsid);
90 mscorlib::IDictionaryPtr pHashtable;
91 CoCreateInstance(clsid, NULL, CLSCTX_ALL, __uuidof(mscorlib::IDictionary), (void **)&pHashtable);
93 ObjectLock olock(args);
94 BOOST_FOREACH(const Dictionary::Pair& kv, args) {
95 String value = kv.second;
96 pHashtable->Add(kv.first.CStr(), value.CStr());
99 mscorlib::_ObjectPtr pObject;
100 vtObject.pdispVal->QueryInterface(__uuidof(mscorlib::_Object), (void**)&pObject);
101 mscorlib::_TypePtr pType = pObject->GetType();
103 SAFEARRAY *psa = SafeArrayCreateVector(VT_VARIANT, 0, 1);
105 variant_t vtHashtable = static_cast<IUnknown *>(pHashtable);
107 SafeArrayPutElement(psa, &idx, &vtHashtable);
109 variant_t result = pType->InvokeMember_3(methodName.CStr(),
110 mscorlib::BindingFlags_InvokeMethod,
115 SafeArrayDestroy(psa);
120 static void FillCheckResult(const CheckResult::Ptr& cr, variant_t vtResult)
122 mscorlib::_ObjectPtr pObject;
123 vtResult.pdispVal->QueryInterface(__uuidof(mscorlib::_Object), (void**)&pObject);
124 mscorlib::_TypePtr pType = pObject->GetType();
126 SAFEARRAY *psa = SafeArrayCreateVector(VT_VARIANT, 0, 0);
127 int lState = pType->InvokeMember_3("State",
128 mscorlib::BindingFlags_GetField,
132 cr->SetState(static_cast<ServiceState>(lState));
133 bstr_t sOutput = pType->InvokeMember_3("Output",
134 mscorlib::BindingFlags_GetField,
138 cr->SetOutput(static_cast<const char *>(sOutput));
139 bstr_t sPerformanceData = pType->InvokeMember_3("PerformanceData",
140 mscorlib::BindingFlags_GetField,
144 SafeArrayDestroy(psa);
145 cr->SetPerformanceData(PluginUtility::SplitPerfdata(static_cast<const char *>(sPerformanceData)));
148 void ClrCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
150 CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
151 Value raw_command = commandObj->GetCommandLine();
154 Service::Ptr service;
155 tie(host, service) = GetHostService(checkable);
157 MacroProcessor::ResolverList resolvers;
159 resolvers.push_back(std::make_pair("service", service));
160 resolvers.push_back(std::make_pair("host", host));
161 resolvers.push_back(std::make_pair("command", commandObj));
162 resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance()));
164 Dictionary::Ptr envMacros = make_shared<Dictionary>();
166 Dictionary::Ptr env = commandObj->GetEnv();
169 ObjectLock olock(env);
170 BOOST_FOREACH(const Dictionary::Pair& kv, env) {
171 String name = kv.second;
173 Value value = MacroProcessor::ResolveMacros(name, resolvers, checkable->GetLastCheckResult());
175 envMacros->Set(kv.first, value);
182 boost::mutex::scoped_lock lock(l_ObjectsMutex);
184 std::map<Checkable::Ptr, variant_t>::iterator it = l_Objects.find(checkable);
186 if (it != l_Objects.end()) {
187 vtObject = it->second;
189 String clr_assembly = MacroProcessor::ResolveMacros("$clr_assembly$", resolvers, checkable->GetLastCheckResult());
190 String clr_type = MacroProcessor::ResolveMacros("$clr_type$", resolvers, checkable->GetLastCheckResult());
192 vtObject = CreateClrType(clr_assembly, clr_type);
193 l_Objects[checkable] = vtObject;
198 variant_t vtResult = InvokeClrMethod(vtObject, "Check", envMacros);
199 FillCheckResult(cr, vtResult);
200 checkable->ProcessCheckResult(cr);
201 } catch (_com_error& error) {
202 cr->SetOutput("Failed to invoke .NET method: " + String(error.Description()));
203 cr->SetState(ServiceUnknown);
204 checkable->ProcessCheckResult(cr);