source: nscp/modules/CheckWMI/WMIQuery.h @ 7e33d82

0.4.00.4.10.4.2stable
Last change on this file since 7e33d82 was 99e4d8f, checked in by Michael Medin <michael@…>, 5 years ago

2007-11-23 MickeM

  • Converted to unicode (damn sometimes I HATE C++) + Added support for escaping " on the /test syntax so now you can do: CheckWMI MaxCrit=3 "MinWarn=1" "Query:load=Select * from win32_Processor"

2007-11-22 MickeM

  • Fixed so the "default path" is correct even when running as a service (issue: #96)
  • Property mode set to 100644
File size: 5.0 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#pragma once
22
23#include <string>
24#include <map>
25#include <strEx.h>
26#include <error.hpp>
27#include <filter_framework.hpp>
28
29class ComError {
30public:
31        static std::wstring getComError() {
32                USES_CONVERSION;
33                CComPtr<IErrorInfo> errorInfo;
34                HRESULT hr = GetErrorInfo(NULL, &errorInfo);
35                if (FAILED(hr) || hr == S_FALSE)
36                        return _T("unknown error: ") + error::format::from_system(hr);
37                CComBSTR bDesc, bSource;
38                hr = errorInfo->GetSource(&bSource);
39                if (FAILED(hr))
40                        return _T("unknown error: ") + error::format::from_system(hr);
41                hr = errorInfo->GetDescription(&bDesc);
42                if (FAILED(hr))
43                        return _T("unknown error: ") + error::format::from_system(hr);
44                std::wstring ret = OLE2T(bSource);
45                ret += _T(" - ");
46                ret += OLE2T(bDesc);
47                return ret;
48        }
49};
50
51class WMIException {
52        std::wstring message_;
53public:
54        WMIException(std::wstring str, HRESULT code) {
55                message_ = str + _T(":") + error::format::from_system(code);
56        }
57        std::wstring getMessage() {
58                return message_;
59        }
60};
61class WMIQuery
62{
63private:
64        bool bInitialized;
65
66public:
67        struct WMIResult {
68                std::wstring alias;
69                std::wstring string;
70                long long numeric;
71                bool isNumeric;
72                WMIResult() : isNumeric(false), numeric(0) {}
73                void setString(std::wstring a, std::wstring s) {
74                        string = s;
75                        numeric = 0;
76                        alias = a;
77                }
78                void setNumeric(std::wstring a, long long n) {
79                        numeric = n;
80                        string = strEx::itos(n);
81                        alias = a;
82                }
83                void setBoth(std::wstring a, long long n, std::wstring s) {
84                        numeric = n;
85                        string = s;
86                        alias = a;
87                }
88        };
89        struct wmi_row {
90                typedef std::map<std::wstring,WMIResult> list_type;
91                list_type results;
92                boolean hasAlias(std::wstring alias) const {
93                        if (alias.empty())
94                                return true;
95                        return results.find(alias) != results.end();
96                }
97                const WMIResult get(std::wstring alias) const {
98                        WMIResult ret;
99                        list_type::const_iterator cit = results.find(alias);
100                        if (cit != results.end())
101                                ret = (*cit).second;
102                        return ret;
103                }
104                void addValue(std::wstring column, WMIResult value) {
105                        results[column] = value;
106                }
107
108                std::wstring render() {
109                        std::wstring ret;
110                        for (list_type::const_iterator it = results.begin(); it != results.end(); ++it) {
111                                if (!ret.empty())       ret += _T(", ");
112                                ret += (*it).first + _T("=") + (*it).second.string;
113                        }
114                        return ret;
115                }
116
117        };
118        typedef std::list<wmi_row> result_type;
119        struct wmi_filter {
120                std::wstring alias;
121                filters::filter_all_strings string;
122                filters::filter_all_numeric<unsigned long long, checkHolders::int64_handler >  numeric;
123
124                inline bool hasFilter() {
125                        return string.hasFilter() || numeric.hasFilter();
126                }
127                bool matchFilter(const wmi_row &value) const {
128                        if (!value.hasAlias(alias)) {
129                                NSC_DEBUG_MSG_STD(_T("We don't have any column matching: ") + alias);
130                                return false;
131                        }
132                        if (alias.empty()) {
133                                for (wmi_row::list_type::const_iterator cit = value.results.begin(); cit != value.results.end(); ++cit) {
134                                        if ((string.hasFilter())&&(string.matchFilter((*cit).second.string)))
135                                                return true;
136                                        else if ((numeric.hasFilter())&&(numeric.matchFilter((*cit).second.numeric)))
137                                                return true;
138                                }
139                        } else {
140                                if ((string.hasFilter())&&(string.matchFilter(value.get(alias).string)))
141                                        return true;
142                                else if ((numeric.hasFilter())&&(numeric.matchFilter(value.get(alias).numeric)))
143                                        return true;
144                        }
145                        NSC_DEBUG_MSG_STD(_T("Value did not match a filter: ") + alias);
146                        return false;
147                }
148        };
149        WMIQuery(void);
150        ~WMIQuery(void);
151
152        result_type  execute(std::wstring query);
153        std::wstring sanitize_string(LPTSTR in);
154
155        bool initialize();
156        void unInitialize();
157};
Note: See TracBrowser for help on using the repository browser.