source: nscp/modules/CheckWMI/WMIQuery.h @ 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.9 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{
63public:
64        struct WMIResult {
65                std::wstring alias;
66                std::wstring string;
67                long long numeric;
68                bool isNumeric;
69                WMIResult() : isNumeric(false), numeric(0) {}
70                void setString(std::wstring a, std::wstring s) {
71                        string = s;
72                        numeric = 0;
73                        alias = a;
74                }
75                void setNumeric(std::wstring a, long long n) {
76                        numeric = n;
77                        string = strEx::itos(n);
78                        alias = a;
79                }
80                void setBoth(std::wstring a, long long n, std::wstring s) {
81                        numeric = n;
82                        string = s;
83                        alias = a;
84                }
85        };
86        struct wmi_row {
87                typedef std::map<std::wstring,WMIResult> list_type;
88                list_type results;
89                boolean hasAlias(std::wstring alias) const {
90                        if (alias.empty())
91                                return true;
92                        return results.find(alias) != results.end();
93                }
94                const WMIResult get(std::wstring alias) const {
95                        WMIResult ret;
96                        list_type::const_iterator cit = results.find(alias);
97                        if (cit != results.end())
98                                ret = (*cit).second;
99                        return ret;
100                }
101                void addValue(std::wstring column, WMIResult value) {
102                        results[column] = value;
103                }
104
105                std::wstring render() {
106                        std::wstring ret;
107                        for (list_type::const_iterator it = results.begin(); it != results.end(); ++it) {
108                                if (!ret.empty())       ret += _T(", ");
109                                ret += (*it).first + _T("=") + (*it).second.string;
110                        }
111                        return ret;
112                }
113
114        };
115        typedef std::list<wmi_row> result_type;
116        struct wmi_filter {
117                std::wstring alias;
118                filters::filter_all_strings string;
119                filters::filter_all_numeric<unsigned long long, checkHolders::int64_handler >  numeric;
120
121                inline bool hasFilter() {
122                        return string.hasFilter() || numeric.hasFilter();
123                }
124                bool matchFilter(const wmi_row &value) const {
125                        if (!value.hasAlias(alias)) {
126                                NSC_DEBUG_MSG_STD(_T("We don't have any column matching: ") + alias);
127                                return false;
128                        }
129                        if (alias.empty()) {
130                                for (wmi_row::list_type::const_iterator cit = value.results.begin(); cit != value.results.end(); ++cit) {
131                                        if ((string.hasFilter())&&(string.matchFilter((*cit).second.string)))
132                                                return true;
133                                        else if ((numeric.hasFilter())&&(numeric.matchFilter((*cit).second.numeric)))
134                                                return true;
135                                }
136                        } else {
137                                if ((string.hasFilter())&&(string.matchFilter(value.get(alias).string)))
138                                        return true;
139                                else if ((numeric.hasFilter())&&(numeric.matchFilter(value.get(alias).numeric)))
140                                        return true;
141                        }
142                        NSC_DEBUG_MSG_STD(_T("Value did not match a filter: ") + alias);
143                        return false;
144                }
145        };
146        WMIQuery(void) {};
147        ~WMIQuery(void) {};
148
149        result_type  execute(std::wstring query);
150        std::wstring sanitize_string(LPTSTR in);
151};
Note: See TracBrowser for help on using the repository browser.