Module: check_mk
Branch: master
Commit: 3839190d1fe16a256c15ccda24c0a4f9cac18944
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=3839190d1fe16a…
Author: Sebastian Herbord <sh(a)mathias-kettner.de>
Date: Wed Dec 16 13:58:07 2015 +0100
slight fixes to wmi helper and added a way to retrieve whole classes without a query
---
agents/windows/wmiHelper.cc | 47 ++++++++++++++++++++++++++++++++++---------
agents/windows/wmiHelper.h | 25 ++++++++++++++---------
2 files changed, 52 insertions(+), 20 deletions(-)
diff --git a/agents/windows/wmiHelper.cc b/agents/windows/wmiHelper.cc
index 58251da..fad09ea 100644
--- a/agents/windows/wmiHelper.cc
+++ b/agents/windows/wmiHelper.cc
@@ -161,7 +161,6 @@ Result::Result(const Result &reference)
: ObjectWrapper(NULL)
, _enumerator(reference._enumerator)
{
- next();
}
@@ -169,7 +168,13 @@ Result::Result(IEnumWbemClassObject *enumerator)
: ObjectWrapper(NULL)
, _enumerator(enumerator, releaseInterface)
{
- next();
+ if (!next()) {
+ // if the first enumeration fails the result is empty
+ // we abstract away two possible reasons:
+ // a) The class doesn't exist at all
+ // b) The result is indeed empty
+ _enumerator = NULL;
+ }
}
@@ -241,7 +246,8 @@ bool Result::next()
if (FAILED(res)) {
// in this case the "current" object isn't changed to guarantee
that the
// Result remains valid
- throw ComException("Failed to retrieve element", res);
+ //throw ComException("Failed to retrieve element", res);
+ return false;
}
if (numReturned == 0) {
@@ -255,7 +261,7 @@ bool Result::next()
}
-template <> int Variant::get()
+template <> int Variant::get() const
{
switch (_value.vt) {
case VT_I1: return _value.bVal;
@@ -269,7 +275,7 @@ template <> int Variant::get()
}
-template <> bool Variant::get()
+template <> bool Variant::get() const
{
switch (_value.vt) {
case VT_BOOL: return _value.boolVal;
@@ -278,7 +284,7 @@ template <> bool Variant::get()
}
-template <> ULONG Variant::get()
+template <> ULONG Variant::get() const
{
switch (_value.vt) {
case VT_UI1: return _value.cVal;
@@ -289,7 +295,7 @@ template <> ULONG Variant::get()
}
-template <> ULONGLONG Variant::get()
+template <> ULONGLONG Variant::get() const
{
switch (_value.vt) {
case VT_UI8: return _value.ullVal;
@@ -298,7 +304,7 @@ template <> ULONGLONG Variant::get()
}
-template <> string Variant::get()
+template <> string Variant::get() const
{
switch (_value.vt) {
case VT_BSTR: return to_utf8(_value.bstrVal);
@@ -307,7 +313,13 @@ template <> string Variant::get()
}
-template <> wstring Variant::get()
+VARTYPE Variant::type() const
+{
+ return _value.vt;
+}
+
+
+template <> wstring Variant::get() const
{
if (_value.vt & VT_ARRAY) {
return L"<array>";
@@ -322,10 +334,11 @@ template <> wstring Variant::get()
case VT_I1:
case VT_I2:
case VT_I4:
+ return std::to_wstring(get<int>());
case VT_UI1:
case VT_UI2:
case VT_UI4:
- return std::to_wstring(get<int>());
+ return std::to_wstring(get<ULONG>());
case VT_UI8:
return std::to_wstring(get<ULONGLONG>());
case VT_BOOL:
@@ -472,6 +485,20 @@ Result Helper::query(LPCWSTR query)
return Result(enumerator);
}
+extern void crash_log(const char *format, ...) __attribute__ ((format (gnu_printf, 1,
2)));
+
+Result Helper::getClass(LPCWSTR className)
+{
+ IEnumWbemClassObject *enumerator = NULL;
+ HRESULT res = _services->CreateInstanceEnum(_bstr_t(className),
+ WBEM_FLAG_FORWARD_ONLY |
WBEM_FLAG_RETURN_IMMEDIATELY,
+ NULL, &enumerator);
+ if (FAILED(res)) {
+ throw ComException(string("Failed to enum class \"") +
to_utf8(className) + "\"", res);
+ }
+ return Result(enumerator);
+}
+
ObjectWrapper Helper::call(ObjectWrapper &result, LPCWSTR method)
{
diff --git a/agents/windows/wmiHelper.h b/agents/windows/wmiHelper.h
index 4695bd1..c1cbff0 100644
--- a/agents/windows/wmiHelper.h
+++ b/agents/windows/wmiHelper.h
@@ -32,6 +32,7 @@
#include <wbemidl.h>
#include <vector>
#include <memory>
+#include <sstream>
std::string to_utf8(const wchar_t *string);
@@ -42,9 +43,9 @@ namespace wmi {
struct ComException : public std::runtime_error {
ComException(const std::string &message, HRESULT result);
+ static std::string resolveError(HRESULT result);
private:
- std::string resolveError(HRESULT result);
- IErrorInfo *getErrorInfo();
+ static IErrorInfo *getErrorInfo();
std::string toStringHex(HRESULT res);
};
@@ -64,18 +65,20 @@ public:
Variant(const VARIANT &val);
~Variant();
- template <typename T> T get();
+ template <typename T> T get() const;
+
+ VARTYPE type() const;
private:
};
-template <> int Variant::get();
-template <> bool Variant::get();
-template <> ULONG Variant::get();
-template <> ULONGLONG Variant::get();
-template <> std::string Variant::get();
-template <> std::wstring Variant::get();
+template <> int Variant::get() const;
+template <> bool Variant::get() const;
+template <> ULONG Variant::get() const;
+template <> ULONGLONG Variant::get() const;
+template <> std::string Variant::get() const;
+template <> std::wstring Variant::get() const;
class ObjectWrapper {
@@ -128,7 +131,7 @@ template <typename T> T ObjectWrapper::get(const wchar_t *key)
const
class Result : public ObjectWrapper
{
- std::shared_ptr<IEnumWbemClassObject> _enumerator;
+ std::shared_ptr<IEnumWbemClassObject> _enumerator { NULL };
public:
@@ -169,6 +172,8 @@ public:
~Helper();
Result query(LPCWSTR query);
+ Result getClass(LPCWSTR className);
+
ObjectWrapper call(ObjectWrapper &result, LPCWSTR method);