Changeset 3b8eb61 in nscp


Ignore:
Timestamp:
05/18/05 22:46:09 (8 years ago)
Author:
Michael Medin <michael@…>
Children:
7da80b5
Parents:
f896cfb
Message:

2005-05-18 MickeM

  • Fixed bug in NSC.ini (section title for Check System was wrong) + Added multitasking for plugins (now two plugins can handle requests simultaniously)
  • Fixed som minor issues here and there
Location:
trunk
Files:
2 added
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/NSClient++.cpp

    rf896cfb r3b8eb61  
    174174 */ 
    175175void NSClientT::loadPlugins(const std::list<std::string> plugins) { 
     176  ReadLock readLock(&m_mutexRW, true, 10000); 
     177  if (!readLock.IsLocked()) { 
     178    LOG_ERROR("FATAL ERROR: Could not get read-mutex."); 
     179    return; 
     180  } 
     181  /* 
    176182  MutexLock lock(pluginMutex); 
    177183  if (!lock.hasMutex()) { 
     
    179185    return; 
    180186  } 
     187  */ 
    181188  std::list<std::string>::const_iterator it; 
    182189  for (it = plugins.begin(); it != plugins.end(); ++it) { 
     
    188195 */ 
    189196void NSClientT::unloadPlugins() { 
    190   MutexLock lock(pluginMutex,20000); 
    191   if (!lock.hasMutex()) { 
     197  { 
     198    WriteLock writeLock(&m_mutexRW, true, 10000); 
     199    if (!writeLock.IsLocked()) { 
     200      LOG_ERROR("FATAL ERROR: Could not get read-mutex."); 
     201      return; 
     202    } 
     203    /*  MutexLock lock(pluginMutex,20000); 
     204    if (!lock.hasMutex()) { 
    192205    LOG_ERROR("FATAL ERROR: Could not get mutex."); 
    193206    return; 
    194   } 
    195   commandHandlers_.clear(); 
     207    } 
     208    */ 
     209    commandHandlers_.clear(); 
     210    { 
     211      /*    MutexLock lock(messageMutex,20000); 
     212      if (!lock.hasMutex()) { 
     213      LOG_ERROR("FATAL ERROR: Could not get mutex (we will now crash BTW)."); 
     214      } else { 
     215      messageHandlers_.clear(); 
     216      } 
     217      */ 
     218      messageHandlers_.clear(); 
     219    } 
     220  } 
    196221  { 
    197     MutexLock lock2(messageMutex,20000); 
    198     if (!lock2.hasMutex()) { 
    199       LOG_ERROR("FATAL ERROR: Could not get mutex (we will now crash BTW)."); 
    200     } else { 
    201       messageHandlers_.clear(); 
    202     } 
    203   } 
    204   for (pluginList::size_type i=plugins_.size();i>0;i--) { 
    205     NSCPlugin *p = plugins_[i-1]; 
    206     LOG_DEBUG_STD("Unloading plugin: " + p->getName() + "..."); 
    207     p->unload(); 
    208   } 
    209  
    210   for (unsigned int i=plugins_.size();i>0;i--) { 
    211     NSCPlugin *p = plugins_[i-1]; 
    212     plugins_[i-1] = NULL; 
    213     delete p; 
    214   } 
    215   plugins_.clear(); 
     222    ReadLock readLock(&m_mutexRW, true, 10000); 
     223    if (!readLock.IsLocked()) { 
     224      LOG_ERROR("FATAL ERROR: Could not get read-mutex."); 
     225      return; 
     226    } 
     227    for (pluginList::size_type i=plugins_.size();i>0;i--) { 
     228      NSCPlugin *p = plugins_[i-1]; 
     229      LOG_DEBUG_STD("Unloading plugin: " + p->getName() + "..."); 
     230      p->unload(); 
     231    } 
     232  } 
     233 
     234  { 
     235    WriteLock writeLock(&m_mutexRW, true, 10000); 
     236    if (!writeLock.IsLocked()) { 
     237      LOG_ERROR("FATAL ERROR: Could not get read-mutex."); 
     238      return; 
     239    } 
     240    for (unsigned int i=plugins_.size();i>0;i--) { 
     241      NSCPlugin *p = plugins_[i-1]; 
     242      plugins_[i-1] = NULL; 
     243      delete p; 
     244    } 
     245    plugins_.clear(); 
     246  } 
    216247} 
    217248/** 
     
    220251 */ 
    221252void NSClientT::loadPlugin(const std::string file) { 
     253  LOG_DEBUG_STD("Loading: " + file); 
    222254  addPlugin(new NSCPlugin(file)); 
    223255} 
     
    227259 */ 
    228260void NSClientT::addPlugin(plugin_type plugin) { 
     261  { 
     262    ReadLock readLock(&m_mutexRW, true, 5000); 
     263    if (!readLock.IsLocked()) { 
     264      LOG_ERROR("FATAL ERROR: Could not get read-mutex."); 
     265      return; 
     266    } 
     267    plugin->load(); 
     268  } 
     269  /* 
    229270  MutexLock lock(pluginMutex); 
    230271  if (!lock.hasMutex()) { 
     
    232273    return; 
    233274  } 
    234   plugin->load(); 
    235   LOG_DEBUG_STD("Loading: " + plugin->getName()); 
    236   // @todo Catch here and unload if we fail perhaps ? 
    237   plugins_.insert(plugins_.end(), plugin); 
    238   if (plugin->hasCommandHandler()) 
    239     commandHandlers_.insert(commandHandlers_.end(), plugin); 
    240   if (plugin->hasMessageHandler()) 
    241     messageHandlers_.insert(messageHandlers_.end(), plugin); 
     275  */ 
     276 
     277  { 
     278    WriteLock writeLock(&m_mutexRW, true, 10000); 
     279    if (!writeLock.IsLocked()) { 
     280      LOG_ERROR("FATAL ERROR: Could not get read-mutex."); 
     281      return; 
     282    } 
     283    // @todo Catch here and unload if we fail perhaps ? 
     284    plugins_.insert(plugins_.end(), plugin); 
     285    if (plugin->hasCommandHandler()) 
     286      commandHandlers_.insert(commandHandlers_.end(), plugin); 
     287    if (plugin->hasMessageHandler()) 
     288      messageHandlers_.insert(messageHandlers_.end(), plugin); 
     289  } 
     290 
    242291} 
    243292 
     
    271320 */ 
    272321NSCAPI::nagiosReturn NSClientT::injectRAW(const char* command, const unsigned int argLen, char **argument, char *returnMessageBuffer, unsigned int returnMessageBufferLen, char *returnPerfBuffer, unsigned int returnPerfBufferLen) { 
    273   MutexLock lock(pluginMutex); 
     322  ReadLock readLock(&m_mutexRW, true, 5000); 
     323  if (!readLock.IsLocked()) { 
     324    LOG_ERROR("FATAL ERROR: Could not get read-mutex."); 
     325    return NSCAPI::returnUNKNOWN; 
     326  } 
     327/*  MutexLock lock(pluginMutex); 
    274328  if (!lock.hasMutex()) { 
    275329    LOG_ERROR_STD("Failed to get mutex (" + strEx::itos(lock.getWaitResult()) + "), command ignored..."); 
    276330    return NSCAPI::returnUNKNOWN; 
    277331  } 
    278  
     332*/ 
    279333  for (pluginList::size_type i = 0; i < commandHandlers_.size(); i++) { 
    280334    try { 
     
    290344        case NSCAPI::returnCRIT: 
    291345        case NSCAPI::returnUNKNOWN: 
    292           LOG_DEBUG_STD("Injected Result: " +(std::string) returnMessageBuffer); 
    293           LOG_DEBUG_STD("Injected Performance Result: " +(std::string) returnPerfBuffer); 
     346//          LOG_DEBUG_STD("Injected Result: " +(std::string) returnMessageBuffer); 
     347//          LOG_DEBUG_STD("Injected Performance Result: " +(std::string) returnPerfBuffer); 
    294348          return c; 
    295349        default: 
     
    314368 */ 
    315369void NSClientT::reportMessage(int msgType, const char* file, const int line, std::string message) { 
     370  ReadLock readLock(&m_mutexRW, true, 5000); 
     371  if (!readLock.IsLocked()) { 
     372    std::cout << "Message was lost as the core was locked..." << std::endl; 
     373    return; 
     374  } 
    316375  MutexLock lock(messageMutex); 
    317376  if (!lock.hasMutex()) { 
     
    320379    return; 
    321380  } 
     381  /* 
     382  MutexLock lock(messageMutex); 
     383  if (!lock.hasMutex()) { 
     384    std::cout << "Message was lost as the core was locked..." << std::endl; 
     385    std::cout << message << std::endl; 
     386    return; 
     387  } 
     388  */ 
    322389  if (g_bConsoleLog) { 
    323390    std::string k = "?"; 
     
    364431} 
    365432std::string NSClientT::getBasePath(void) { 
    366   MutexLock lock(pluginMutex); 
     433  MutexLock lock(internalVariables); 
    367434  if (!lock.hasMutex()) { 
    368435    LOG_ERROR("FATAL ERROR: Could not get mutex."); 
  • trunk/NSClient++.h

    r5d8e0b5 r3b8eb61  
    77#include <Mutex.h> 
    88#include <NSCAPI.h> 
     9#include <MutexRW.h> 
    910 
    1011 
     
    4243  pluginList messageHandlers_; 
    4344  std::string basePath; 
    44   MutexHandler pluginMutex; 
     45  MutexHandler internalVariables; 
     46//  MutexHandler pluginMutex; 
    4547  MutexHandler messageMutex; 
     48  MutexRW  m_mutexRW; 
    4649 
    4750public: 
  • trunk/NSClient++.vcproj

    rf896cfb r3b8eb61  
    296296      </File> 
    297297      <File 
     298        RelativePath=".\include\MutexRW.h"> 
     299      </File> 
     300      <File 
    298301        RelativePath=".\include\NSCAPI.h"> 
    299302      </File> 
  • trunk/changelog

    rf896cfb r3b8eb61  
     12005-05-18 MickeM 
     2 * Fixed bug in NSC.ini (section title for Check System was wrong) 
     3 + Added multitasking for plugins (now two plugins can handle requests simultaniously) 
     4 * Fixed som minor issues here and there 
     5 
    162005-05-15 MickeM 
    27 + Added NRPE support for checkCounter 
  • trunk/include/EnumProcess.cpp

    rf22f5a9 r3b8eb61  
    55#include "stdafx.h" 
    66#include "EnumProcess.h" 
    7  
    8  
    9 #ifdef _DEBUG 
    10 #undef THIS_FILE 
    11 static char THIS_FILE[]=__FILE__; 
    12 #define new DEBUG_NEW 
    13 #endif 
    147 
    158////////////////////////////////////////////////////////////////////// 
  • trunk/include/PDHCounter.h

    rf7f536b r3b8eb61  
    5050      PDH_STATUS status; 
    5151      assert(hQuery != NULL); 
    52       assert(hCounter_ == NULL); 
     52      if (hCounter_ != NULL) 
     53        throw PDHException("addToQuery failed (already opened): " + name_); 
    5354      if (listener_) 
    5455        listener_->attach(*this); 
    5556      if ((status = PdhAddCounter(hQuery, name_.c_str(), 0, &hCounter_)) != ERROR_SUCCESS)  
    56         throw PDHException("PdhOpenQuery failed", status); 
     57        throw PDHException("PdhOpenQuery failed: " + name_, status); 
    5758      assert(hCounter_ != NULL); 
    5859    } 
    5960    void remove() { 
    60       assert(hCounter_ != NULL); 
     61      if (hCounter_ == NULL) 
     62        return; 
    6163      PDH_STATUS status; 
    6264      if (listener_) 
     
    6769    } 
    6870    void collect() { 
    69       assert(hCounter_ != NULL); 
     71      if (hCounter_ == NULL) 
     72        return; 
    7073      PDH_STATUS status; 
    7174      if ((status = PdhGetFormattedCounterValue(hCounter_, PDH_FMT_LARGE , NULL, &data_)) != ERROR_SUCCESS) 
  • trunk/modules/CheckHelpers/CheckHelpers.cpp

    rf896cfb r3b8eb61  
    103103    std::string tMsg, tPerf; 
    104104    NSCAPI::nagiosReturn tRet = NSCModuleHelper::InjectCommand((*cit2).first.c_str(), length, args, tMsg, tPerf); 
     105    arrayBuffer::destroyArrayBuffer(args, length); 
    105106    returnCode = NSCHelper::maxState(returnCode, tRet); 
    106107    if (!msg.empty()) 
  • trunk/modules/CheckSystem/CheckSystem.cpp

    rf896cfb r3b8eb61  
    4848 
    4949  if (wantedMethod == C_SYSTEM_ENUMPROC_METHOD_PSAPI) { 
     50    NSC_DEBUG_MSG_STD("Using PSAPI method."); 
    5051    if (method == (method|ENUM_METHOD::PSAPI)) { 
    5152      processMethod_ = ENUM_METHOD::PSAPI; 
    5253    } else { 
    53       NSC_LOG_ERROR_STD("PSAPI method not avalible, check " C_SYSTEM_ENUMPROC_METHOD " option."); 
     54      NSC_LOG_ERROR_STD("PSAPI method not available, check " C_SYSTEM_ENUMPROC_METHOD " option."); 
    5455    } 
    5556  } else { 
     57    NSC_DEBUG_MSG_STD("Using TOOLHELP method."); 
    5658    if (method == (method|ENUM_METHOD::TOOLHELP)) { 
    5759      processMethod_ = ENUM_METHOD::TOOLHELP; 
     
    6870 */ 
    6971bool CheckSystem::unloadModule() { 
    70   if (!pdhThread.exitThread(20000)) 
     72  if (!pdhThread.exitThread(20000)) { 
     73    std::cout << "MAJOR ERROR: Could not unload thread..." << std::endl; 
    7174    NSC_LOG_ERROR("Could not exit the thread, memory leak and potential corruption may be the result..."); 
     75  } 
    7276  return true; 
    7377} 
     
    168172  for (std::list<std::string>::iterator it = times.begin(); it != times.end(); ++it) { 
    169173    PDHCollector *pObject = pdhThread.getThread(); 
    170     assert(pObject); 
     174    if (!pObject) { 
     175      msg = "ERROR: PDH Collection thread not running."; 
     176      return NSCAPI::returnUNKNOWN; 
     177    } 
    171178    if (bNSCLientCompatible) { 
    172179      int v = pObject->getCPUAvrage((*it) + "m"); 
     
    233240  } 
    234241  PDHCollector *pObject = pdhThread.getThread(); 
    235   assert(pObject); 
     242  if (!pObject) { 
     243    msg = "ERROR: PDH Collection thread not running."; 
     244    return NSCAPI::returnUNKNOWN; 
     245  } 
    236246  unsigned long long uptime = pObject->getUptime(); 
    237247  if (bNSCLientCompatible) { 
     
    381391 
    382392  PDHCollector *pObject = pdhThread.getThread(); 
    383   assert(pObject); 
     393  if (!pObject) { 
     394    msg = "ERROR: PDH Collection thread not running."; 
     395    return NSCAPI::returnUNKNOWN; 
     396  } 
    384397  long long pageCommit = pObject->getMemCommit();  
    385398  long long pageCommitLimit = pObject->getMemCommitLimit();  
     
    567580 
    568581  for (std::list<std::pair<std::string,std::string> >::iterator it = counters.begin(); it != counters.end(); ++it) { 
     582    std::string name; 
    569583    try { 
    570       try { 
    571         PDH::PDHQuery pdh; 
    572         PDHCollectors::StaticPDHCounterListener counter; 
    573         pdh.addCounter((*it).second, &counter); 
    574         pdh.open(); 
    575         pdh.collect(); 
    576         std::string name = (*it).first; 
    577         if (name.empty()) 
    578           name = (*it).second; 
    579         if (bNSCLientCompatible) { 
    580           msg += strEx::itos(counter.getValue()); 
    581         } else { 
    582           std::string tStr; 
    583           if (crit.max.hasBounds() && crit.max.checkMAX(counter.getValue())) { 
    584             tStr = crit.max.prettyPrint(name, counter.getValue()) + " > critical"; 
    585             NSCHelper::escalteReturnCodeToCRIT(returnCode); 
    586           } else if (crit.min.hasBounds() && crit.min.checkMIN(counter.getValue())) { 
    587             tStr = crit.min.prettyPrint(name, counter.getValue()) + " < critical"; 
    588             NSCHelper::escalteReturnCodeToCRIT(returnCode); 
    589           } else if (warn.max.hasBounds() && warn.max.checkMAX(counter.getValue())) { 
    590             tStr = warn.max.prettyPrint(name, counter.getValue()) + " > warning"; 
    591             NSCHelper::escalteReturnCodeToWARN(returnCode); 
    592           } else if (warn.min.hasBounds() && warn.min.checkMIN(counter.getValue())) { 
    593             tStr = warn.min.prettyPrint(name, counter.getValue()) + " < warning"; 
    594             NSCHelper::escalteReturnCodeToWARN(returnCode); 
    595           } else if (bShowAll) { 
    596             tStr = name + ": " + strEx::itos(counter.getValue()); 
    597           } 
    598           perf += checkHolders::SizeMaxMin<__int64, checkHolders::int64_handler<> >::printPerf(name, counter.getValue(), warn, crit); 
    599           msg += tStr; 
     584      PDH::PDHQuery pdh; 
     585      PDHCollectors::StaticPDHCounterListener counter; 
     586      std::string name = (*it).first; 
     587      if (name.empty()) 
     588        name = (*it).second; 
     589      pdh.addCounter((*it).second, &counter); 
     590      pdh.open(); 
     591      pdh.collect(); 
     592      if (bNSCLientCompatible) { 
     593        msg += strEx::itos(counter.getValue()); 
     594      } else { 
     595        std::string tStr; 
     596        if (crit.max.hasBounds() && crit.max.checkMAX(counter.getValue())) { 
     597          tStr = crit.max.prettyPrint(name, counter.getValue()) + " > critical"; 
     598          NSCHelper::escalteReturnCodeToCRIT(returnCode); 
     599        } else if (crit.min.hasBounds() && crit.min.checkMIN(counter.getValue())) { 
     600          tStr = crit.min.prettyPrint(name, counter.getValue()) + " < critical"; 
     601          NSCHelper::escalteReturnCodeToCRIT(returnCode); 
     602        } else if (warn.max.hasBounds() && warn.max.checkMAX(counter.getValue())) { 
     603          tStr = warn.max.prettyPrint(name, counter.getValue()) + " > warning"; 
     604          NSCHelper::escalteReturnCodeToWARN(returnCode); 
     605        } else if (warn.min.hasBounds() && warn.min.checkMIN(counter.getValue())) { 
     606          tStr = warn.min.prettyPrint(name, counter.getValue()) + " < warning"; 
     607          NSCHelper::escalteReturnCodeToWARN(returnCode); 
     608        } else if (bShowAll) { 
     609          tStr = name + ": " + strEx::itos(counter.getValue()); 
    600610        } 
    601  
    602         pdh.close(); 
    603       } catch (const PDH::PDHException &e) { 
    604         NSC_LOG_ERROR_STD("ERROR: " + e.str_); 
    605         msg = static_cast<std::string>("ERROR: ") + e.str_; 
    606         return 0; 
     611        perf += checkHolders::SizeMaxMin<__int64, checkHolders::int64_handler<> >::printPerf(name, counter.getValue(), warn, crit); 
     612        msg += tStr; 
    607613      } 
    608     } catch (PDH::PDHException e) { 
    609       NSC_LOG_ERROR_STD("ERROR: " + e.str_); 
     614      pdh.close(); 
     615    } catch (const PDH::PDHException &e) { 
     616      NSC_LOG_ERROR_STD("ERROR: " + e.str_ + " (" + name + ")"); 
    610617      msg = static_cast<std::string>("ERROR: ") + e.str_; 
    611       return NSCAPI::returnCRIT; 
     618      return 0; 
    612619    } 
    613620  } 
  • trunk/modules/CheckSystem/PDHCollector.cpp

    rae192e3 r3b8eb61  
    8282  }while (!(WaitForSingleObject(hStopEvent_, checkIntervall_*100) == WAIT_OBJECT_0)); 
    8383 
    84   if (!CloseHandle(hStopEvent_)) 
    85     NSC_LOG_ERROR_STD("Failed to close stopEvent handle: " + strEx::itos(GetLastError())); 
    86   else 
    87     hStopEvent_ = NULL; 
    88   try { 
    89     pdh.close(); 
    90   } catch (const PDH::PDHException &e) { 
    91     NSC_LOG_ERROR_STD("Failed to close performance counters: " + e.str_); 
     84  { 
     85    MutexLock mutex(mutexHandler); 
     86    if (!mutex.hasMutex()) { 
     87      NSC_LOG_ERROR("Failed to get Mute when closing thread!"); 
     88    } 
     89 
     90    if (!CloseHandle(hStopEvent_)) 
     91      NSC_LOG_ERROR_STD("Failed to close stopEvent handle: " + strEx::itos(GetLastError())); 
     92    else 
     93      hStopEvent_ = NULL; 
     94    try { 
     95      pdh.close(); 
     96    } catch (const PDH::PDHException &e) { 
     97      NSC_LOG_ERROR_STD("Failed to close performance counters: " + e.str_); 
     98    } 
    9299  } 
    93100  return 0; 
     
    99106 */ 
    100107void PDHCollector::exitThread(void) { 
     108  MutexLock mutex(mutexHandler); 
     109  if (!mutex.hasMutex()) { 
     110    NSC_LOG_ERROR("Failed to get Mute when trying to close thread!"); 
     111    return; 
     112  } 
    101113  if (hStopEvent_ == NULL) 
    102     NSC_LOG_ERROR("Failed to get Mutex!"); 
     114    NSC_LOG_ERROR("Failed to get stop event!"); 
    103115  else 
    104116    if (!SetEvent(hStopEvent_)) { 
  • trunk/modules/CheckSystem/PDHCollector.h

    r5d8e0b5 r3b8eb61  
    4949 
    5050private: 
    51   bool isRunning(void); 
    52   void startRunning(void); 
    53   void stopRunning(void); 
     51//  bool isRunning(void); 
     52//  void startRunning(void); 
     53//  void stopRunning(void); 
    5454 
    5555}; 
Note: See TracChangeset for help on using the changeset viewer.