Changeset 29655a1 in nscp
- Timestamp:
- 03/06/12 23:33:14 (15 months ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- fb8241a
- Parents:
- 836e9cd
- Files:
-
- 8 edited
-
changelog (modified) (1 diff)
-
include/com_helpers.hpp (modified) (1 diff)
-
modules/CheckWMI/CheckWMI.cpp (modified) (3 diffs)
-
modules/CheckWMI/CheckWMI.def (modified) (1 diff)
-
modules/CheckWMI/CheckWMI.h (modified) (2 diffs)
-
modules/CheckWMI/WMIQuery.cpp (modified) (2 diffs)
-
modules/CheckWMI/WMIQuery.h (modified) (2 diffs)
-
service/cli_parser.hpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
changelog
r836e9cd r29655a1 4 4 * Fixa dependonservice LanManWorkStation (old win) 5 5 * Fix RtlStringFromGUID problem on NT4 6 7 8 2012-03-06 MickeM 9 * Added "back" WMI Query command line exec like so: 10 nscp wmi --exec wmi-query 11 CheckWMI Command line syntax: 12 Allowed options: 13 -h [ --help ] Show help screen 14 -w [ --wmi-query ] arg Query to execute 15 -l [ --limit ] arg Limit number of rows 16 -n [ --namespace ] arg (=root\cimv2) Namespace 17 nscp wmi --exec wmi-query --query="Select * from Win32_Process" --limit 2 6 18 7 19 2012-03-05 MickeM -
include/com_helpers.hpp
r96c1461 r29655a1 42 42 isInitialized_ = true; 43 43 //hRes = CoInitializeSecurity(NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_PKT,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE,NULL); 44 hRes = CoInitializeSecurity(NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_DEFAULT,RPC_C_IMP_LEVEL_I DENTIFY,NULL,EOAC_NONE,NULL);44 hRes = CoInitializeSecurity(NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_DEFAULT,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE,NULL); 45 45 if (FAILED(hRes)) 46 46 throw com_exception(_T("CoInitializeSecurity failed: "), hRes); -
modules/CheckWMI/CheckWMI.cpp
ra44cb15 r29655a1 26 26 27 27 #include <boost/optional.hpp> 28 #include <boost/program_options.hpp> 28 29 29 30 #include "CheckWMI.h" … … 377 378 return NSCAPI::returnIgnored; 378 379 } 379 int CheckWMI::commandLineExec(const wchar_t* command, const unsigned int argLen, wchar_t** char_args) { 380 //WMIQuery wmiQuery; 381 std::wstring query = command; 382 std::wstring ns = _T("root\\cimv2"); 383 query += _T(" ") + array_buffer::arrayBuffer2string(char_args, argLen, _T(" ")); 384 WMIQuery::result_type rows; 385 try { 386 WMIQuery wmiQuery; 387 NSC_DEBUG_MSG_STD(_T("Running query: '") + query + _T("' on: ") + ns); 388 rows = wmiQuery.execute(ns, query); 389 } catch (WMIException e) { 390 NSC_LOG_ERROR_STD(_T("WMIQuery failed: ") + e.getMessage()); 391 return -1; 392 } 393 if (rows.empty()) { 394 NSC_LOG_MESSAGE(_T("Query returned no rows.")); 395 } else { 396 NSC_DEBUG_MSG_STD(_T("Query returned: ") + strEx::itos(rows.size()) + _T(" rows.")); 397 std::vector<std::wstring::size_type> widths; 398 for (WMIQuery::result_type::const_iterator citRow = rows.begin(); citRow != rows.end(); ++citRow) { 399 const WMIQuery::wmi_row vals = *citRow; 400 if (citRow == rows.begin()) { 401 for (WMIQuery::wmi_row::list_type::const_iterator citCol = vals.results.begin(); citCol != vals.results.end(); ++citCol) { 402 widths.push_back( (*citCol).first.length()+1 ); 380 NSCAPI::nagiosReturn CheckWMI::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 381 try { 382 if (command == _T("wmi-query") || command == _T("help") || command == _T("query")) { 383 384 namespace po = boost::program_options; 385 386 std::wstring query, ns; 387 int limit = -1; 388 po::options_description desc("Allowed options"); 389 desc.add_options() 390 ("help,h", "Show help screen") 391 ("wmi-query,w", po::wvalue<std::wstring>(&query), "Query to execute") 392 ("limit,l", po::value<int>(&limit), "Limit number of rows") 393 ("namespace,n", po::wvalue<std::wstring>(&ns)->default_value(_T("root\\cimv2")), "Namespace") 394 ; 395 396 boost::program_options::variables_map vm; 397 398 if (command == _T("help")) { 399 std::stringstream ss; 400 ss << "CheckWMI Command line syntax:" << std::endl; 401 ss << desc; 402 result = utf8::cvt<std::wstring>(ss.str()); 403 return NSCAPI::isSuccess; 404 } 405 406 std::vector<std::wstring> args(arguments.begin(), arguments.end()); 407 po::wparsed_options parsed = po::basic_command_line_parser<wchar_t>(args).options(desc).run(); 408 po::store(parsed, vm); 409 po::notify(vm); 410 411 if (vm.count("help") || query.empty()) { 412 std::stringstream ss; 413 ss << "CheckWMI Command line syntax:" << std::endl; 414 ss << desc; 415 result = utf8::cvt<std::wstring>(ss.str()); 416 return NSCAPI::isSuccess; 417 } 418 419 WMIQuery::result_type rows; 420 try { 421 WMIQuery wmiQuery; 422 NSC_DEBUG_MSG_STD(_T("Running query: '") + query + _T("' on: ") + ns); 423 rows = wmiQuery.execute(ns, query); 424 } catch (WMIException e) { 425 NSC_LOG_ERROR_STD(_T("WMIQuery failed: ") + e.getMessage()); 426 result += _T("ERROR: ") + e.getMessage(); 427 return NSCAPI::hasFailed; 428 } 429 if (rows.empty()) { 430 result += _T("No result"); 431 return NSCAPI::isSuccess; 432 } else { 433 NSC_DEBUG_MSG_STD(_T("Query returned: ") + strEx::itos(rows.size()) + _T(" rows.")); 434 std::vector<std::wstring::size_type> widths; 435 int rownum=0; 436 for (WMIQuery::result_type::const_iterator citRow = rows.begin(); citRow != rows.end(); ++citRow) { 437 if (limit != -1 && rownum++ > limit) 438 break; 439 const WMIQuery::wmi_row vals = *citRow; 440 if (citRow == rows.begin()) { 441 for (WMIQuery::wmi_row::list_type::const_iterator citCol = vals.results.begin(); citCol != vals.results.end(); ++citCol) { 442 widths.push_back( (*citCol).first.length()+1 ); 443 } 444 } 445 int i=0; 446 for (WMIQuery::wmi_row::list_type::const_iterator citCol = vals.results.begin(); citCol != vals.results.end(); ++citCol, i++) { 447 widths[i] = max(widths[i], (*citCol).second.string.length()+1); 448 } 403 449 } 404 } 405 int i=0; 406 for (WMIQuery::wmi_row::list_type::const_iterator citCol = vals.results.begin(); citCol != vals.results.end(); ++citCol, i++) { 407 widths[i] = max(widths[i], (*citCol).second.string.length()+1); 408 } 409 } 410 std::wstring row2 = _T("|"); 411 for (WMIQuery::result_type::iterator citRow = rows.begin(); citRow != rows.end(); ++citRow) { 412 const WMIQuery::wmi_row vals = *citRow; 413 if (citRow == rows.begin()) { 414 int i=0; 415 std::wstring row1 = _T("|"); 416 for (WMIQuery::wmi_row::list_type::const_iterator citCol = vals.results.begin(); citCol != vals.results.end(); ++citCol, i++) { 417 std::wstring::size_type w = widths[i]-(*citCol).first.length(); 418 if (w<0) w=0; 419 row1 += std::wstring(w, ' ') + (*citCol).first + _T(" |"); 420 row2 += std::wstring(widths[i], '-') + _T("-+"); 421 450 std::wstring row2 = _T("|"); 451 rownum=0; 452 for (WMIQuery::result_type::iterator citRow = rows.begin(); citRow != rows.end(); ++citRow) { 453 if (limit != -1 && rownum++ > limit) 454 break; 455 const WMIQuery::wmi_row vals = *citRow; 456 if (citRow == rows.begin()) { 457 int i=0; 458 std::wstring row1 = _T("|"); 459 for (WMIQuery::wmi_row::list_type::const_iterator citCol = vals.results.begin(); citCol != vals.results.end(); ++citCol, i++) { 460 std::wstring::size_type w = widths[i]-(*citCol).first.length(); 461 if (w<0) w=0; 462 row1 += std::wstring(w, ' ') + (*citCol).first + _T(" |"); 463 row2 += std::wstring(widths[i], '-') + _T("-+"); 464 465 } 466 result += row2; 467 result += row1; 468 result += row2; 469 } 470 int i=0; 471 std::wstring row = _T("|"); 472 for (WMIQuery::wmi_row::list_type::const_iterator citCol = vals.results.begin(); citCol != vals.results.end(); ++citCol, i++) { 473 std::wstring::size_type w = widths[i]-(*citCol).second.string.length(); 474 if (w<0) w=0; 475 row += std::wstring(w, ' ') + (*citCol).second.string + _T(" |"); 476 } 477 result += row; 422 478 } 423 NSC_LOG_MESSAGE(row2); 424 NSC_LOG_MESSAGE(row1); 425 NSC_LOG_MESSAGE(row2); 426 } 427 int i=0; 428 std::wstring row = _T("|"); 429 for (WMIQuery::wmi_row::list_type::const_iterator citCol = vals.results.begin(); citCol != vals.results.end(); ++citCol, i++) { 430 std::wstring::size_type w = widths[i]-(*citCol).second.string.length(); 431 if (w<0) w=0; 432 row += std::wstring(w, ' ') + (*citCol).second.string + _T(" |"); 433 } 434 NSC_LOG_MESSAGE(row); 435 } 436 NSC_LOG_MESSAGE(row2); 437 } 438 return 0; 479 result += row2; 480 } 481 } 482 return NSCAPI::isSuccess; 483 } catch (const std::exception &e) { 484 result += _T("ERROR: Failed to parse command line: ") + utf8::cvt<std::wstring>(e.what()); 485 NSC_LOG_ERROR_STD(_T("Failed to parse command line: ") + utf8::cvt<std::wstring>(e.what())); 486 return NSCAPI::hasFailed; 487 } 488 return NSCAPI::returnIgnored; 439 489 } 440 490 … … 444 494 NSC_WRAPPERS_IGNORE_MSG_DEF(); 445 495 NSC_WRAPPERS_HANDLE_CMD_DEF(); 446 //NSC_WRAPPERS_CLI_DEF(gCheckWMI);496 NSC_WRAPPERS_CLI_DEF(); -
modules/CheckWMI/CheckWMI.def
r1ecd26f r29655a1 14 14 NSGetModuleDescription 15 15 NSDeleteBuffer 16 NSCommandLineExec -
modules/CheckWMI/CheckWMI.h
ra78a985 r29655a1 88 88 89 89 90 class CheckWMI : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin {90 class CheckWMI : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec { 91 91 public: 92 92 CheckWMI(); … … 111 111 bool hasMessageHandler(); 112 112 NSCAPI::nagiosReturn handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf); 113 int CheckWMI::commandLineExec(const wchar_t* command,const unsigned int argLen,wchar_t** args);113 NSCAPI::nagiosReturn commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result); 114 114 115 115 // Check commands -
modules/CheckWMI/WMIQuery.cpp
rfe75eff r29655a1 126 126 127 127 CComPtr< IEnumWbemClassObject > enumerator; 128 hr = service->ExecQuery( strQL, strQuery, WBEM_FLAG_FORWARD_ONLY |WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &enumerator );128 hr = service->ExecQuery( strQL, strQuery, WBEM_FLAG_FORWARD_ONLY, NULL, &enumerator ); 129 129 if (FAILED(hr)) 130 130 throw WMIException(_T("ExecQuery of '") + query + _T("' failed: ") + ComError::getComError(ComError::getWMIError(hr)) + _T(")")); … … 147 147 end = arr.GetUpperBound(); 148 148 for ( index = begin; index <= end; index++ ) { 149 USES_CONVERSION; 150 CComBSTR bColumn = arr.GetAt(index); 151 std::wstring column = OLE2T(bColumn); 152 CComVariant vValue; 153 hr = row->Get(bColumn, 0, &vValue, 0, 0); 154 if (FAILED(hr)) 155 throw WMIException(_T("Failed to get value for ") + column + _T(" in query: ") + query, hr); 156 WMIResult value; 149 try { 150 USES_CONVERSION; 151 CComBSTR bColumn = arr.GetAt(index); 152 std::wstring column = OLE2T(bColumn); 153 CComVariant vValue; 154 hr = row->Get(bColumn, 0, &vValue, 0, 0); 155 if (FAILED(hr)) 156 throw WMIException(_T("Failed to get value for ") + column + _T(" in query: ") + query, hr); 157 WMIResult value; 157 158 158 if (vValue.vt == VT_INT) { 159 value.setNumeric(column, vValue.intVal); 160 } else if (vValue.vt == VT_I4) { 161 value.setNumeric(column, vValue.lVal); 162 } else if (vValue.vt == VT_UI1) { 163 value.setNumeric(column, vValue.uintVal); 164 } else if (vValue.vt == VT_UINT) { 165 value.setNumeric(column, vValue.uintVal); 166 } else if (vValue.vt == VT_BSTR) { 167 value.setString(column, OLE2T(vValue.bstrVal)); 168 } else if (vValue.vt == VT_NULL) { 169 value.setString(column, _T("NULL")); 170 } else if (vValue.vt == VT_BOOL) { 171 value.setBoth(column, vValue.iVal, vValue.iVal?_T("TRUE"):_T("FALSE")); 172 } else { 173 NSC_LOG_ERROR_STD(column + _T(" is not supported (type-id: ") + strEx::itos(vValue.vt) + _T(")")); 159 if (vValue.vt == VT_INT) { 160 value.setNumeric(column, vValue.intVal); 161 } else if (vValue.vt == VT_I4) { 162 value.setNumeric(column, vValue.lVal); 163 } else if (vValue.vt == VT_UI1) { 164 value.setNumeric(column, vValue.uintVal); 165 } else if (vValue.vt == VT_UINT) { 166 value.setNumeric(column, vValue.uintVal); 167 } else if (vValue.vt == VT_BSTR) { 168 value.setString(column, OLE2T(vValue.bstrVal)); 169 } else if (vValue.vt == VT_NULL) { 170 value.setString(column, _T("NULL")); 171 } else if (vValue.vt == VT_BOOL) { 172 value.setBoth(column, vValue.iVal, vValue.iVal?_T("TRUE"):_T("FALSE")); 173 } else { 174 NSC_LOG_ERROR_STD(column + _T(" is not supported (type-id: ") + strEx::itos(vValue.vt) + _T(")")); 175 } 176 returnRow.addValue(column, value); 177 } catch (const std::exception &e) { 178 throw WMIException(_T("Failed to convert data: ") + utf8::cvt<std::wstring>(e.what())); 174 179 } 175 returnRow.addValue(column, value);176 180 } 177 181 ret.push_back(returnRow); -
modules/CheckWMI/WMIQuery.h
rfe75eff r29655a1 58 58 } 59 59 static std::wstring getComError(std::wstring inDesc = _T("")) { 60 USES_CONVERSION;61 CComPtr<IErrorInfo> errorInfo;62 HRESULT hr = GetErrorInfo(NULL, &errorInfo);63 if (FAILED(hr) || hr == S_FALSE)64 return _T("unknown error: ") + error::format::from_system(hr);65 CComBSTR bDesc, bSource;66 60 std::wstring src = _T("unknown"); 67 hr = errorInfo->GetSource(&bSource);68 if (SUCCEEDED(hr))69 src = OLE2T(bSource);70 61 std::wstring desc; 71 hr = errorInfo->GetDescription(&bDesc); 72 if (SUCCEEDED(hr)) 73 desc = OLE2T(bDesc); 74 if (desc.empty() && !inDesc.empty()) 75 desc = inDesc; 76 else if (desc.empty()) 77 desc = _T("unknown error: ") + error::format::from_system(hr); 78 return src + _T(" - ") + desc; 62 try { 63 USES_CONVERSION; 64 CComPtr<IErrorInfo> errorInfo; 65 HRESULT hr = GetErrorInfo(NULL, &errorInfo); 66 if (FAILED(hr) || hr == S_FALSE) 67 return _T("unknown error: ") + error::format::from_system(hr); 68 CComBSTR bDesc, bSource; 69 hr = errorInfo->GetSource(&bSource); 70 if (SUCCEEDED(hr)) 71 src = OLE2T(bSource); 72 hr = errorInfo->GetDescription(&bDesc); 73 if (SUCCEEDED(hr)) 74 desc = OLE2T(bDesc); 75 if (desc.empty() && !inDesc.empty()) 76 desc = inDesc; 77 else if (desc.empty()) 78 desc = _T("unknown error: ") + error::format::from_system(hr); 79 return src + _T(" - ") + desc; 80 } catch (...) { 81 return src + _T(" - ") + desc; 82 } 79 83 } 80 84 }; … … 104 108 void setString(std::wstring a, std::wstring s) { 105 109 string = s; 106 numeric = boost::lexical_cast<long long>(s); 110 try { 111 numeric = boost::lexical_cast<long long>(s); 112 } catch (...) { 113 numeric = 0; 114 } 107 115 alias = a; 108 116 } -
service/cli_parser.hpp
rc74d7b6 r29655a1 129 129 aliases["lua"] = "LuaScript"; 130 130 aliases["syslog"] = "SyslogClient"; 131 aliases["sys"] = "CheckSystem"; 132 aliases["wmi"] = "CheckWMI"; 131 133 return aliases; 132 134 }
Note: See TracChangeset
for help on using the changeset viewer.








