source: nscp/modules/CheckWMI/CheckWMI.cpp @ 6b690bf

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

+ Added API for scripts to register commands

+ Added command descriptions (for some modules)

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