source: nscp/modules/CheckWMI/CheckWMI.cpp @ 34e7428

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

Added new option (namespace) to CheckWMI and CheckWMIValue use like so:

CheckWMI namespace=root
cimv2 MaxCrit=3 MinWarn=1 "Query:load=Select * from win32_Processor"

  • Property mode set to 100644
File size: 12.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
22#include "stdafx.h"
23#include "CheckWMI.h"
24#include <strEx.h>
25#include <time.h>
26#include <map>
27#include <vector>
28
29
30CheckWMI gCheckWMI;
31
32BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
33{
34        NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call);
35        return TRUE;
36}
37
38CheckWMI::CheckWMI() {
39}
40CheckWMI::~CheckWMI() {
41}
42
43
44bool CheckWMI::loadModule() {
45        try {
46                NSCModuleHelper::registerCommand(_T("CheckWMIValue"), _T("Run a WMI query and check the resulting value (the values of each row determin the state)."));
47                NSCModuleHelper::registerCommand(_T("CheckWMI"), _T("Run a WMI query and check the resulting rows (the number of hits determine state)."));
48        } catch (NSCModuleHelper::NSCMHExcpetion &e) {
49                NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_);
50        } catch (...) {
51                NSC_LOG_ERROR_STD(_T("Failed to register command."));
52        }
53        return true;
54}
55bool CheckWMI::unloadModule() {
56        return true;
57}
58
59bool CheckWMI::hasCommandHandler() {
60        return true;
61}
62bool CheckWMI::hasMessageHandler() {
63        return false;
64}
65
66
67#define MAP_CHAINED_FILTER(value, obj) \
68                        else if (p__.first.length() > 8 && p__.first.substr(1,6) == _T("filter") && p__.first.substr(7,1) == _T("-") && p__.first.substr(8) == value) { \
69                        WMIQuery::wmi_filter filter; filter.obj = p__.second; chain.push_filter(p__.first, filter); }
70
71#define MAP_SECONDARY_CHAINED_FILTER(value, obj) \
72                        else if (p2.first.length() > 8 && p2.first.substr(1,6) == _T("filter") && p2.first.substr(7,1) == _T("-") && p2.first.substr(8) == value) { \
73                        WMIQuery::wmi_filter filter; filter.obj = p__.second; filter.alias = p2.second; chain.push_filter(p__.first, filter); }
74
75#define MAP_CHAINED_FILTER_STRING(value) \
76        MAP_CHAINED_FILTER(value, string)
77
78#define MAP_CHAINED_FILTER_NUMERIC(value) \
79        MAP_CHAINED_FILTER(value, numeric)
80
81NSCAPI::nagiosReturn CheckWMI::CheckSimpleWMI(const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf) {
82        typedef checkHolders::CheckContainer<checkHolders::MaxMinBounds<checkHolders::NumericBounds<int, checkHolders::int_handler> > > WMIContainer;
83
84        NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK;
85        typedef filters::chained_filter<WMIQuery::wmi_filter,WMIQuery::wmi_row> filter_chain;
86        filter_chain chain;
87        std::list<std::wstring> args = arrayBuffer::arrayBuffer2list(argLen, char_args);
88        if (args.empty()) {
89                message = _T("Missing argument(s).");
90                return NSCAPI::returnCRIT;
91        }
92        unsigned int truncate = 0;
93        std::wstring query, alias;
94        std::wstring ns = _T("root\\cimv2");
95        bool bPerfData = true;
96
97        WMIContainer result_query;
98        try {
99                MAP_OPTIONS_BEGIN(args)
100                MAP_OPTIONS_STR(_T("Query"), query)
101                MAP_OPTIONS_STR2INT(_T("truncate"), truncate)
102                MAP_OPTIONS_STR2INT(_T("namespace"), ns)
103                MAP_OPTIONS_STR(_T("Alias"), alias)
104                MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData)
105                MAP_OPTIONS_NUMERIC_ALL(result_query, _T(""))
106                MAP_OPTIONS_SHOWALL(result_query)
107                MAP_CHAINED_FILTER(_T("string"),string)
108                MAP_CHAINED_FILTER(_T("numeric"),numeric)
109                MAP_OPTIONS_SECONDARY_BEGIN(_T(":"), p2)
110                MAP_SECONDARY_CHAINED_FILTER(_T("string"),string)
111                MAP_SECONDARY_CHAINED_FILTER(_T("numeric"),numeric)
112                        else if (p2.first == _T("Query")) {
113                                        query = p__.second;
114                                        alias = p2.second;
115                                }
116                MAP_OPTIONS_MISSING_EX(p2, message, _T("Unknown argument: "))
117                        MAP_OPTIONS_SECONDARY_END()
118                MAP_OPTIONS_END()
119        } catch (filters::parse_exception e) {
120                message = _T("WMIQuery failed: ") + e.getMessage();
121                return NSCAPI::returnCRIT;
122        }
123
124        WMIQuery::result_type rows;
125        try {
126                WMIQuery wmiQuery;
127                rows = wmiQuery.execute(ns, query);
128        } catch (WMIException e) {
129                message = _T("WMIQuery failed: ") + e.getMessage();
130                return NSCAPI::returnCRIT;
131        }
132        int hit_count = 0;
133
134        bool match = chain.get_inital_state();
135        for (WMIQuery::result_type::iterator citRow = rows.begin(); citRow != rows.end(); ++citRow) {
136                WMIQuery::wmi_row vals = *citRow;
137                match = chain.match(match, vals);
138                if (match) {
139                        strEx::append_list(message, vals.render());
140                        hit_count++;
141                }
142        }
143
144        if (!bPerfData)
145                result_query.perfData = false;
146        result_query.runCheck(hit_count, returnCode, message, perf);
147        if ((truncate > 0) && (message.length() > (truncate-4)))
148                message = message.substr(0, truncate-4) + _T("...");
149        if (message.empty())
150                message = _T("OK: WMI Query returned no results.");
151        return returnCode;
152}
153
154NSCAPI::nagiosReturn CheckWMI::CheckSimpleWMIValue(const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf) {
155        typedef checkHolders::CheckContainer<checkHolders::MaxMinBounds<checkHolders::NumericBounds<long long, checkHolders::int64_handler> > > WMIContainer;
156        std::list<std::wstring> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args);
157        if (stl_args.empty()) {
158                message = _T("ERROR: Missing argument exception.");
159                return NSCAPI::returnUNKNOWN;
160        }
161        std::list<WMIContainer> list;
162        NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK;
163        WMIContainer tmpObject;
164        bool bPerfData = true;
165        unsigned int truncate = 0;
166        std::wstring query;
167        std::wstring ns = _T("root\\cimv2");
168        std::wstring aliasCol;
169
170        // Query=Select ... MaxWarn=5 MaxCrit=12 Check=Col1 --(later)-- Match==test Check=Col2
171        // MaxWarnNumeric:ID=>5
172        try {
173                MAP_OPTIONS_BEGIN(stl_args)
174                        MAP_OPTIONS_SHOWALL(tmpObject)
175                        MAP_OPTIONS_NUMERIC_ALL(tmpObject, _T(""))
176                        MAP_OPTIONS_STR2INT(_T("namespace"), ns)
177                        MAP_OPTIONS_STR(_T("Alias"), tmpObject.data)
178                        MAP_OPTIONS_STR(_T("AliasCol"), aliasCol)
179                        MAP_OPTIONS_STR(_T("Query"), query)
180                        MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData)
181                        MAP_OPTIONS_STR_AND(_T("Check"), tmpObject.data, list.push_back(tmpObject))
182                        MAP_OPTIONS_STR(_T("Alias"), tmpObject.data)
183                        MAP_OPTIONS_SECONDARY_BEGIN(_T(":"), p2)
184                        MAP_OPTIONS_SECONDARY_STR_AND(p2,_T("Check"), tmpObject.data, tmpObject.alias, list.push_back(tmpObject))
185                                MAP_OPTIONS_MISSING_EX(p2, message, _T("Unknown argument: "))
186                                MAP_OPTIONS_SECONDARY_END()
187                        MAP_OPTIONS_MISSING(message, _T("Unknown argument: "))
188                        MAP_OPTIONS_END()
189
190        } catch (filters::parse_exception e) {
191                message = _T("WMIQuery failed: ") + e.getMessage();
192                return NSCAPI::returnCRIT;
193        }
194
195        WMIQuery::result_type rows;
196        try {
197                WMIQuery wmiQuery;
198                rows = wmiQuery.execute(ns, query);
199        } catch (WMIException e) {
200                message = _T("WMIQuery failed: ") + e.getMessage();
201                return NSCAPI::returnCRIT;
202        }
203        int hit_count = 0;
204
205        for (std::list<WMIContainer>::const_iterator it = list.begin(); it != list.end(); ++it) {
206                WMIContainer itm = (*it);
207                itm.setDefault(tmpObject);
208                itm.perfData = bPerfData;
209                if (itm.data == _T("*")) {
210                        for (WMIQuery::result_type::const_iterator citRow = rows.begin(); citRow != rows.end(); ++citRow) {
211                                for (WMIQuery::wmi_row::list_type::const_iterator citCol = (*citRow).results.begin(); citCol != (*citRow).results.end(); ++citCol) {
212                                        long long value = (*citCol).second.numeric;
213                                        itm.runCheck(value, returnCode, message, perf);
214                                }
215                        }
216                }
217        }
218        for (WMIQuery::result_type::const_iterator citRow = rows.begin(); citRow != rows.end(); ++citRow) {
219                bool found = false;
220                std::wstring alias;
221                if (!aliasCol.empty()) {
222                        alias = (*citRow).get(aliasCol).string;
223                }
224                for (WMIQuery::wmi_row::list_type::const_iterator citCol = (*citRow).results.begin(); citCol != (*citRow).results.end(); ++citCol) {
225                        for (std::list<WMIContainer>::const_iterator it = list.begin(); it != list.end(); ++it) {
226                                WMIContainer itm = (*it);
227                                if (itm.data == _T("*")) {
228                                        found = true;
229                                } else if ((*citCol).first == itm.data) {
230                                        std::wstring oldAlias = itm.alias;
231                                        if (!alias.empty())
232                                                itm.alias = alias + _T(" ") + itm.getAlias();
233                                        found = true;
234                                        long long value = (*citCol).second.numeric;
235                                        itm.runCheck(value, returnCode, message, perf);
236                                        itm.alias = oldAlias;
237                                }
238                        }
239                }
240                if (!found) {
241                        NSC_LOG_ERROR_STD(_T("At least one of the queried columns was not found!"));
242                }
243        }
244
245        if ((truncate > 0) && (message.length() > (truncate-4)))
246                message = message.substr(0, truncate-4) + _T("...");
247        if (message.empty())
248                message = _T("OK: WMI Query returned no results.");
249        return returnCode;
250}
251
252
253NSCAPI::nagiosReturn CheckWMI::handleCommand(const strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf) {
254        if (command == _T("CheckWMI")) {
255                return CheckSimpleWMI(argLen, char_args, msg, perf);
256        } else if (command == _T("CheckWMIValue")) {
257                return CheckSimpleWMIValue(argLen, char_args, msg, perf);
258        }       
259        return NSCAPI::returnIgnored;
260}
261int CheckWMI::commandLineExec(const TCHAR* command, const unsigned int argLen, TCHAR** char_args) {
262        //WMIQuery wmiQuery;
263        std::wstring query = command;
264        std::wstring ns = _T("root\\cimv2");
265        query += _T(" ") + arrayBuffer::arrayBuffer2string(char_args, argLen, _T(" "));
266        WMIQuery::result_type rows;
267        try {
268                WMIQuery wmiQuery;
269                rows = wmiQuery.execute(ns, query);
270        } catch (WMIException e) {
271                NSC_LOG_ERROR_STD(_T("WMIQuery failed: ") + e.getMessage());
272                return -1;
273        }
274        std::vector<std::wstring::size_type> widths;
275        for (WMIQuery::result_type::const_iterator citRow = rows.begin(); citRow != rows.end(); ++citRow) {
276                const WMIQuery::wmi_row vals = *citRow;
277                if (citRow == rows.begin()) {
278                        for (WMIQuery::wmi_row::list_type::const_iterator citCol = vals.results.begin(); citCol != vals.results.end(); ++citCol) {
279                                widths.push_back( (*citCol).first.length()+1 );
280                        }
281                }
282                int i=0;
283                for (WMIQuery::wmi_row::list_type::const_iterator citCol = vals.results.begin(); citCol != vals.results.end(); ++citCol, i++) {
284                        widths[i] = max(widths[i], (*citCol).second.string.length()+1);
285                }
286        }
287
288        std::wstring row2 = _T("|");
289        for (WMIQuery::result_type::iterator citRow = rows.begin(); citRow != rows.end(); ++citRow) {
290                const WMIQuery::wmi_row vals = *citRow;
291                if (citRow == rows.begin()) {
292                        int i=0;
293                        std::wstring row1 = _T("|");
294                        for (WMIQuery::wmi_row::list_type::const_iterator citCol = vals.results.begin(); citCol != vals.results.end(); ++citCol, i++) {
295                                std::wstring::size_type w = widths[i]-(*citCol).first.length();
296                                if (w<0) w=0;
297                                row1 += std::wstring(w, ' ') + (*citCol).first + _T(" |");
298                                row2 += std::wstring(widths[i], '-') + _T("-+");
299
300                        }
301                        NSC_LOG_MESSAGE(row2);
302                        NSC_LOG_MESSAGE(row1);
303                        NSC_LOG_MESSAGE(row2);
304                }
305                int i=0;
306                std::wstring row = _T("|");
307                for (WMIQuery::wmi_row::list_type::const_iterator citCol = vals.results.begin(); citCol != vals.results.end(); ++citCol, i++) {
308                        std::wstring::size_type w = widths[i]-(*citCol).second.string.length();
309                        if (w<0) w=0;
310                        row += std::wstring(w, ' ') + (*citCol).second.string + _T(" |");
311                }
312                NSC_LOG_MESSAGE(row);
313        }
314        NSC_LOG_MESSAGE(row2);
315        return 0;
316}
317
318
319NSC_WRAPPERS_MAIN_DEF(gCheckWMI);
320NSC_WRAPPERS_IGNORE_MSG_DEF();
321NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckWMI);
322NSC_WRAPPERS_CLI_DEF(gCheckWMI);
Note: See TracBrowser for help on using the repository browser.