Changeset 29655a1 in nscp


Ignore:
Timestamp:
03/06/12 23:33:14 (15 months ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
fb8241a
Parents:
836e9cd
Message:
  • Added "back" WMI Query command line exec like so:

nscp wmi --exec wmi-query

CheckWMI Command line syntax:
Allowed options:

-h [ --help ] Show help screen
-w [ --wmi-query ] arg Query to execute
-l [ --limit ] arg Limit number of rows
-n [ --namespace ] arg (=root\cimv2) Namespace

nscp wmi --exec wmi-query --query="Select * from Win32_Process" --limit 2

Files:
8 edited

Legend:

Unmodified
Added
Removed
  • changelog

    r836e9cd r29655a1  
    44 * Fixa dependonservice LanManWorkStation (old win) 
    55 * Fix RtlStringFromGUID problem on NT4 
     6 
     7  
     82012-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 
    618 
    7192012-03-05 MickeM 
  • include/com_helpers.hpp

    r96c1461 r29655a1  
    4242      isInitialized_ = true; 
    4343      //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_IDENTIFY,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); 
    4545      if (FAILED(hRes))  
    4646        throw com_exception(_T("CoInitializeSecurity failed: "), hRes); 
  • modules/CheckWMI/CheckWMI.cpp

    ra44cb15 r29655a1  
    2626 
    2727#include <boost/optional.hpp> 
     28#include <boost/program_options.hpp> 
    2829 
    2930#include "CheckWMI.h" 
     
    377378  return NSCAPI::returnIgnored; 
    378379} 
    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 ); 
     380NSCAPI::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          } 
    403449        } 
    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; 
    422478        } 
    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; 
    439489} 
    440490 
     
    444494NSC_WRAPPERS_IGNORE_MSG_DEF(); 
    445495NSC_WRAPPERS_HANDLE_CMD_DEF(); 
    446 //NSC_WRAPPERS_CLI_DEF(gCheckWMI); 
     496NSC_WRAPPERS_CLI_DEF(); 
  • modules/CheckWMI/CheckWMI.def

    r1ecd26f r29655a1  
    1414  NSGetModuleDescription 
    1515  NSDeleteBuffer 
     16  NSCommandLineExec 
  • modules/CheckWMI/CheckWMI.h

    ra78a985 r29655a1  
    8888 
    8989 
    90 class CheckWMI : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin { 
     90class CheckWMI : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec { 
    9191public: 
    9292  CheckWMI(); 
     
    111111  bool hasMessageHandler(); 
    112112  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); 
    114114 
    115115  // Check commands 
  • modules/CheckWMI/WMIQuery.cpp

    rfe75eff r29655a1  
    126126 
    127127  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 ); 
    129129  if (FAILED(hr)) 
    130130    throw WMIException(_T("ExecQuery of '") + query + _T("' failed: ") + ComError::getComError(ComError::getWMIError(hr)) + _T(")")); 
     
    147147        end = arr.GetUpperBound(); 
    148148        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; 
    157158 
    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())); 
    174179          } 
    175           returnRow.addValue(column, value); 
    176180        } 
    177181        ret.push_back(returnRow); 
  • modules/CheckWMI/WMIQuery.h

    rfe75eff r29655a1  
    5858  } 
    5959  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; 
    6660    std::wstring src = _T("unknown"); 
    67     hr = errorInfo->GetSource(&bSource); 
    68     if (SUCCEEDED(hr)) 
    69       src = OLE2T(bSource); 
    7061    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    } 
    7983  } 
    8084}; 
     
    104108    void setString(std::wstring a, std::wstring s) { 
    105109      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      } 
    107115      alias = a; 
    108116    } 
  • service/cli_parser.hpp

    rc74d7b6 r29655a1  
    129129    aliases["lua"] = "LuaScript"; 
    130130    aliases["syslog"] = "SyslogClient"; 
     131    aliases["sys"] = "CheckSystem"; 
     132    aliases["wmi"] = "CheckWMI"; 
    131133    return aliases; 
    132134  } 
Note: See TracChangeset for help on using the changeset viewer.