| 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 | |
|---|
| 30 | CheckWMI gCheckWMI; |
|---|
| 31 | |
|---|
| 32 | BOOL 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 | |
|---|
| 38 | CheckWMI::CheckWMI() { |
|---|
| 39 | } |
|---|
| 40 | CheckWMI::~CheckWMI() { |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | bool 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 | } |
|---|
| 55 | bool CheckWMI::unloadModule() { |
|---|
| 56 | return true; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | bool CheckWMI::hasCommandHandler() { |
|---|
| 60 | return true; |
|---|
| 61 | } |
|---|
| 62 | bool 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 | |
|---|
| 81 | NSCAPI::nagiosReturn CheckWMI::CheckSimpleWMI(const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf) { |
|---|
| 82 | typedef checkHolders::CheckConatiner<checkHolders::MaxMinBounds<checkHolders::NumericBounds<int, checkHolders::int_handler> > > WMIConatiner; |
|---|
| 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 | bool bPerfData = true; |
|---|
| 95 | |
|---|
| 96 | WMIConatiner result_query; |
|---|
| 97 | try { |
|---|
| 98 | MAP_OPTIONS_BEGIN(args) |
|---|
| 99 | MAP_OPTIONS_STR(_T("Query"), query) |
|---|
| 100 | MAP_OPTIONS_STR2INT(_T("truncate"), truncate) |
|---|
| 101 | MAP_OPTIONS_STR(_T("Alias"), alias) |
|---|
| 102 | MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) |
|---|
| 103 | MAP_OPTIONS_NUMERIC_ALL(result_query, _T("")) |
|---|
| 104 | MAP_OPTIONS_SHOWALL(result_query) |
|---|
| 105 | MAP_CHAINED_FILTER(_T("string"),string) |
|---|
| 106 | MAP_CHAINED_FILTER(_T("numeric"),numeric) |
|---|
| 107 | MAP_OPTIONS_SECONDARY_BEGIN(_T(":"), p2) |
|---|
| 108 | MAP_SECONDARY_CHAINED_FILTER(_T("string"),string) |
|---|
| 109 | MAP_SECONDARY_CHAINED_FILTER(_T("numeric"),numeric) |
|---|
| 110 | else if (p2.first == _T("Query")) { |
|---|
| 111 | query = p__.second; |
|---|
| 112 | alias = p2.second; |
|---|
| 113 | } |
|---|
| 114 | MAP_OPTIONS_MISSING_EX(p2, message, _T("Unknown argument: ")) |
|---|
| 115 | MAP_OPTIONS_SECONDARY_END() |
|---|
| 116 | MAP_OPTIONS_END() |
|---|
| 117 | } catch (filters::parse_exception e) { |
|---|
| 118 | message = _T("WMIQuery failed: ") + e.getMessage(); |
|---|
| 119 | return NSCAPI::returnCRIT; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | WMIQuery::result_type rows; |
|---|
| 123 | try { |
|---|
| 124 | WMIQuery wmiQuery; |
|---|
| 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 | |
|---|
| 152 | NSCAPI::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 | WMIQuery wmiQuery; |
|---|
| 194 | rows = wmiQuery.execute(query); |
|---|
| 195 | } catch (WMIException e) { |
|---|
| 196 | message = _T("WMIQuery failed: ") + e.getMessage(); |
|---|
| 197 | return NSCAPI::returnCRIT; |
|---|
| 198 | } |
|---|
| 199 | int hit_count = 0; |
|---|
| 200 | |
|---|
| 201 | for (std::list<WMIConatiner>::const_iterator it = list.begin(); it != list.end(); ++it) { |
|---|
| 202 | WMIConatiner itm = (*it); |
|---|
| 203 | itm.setDefault(tmpObject); |
|---|
| 204 | itm.perfData = bPerfData; |
|---|
| 205 | if (itm.data == _T("*")) { |
|---|
| 206 | for (WMIQuery::result_type::const_iterator citRow = rows.begin(); citRow != rows.end(); ++citRow) { |
|---|
| 207 | for (WMIQuery::wmi_row::list_type::const_iterator citCol = (*citRow).results.begin(); citCol != (*citRow).results.end(); ++citCol) { |
|---|
| 208 | long long value = (*citCol).second.numeric; |
|---|
| 209 | itm.runCheck(value, returnCode, message, perf); |
|---|
| 210 | } |
|---|
| 211 | } |
|---|
| 212 | } |
|---|
| 213 | } |
|---|
| 214 | for (WMIQuery::result_type::const_iterator citRow = rows.begin(); citRow != rows.end(); ++citRow) { |
|---|
| 215 | bool found = false; |
|---|
| 216 | std::wstring alias; |
|---|
| 217 | if (!aliasCol.empty()) { |
|---|
| 218 | alias = (*citRow).get(aliasCol).string; |
|---|
| 219 | } |
|---|
| 220 | for (WMIQuery::wmi_row::list_type::const_iterator citCol = (*citRow).results.begin(); citCol != (*citRow).results.end(); ++citCol) { |
|---|
| 221 | for (std::list<WMIConatiner>::const_iterator it = list.begin(); it != list.end(); ++it) { |
|---|
| 222 | WMIConatiner itm = (*it); |
|---|
| 223 | if (itm.data == _T("*")) { |
|---|
| 224 | found = true; |
|---|
| 225 | } else if ((*citCol).first == itm.data) { |
|---|
| 226 | std::wstring oldAlias = itm.alias; |
|---|
| 227 | if (!alias.empty()) |
|---|
| 228 | itm.alias = alias + _T(" ") + itm.getAlias(); |
|---|
| 229 | found = true; |
|---|
| 230 | long long value = (*citCol).second.numeric; |
|---|
| 231 | itm.runCheck(value, returnCode, message, perf); |
|---|
| 232 | itm.alias = oldAlias; |
|---|
| 233 | } |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | if (!found) { |
|---|
| 237 | NSC_LOG_ERROR_STD(_T("At least one of the queried columns was not found!")); |
|---|
| 238 | } |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | if ((truncate > 0) && (message.length() > (truncate-4))) |
|---|
| 242 | message = message.substr(0, truncate-4) + _T("..."); |
|---|
| 243 | if (message.empty()) |
|---|
| 244 | message = _T("OK: WMI Query returned no results."); |
|---|
| 245 | return returnCode; |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | NSCAPI::nagiosReturn CheckWMI::handleCommand(const strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf) { |
|---|
| 250 | if (command == _T("CheckWMI")) { |
|---|
| 251 | return CheckSimpleWMI(argLen, char_args, msg, perf); |
|---|
| 252 | } else if (command == _T("CheckWMIValue")) { |
|---|
| 253 | return CheckSimpleWMIValue(argLen, char_args, msg, perf); |
|---|
| 254 | } |
|---|
| 255 | return NSCAPI::returnIgnored; |
|---|
| 256 | } |
|---|
| 257 | int CheckWMI::commandLineExec(const TCHAR* command, const unsigned int argLen, TCHAR** char_args) { |
|---|
| 258 | //WMIQuery wmiQuery; |
|---|
| 259 | std::wstring query = command; |
|---|
| 260 | query += _T(" ") + arrayBuffer::arrayBuffer2string(char_args, argLen, _T(" ")); |
|---|
| 261 | WMIQuery::result_type rows; |
|---|
| 262 | try { |
|---|
| 263 | WMIQuery wmiQuery; |
|---|
| 264 | rows = wmiQuery.execute(query); |
|---|
| 265 | } catch (WMIException e) { |
|---|
| 266 | NSC_LOG_ERROR_STD(_T("WMIQuery failed: ") + e.getMessage()); |
|---|
| 267 | return -1; |
|---|
| 268 | } |
|---|
| 269 | std::vector<std::wstring::size_type> widths; |
|---|
| 270 | for (WMIQuery::result_type::const_iterator citRow = rows.begin(); citRow != rows.end(); ++citRow) { |
|---|
| 271 | const WMIQuery::wmi_row vals = *citRow; |
|---|
| 272 | if (citRow == rows.begin()) { |
|---|
| 273 | for (WMIQuery::wmi_row::list_type::const_iterator citCol = vals.results.begin(); citCol != vals.results.end(); ++citCol) { |
|---|
| 274 | widths.push_back( (*citCol).first.length()+1 ); |
|---|
| 275 | } |
|---|
| 276 | } |
|---|
| 277 | int i=0; |
|---|
| 278 | for (WMIQuery::wmi_row::list_type::const_iterator citCol = vals.results.begin(); citCol != vals.results.end(); ++citCol, i++) { |
|---|
| 279 | widths[i] = max(widths[i], (*citCol).second.string.length()+1); |
|---|
| 280 | } |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | std::wstring row2 = _T("|"); |
|---|
| 284 | for (WMIQuery::result_type::iterator citRow = rows.begin(); citRow != rows.end(); ++citRow) { |
|---|
| 285 | const WMIQuery::wmi_row vals = *citRow; |
|---|
| 286 | if (citRow == rows.begin()) { |
|---|
| 287 | int i=0; |
|---|
| 288 | std::wstring row1 = _T("|"); |
|---|
| 289 | for (WMIQuery::wmi_row::list_type::const_iterator citCol = vals.results.begin(); citCol != vals.results.end(); ++citCol, i++) { |
|---|
| 290 | std::wstring::size_type w = widths[i]-(*citCol).first.length(); |
|---|
| 291 | if (w<0) w=0; |
|---|
| 292 | row1 += std::wstring(w, ' ') + (*citCol).first + _T(" |"); |
|---|
| 293 | row2 += std::wstring(widths[i], '-') + _T("-+"); |
|---|
| 294 | |
|---|
| 295 | } |
|---|
| 296 | NSC_LOG_MESSAGE(row2); |
|---|
| 297 | NSC_LOG_MESSAGE(row1); |
|---|
| 298 | NSC_LOG_MESSAGE(row2); |
|---|
| 299 | } |
|---|
| 300 | int i=0; |
|---|
| 301 | std::wstring row = _T("|"); |
|---|
| 302 | for (WMIQuery::wmi_row::list_type::const_iterator citCol = vals.results.begin(); citCol != vals.results.end(); ++citCol, i++) { |
|---|
| 303 | std::wstring::size_type w = widths[i]-(*citCol).second.string.length(); |
|---|
| 304 | if (w<0) w=0; |
|---|
| 305 | row += std::wstring(w, ' ') + (*citCol).second.string + _T(" |"); |
|---|
| 306 | } |
|---|
| 307 | NSC_LOG_MESSAGE(row); |
|---|
| 308 | } |
|---|
| 309 | NSC_LOG_MESSAGE(row2); |
|---|
| 310 | return 0; |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | NSC_WRAPPERS_MAIN_DEF(gCheckWMI); |
|---|
| 315 | NSC_WRAPPERS_IGNORE_MSG_DEF(); |
|---|
| 316 | NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckWMI); |
|---|
| 317 | NSC_WRAPPERS_CLI_DEF(gCheckWMI); |
|---|