source: nscp/modules/CheckWMI/WMIQuery.cpp @ 367bf20

0.4.00.4.10.4.2stable
Last change on this file since 367bf20 was 367bf20, checked in by Michael Medin <michael@…>, 5 years ago

2008-04-03 MickeM

  • Moved COM init to "core" (from WMI module) + Added new Check command: CheckTaskSched Use like so:

CheckTaskSched +filter-exit-code==1 ShowAll MaxWarn=1 MaxCrit=1

  • Property mode set to 100644
File size: 4.4 KB
Line 
1/**************************************************************************
2*   Copyright (C) 2004-2007 by Michael Medin <michael@medin.name>         *
3*                                                                         *
4*   This code is part of NSClient++ - http://trac.nakednuns.org/nscp      *
5*                                                                         *
6*   This program is free software; you can redistribute it and/or modify  *
7*   it under the terms of the GNU General Public License as published by  *
8*   the Free Software Foundation; either version 2 of the License, or     *
9*   (at your option) any later version.                                   *
10*                                                                         *
11*   This program is distributed in the hope that it will be useful,       *
12*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14*   GNU General Public License for more details.                          *
15*                                                                         *
16*   You should have received a copy of the GNU General Public License     *
17*   along with this program; if not, write to the                         *
18*   Free Software Foundation, Inc.,                                       *
19*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20***************************************************************************/
21#include "StdAfx.h"
22#include ".\wmiquery.h"
23
24#include <objidl.h>
25#include <Wbemidl.h>
26#include <map>
27
28std::wstring WMIQuery::sanitize_string(LPTSTR in) {
29        TCHAR *p = in;
30        while (*p) {
31                if (p[0] < ' ' || p[0] > '}')
32                        p[0] = '.';
33                p++;
34        }
35        return in;
36}
37
38WMIQuery::result_type WMIQuery::execute(std::wstring query)
39{
40        result_type ret;
41
42        CComPtr< IWbemLocator > locator;
43        HRESULT hr = CoCreateInstance( CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER, IID_IWbemLocator, reinterpret_cast< void** >( &locator ) );
44        if (FAILED(hr)) {
45                throw WMIException(_T("CoCreateInstance for CLSID_WbemAdministrativeLocator failed!"), hr);
46        }
47
48        BSTR bstrNamespace = (_T("root\\cimv2"));
49        CComPtr< IWbemServices > service;
50        hr = locator->ConnectServer( bstrNamespace, NULL, NULL, NULL, WBEM_FLAG_CONNECT_USE_MAX_WAIT, NULL, NULL, &service );
51        if (FAILED(hr)) {
52                throw WMIException(_T("ConnectServer failed!"), hr);
53        }
54        CComBSTR strQuery(query.c_str());
55        BSTR strQL = _T("WQL");
56
57        CComPtr< IEnumWbemClassObject > enumerator;
58        hr = service->ExecQuery( strQL, strQuery, WBEM_FLAG_FORWARD_ONLY, NULL, &enumerator );
59        if (FAILED(hr)) {
60                throw WMIException(_T("ExecQuery failed:") + query + _T(" (reason is: ") + ComError::getComError() + _T(")"), hr);
61        }
62
63        CComPtr< IWbemClassObject > row = NULL;
64        ULONG retcnt;
65        int i=0;
66        while (hr = enumerator->Next( WBEM_INFINITE, 1L, &row, &retcnt ) == WBEM_S_NO_ERROR) {
67                if (SUCCEEDED(hr)) {
68                        if (retcnt > 0) {
69                                SAFEARRAY* pstrNames;
70                                wmi_row returnRow;
71                                hr = row->GetNames(NULL,WBEM_FLAG_ALWAYS|WBEM_FLAG_NONSYSTEM_ONLY,NULL,&pstrNames);
72                                if (FAILED(hr)) {
73                                        throw WMIException(_T("GetNames failed:") + query, hr);
74                                }
75
76                                long index = 0, begin, end;
77                                CComSafeArray<BSTR> arr = pstrNames;
78                                begin = arr.GetLowerBound();
79                                end = arr.GetUpperBound();
80                                for ( index = begin; index <= end; index++ ) {
81                                        USES_CONVERSION;
82                                        CComBSTR bColumn = arr.GetAt(index);
83                                        std::wstring column = OLE2T(bColumn);
84                                        CComVariant vValue;
85                                        hr = row->Get(bColumn, 0, &vValue, 0, 0);
86                                        if (FAILED(hr)) {
87                                                throw WMIException(_T("Failed to get value for ") + column + _T(" in query: ") + query, hr);
88                                        }
89                                        WMIResult value;
90
91                                        if (vValue.vt == VT_INT) {
92                                                value.setNumeric(column, vValue.intVal);
93                                        } else if (vValue.vt == VT_I4) {
94                                                value.setNumeric(column, vValue.lVal);
95                                        } else if (vValue.vt == VT_UINT) {
96                                                value.setNumeric(column, vValue.uintVal);
97                                        } else if (vValue.vt == VT_BSTR) {
98                                                value.setString(column, OLE2T(vValue.bstrVal));
99                                        } else if (vValue.vt == VT_NULL) {
100                                                value.setString(column, _T("NULL"));
101                                        } else if (vValue.vt == VT_BOOL) {
102                                                value.setBoth(column, vValue.iVal, vValue.iVal?_T("TRUE"):_T("FALSE"));
103                                        } else {
104                                                NSC_LOG_ERROR_STD(column + _T(" is not supported (type-id: ") + strEx::itos(vValue.vt) + _T(")"));
105                                        }
106                                        returnRow.addValue(column, value);
107                                }
108                                ret.push_back(returnRow);
109                        }
110                }
111                row.Release();
112        }
113        return ret;
114}
Note: See TracBrowser for help on using the repository browser.