Changeset 390faae in nscp


Ignore:
Timestamp:
11/04/07 17:35:28 (6 years ago)
Author:
Michael Medin <michael@…>
Children:
89ac56a
Parents:
86fc50e
Message:

2007-11-04 MickeM

  • Rewritten WMI handling as well as checks so things should be more usefull now (cf. the wiki page for details)
Location:
trunk
Files:
20 edited

Legend:

Unmodified
Added
Removed
  • trunk/AutoBuild.h

    r86fc50e r390faae  
    33// change the FALSE to TRUE for autoincrement of build number 
    44#define INCREMENT_VERSION TRUE 
    5 #define FILEVER        0,2,7,41 
    6 #define PRODUCTVER     0,2,7,139 
    7 #define STRFILEVER     "0.2.7.41" 
    8 #define STRPRODUCTVER  "0.2.7.139" 
    9 #define STRPRODUCTDATE  "2007-11-03" 
     5#define FILEVER        0,2,7,42 
     6#define PRODUCTVER     0,2,7,140 
     7#define STRFILEVER     "0.2.7.42" 
     8#define STRPRODUCTVER  "0.2.7.140" 
     9#define STRPRODUCTDATE  "2007-11-04" 
    1010#endif // AUTOBUILD_H 
  • trunk/NSClient++.cpp

    r86fc50e r390faae  
    113113#endif 
    114114      g_bConsoleLog = true; 
     115      mainClient.enableDebug(true); 
    115116      if (!mainClient.InitiateService()) { 
    116117        LOG_ERROR_STD("Service *NOT* started!"); 
     
    166167 * When the program is started as a service this will be the entry point. 
    167168 */ 
    168 bool NSClientT::InitiateService(void) { 
     169bool NSClientT::InitiateService() { 
    169170  try { 
    170171    Settings::getInstance()->setFile(getBasePath(), "NSC.ini"); 
     
    173174    return false; 
    174175  } 
     176  if (debug_) { 
     177    Settings::getInstance()->setInt("log", "debug", 1); 
     178  } 
     179 
    175180  try { 
    176181    simpleSocket::WSAStartup(); 
  • trunk/NSClient++.h

    r86fc50e r390faae  
    6666  MutexHandler messageMutex; 
    6767  MutexRW  m_mutexRW; 
     68  bool debug_; 
    6869 
    6970public: 
    7071  // c-tor, d-tor 
    71   NSClientT(void) {} 
     72  NSClientT(void) : debug_(false) {} 
    7273  virtual ~NSClientT(void) {} 
     74  void enableDebug(bool debug = true) { 
     75    debug_ = debug; 
     76  } 
    7377 
    7478  // Service helper functions 
    75   bool InitiateService(void); 
     79  bool InitiateService(); 
    7680  void TerminateService(void); 
    7781  static void WINAPI service_main_dispatch(DWORD dwArgc, LPTSTR *lpszArgv); 
  • trunk/NSClient++.sln

    r86fc50e r390faae  
    132132    {BA246C01-063A-4548-8957-32D5CC76171B}.Debug|Mixed Platforms.Build.0 = Debug|x64 
    133133    {BA246C01-063A-4548-8957-32D5CC76171B}.Debug|Win32.ActiveCfg = Debug|Win32 
    134     {BA246C01-063A-4548-8957-32D5CC76171B}.Debug|Win32.Build.0 = Debug|Win32 
    135134    {BA246C01-063A-4548-8957-32D5CC76171B}.Debug|x64.ActiveCfg = Debug|x64 
    136135    {BA246C01-063A-4548-8957-32D5CC76171B}.Debug|x64.Build.0 = Debug|x64 
  • trunk/changelog

    r484be40 r390faae  
    88 * Fix PDH problems 
    99 * Add API for rehashing the daemon (or implement it the API is there but does nothing) 
     10 
     112007-11-04 MickeM 
     12 * Rewritten WMI handling as well as checks so things should be more usefull now (cf. the wiki page for details) 
    1013 
    11142007-10-29 MickeM 
  • trunk/include/PDHCounter.h

    r2b180e0 r390faae  
    210210 
    211211    PDHCounter* addCounter(std::string name, PDHCounterListener *listener) { 
    212       std::cout << "Adding counter: " << name << std::endl; 
     212      //std::cout << "Adding counter: " << name << std::endl; 
    213213      PDHCounter *counter = new PDHCounter(name, listener); 
    214214      counters_.push_back(counter); 
  • trunk/include/ServiceCmd.cpp

    r484be40 r390faae  
    1818#include "ServiceCmd.h" 
    1919#include <strEx.h> 
     20#include <tchar.h> 
    2021#include <iostream> 
    2122 
  • trunk/include/Socket.h

    r484be40 r390faae  
    3535  public: 
    3636    SocketException(std::string error) : error_(error) {} 
    37     SocketException(std::string error, int errorCode) : error_(error) { 
    38       error_ += strEx::itos(errorCode); 
     37    SocketException(std::string error, unsigned int errorCode) : error_(error) { 
     38      error_ += error::format::from_system(errorCode); 
    3939    } 
    4040    std::string getMessage() const { 
  • trunk/include/checkHelpers.hpp

    r2172776 r390faae  
    3434 
    3535 
    36   std::string formatAbove(std::string str, ResultType what) { 
     36  static std::string formatAbove(std::string str, ResultType what) { 
    3737    if (what == warning) 
    3838      return str + " > warning"; 
     
    4242  } 
    4343 
    44   std::string formatBelow(std::string str, ResultType what) { 
     44  static std::string formatBelow(std::string str, ResultType what) { 
    4545    if (what == warning) 
    4646      return str + " < warning"; 
  • trunk/include/filter_framework.hpp

    re6722ca r390faae  
    2222 
    2323#include <strEx.h> 
     24#include <checkHelpers.hpp> 
    2425 
    2526namespace filters { 
     
    297298        inList = value.substr(3); 
    298299      } else { 
    299         throw parse_exception("Unknown filter key: " + value); 
     300        throw parse_exception("Unknown filter key: " + value + " (numeric filters have to have an operator as well ie. foo=>5 or bar==5)"); 
    300301      } 
    301302      return *this; 
     
    303304  }; 
    304305  typedef filter_all_numeric<unsigned long long, checkHolders::time_handler<unsigned long long> > filter_all_times; 
     306 
     307  template <typename TFilterType, typename TValueType> 
     308  struct chained_filter { 
     309    enum filter_mode { 
     310      plus = 1, 
     311      minus = 2, 
     312      normal = 3, 
     313    }; 
     314    typedef std::pair<filter_mode,TFilterType> filteritem_type; 
     315    typedef std::list<filteritem_type> filterlist_type; 
     316 
     317    filterlist_type chain; 
     318    bool filterAll; 
     319     
     320    chained_filter() : filterAll(false) {} 
     321 
     322    void push_filter(std::string key, TFilterType filter) { 
     323      filter_mode mode = normal; 
     324      if (key.substr(0,1) == "+") 
     325        mode = plus; 
     326      if (key.substr(0,1) == "-") 
     327        mode = minus; 
     328      chain.push_back(filteritem_type(mode, filter)); 
     329    } 
     330 
     331    bool hasFilter() { 
     332      return !chain.empty(); 
     333    } 
     334    bool get_inital_state() { 
     335      return filterAll; 
     336    } 
     337 
     338    bool match(bool state, const TValueType item) { 
     339      bool matched = state; 
     340      for (filterlist_type::const_iterator cit = chain.begin(); cit != chain.end(); ++cit ) { 
     341        int mode = (*cit).first; 
     342        bool bTmpMatched = (*cit).second.matchFilter(item); 
     343        if ((mode == minus)&&(bTmpMatched)) { 
     344          // a -<filter> hit so thrash result! 
     345          matched = false; 
     346          break; 
     347        } else if ((mode == plus)&&(!bTmpMatched)) { 
     348          // a +<filter> missed hit so thrash result! 
     349          matched = false; 
     350          break; 
     351        } else if (bTmpMatched) { 
     352          matched = true; 
     353        } 
     354      } 
     355      return matched; 
     356    } 
     357 
     358  }; 
     359 
     360 
    305361} 
  • trunk/modules/CheckDisk/CheckDisk.cpp

    rd1dd484 r390faae  
    217217 
    218218    if ((!bFilter)&&!((drvType == DRIVE_FIXED)||(drvType == DRIVE_NO_ROOT_DIR))) { 
    219       message = "UNKNOWN: Drive is not a fixed drive: " + drive.getAlias() + " (it is a: " + strEx::itos(drvType) + ")"; 
     219      message = "UNKNOWN: Drive is not a fixed drive: " + drive.getAlias() + " (it is a " + get_filter(drvType) + " drive)"; 
    220220      return NSCAPI::returnUNKNOWN; 
    221221    } else if ( (bFilter)&&( (!bFilterFixed)&&((drvType==DRIVE_FIXED)||(drvType==DRIVE_NO_ROOT_DIR))) || 
     
    223223      ((!bFilterRemote)&&(drvType==DRIVE_REMOTE)) || 
    224224      ((!bFilterRemovable)&&(drvType==DRIVE_REMOVABLE)) ) { 
    225       message = "UNKNOWN: Drive does not match the current filter: " + drive.getAlias() + " (it is a: " + strEx::itos(drvType) + ")"; 
     225      message = "UNKNOWN: Drive does not match the current filter: " + drive.getAlias() + " (it is a " + get_filter(drvType) + " drive)"; 
    226226      return NSCAPI::returnUNKNOWN; 
    227227    } 
     
    255255    message = NSCHelper::translateReturn(returnCode) + ": " + message; 
    256256  return returnCode; 
     257} 
     258 
     259std::string CheckDisk::get_filter(unsigned int drvType) { 
     260  if (drvType==DRIVE_FIXED) 
     261    return "fixed"; 
     262  if (drvType==DRIVE_NO_ROOT_DIR) 
     263    return "no_root"; 
     264  if (drvType==DRIVE_CDROM) 
     265    return "cdrom"; 
     266  if (drvType==DRIVE_REMOTE) 
     267    return "remote"; 
     268  if (drvType==DRIVE_REMOVABLE) 
     269    return "removable"; 
     270  return "unknown: " + strEx::itos(drvType); 
    257271} 
    258272 
  • trunk/modules/CheckDisk/CheckDisk.h

    r2172776 r390faae  
    4848  bool hasCommandHandler(); 
    4949  bool hasMessageHandler(); 
     50  std::string get_filter(unsigned int drvType); 
    5051  NSCAPI::nagiosReturn handleCommand(const strEx::blindstr command, const unsigned int argLen, char **char_args, std::string &message, std::string &perf); 
    5152 
  • trunk/modules/CheckEventLog/CheckEventLog.cpp

    r2b180e0 r390faae  
    301301 
    302302  for (std::list<std::string>::const_iterator cit2 = files.begin(); cit2 != files.end(); ++cit2) { 
    303     std::cout << "Opening: " << (*cit2) << std::endl; 
    304303    HANDLE hLog = OpenEventLog(NULL, (*cit2).c_str()); 
    305304    if (hLog == NULL) { 
  • trunk/modules/CheckWMI/CheckWMI.cpp

    r2172776 r390faae  
    5757 
    5858 
     59#define MAP_CHAINED_FILTER(value, obj) \ 
     60      else if (p__.first.length() > 8 && p__.first.substr(1,6) == "filter" && p__.first.substr(7,1) == "-" && p__.first.substr(8) == value) { \ 
     61        WMIQuery::wmi_filter filter; filter.obj = p__.second; chain.push_filter(p__.first, filter); } 
     62 
     63#define MAP_SECONDARY_CHAINED_FILTER(value, obj) \ 
     64      else if (p2.first.length() > 8 && p2.first.substr(1,6) == "filter" && p2.first.substr(7,1) == "-" && p2.first.substr(8) == value) { \ 
     65      WMIQuery::wmi_filter filter; filter.obj = p__.second; filter.alias = p2.second; chain.push_filter(p__.first, filter); } 
     66 
     67#define MAP_CHAINED_FILTER_STRING(value) \ 
     68  MAP_CHAINED_FILTER(value, string) 
     69 
     70#define MAP_CHAINED_FILTER_NUMERIC(value) \ 
     71  MAP_CHAINED_FILTER(value, numeric) 
    5972 
    6073NSCAPI::nagiosReturn CheckWMI::CheckSimpleWMI(const unsigned int argLen, char **char_args, std::string &message, std::string &perf) { 
     
    6275 
    6376  NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; 
     77  typedef filters::chained_filter<WMIQuery::wmi_filter,WMIQuery::wmi_row> filter_chain; 
     78  filter_chain chain; 
    6479  std::list<std::string> args = arrayBuffer::arrayBuffer2list(argLen, char_args); 
    6580  if (args.empty()) { 
     
    6782    return NSCAPI::returnCRIT; 
    6883  } 
     84  unsigned int truncate = 0; 
     85  std::string query, alias; 
     86  bool bPerfData = true; 
    6987 
    70   WMIConatiner tmpObject; 
    71   std::list<WMIConatiner> queries; 
    72  
    73   MAP_OPTIONS_BEGIN(args) 
    74     MAP_OPTIONS_STR_AND("Query", tmpObject.data, queries.push_back(tmpObject)) 
    75     MAP_OPTIONS_NUMERIC_ALL(tmpObject, "") 
    76     MAP_OPTIONS_SHOWALL(tmpObject) 
     88  WMIConatiner result_query; 
     89  try { 
     90    MAP_OPTIONS_BEGIN(args) 
     91    MAP_OPTIONS_STR("Query", query) 
     92    MAP_OPTIONS_STR2INT("truncate", truncate) 
     93    MAP_OPTIONS_STR("Alias", alias) 
     94    MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) 
     95    MAP_OPTIONS_NUMERIC_ALL(result_query, "") 
     96    MAP_OPTIONS_SHOWALL(result_query) 
     97    MAP_CHAINED_FILTER("string",string) 
     98    MAP_CHAINED_FILTER("numeric",numeric) 
    7799    MAP_OPTIONS_SECONDARY_BEGIN(":", p2) 
    78       else if (p2.first == "Query") { 
    79         tmpObject.data = p__.second; 
    80         tmpObject.alias = p2.second; 
    81         queries.push_back(tmpObject); 
    82       } 
     100    MAP_SECONDARY_CHAINED_FILTER("string",string) 
     101    MAP_SECONDARY_CHAINED_FILTER("numeric",numeric) 
     102        else if (p2.first == "Query") { 
     103          query = p__.second; 
     104          alias = p2.second; 
     105        } 
    83106      MAP_OPTIONS_MISSING_EX(p2, message, "Unknown argument: ") 
    84107    MAP_OPTIONS_SECONDARY_END() 
    85   MAP_OPTIONS_FALLBACK_AND(tmpObject.data, queries.push_back(tmpObject)) 
    86   MAP_OPTIONS_END() 
     108    MAP_OPTIONS_END() 
     109  } catch (filters::parse_exception e) { 
     110    message = "WMIQuery failed: " + e.getMessage(); 
     111    return NSCAPI::returnCRIT; 
     112  } 
    87113 
    88   for (std::list<WMIConatiner>::const_iterator pit = queries.begin();pit!=queries.end();++pit) { 
    89     WMIConatiner query = (*pit); 
    90     std::map<std::string,int> vals; 
    91     try { 
    92       vals = wmiQuery.execute(query.data); 
    93     } catch (WMIException e) { 
    94       message = "WMIQuery failed..."; 
    95       return NSCAPI::returnCRIT; 
     114  WMIQuery::result_type rows; 
     115  try { 
     116    rows = wmiQuery.execute(query); 
     117  } catch (WMIException e) { 
     118    message = "WMIQuery failed: " + e.getMessage(); 
     119    return NSCAPI::returnCRIT; 
     120  } 
     121  int hit_count = 0; 
     122 
     123  bool match = chain.get_inital_state(); 
     124  for (WMIQuery::result_type::iterator citRow = rows.begin(); citRow != rows.end(); ++citRow) { 
     125    WMIQuery::wmi_row vals = *citRow; 
     126    match = chain.match(match, vals); 
     127    if (match) { 
     128      strEx::append_list(message, vals.render()); 
     129      hit_count++; 
    96130    } 
    97     int val = 0; //(*vals.begin()).second; 
     131  } 
    98132 
    99     for (std::map<std::string,int>::const_iterator it = vals.begin(); it != vals.end(); ++it) { 
    100       std::cout << "Values: " << (*it).first << " = " << (*it).second << std::endl; 
    101     } 
    102  
    103     query.setDefault(tmpObject); 
    104     query.runCheck(val, returnCode, message, perf); 
    105   } 
     133  if (!bPerfData) 
     134    result_query.perfData = false; 
     135  result_query.runCheck(hit_count, returnCode, message, perf); 
     136  if ((truncate > 0) && (message.length() > (truncate-4))) 
     137    message = message.substr(0, truncate-4) + "..."; 
    106138  if (message.empty()) 
    107     message = "OK: Queries within bounds."; 
     139    message = "OK: WMI Query returned no results."; 
    108140  return returnCode; 
    109141} 
    110142 
     143NSCAPI::nagiosReturn CheckWMI::CheckSimpleWMIValue(const unsigned int argLen, char **char_args, std::string &message, std::string &perf) { 
     144  message = "Not yet implemented :("; 
     145  return NSCAPI::returnCRIT; 
     146} 
    111147 
    112148 
     
    114150  if (command == "CheckWMI") { 
    115151    return CheckSimpleWMI(argLen, char_args, msg, perf); 
     152  } else if (command == "CheckWMIValue") { 
     153    return CheckSimpleWMIValue(argLen, char_args, msg, perf); 
    116154  }  
    117155  return NSCAPI::returnIgnored; 
  • trunk/modules/CheckWMI/CheckWMI.h

    r2172776 r390faae  
    5454  // Check commands 
    5555  NSCAPI::nagiosReturn CheckSimpleWMI(const unsigned int argLen, char **char_args, std::string &message, std::string &perf); 
     56  NSCAPI::nagiosReturn CheckSimpleWMIValue(const unsigned int argLen, char **char_args, std::string &message, std::string &perf); 
     57 
    5658 
    5759 
  • trunk/modules/CheckWMI/CheckWMI.vcproj

    ra148d28 r390faae  
    996996      </File> 
    997997      <File 
     998        RelativePath="..\..\include\filter_framework.hpp" 
     999        > 
     1000      </File> 
     1001      <File 
    9981002        RelativePath="..\..\include\NSCHelper.h" 
    9991003        > 
  • trunk/modules/CheckWMI/WMIQuery.cpp

    r2172776 r390faae  
    3939bool WMIQuery::initialize() 
    4040{ 
    41   if (CoInitialize(NULL) != S_OK) 
    42     return false; 
    43   bInitialized = true; 
    44   if(CoInitializeSecurity(NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_PKT,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,0,0) != S_OK) { 
     41  NSC_LOG_ERROR_STD("Attempting Inialized WMI component"); 
     42 
     43  HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED); 
     44  if (FAILED(hRes)) { 
     45    NSC_LOG_ERROR_STD("CoInitialize failed: " + error::format::from_system(hRes)); 
    4546    return false; 
    4647  } 
     48  bInitialized = true; 
     49  hRes = CoInitializeSecurity(NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_PKT,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE,NULL); 
     50  if (FAILED(hRes)) { 
     51    NSC_LOG_ERROR_STD("CoInitializeSecurity failed: " + error::format::from_system(hRes)); 
     52    return false; 
     53  } 
     54 
     55  NSC_LOG_ERROR_STD("Inialized WMI component"); 
    4756  return true; 
    4857} 
     
    5463 
    5564 
    56 std::map<std::string,int> WMIQuery::execute(std::string query) 
     65WMIQuery::result_type WMIQuery::execute(std::string query) 
    5766{ 
    58   std::map<std::string,int> ret; 
    59   IWbemLocator * pIWbemLocator = NULL; 
     67  if (!bInitialized) { 
     68    initialize(); 
     69  } 
     70  result_type ret; 
     71 
     72  CComPtr< IWbemLocator > locator; 
     73  HRESULT hr = CoCreateInstance( CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER, IID_IWbemLocator, reinterpret_cast< void** >( &locator ) ); 
     74  if (FAILED(hr)) { 
     75    throw WMIException("CoCreateInstance for CLSID_WbemAdministrativeLocator failed!", hr); 
     76  } 
     77 
    6078  BSTR bstrNamespace = (L"root\\cimv2"); 
    61   //BSTR bstrNamespace = (L"root\\default"); 
    62   HRESULT hRes = CoCreateInstance(CLSID_WbemAdministrativeLocator,NULL,CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER,  
    63     IID_IUnknown,(void**)&pIWbemLocator); 
    64   if (FAILED(hRes)) { 
    65     throw WMIException("CoCreateInstance for CLSID_WbemAdministrativeLocator failed!", hRes); 
    66   } 
    67   IWbemServices * pWbemServices = NULL; 
    68   hRes = pIWbemLocator->ConnectServer(bstrNamespace,NULL,NULL,NULL,0,NULL,NULL,&pWbemServices); 
    69   if (FAILED(hRes)) { 
    70     pIWbemLocator->Release(); 
    71     pIWbemLocator = NULL; 
    72     throw WMIException("ConnectServer failed!", hRes); 
     79  CComPtr< IWbemServices > service; 
     80  hr = locator->ConnectServer( bstrNamespace, NULL, NULL, NULL, WBEM_FLAG_CONNECT_USE_MAX_WAIT, NULL, NULL, &service ); 
     81  if (FAILED(hr)) { 
     82    throw WMIException("ConnectServer failed!", hr); 
    7383  } 
    7484  CComBSTR strQuery(query.c_str()); 
    7585  BSTR strQL = (L"WQL"); 
    76   IEnumWbemClassObject * pEnumObject = NULL; 
    77   hRes = pWbemServices->ExecQuery(strQL, strQuery,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject); 
    78   if (FAILED(hRes)) { 
    79     pWbemServices->Release(); 
    80     pIWbemLocator->Release(); 
    81     pIWbemLocator = NULL; 
    82     throw WMIException("ExecQuery failed:" + query, hRes); 
    83   } 
    84   hRes = pEnumObject->Reset(); 
    85   if (FAILED(hRes)) { 
    86     pWbemServices->Release(); 
    87     pIWbemLocator->Release(); 
    88     pIWbemLocator = NULL; 
    89     throw WMIException("ExecQuery failed:" + query, hRes); 
    90   } 
    91   ULONG uCount = 1, uReturned; 
    92   IWbemClassObject * pClassObject = NULL; 
    93   hRes = pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject, &uReturned); 
    94   if (FAILED(hRes)) { 
    95     pWbemServices->Release(); 
    96     pIWbemLocator->Release(); 
    97     pIWbemLocator = NULL; 
    98     throw WMIException("ExecQuery failed!" + query, hRes); 
     86 
     87  CComPtr< IEnumWbemClassObject > enumerator; 
     88  hr = service->ExecQuery( strQL, strQuery, WBEM_FLAG_FORWARD_ONLY, NULL, &enumerator ); 
     89  if (FAILED(hr)) { 
     90    throw WMIException("ExecQuery failed:" + query + " (reason is: " + ComError::getComError() + ")", hr); 
    9991  } 
    10092 
     93  CComPtr< IWbemClassObject > row = NULL; 
     94  ULONG retcnt; 
     95  int i=0; 
     96  while (hr = enumerator->Next( WBEM_INFINITE, 1L, &row, &retcnt ) == WBEM_S_NO_ERROR) { 
     97    if (SUCCEEDED(hr)) { 
     98      if (retcnt > 0) { 
     99        SAFEARRAY* pstrNames; 
     100        wmi_row returnRow; 
     101        hr = row->GetNames(NULL,WBEM_FLAG_ALWAYS|WBEM_FLAG_NONSYSTEM_ONLY,NULL,&pstrNames); 
     102        if (FAILED(hr)) { 
     103          throw WMIException("GetNames failed:" + query, hr); 
     104        } 
    101105 
    102   SAFEARRAY* pstrNames; 
    103   hRes = pClassObject->GetNames(NULL,WBEM_FLAG_ALWAYS|WBEM_FLAG_NONSYSTEM_ONLY,NULL,&pstrNames); 
    104   if (FAILED(hRes)) { 
    105     pClassObject->Release(); 
    106     pWbemServices->Release(); 
    107     pIWbemLocator->Release(); 
    108     throw WMIException("GetNames failed!" + query, hRes); 
     106        long index = 0, begin, end; 
     107        CComSafeArray<BSTR> arr = pstrNames; 
     108        begin = arr.GetLowerBound(); 
     109        end = arr.GetUpperBound(); 
     110        for ( index = begin; index <= end; index++ ) { 
     111          USES_CONVERSION; 
     112          CComBSTR bColumn = arr.GetAt(index); 
     113          std::string column = OLE2T(bColumn); 
     114          CComVariant vValue; 
     115          hr = row->Get(bColumn, 0, &vValue, 0, 0); 
     116          if (FAILED(hr)) { 
     117            throw WMIException("Failed to get value for " + column + " in query: " + query, hr); 
     118          } 
     119          WMIResult value; 
     120 
     121          if (vValue.vt == VT_INT) { 
     122            value.setNumeric(column, vValue.intVal); 
     123          } else if (vValue.vt == VT_I4) { 
     124            value.setNumeric(column, vValue.lVal); 
     125          } else if (vValue.vt == VT_UINT) { 
     126            value.setNumeric(column, vValue.uintVal); 
     127          } else if (vValue.vt == VT_BSTR) { 
     128            value.setString(column, OLE2T(vValue.bstrVal)); 
     129          } else if (vValue.vt == VT_NULL) { 
     130            value.setString(column, "NULL"); 
     131          } else if (vValue.vt == VT_BOOL) { 
     132            value.setBoth(column, vValue.iVal, vValue.iVal?"TRUE":"FALSE"); 
     133          } else { 
     134            NSC_LOG_ERROR_STD(column + " is not supported (type-id: " + strEx::itos(vValue.vt) + ")"); 
     135          } 
     136          returnRow.addValue(column, value); 
     137        } 
     138        ret.push_back(returnRow); 
     139      } 
     140    } 
     141    row.Release(); 
    109142  } 
    110   CComSafeArray<BSTR> arr = pstrNames; 
    111   long index = 0, begin, end; 
    112   begin = arr.GetLowerBound(); 
    113   end = arr.GetUpperBound(); 
    114   for ( index = begin; index <= end; index++ ) { 
    115     BSTR bStr = arr.GetAt(index); 
    116     CString str = bStr; 
    117     std::string std_str = str; 
    118     CComVariant vValue; 
    119     hRes = pClassObject->Get(bStr, 0, &vValue, 0, 0); 
    120     if (vValue.vt == VT_INT) { 
    121       ret[std_str] = vValue.intVal; 
    122       //std::cout << (LPCTSTR)str << " = (INT) " << vValue.intVal << std::endl; 
    123     } else if (vValue.vt == VT_I4) { 
    124       ret[std_str] = vValue.lVal; 
    125       //std::cout << (LPCTSTR)str << " = (I4) " << vValue.lVal << std::endl; 
    126     } else if (vValue.vt == VT_UINT) { 
    127       ret[std_str] = vValue.uintVal; 
    128       //std::cout << (LPCTSTR)str << " = (UINT) " << vValue.uintVal << std::endl; 
    129     } else if (vValue.vt == VT_BSTR) { 
    130       std::cout << (LPCTSTR)str << " = UNSUPPORTED (BSTR)" << std::endl; 
    131       CString val = vValue; 
    132       //ret[std_str] = std::string(val); 
    133     } else { 
    134       std::cout << (LPCTSTR)str << " = UNSUPPORTED" << vValue.vt << std::endl; 
    135     } 
    136   } 
    137   pIWbemLocator->Release(); 
    138   pWbemServices->Release(); 
    139   pEnumObject->Release(); 
    140   pClassObject->Release(); 
    141143  return ret; 
    142144} 
  • trunk/modules/CheckWMI/WMIQuery.h

    r2172776 r390faae  
    2323#include <string> 
    2424#include <map> 
     25#include <strEx.h> 
     26#include <error.hpp> 
     27#define NO_BOOST_DEP 
     28#include <filter_framework.hpp> 
    2529 
     30class ComError { 
     31public: 
     32  static std::string getComError() { 
     33    USES_CONVERSION; 
     34    CComPtr<IErrorInfo> errorInfo; 
     35    HRESULT hr = GetErrorInfo(NULL, &errorInfo); 
     36    if (FAILED(hr)) 
     37      return "unknown error: " + error::format::from_system(hr); 
     38    CComBSTR bDesc, bSource; 
     39    hr = errorInfo->GetSource(&bSource); 
     40    if (FAILED(hr)) 
     41      return "unknown error: " + error::format::from_system(hr); 
     42    hr = errorInfo->GetDescription(&bDesc); 
     43    if (FAILED(hr)) 
     44      return "unknown error: " + error::format::from_system(hr); 
     45    std::string ret = OLE2T(bSource); 
     46    ret += " - "; 
     47    ret += OLE2T(bDesc); 
     48    return ret; 
     49  } 
     50}; 
    2651 
    2752class WMIException { 
     53  std::string message_; 
    2854public: 
    2955  WMIException(std::string str, HRESULT code) { 
    30     std::cout << str << std::endl; 
    31  
     56    message_ = str + ":" + error::format::from_system(code); 
     57  } 
     58  std::string getMessage() { 
     59    return message_; 
    3260  } 
    3361}; 
     
    3866 
    3967public: 
     68  struct WMIResult { 
     69    std::string alias; 
     70    std::string string; 
     71    long long numeric; 
     72    bool isNumeric; 
     73    WMIResult() : isNumeric(false), numeric(0) {} 
     74    void setString(std::string a, std::string s) { 
     75      string = s; 
     76      numeric = 0; 
     77      alias = a; 
     78    } 
     79    void setNumeric(std::string a, long long n) { 
     80      numeric = n; 
     81      string = strEx::itos(n); 
     82      alias = a; 
     83    } 
     84    void setBoth(std::string a, long long n, std::string s) { 
     85      numeric = n; 
     86      string = s; 
     87      alias = a; 
     88    } 
     89  }; 
     90  struct wmi_row { 
     91    typedef std::map<std::string,WMIResult> list_type; 
     92    list_type results; 
     93    boolean hasAlias(std::string alias) const { 
     94      if (alias.empty()) 
     95        return true; 
     96      return results.find(alias) != results.end(); 
     97    } 
     98    const WMIResult get(std::string alias) const { 
     99      WMIResult ret; 
     100      list_type::const_iterator cit = results.find(alias); 
     101      if (cit != results.end()) 
     102        ret = (*cit).second; 
     103      return ret; 
     104    } 
     105    void addValue(std::string column, WMIResult value) { 
     106      results[column] = value; 
     107    } 
     108 
     109    std::string render() { 
     110      std::string ret; 
     111      for (list_type::const_iterator it = results.begin(); it != results.end(); ++it) { 
     112        if (!ret.empty()) ret += ", "; 
     113        ret += (*it).first + "=" + (*it).second.string; 
     114      } 
     115      return ret; 
     116    } 
     117 
     118  }; 
     119  typedef std::list<wmi_row> result_type; 
     120  struct wmi_filter { 
     121    std::string alias; 
     122    filters::filter_all_strings string; 
     123    filters::filter_all_numeric<unsigned long long, checkHolders::int64_handler >  numeric; 
     124 
     125    inline bool hasFilter() { 
     126      return string.hasFilter() || numeric.hasFilter(); 
     127    } 
     128    bool matchFilter(const wmi_row &value) const { 
     129      if (!value.hasAlias(alias)) { 
     130        NSC_DEBUG_MSG_STD("We don't have any column matching: " + alias); 
     131        return false; 
     132      } 
     133      if (alias.empty()) { 
     134        for (wmi_row::list_type::const_iterator cit = value.results.begin(); cit != value.results.end(); ++cit) { 
     135          if ((string.hasFilter())&&(string.matchFilter((*cit).second.string))) 
     136            return true; 
     137          else if ((numeric.hasFilter())&&(numeric.matchFilter((*cit).second.numeric))) 
     138            return true; 
     139        } 
     140      } else { 
     141        if ((string.hasFilter())&&(string.matchFilter(value.get(alias).string))) 
     142          return true; 
     143        else if ((numeric.hasFilter())&&(numeric.matchFilter(value.get(alias).numeric))) 
     144          return true; 
     145      } 
     146      NSC_DEBUG_MSG_STD("Value did not match a filter: " + alias); 
     147      return false; 
     148    } 
     149  }; 
    40150  WMIQuery(void); 
    41151  ~WMIQuery(void); 
    42152 
    43   std::map<std::string,int> execute(std::string query); 
     153  result_type execute(std::string query); 
    44154 
    45155  bool initialize(); 
  • trunk/modules/CheckWMI/stdafx.h

    r86fc50e r390faae  
    3232#define _WIN32_DCOM 
    3333#include <objbase.h> 
     34 
    3435#include <NSCAPI.h> 
    3536#include <NSCHelper.h> 
     37#define NO_BOOST_DEP 
     38#include <checkHelpers.hpp> 
     39#include <filter_framework.hpp> 
    3640 
    3741#include <atlbase.h> 
    38 #include <atlcom.h> 
    39 #include <atlstr.h> 
     42//#include <atlcom.h> 
     43//#include <atlstr.h> 
    4044#include <atlsafe.h> 
    4145 
  • trunk/modules/NRPEListener/NRPEListener.vcproj

    r86fc50e r390faae  
    11491149      </File> 
    11501150      <File 
     1151        RelativePath="..\..\include\error.hpp" 
     1152        > 
     1153      </File> 
     1154      <File 
    11511155        RelativePath=".\NRPEListener.h" 
    11521156        > 
Note: See TracChangeset for help on using the changeset viewer.