Changeset 394f7a1 in nscp


Ignore:
Timestamp:
02/18/08 23:21:23 (5 years ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2, stable
Children:
95e4ace
Parents:
c1fe385
Message:

+ Added propper output handling to process subsystem (now you can execute programs tat return "much" data.

+ Added select support for SSL_write (now you can send "any amount of data" to the (SSL) socket.

Since check_nrpe doesn't do this it wont work in that end, but still...

Files:
6 added
2 deleted
26 edited

Legend:

Unmodified
Added
Removed
  • AutoBuild.h

    r7a156f4 r394f7a1  
    33// change the FALSE to TRUE for autoincrement of build number 
    44#define INCREMENT_VERSION TRUE 
    5 #define FILEVER        0,3,0,54 
    6 #define PRODUCTVER     0,3,0,54 
    7 #define STRFILEVER     _T("0.3.0.54") 
    8 #define STRPRODUCTVER  _T("0.3.0.54") 
    9 #define STRPRODUCTDATE  _T("2008-02-12") 
     5#define FILEVER        0,3,0,58 
     6#define PRODUCTVER     0,3,0,58 
     7#define STRFILEVER     _T("0.3.0.58") 
     8#define STRPRODUCTVER  _T("0.3.0.58") 
     9#define STRPRODUCTDATE  _T("2008-02-14") 
    1010#endif // AUTOBUILD_H 
  • NSC.dist

    rc1fe385 r394f7a1  
    8383;socket_timeout=30 
    8484 
     85[NRPE] 
     86;# NRPE PORT NUMBER 
     87;  This is the port the NRPEListener.dll will listen to. 
     88;port=5666 
     89; 
     90;# COMMAND TIMEOUT 
     91;  This specifies the maximum number of seconds that the NRPE daemon will allow plug-ins to finish executing before killing them off. 
     92;command_timeout=60 
     93; 
     94;# COMMAND ARGUMENT PROCESSING 
     95;  This option determines whether or not the NRPE daemon will allow clients to specify arguments to commands that are executed. 
     96;allow_arguments=0 
     97; 
     98;# COMMAND ALLOW NASTY META CHARS 
     99;  This option determines whether or not the NRPE daemon will allow clients to specify nasty (as in |`&><'"\[]{}) characters in arguments. 
     100;allow_nasty_meta_chars=0 
     101; 
     102;# USE SSL SOCKET 
     103;  This option controls if SSL should be used on the socket. 
     104;use_ssl=1 
     105; 
     106;# BIND TO ADDRESS 
     107;  Allows you to bind server to a specific local address. This has to be a dotted ip adress not a hostname. 
     108;  Leaving this blank will bind to all avalible IP adresses. 
     109; bind_to_address= 
     110; 
     111;# ALLOWED HOST ADDRESSES 
     112;  This is a comma-delimited list of IP address of hosts that are allowed to talk to NRPE deamon. 
     113;  If you leave this blank the global version will be used instead. 
     114;allowed_hosts= 
     115; 
     116;# SCRIPT DIRECTORY 
     117;  All files in this directory will become check commands. 
     118;  *WARNING* This is undoubtedly dangerous so use with care! 
     119;script_dir=scripts\ 
     120; 
     121;# SOCKET TIMEOUT 
     122;  Timeout when reading packets on incoming sockets. If the data has not arrived withint this time we will bail out. 
     123;socket_timeout=30 
    85124 
    86125[Check System] 
     
    136175 
    137176 
    138 [NRPE] 
    139 ;# NRPE PORT NUMBER 
    140 ;  This is the port the NRPEListener.dll will listen to. 
    141 ;port=5666 
    142 ; 
    143 ;# COMMAND TIMEOUT 
    144 ;  This specifies the maximum number of seconds that the NRPE daemon will allow plug-ins to finish executing before killing them off. 
    145 ;command_timeout=60 
    146 ; 
    147 ;# COMMAND ARGUMENT PROCESSING 
    148 ;  This option determines whether or not the NRPE daemon will allow clients to specify arguments to commands that are executed. 
    149 ;allow_arguments=0 
    150 ; 
    151 ;# COMMAND ALLOW NASTY META CHARS 
    152 ;  This option determines whether or not the NRPE daemon will allow clients to specify nasty (as in |`&><'"\[]{}) characters in arguments. 
    153 ;allow_nasty_meta_chars=0 
    154 ; 
    155 ;# USE SSL SOCKET 
    156 ;  This option controls if SSL should be used on the socket. 
    157 ;use_ssl=1 
     177 
     178 
     179; [includes] 
     180;# The order when used is "reversed" thus the last included file will be "first" 
     181;# Included files can include other files (be carefull only do basic recursive checking) 
     182; 
     183; myotherfile.ini 
     184; real.ini 
     185 
     186 
     187[NSCA Agent] 
     188;# CHECK INTERVALL (in seconds) 
     189;   How often we should run the checks and submit the results. 
     190;interval=5 
     191; 
     192;# ENCRYPTION METHOD 
     193;   This option determines the method by which the send_nsca client will encrypt the packets it sends  
     194;   to the nsca daemon. The encryption method you choose will be a balance between security and  
     195;   performance, as strong encryption methods consume more processor resources. 
     196;   You should evaluate your security needs when choosing an encryption method. 
     197; 
     198; Note: The encryption method you specify here must match the decryption method the nsca daemon uses  
     199;       (as specified in the nsca.cfg file)!! 
     200; Values: 
     201; 0 = None  (Do NOT use this option) 
     202; 1 = Simple XOR  (No security, just obfuscation, but very fast) 
     203;   2 = DES 
     204;   3 = 3DES (Triple DES) 
     205; 4 = CAST-128 
     206; 6 = xTEA 
     207; 8 = BLOWFISH 
     208; 9 = TWOFISH 
     209; 11 = RC2 
     210; 14 = RIJNDAEL-128 (AES) 
     211; 20 = SERPENT 
     212;encryption_method=14 
     213; 
     214;# ENCRYPTION PASSWORD 
     215;  This is the password/passphrase that should be used to encrypt the sent packets.  
     216;password= 
    158217; 
    159218;# BIND TO ADDRESS 
    160219;  Allows you to bind server to a specific local address. This has to be a dotted ip adress not a hostname. 
    161 ;  Leaving this blank will bind to all avalible IP adresses. 
    162 ; bind_to_address= 
    163 ; 
    164 ;# ALLOWED HOST ADDRESSES 
    165 ;  This is a comma-delimited list of IP address of hosts that are allowed to talk to NRPE deamon. 
    166 ;  If you leave this blank the global version will be used instead. 
    167 ;allowed_hosts= 
    168 ; 
    169 ;# SCRIPT DIRECTORY 
    170 ;  All files in this directory will become check commands. 
    171 ;  *WARNING* This is undoubtedly dangerous so use with care! 
    172 ;script_dir=scripts\ 
    173 ; 
    174 ;# SOCKET TIMEOUT 
    175 ;  Timeout when reading packets on incoming sockets. If the data has not arrived withint this time we will bail out. 
    176 ;socket_timeout=30 
    177  
    178  
     220;  Leaving this blank will bind to "one" local interface. 
     221; -- not supported as of now --  
     222;bind_to_address= 
     223; 
     224;# LOCAL HOST NAME 
     225;  The name of this host (if empty "computername" will be used. 
     226;hostname= 
     227; 
     228;# NAGIOS SERVER ADDRESS 
     229;  The address to the nagios server to submit results to. 
     230;nsca_host=192.168.0.1 
     231; 
     232;# NAGIOS SERVER PORT 
     233;  The port to the nagios server to submit results to. 
     234;nsca_port=5667 
     235; 
     236 
     237;# CHECK COMMAND LIST 
     238;  The checks to run everytime we submit results back to nagios 
     239;  Any command(alias/key) starting with a host_ is sent as HOST_COMMAND others are sent as SERVICE_COMMANDS 
     240;  where the alias/key is used as service name. 
     241; 
     242[NSCA Commands] 
     243;my_cpu_check=checkCPU warn=80 crit=90 time=20m time=10s time=4 
     244;my_mem_check=checkMem MaxWarn=80% MaxCrit=90% ShowAll type=page 
     245;my_svc_check=checkServiceState CheckAll exclude=wampmysqld exclude=MpfService 
     246;host_check=check_ok 
    179247 
    180248[NRPE Handlers] 
     
    206274;check_nok=scripts\xlong.bat 
    207275;check_vbs=cscript.exe //T:30 //NoLogo scripts\check_vb.vbs 
    208  
    209  
    210 ; [includes] 
    211 ;# The order when used is "reversed" thus the last included file will be "first" 
    212 ;# Included files can include other files (be carefull only do basic recursive checking) 
    213 ; 
    214 ; myotherfile.ini 
    215 ; real.ini 
    216  
    217  
    218 [NSCA Agent] 
    219 ;# CHECK INTERVALL (in seconds) 
    220 ;   How often we should run the checks and submit the results. 
    221 ;interval=5 
    222 ; 
    223 ;# ENCRYPTION METHOD 
    224 ;   This option determines the method by which the send_nsca client will encrypt the packets it sends  
    225 ;   to the nsca daemon. The encryption method you choose will be a balance between security and  
    226 ;   performance, as strong encryption methods consume more processor resources. 
    227 ;   You should evaluate your security needs when choosing an encryption method. 
    228 ; 
    229 ; Note: The encryption method you specify here must match the decryption method the nsca daemon uses  
    230 ;       (as specified in the nsca.cfg file)!! 
    231 ; Values: 
    232 ; 0 = None  (Do NOT use this option) 
    233 ; 1 = Simple XOR  (No security, just obfuscation, but very fast) 
    234 ;   2 = DES 
    235 ;   3 = 3DES (Triple DES) 
    236 ; 4 = CAST-128 
    237 ; 6 = xTEA 
    238 ; 8 = BLOWFISH 
    239 ; 9 = TWOFISH 
    240 ; 11 = RC2 
    241 ; 14 = RIJNDAEL-128 (AES) 
    242 ; 20 = SERPENT 
    243 ;encryption_method=14 
    244 ; 
    245 ;# ENCRYPTION PASSWORD 
    246 ;  This is the password/passphrase that should be used to encrypt the sent packets.  
    247 ;password= 
    248 ; 
    249 ;# BIND TO ADDRESS 
    250 ;  Allows you to bind server to a specific local address. This has to be a dotted ip adress not a hostname. 
    251 ;  Leaving this blank will bind to "one" local interface. 
    252 ; -- not supported as of now --  
    253 ;bind_to_address= 
    254 ; 
    255 ;# LOCAL HOST NAME 
    256 ;  The name of this host (if empty "computername" will be used. 
    257 ;hostname= 
    258 ; 
    259 ;# NAGIOS SERVER ADDRESS 
    260 ;  The address to the nagios server to submit results to. 
    261 ;nsca_host=192.168.0.1 
    262 ; 
    263 ;# NAGIOS SERVER PORT 
    264 ;  The port to the nagios server to submit results to. 
    265 ;nsca_port=5667 
    266 ; 
    267  
    268 ;# CHECK COMMAND LIST 
    269 ;  The checks to run everytime we submit results back to nagios 
    270 ;  Any command(alias/key) starting with a host_ is sent as HOST_COMMAND others are sent as SERVICE_COMMANDS 
    271 ;  where the alias/key is used as service name. 
    272 ; 
    273 [NSCA Commands] 
    274 ;my_cpu_check=checkCPU warn=80 crit=90 time=20m time=10s time=4 
    275 ;my_mem_check=checkMem MaxWarn=80% MaxCrit=90% ShowAll type=page 
    276 ;my_svc_check=checkServiceState CheckAll exclude=wampmysqld exclude=MpfService 
    277 ;host_check=check_ok 
  • NSClient++-2005.sln

    rce57b1b r394f7a1  
    5252EndProject 
    5353Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Crypt++", "..\Crypt++\Crypt++.vcproj", "{630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}" 
     54EndProject 
     55Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "NRPEClient", "modules\NRPEClient\NRPEClient-2005.vcproj", "{4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}" 
    5456EndProject 
    5557Global 
     
    720722    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Release|Win32.Build.0 = Release|Win32 
    721723    {630857C0-6AD8-4CEE-B5F7-CA1DF620CA9A}.Release|x64.ActiveCfg = Release|Win32 
     724    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Debug|Any CPU.ActiveCfg = Debug|x64 
     725    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Debug|Itanium.ActiveCfg = Debug|x64 
     726    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Debug|Mixed Platforms.ActiveCfg = Debug|x64 
     727    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Debug|Mixed Platforms.Build.0 = Debug|x64 
     728    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Debug|Win32.ActiveCfg = Debug|Win32 
     729    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Debug|Win32.Build.0 = Debug|Win32 
     730    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Debug|x64.ActiveCfg = Debug|x64 
     731    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Debug|x64.Build.0 = Debug|x64 
     732    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Debug-MemCheck|Any CPU.ActiveCfg = Debug-MemCheck|x64 
     733    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Debug-MemCheck|Itanium.ActiveCfg = Debug-MemCheck|x64 
     734    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Debug-MemCheck|Mixed Platforms.ActiveCfg = Debug-MemCheck|x64 
     735    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Debug-MemCheck|Mixed Platforms.Build.0 = Debug-MemCheck|x64 
     736    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Debug-MemCheck|Win32.ActiveCfg = Debug-MemCheck|Win32 
     737    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Debug-MemCheck|Win32.Build.0 = Debug-MemCheck|Win32 
     738    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Debug-MemCheck|x64.ActiveCfg = Debug-MemCheck|x64 
     739    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Debug-MemCheck|x64.Build.0 = Debug-MemCheck|x64 
     740    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Distribution|Any CPU.ActiveCfg = Distribution|x64 
     741    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Distribution|Itanium.ActiveCfg = Distribution|x64 
     742    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Distribution|Mixed Platforms.ActiveCfg = Distribution|x64 
     743    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Distribution|Mixed Platforms.Build.0 = Distribution|x64 
     744    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Distribution|Win32.ActiveCfg = Distribution|Win32 
     745    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Distribution|Win32.Build.0 = Distribution|Win32 
     746    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Distribution|x64.ActiveCfg = Distribution|x64 
     747    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Distribution|x64.Build.0 = Distribution|x64 
     748    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Nightly|Any CPU.ActiveCfg = Nightly|x64 
     749    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Nightly|Itanium.ActiveCfg = Nightly|x64 
     750    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Nightly|Mixed Platforms.ActiveCfg = Nightly|x64 
     751    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Nightly|Mixed Platforms.Build.0 = Nightly|x64 
     752    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Nightly|Win32.ActiveCfg = Nightly|Win32 
     753    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Nightly|Win32.Build.0 = Nightly|Win32 
     754    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Nightly|x64.ActiveCfg = Nightly|x64 
     755    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Nightly|x64.Build.0 = Nightly|x64 
     756    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Release|Any CPU.ActiveCfg = Release|x64 
     757    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Release|Itanium.ActiveCfg = Release|x64 
     758    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Release|Mixed Platforms.ActiveCfg = Release|x64 
     759    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Release|Mixed Platforms.Build.0 = Release|x64 
     760    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Release|Win32.ActiveCfg = Release|Win32 
     761    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Release|Win32.Build.0 = Release|Win32 
     762    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Release|x64.ActiveCfg = Release|x64 
     763    {4241C6CF-EC01-4AD9-89B0-B75EBA8A5996}.Release|x64.Build.0 = Release|x64 
    722764  EndGlobalSection 
    723765  GlobalSection(SolutionProperties) = preSolution 
  • NSClient++.cpp

    r7a156f4 r394f7a1  
    4040int wmain(int argc, TCHAR* argv[], TCHAR* envp[]) 
    4141{ 
     42  srand( (unsigned)time( NULL ) ); 
    4243  int nRetCode = 0; 
    4344  if ( (argc > 1) && ((*argv[1] == '-') || (*argv[1] == '/')) ) { 
     
    133134      std::wstring s = _T(""); 
    134135      std::wstring buff = _T(""); 
    135       std::wcin >> s; 
    136       while (s != _T("exit")) { 
    137         if (std::cin.peek() < 15) { 
     136      while (true) { 
     137        std::wcin >> s; 
     138        if (s == _T("exit")) { 
     139          std::wcout << _T("Exiting...") << std::endl; 
     140          break; 
     141        } else if (s == _T("off") && buff == _T("debug ")) { 
     142          std::wcout << _T("Setting debug log off...") << std::endl; 
     143          mainClient.enableDebug(false); 
     144        } else if (s == _T("on") && buff == _T("debug ")) { 
     145          std::wcout << _T("Setting debug log on...") << std::endl; 
     146          mainClient.enableDebug(true); 
     147        } else if (std::cin.peek() < 15) { 
    138148          buff += s; 
    139149          strEx::token t = strEx::getToken(buff, ' '); 
     
    148158          buff += s + _T(" "); 
    149159        } 
    150         std::wcin >> s; 
    151160      } 
    152161      mainClient.TerminateService(); 
     
    533542 
    534543bool NSClientT::logDebug() { 
    535   typedef enum status {unknown, debug, nodebug }; 
    536   static status d = unknown; 
    537   if (d == unknown) { 
     544  if (debug_ == log_unknown) { 
    538545    try { 
    539546      if (Settings::getInstance()->getInt(_T("log"), _T("debug"), 0) == 1) 
    540         d = debug; 
     547        debug_ = log_debug; 
    541548      else 
    542         d = nodebug; 
     549        debug_ = log_nodebug; 
    543550    } catch (SettingsException e) { 
    544       d = debug; 
    545     } 
    546   } 
    547   return (d == debug); 
     551      debug_ = log_debug; 
     552    } 
     553  } 
     554  return (debug_ == log_debug); 
    548555} 
    549556 
  • NSClient++.h

    r7a156f4 r394f7a1  
    7070  MutexRW  m_mutexRWcmdDescriptions; 
    7171  cmdMap cmdDescriptions_; 
    72   bool debug_; 
     72  typedef enum log_status {log_unknown, log_debug, log_nodebug }; 
     73  log_status debug_; 
    7374 
    7475public: 
    7576  // c-tor, d-tor 
    76   NSClientT(void) : debug_(false) {} 
     77  NSClientT(void) : debug_(log_unknown) {} 
    7778  virtual ~NSClientT(void) {} 
    7879  void enableDebug(bool debug = true) { 
    79     debug_ = debug; 
     80    if (debug) 
     81      debug_ = log_debug; 
     82    else 
     83      debug_ = log_nodebug; 
    8084  } 
    8185 
  • changelog

    rc1fe385 r394f7a1  
    55 * Add module for relaying events 
    66 * Add API for rehashing the daemon (or implement it the API is there but does nothing) 
     7 
     82008-02-18 MickeM 
     9 + Added propper output handling to process subsystem (now you can execute programs tat return "much" data. 
     10 + Added select support for SSL_write (now you can send "any amount of data" to the (SSL) socket. 
     11   Since check_nrpe doesn't do this it wont work in that end, but still... 
     12 
     132008-02-16 MickeM 
     14 + Refactored ExternalCommand handling so NRPE and new module does the same thing. 
    715 
    8162008-02-14 MickeM 
  • include/Mutex.h

    r1a35b3f r394f7a1  
    174174  } 
    175175}; 
     176 
     177class ManualMutexLock { 
     178private: 
     179  bool bHasMutex;   // Status: true if we have a mutex lock 
     180  HANDLE hMutex_;   // Handle to the mutex object. 
     181  DWORD dwWaitResult; // Result from the mutex operations 
     182public: 
     183  /** 
     184  * Default c-tor. 
     185  * Waits for the mutex object. 
     186  * @param hMutex The mutex to use 
     187  * @timeout The timeout before abandoning wait 
     188  */ 
     189  ManualMutexLock(HANDLE hMutex) : bHasMutex(false), hMutex_(hMutex) {} 
     190   
     191  /** 
     192  * Waits for the mutex object. 
     193  * @timeout The timeout before abandoning wait 
     194  */ 
     195  void lock(DWORD timeout = 5000L) { 
     196    if (hMutex_ == NULL) { 
     197      std::wcout << _T("Error in mutex lock: ") << std::endl; 
     198      bHasMutex = false; 
     199      return; 
     200    } 
     201    dwWaitResult = WaitForSingleObject(hMutex_, timeout); 
     202    switch (dwWaitResult) { 
     203      // The thread got mutex ownership. 
     204    case WAIT_OBJECT_0: 
     205      bHasMutex = true; 
     206      break; 
     207    case WAIT_TIMEOUT:  
     208      bHasMutex = false; 
     209      break; 
     210    case WAIT_ABANDONED:  
     211      bHasMutex = false; 
     212      break; 
     213    } 
     214  } 
     215  /** 
     216  * An attempt to simplify the has mutex thingy (don't know if it works, haven't tried it since I wrote this class a few years ago :) 
     217  * @return true if we have a mutex lock. 
     218  */ 
     219  operator bool () const { 
     220    return bHasMutex; 
     221  } 
     222  /** 
     223  * Check if we actually got the mutex (might have timed out) 
     224  * @return  
     225  */ 
     226  bool hasMutex() const { 
     227    return bHasMutex; 
     228  } 
     229  /** 
     230  * Default d-tor. 
     231  * Release the mutex 
     232  */ 
     233  virtual ~ManualMutexLock() { 
     234    if (bHasMutex) 
     235      ReleaseMutex(hMutex_); 
     236    bHasMutex = false; 
     237  } 
     238  /** 
     239  * Release the mutex 
     240  */ 
     241  void release() { 
     242    if (bHasMutex) 
     243      ReleaseMutex(hMutex_); 
     244    bHasMutex = false; 
     245  } 
     246  /** 
     247  * Get the result of the wait operation. 
     248  * @return Result of the wait operation 
     249  */ 
     250  DWORD getWaitResult() const { 
     251    return dwWaitResult; 
     252  } 
     253}; 
  • include/PDHCollectors.h

    rdc65e35 r394f7a1  
    2222 
    2323#include <PDHCounter.h> 
    24  
     24#include <Mutex.h> 
    2525namespace PDHCollectors { 
    2626  const int format_large = 0x00000400; 
     
    2828  const int format_double = 0x00000200; 
    2929 
    30   template <class TType = __int64, int TCollectionFormat = format_large> 
     30  class PDHException { 
     31    std::wstring error_; 
     32  public: 
     33    PDHException(std::wstring error) : error_(error) {} 
     34    std::wstring getError() const { return error_; } 
     35 
     36  }; 
     37  class TPDHCounterMutex { 
     38  public: 
     39    virtual void lock() = 0; 
     40    virtual bool hasLock(bool silent = true) = 0; 
     41    virtual void release() = 0; 
     42  }; 
     43 
     44  class PDHCounterNoMutex : public TPDHCounterMutex { 
     45  public: 
     46    void lock() {} 
     47    bool hasLock(bool silent = true) { 
     48      return true; 
     49    } 
     50    void release() {} 
     51  }; 
     52  class PDHCounterNormalMutex : public TPDHCounterMutex { 
     53    MutexHandler mutex_; 
     54    ManualMutexLock lock_; 
     55  public: 
     56    PDHCounterNormalMutex() : lock_(mutex_) {} 
     57    void lock() { 
     58      lock_.lock(); 
     59    } 
     60    bool hasLock(bool silent = true) { 
     61      if (!silent && !lock_.hasMutex()) { 
     62        std::wcout << _T("We never got the mutex... sorry...") << std::endl; 
     63      } 
     64      return lock_.hasMutex(); 
     65    } 
     66    void release() { 
     67      lock_.release(); 
     68    } 
     69  }; 
     70  class PDHCounterMutexHandler { 
     71  private: 
     72    TPDHCounterMutex *mutex_; // Handle to the mutex object. 
     73  public: 
     74    /** 
     75    * Default c-tor. 
     76    * Waits for the mutex object. 
     77    * @param mutex The mutex to use 
     78    * @timeout The timeout before abandoning wait 
     79    */ 
     80    PDHCounterMutexHandler(TPDHCounterMutex *mutex) : mutex_(mutex) { 
     81      if (mutex == NULL) { 
     82        std::wcout << _T("Error in mutex lock: ") << std::endl; 
     83        mutex = NULL; 
     84        return; 
     85      } 
     86      mutex->lock(); 
     87    } 
     88    /** 
     89    * An attempt to simplify the has mutex thingy (don't know if it works, haven't tried it since I wrote this class a few years ago :) 
     90    * @return true if we have a mutex lock. 
     91    */ 
     92    operator bool () const { 
     93      return mutex_!=NULL&&mutex_->hasLock(); 
     94    } 
     95    /** 
     96    * Check if we actually got the mutex (might have timed out) 
     97    * @return  
     98    */ 
     99    bool hasLock(bool silent = true) const { 
     100      return mutex_!=NULL&&mutex_->hasLock(silent); 
     101    } 
     102    /** 
     103    * Default d-tor. 
     104    * Release the mutex 
     105    */ 
     106    virtual ~PDHCounterMutexHandler() { 
     107      mutex_->release(); 
     108    } 
     109  }; 
     110 
     111  template <class TType, int TCollectionFormat, class TMutextHandler = PDHCounterNoMutex> 
    31112  class StaticPDHCounterListener {}; 
    32113 
    33   template <class TType> 
    34   class StaticPDHCounterListener<TType, format_double> : public PDH::PDHCounterListener { 
     114  template <class TType, class TMutextHandler> 
     115  class StaticPDHCounterListener<TType, format_double, TMutextHandler> : public PDH::PDHCounterListener { 
    35116    TType value_; 
     117    TMutextHandler mutex_; 
    36118  public: 
    37119    StaticPDHCounterListener() : value_(0) {} 
    38120    virtual void collect(const PDH::PDHCounter &counter) { 
     121      PDHCounterMutexHandler mutex(&mutex_); 
     122      if (!mutex.hasLock()) 
     123        return; 
    39124      value_ = counter.getDoubleValue(); 
    40125    } 
    41126    void attach(const PDH::PDHCounter &counter){} 
    42127    void detach(const PDH::PDHCounter &counter){} 
    43     TType getValue() const { 
     128    TType getValue() { 
     129      PDHCounterMutexHandler mutex(&mutex_); 
     130      if (!mutex.hasLock()) 
     131        return -1; 
    44132      return value_; 
    45133    } 
     
    49137  }; 
    50138 
    51   template <class TType> 
    52   class StaticPDHCounterListener<TType, format_long> : public PDH::PDHCounterListener { 
     139  template <class TType, class TMutextHandler> 
     140  class StaticPDHCounterListener<TType, format_long, TMutextHandler> : public PDH::PDHCounterListener { 
    53141    TType value_; 
     142    TMutextHandler mutex_; 
    54143  public: 
    55144    StaticPDHCounterListener() : value_(0) {} 
    56145    virtual void collect(const PDH::PDHCounter &counter) { 
     146      PDHCounterMutexHandler mutex(&mutex_); 
     147      if (!mutex.hasLock()) 
     148        return; 
    57149      value_ = counter.getIntValue(); 
    58150    } 
    59151    void attach(const PDH::PDHCounter &counter){} 
    60152    void detach(const PDH::PDHCounter &counter){} 
    61     TType getValue() const { 
     153    TType getValue() { 
     154      PDHCounterMutexHandler mutex(&mutex_); 
     155      if (!mutex.hasLock()) 
     156        return -1; 
    62157      return value_; 
    63158    } 
     
    67162  }; 
    68163 
    69   template <class TType> 
    70   class StaticPDHCounterListener<TType, format_large> : public PDH::PDHCounterListener { 
     164  template <class TType, class TMutextHandler> 
     165  class StaticPDHCounterListener<TType, format_large, TMutextHandler> : public PDH::PDHCounterListener { 
     166    TMutextHandler mutex_; 
    71167    TType value_; 
    72168  public: 
    73169    StaticPDHCounterListener() : value_(0) {} 
    74170    virtual void collect(const PDH::PDHCounter &counter) { 
     171      PDHCounterMutexHandler mutex(&mutex_); 
     172      if (!mutex.hasLock()) 
     173        return; 
    75174      value_ = counter.getInt64Value(); 
    76175    } 
    77176    void attach(const PDH::PDHCounter &counter){} 
    78177    void detach(const PDH::PDHCounter &counter){} 
    79     TType getValue() const { 
     178    TType getValue() { 
     179      PDHCounterMutexHandler mutex(&mutex_); 
     180      if (!mutex.hasLock()) 
     181        return -1; 
    80182      return value_; 
    81183    } 
     
    86188 
    87189 
    88   template <class TType = __int64> 
     190  template <class TType, class TMutextHandler> 
    89191  class RoundINTPDHBufferListenerImpl : public PDH::PDHCounterListener { 
     192    TMutextHandler mutex_; 
    90193    unsigned int length; 
    91194    TType *buffer; 
     
    94197    RoundINTPDHBufferListenerImpl() : buffer(NULL), length(0), current(0) {} 
    95198    RoundINTPDHBufferListenerImpl(int length_) : length(length_), current(0) { 
     199      PDHCounterMutexHandler mutex(mutex_); 
     200      if (!mutex.hasLock()) 
     201        return; 
    96202      buffer = new int[length]; 
    97203      for (unsigned int i=0; i<length;i++) 
     
    99205    } 
    100206    virtual ~RoundINTPDHBufferListenerImpl() { 
     207      PDHCounterMutexHandler mutex(&mutex_); 
     208      if (!mutex.hasLock()) 
     209        return; 
    101210      delete [] buffer; 
    102211    } 
     
    110219    */ 
    111220    void resize(int newLength) { 
     221      PDHCounterMutexHandler mutex(&mutex_); 
     222      if (!mutex.hasLock()) 
     223        return; 
    112224      delete [] buffer; 
    113225 
     
    126238    void detach(const PDH::PDHCounter &counter){} 
    127239    void pushValue(TType value) { 
     240      PDHCounterMutexHandler mutex(&mutex_); 
     241      if (!mutex.hasLock()) 
     242        return; 
    128243      if (buffer == NULL) 
    129244        return; 
     
    134249        current = 0; 
    135250    } 
    136     double getAvrage(unsigned int backItems) const { 
     251    double getAvrage(unsigned int backItems) { 
     252      PDHCounterMutexHandler mutex(&mutex_); 
     253      if (!mutex.hasLock(true)) 
     254        throw PDHException(_T("Failed to get mutex :(")); 
    137255      if ((backItems == 0) || (backItems >= length)) 
    138256        return -1; 
     
    157275 
    158276 
    159   template <class TType = __int64, DWORD TCollectionFormat = format_large> 
    160   class RoundINTPDHBufferListener : public RoundINTPDHBufferListenerImpl<TType> { 
    161   }; 
    162  
    163   template <class TType> 
    164   class RoundINTPDHBufferListener<TType, format_double> : public RoundINTPDHBufferListenerImpl<TType> { 
     277  template <class TType, DWORD TCollectionFormat, class TMutextHandler = PDHCounterNoMutex> 
     278  class RoundINTPDHBufferListener : public RoundINTPDHBufferListenerImpl<TType, TMutextHandler> { 
     279  }; 
     280 
     281  template <class TType, class TMutextHandler> 
     282  class RoundINTPDHBufferListener<TType, format_double, TMutextHandler> : public RoundINTPDHBufferListenerImpl<TType, TMutextHandler> { 
    165283  public: 
    166284    RoundINTPDHBufferListener() {} 
     
    175293  }; 
    176294 
    177   template <class TType> 
    178   class RoundINTPDHBufferListener<TType, format_long> : public RoundINTPDHBufferListenerImpl<TType> { 
     295  template <class TType, class TMutextHandler> 
     296  class RoundINTPDHBufferListener<TType, format_long, TMutextHandler> : public RoundINTPDHBufferListenerImpl<TType, TMutextHandler> { 
    179297  public: 
    180298    RoundINTPDHBufferListener() {} 
     
    189307  }; 
    190308 
    191   template <class TType> 
    192   class RoundINTPDHBufferListener<TType, format_large> : public RoundINTPDHBufferListenerImpl<TType> { 
     309  template <class TType, class TMutextHandler> 
     310  class RoundINTPDHBufferListener<TType, format_large, TMutextHandler> : public RoundINTPDHBufferListenerImpl<TType, TMutextHandler> { 
    193311  public: 
    194312    RoundINTPDHBufferListener() {} 
  • include/SSLSocket.cpp

    r47b843a r394f7a1  
    6868 
    6969 
    70 bool simpleSSL::sSSL::readAll(simpleSocket::Socket *report_to, simpleSocket::DataBuffer &buffer, unsigned int tmpBufferLength /* = 1024*/, int maxLength /*= -1*/) { 
     70bool simpleSSL::sSSL::readAll(simpleSocket::DataBuffer &buffer, unsigned int tmpBufferLength /* = 1024*/, int maxLength /*= -1*/) { 
    7171  char *tmpBuffer = new char[tmpBufferLength+1]; 
    7272  if (!ssl_) 
     
    8181    if ((rc == SSL_ERROR_WANT_READ) || (rc == SSL_ERROR_WANT_WRITE)) 
    8282      return true; 
    83     report_to->printError(_T(__FILE__), __LINE__, _T("Could not read from socket: ") + strEx::itos(rc)); 
    8483    throw simpleSocket::SocketException(_T("Could not read from socket: ") + strEx::itos(rc)); 
    8584  } 
    8685  return n>0; 
    8786} 
    88 void simpleSSL::sSSL::send(const char * buf, unsigned int len) { 
     87 
     88bool simpleSSL::sSSL::sendAll(const char * buffer, unsigned int len) { 
    8989  if (!ssl_) 
    9090    create(); 
    91   int rc = SSL_write(ssl_, buf, len); 
    92   if (rc <= 0) 
    93     throw SSLException(_T("Socket write failed: "), rc, getError(rc)); 
     91  int n = SSL_write(ssl_, buffer, len); 
     92  if (n <= 0) { 
     93    int rc = getError(n); 
     94    if ((rc == SSL_ERROR_WANT_READ) || (rc == SSL_ERROR_WANT_WRITE)) 
     95      return true; 
     96    throw simpleSocket::SocketException(_T("Could not write to socket: ") + strEx::itos(rc)); 
     97  } 
     98  return false; 
    9499} 
    95100 
  • include/SSLSocket.h

    r47b843a r394f7a1  
    230230      context_ = context; 
    231231    } 
    232     bool readAll (simpleSocket::Socket *report_to, simpleSocket::DataBuffer &buffer, unsigned int tmpBufferLength = 1024, int maxLength = -1); 
     232    bool readAll (simpleSocket::DataBuffer &buffer, unsigned int tmpBufferLength = 1024, int maxLength = -1); 
     233    bool sendAll(const char * buffer, unsigned int len); 
    233234    void send(const char * buf, unsigned int len); 
    234235  }; 
     
    262263    virtual bool readAll (simpleSocket::DataBuffer &buffer, unsigned int tmpBufferLength = 1024, int maxLength = -1) { 
    263264      try { 
    264         return ssl.readAll(this, buffer, tmpBufferLength, maxLength); 
    265       } catch (simpleSSL::SSLException e) { 
    266         throw simpleSocket::SocketException(e.getMessage()); 
    267       } 
    268     } 
     265        return ssl.readAll(buffer, tmpBufferLength, maxLength); 
     266      } catch (simpleSSL::SSLException e) { 
     267        throw simpleSocket::SocketException(e.getMessage()); 
     268      } catch (...) { 
     269        throw simpleSocket::SocketException(_T("Unhandeled socket exception")); 
     270      } 
     271    } 
     272    virtual bool sendAll(const char * buffer, unsigned int len) { 
     273      try { 
     274        return ssl.sendAll(buffer, len); 
     275      } catch (simpleSSL::SSLException e) { 
     276        throw simpleSocket::SocketException(e.getMessage()); 
     277      } catch (...) { 
     278        throw simpleSocket::SocketException(_T("Unhandeled socket exception")); 
     279      } 
     280    } 
     281    /* 
     282 
    269283    virtual int send(const char * buf, unsigned int len, int flags = 0) { 
    270284      try { 
     
    275289      return 0; 
    276290    } 
     291    */ 
    277292    virtual void close() { 
    278293      ssl.shutdown(); 
  • include/Socket.cpp

    r035c51f r394f7a1  
    5050        break; 
    5151      n=recv(socket_,tmpBuffer,tmpBufferLength,0); 
    52       std::wcout << _T("read (2): ") << n << std::endl; 
    5352    } else { 
    5453      // Buffer not full, we got it "all" 
     
    6867} 
    6968 
     69bool simpleSocket::Socket::sendAll(const char * buffer, unsigned int len) { 
     70  int n = send(buffer, len, 0); 
     71  if (n == SOCKET_ERROR) { 
     72    int ret = ::WSAGetLastError(); 
     73    if (ret == WSAEWOULDBLOCK) 
     74      return false; 
     75    throw SocketException(_T("recv returned SOCKET_ERROR: "), ret); 
     76  } 
     77  return false; 
     78} 
    7079 
    7180/** 
  • include/Socket.h

    rce57b1b r394f7a1  
    143143      buffer_[length_] = 0; 
    144144    } 
    145     std::wstring toString() { 
    146       std::wstringstream ss; 
    147       for (unsigned int i =0;i<length_;i++) { 
    148         if (i%64==0) { 
    149           ss << std::endl; 
    150         } 
    151         if (buffer_[i] < 30 || buffer_[i] > 'z') 
    152           ss << _T(" "); 
    153         else 
    154           ss << buffer_[i]; 
    155       } 
    156       return ss.str(); 
     145    std::string toString() { 
     146      return strEx::format_buffer(buffer_, length_); 
    157147    } 
    158148  }; 
     
    163153    sockaddr_in from_; 
    164154    sockaddr_in to_; 
     155    fd_set read_, write_, excp_; 
    165156 
    166157  public: 
    167158    Socket() : socket_(NULL) { 
     159      FD_ZERO(&read_); 
     160      FD_ZERO(&write_); 
     161      FD_ZERO(&excp_); 
    168162    } 
    169163    Socket(SOCKET socket) : socket_(socket) { 
     164      FD_ZERO(&read_); 
     165      FD_ZERO(&write_); 
     166      FD_ZERO(&excp_); 
    170167    } 
    171168    Socket(int type, int protocol) : socket_(NULL) { 
    172169      socket_ = ::socket(AF_INET, type, protocol); 
     170      FD_ZERO(&read_); 
     171      FD_ZERO(&write_); 
     172      FD_ZERO(&excp_); 
    173173    } 
    174174    Socket(bool create) : socket_(NULL) { 
    175175      if (create) 
    176176        socket_ = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
     177      FD_ZERO(&read_); 
     178      FD_ZERO(&write_); 
     179      FD_ZERO(&excp_); 
    177180    } 
    178181 
     
    181184      from_ = other.from_; 
    182185      other.socket_ = NULL; 
     186      read_ = other.read_; 
     187      write_ = other.write_; 
     188      excp_ = other.excp_; 
    183189    } 
    184190    virtual ~Socket() { 
     
    217223    virtual void setNonBlock() { 
    218224      unsigned long NoBlock = 1; 
     225      FD_SET(socket_, &read_); 
     226      FD_SET(socket_, &write_); 
     227      FD_SET(socket_, &excp_); 
    219228      this->ioctlsocket(FIONBIO, &NoBlock); 
    220229    } 
     230    virtual bool canRead(long timeout = -1) { 
     231      timeval timeout_; 
     232      timeout_.tv_sec = timeout; 
     233      FD_SET(socket_, &read_); 
     234      FD_SET(socket_, &write_); 
     235      FD_SET(socket_, &excp_); 
     236      if (timeout == -1) 
     237        ::select(NULL, &read_, &write_, &excp_, NULL); 
     238      else 
     239        ::select(NULL, &read_, &write_, &excp_, &timeout_); 
     240      if (FD_ISSET(socket_, &read_)) 
     241        return true; 
     242      return false; 
     243    } 
     244    virtual bool canWrite(long timeout = -1) { 
     245      timeval timeout_; 
     246      timeout_.tv_sec = timeout; 
     247      FD_SET(socket_, &read_); 
     248      FD_SET(socket_, &write_); 
     249      FD_SET(socket_, &excp_); 
     250      if (timeout == -1) 
     251        ::select(NULL, &read_, &write_, &excp_, NULL); 
     252      else 
     253        ::select(NULL, &read_, &write_, &excp_, &timeout_); 
     254      return FD_ISSET(socket_, &write_); 
     255    } 
     256 
    221257    static unsigned long inet_addr(std::wstring addr) { 
    222258      return ::inet_addr(strEx::wstring_to_string(addr).c_str()); 
     
    268304    } 
    269305    virtual bool readAll(DataBuffer &buffer, unsigned int tmpBufferLength = 1024, int maxLength = -1); 
     306    virtual bool sendAll(const char * buffer, unsigned int len); 
     307 
     308    int inline sendAll(DataBuffer &buffer) { 
     309      return sendAll(buffer.getBuffer(), buffer.getLength()); 
     310    } 
     311 
     312    virtual int send(const char * buf, unsigned int len, int flags = 0) { 
     313      assert(socket_); 
     314      return ::send(socket_, buf, len, flags); 
     315    } 
     316    int inline send(DataBuffer &buffer, int flags = 0) { 
     317      return send(buffer.getBuffer(), buffer.getLength(), flags); 
     318    } 
    270319 
    271320    virtual void socket(int af, int type, int protocol ) { 
     
    300349      from_.sin_addr.s_addr=addr; 
    301350      from_.sin_port=port; 
    302     } 
    303     virtual int send(const char * buf, unsigned int len, int flags = 0) { 
    304       assert(socket_); 
    305       return ::send(socket_, buf, len, flags); 
    306     } 
    307     int inline send(DataBuffer &buffer, int flags = 0) { 
    308       return send(buffer.getBuffer(), buffer.getLength(), flags); 
    309351    } 
    310352    virtual void ioctlsocket(long cmd, u_long *argp) { 
  • include/config.h

    r65ec1fa r394f7a1  
    163163#define NSCA_PASSWORD _T("password") 
    164164#define NSCA_PASSWORD_DEFAULT _T("") 
     165#define NSCA_DEBUG_THREADS _T("debug_threads") 
     166#define NSCA_DEBUG_THREADS_DEFAULT 1 
    165167 
    166168#define C_SYSTEM_SVC_ALL_0 _T("check_all_services[SERVICE_BOOT_START]") 
  • include/strEx.h

    rde8ef76 r394f7a1  
    8989  } 
    9090 
     91  inline std::wstring format_buffer(const wchar_t* buf, unsigned int len) { 
     92    std::wstringstream ss; 
     93    std::wstring chars; 
     94    for (unsigned int i=0;i<len;i++) { 
     95      ss << std::hex << buf[i]; 
     96      ss << _T(", "); 
     97      if (buf[i] >= ' ' && buf[i] <= 'z') 
     98        chars += buf[i]; 
     99      else 
     100        chars += '?'; 
     101      if (i%32==0) { 
     102        ss << chars; 
     103        ss << _T("\n"); 
     104        chars = _T(""); 
     105      } 
     106    } 
     107    return ss.str(); 
     108  } 
     109  inline std::string format_buffer(const char* buf, unsigned int len) { 
     110    std::stringstream ss; 
     111    std::string chars; 
     112    for (unsigned int i=0;i<len;i++) { 
     113      if (i%32==0) { 
     114        if (i > 0) { 
     115          ss << chars; 
     116          ss << "\n"; 
     117        } 
     118        chars = ""; 
     119        ss << std::hex << std::setw(8) << std::setfill('0') << i; 
     120        ss << ": "; 
     121      } 
     122      ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(static_cast<unsigned char>(buf[i])); 
     123      ss << ", "; 
     124      if (buf[i] < 30 || buf[i] == 127) 
     125        chars += '?'; 
     126      else 
     127        chars += buf[i]; 
     128    } 
     129    return ss.str(); 
     130  } 
     131 
    91132  inline std::wstring format_date(time_t time, std::wstring format = _T("%Y-%m-%d %H:%M:%S")) { 
    92133    TCHAR buf[51]; 
  • modules/CheckExternalScripts/CheckExternalScripts.cpp

    rce57b1b r394f7a1  
    9191    addAllScriptsFrom(scriptDirectory_); 
    9292  } 
     93  root_ = NSCModuleHelper::getBasePath(); 
    9394  return true; 
    9495} 
     
    136137    return NSCModuleHelper::InjectSplitAndCommand(cd.command, cd.arguments, ' ', message, perf, true); 
    137138  } else { 
    138     return executeNRPECommand(cd.command + _T(" ") + args, message, perf); 
     139    int result = process::executeProcess(root_, cd.command + _T(" ") + args, message, perf, timeout); 
     140    if (!NSCHelper::isNagiosReturnCode(result)) { 
     141      NSC_LOG_ERROR_STD(_T("The command (") + cd.command + _T(") returned an invalid return code: ") + strEx::itos(result)); 
     142      return NSCAPI::returnUNKNOWN; 
     143    } 
     144    return NSCHelper::int2nagios(result); 
    139145    /* 
    140146  } else if (cd.type == script_dir) { 
     
    149155 
    150156} 
    151 #define MAX_INPUT_BUFFER 1024 
    152  
    153 int CheckExternalScripts::executeNRPECommand(std::wstring command, std::wstring &msg, std::wstring &perf) 
    154 { 
    155   NSCAPI::nagiosReturn result; 
    156   PROCESS_INFORMATION pi; 
    157   STARTUPINFO si; 
    158   HANDLE hChildOutR, hChildOutW, hChildInR, hChildInW; 
    159   SECURITY_ATTRIBUTES sec; 
    160   DWORD dwstate, dwexitcode; 
    161   int retval; 
    162  
    163  
    164   // Set up members of SECURITY_ATTRIBUTES structure.  
    165  
    166   sec.nLength = sizeof(SECURITY_ATTRIBUTES); 
    167   sec.bInheritHandle = TRUE; 
    168   sec.lpSecurityDescriptor = NULL; 
    169  
    170   // CreateProcess doesn't work with a const command 
    171   TCHAR *cmd = new TCHAR[command.length()+1]; 
    172   if (cmd == NULL) { 
    173     NSC_LOG_ERROR(_T("Failed to allocate memory for command buffer (") + command + _T(").")); 
    174     return NSCAPI::returnUNKNOWN; 
    175   } 
    176   wcsncpy_s(cmd, command.length()+1, command.c_str(), command.length()); 
    177   cmd[command.length()] = 0; 
    178   std::wstring root = NSCModuleHelper::getBasePath(); 
    179  
    180   // Create Pipes 
    181   if (!CreatePipe(&hChildInR, &hChildInW, &sec, 0)) { 
    182     NSC_LOG_ERROR(_T("Failed to create pipe for (") + command + _T(") return code: ") + error::lookup::last_error()); 
    183     return NSCAPI::returnUNKNOWN; 
    184   } 
    185   if (!CreatePipe(&hChildOutR, &hChildOutW, &sec, 0)) { 
    186     NSC_LOG_ERROR(_T("Failed to create pipe for (") + command + _T(") return code: ") + error::lookup::last_error()); 
    187     return NSCAPI::returnUNKNOWN; 
    188   } 
    189  
    190   // Set up members of STARTUPINFO structure.  
    191  
    192   ZeroMemory(&si, sizeof(STARTUPINFO)); 
    193   si.cb = sizeof(STARTUPINFO); 
    194   si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW; 
    195   si.hStdInput = hChildInR; 
    196   si.hStdOutput = hChildOutW; 
    197   si.hStdError = hChildOutW; 
    198   si.wShowWindow = SW_HIDE; 
    199  
    200   // Create the child process.  
    201   BOOL processOK = CreateProcess(NULL, cmd,        // command line  
    202     NULL, // process security attributes  
    203     NULL, // primary thread security attributes  
    204     TRUE, // handles are inherited  
    205     0,    // creation flags  
    206     NULL, // use parent's environment  
    207     root.c_str(), // use parent's current directory  
    208     &si,  // STARTUPINFO pointer  
    209     &pi); // receives PROCESS_INFORMATION  
    210   delete [] cmd; 
    211  
    212   if (processOK) { 
    213     dwstate = WaitForSingleObject(pi.hProcess, 1000*timeout); 
    214     CloseHandle(hChildInR); 
    215     CloseHandle(hChildInW); 
    216     CloseHandle(hChildOutW); 
    217  
    218     if (dwstate == WAIT_TIMEOUT) { 
    219       TerminateProcess(pi.hProcess, 5); 
    220       msg = _T("The check (") + command + _T(") didn't respond within the timeout period (") + strEx::itos(timeout) + _T("s)!"); 
    221       result = NSCAPI::returnUNKNOWN; 
    222     } else { 
    223       DWORD dwread; 
    224       //TCHAR *buf = new TCHAR[MAX_INPUT_BUFFER+1]; 
    225       char *buf = new char[MAX_INPUT_BUFFER+1]; 
    226       //retval = ReadFile(hChildOutR, buf, MAX_INPUT_BUFFER*sizeof(WCHAR), &dwread, NULL); 
    227       retval = ReadFile(hChildOutR, buf, MAX_INPUT_BUFFER*sizeof(char), &dwread, NULL); 
    228       if (!retval || dwread == 0) { 
    229         msg = _T("No output available from command..."); 
    230       } else { 
    231         buf[dwread] = 0; 
    232         msg = strEx::string_to_wstring(buf); 
    233         //msg = buf; 
    234         //strEx::token t = strEx::getToken(msg, '\n'); 
    235         strEx::token t = strEx::getToken(msg, '|'); 
    236         msg = t.first; 
    237         std::wstring::size_type pos = msg.find_last_not_of(_T("\n\r ")); 
    238         if (pos != std::wstring::npos) { 
    239           if (pos == msg.size()) 
    240             msg = msg.substr(0,pos); 
    241           else 
    242             msg = msg.substr(0,pos+1); 
    243         } 
    244         //if (msg[msg.size()-1] == '\n') 
    245         perf = t.second; 
    246       } 
    247       delete [] buf; 
    248       if (GetExitCodeProcess(pi.hProcess, &dwexitcode) == 0) { 
    249         NSC_LOG_ERROR(_T("Failed to get commands (") + command + _T(") return code: ") + error::lookup::last_error()); 
    250         dwexitcode = NSCAPI::returnUNKNOWN; 
    251       } 
    252       if (!NSCHelper::isNagiosReturnCode(dwexitcode)) { 
    253         NSC_LOG_ERROR(_T("The command (") + command + _T(") returned an invalid return code: ") + strEx::itos(dwexitcode)); 
    254         dwexitcode = NSCAPI::returnUNKNOWN; 
    255       } 
    256       result = NSCHelper::int2nagios(dwexitcode); 
    257     } 
    258     CloseHandle(pi.hThread); 
    259     CloseHandle(pi.hProcess); 
    260     CloseHandle(hChildOutR); 
    261   } 
    262   else { 
    263     msg = _T("NSCP failed to create process (") + command + _T("): ") + error::lookup::last_error(); 
    264     result = NSCAPI::returnUNKNOWN; 
    265     CloseHandle(hChildInR); 
    266     CloseHandle(hChildInW); 
    267     CloseHandle(hChildOutW); 
    268     CloseHandle(pi.hThread); 
    269     CloseHandle(pi.hProcess); 
    270     CloseHandle(hChildOutR); 
    271   } 
    272   return result; 
    273 } 
    274157 
    275158 
  • modules/CheckExternalScripts/CheckExternalScripts.h

    rc1d545e r394f7a1  
    2323#include <map> 
    2424#include <error.hpp> 
     25#include <execute_process.hpp> 
    2526 
    2627class CheckExternalScripts { 
     
    3738  unsigned int timeout; 
    3839  std::wstring scriptDirectory_; 
     40  std::wstring root_; 
    3941 
    4042public: 
     
    7678 
    7779private: 
    78   int executeNRPECommand(std::wstring command, std::wstring &msg, std::wstring &perf); 
    7980  void addAllScriptsFrom(std::wstring path); 
    8081  void addCommand(strEx::blindstr key, std::wstring cmd = _T(""), std::wstring args = _T("")) { 
  • modules/CheckSystem/CheckSystem.cpp

    rde8ef76 r394f7a1  
    402402    int value = pObject->getCPUAvrage(load.data + _T("m")); 
    403403    if (value == -1) { 
    404       msg = _T("ERROR: We don't collect data this far back: ") + load.getAlias(); 
     404      msg = _T("ERROR: Could not get data for ") + load.getAlias() + _T(" perhaps we don't collect data this far back?"); 
    405405      return NSCAPI::returnUNKNOWN; 
    406406    } 
     
    720720    } 
    721721  } 
    722   NSC_DEBUG_MSG_STD(_T("Perf data: ") + strEx::itos(bPerfData) + _T(":") + perf); 
    723722 
    724723  if (msg.empty()) 
  • modules/CheckSystem/PDHCollector.cpp

    r7a156f4 r394f7a1  
    159159  bool bInit = true; 
    160160 
    161   if (!loadCounter(pdh)) { 
    162     pdh.removeAllCounters(); 
    163     NSC_DEBUG_MSG_STD(_T("We aparently failed to load counters trying to use default (English) counters or those configured in nsc.ini")); 
    164     SetThreadLocale(MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT)); 
    165     pdh.addCounter(NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_MEM_PAGE_LIMIT, C_SYSTEM_MEM_PAGE_LIMIT_DEFAULT), &memCmtLim); 
    166     pdh.addCounter(NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_MEM_PAGE, C_SYSTEM_MEM_PAGE_DEFAULT), &memCmt); 
    167     pdh.addCounter(NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_UPTIME, C_SYSTEM_UPTIME_DEFAULT), &upTime); 
    168     pdh.addCounter(NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_CPU, C_SYSTEM_MEM_CPU_DEFAULT), &cpu); 
    169     try { 
    170       pdh.open(); 
    171     } catch (const PDH::PDHException &e) { 
    172       NSC_LOG_ERROR_STD(_T("Failed to open performance counters: ") + e.getError()); 
     161  { 
     162    WriteLock lock(&mutex_, true, 5000); 
     163    if (!lock.IsLocked()) { 
     164      NSC_LOG_ERROR_STD(_T("Failed to get mutex when trying to start thread... thread will now die...")); 
    173165      bInit = false; 
     166    } else if (!loadCounter(pdh)) { 
     167      pdh.removeAllCounters(); 
     168      NSC_DEBUG_MSG_STD(_T("We aparently failed to load counters trying to use default (English) counters or those configured in nsc.ini")); 
     169      SetThreadLocale(MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT)); 
     170      pdh.addCounter(NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_MEM_PAGE_LIMIT, C_SYSTEM_MEM_PAGE_LIMIT_DEFAULT), &memCmtLim); 
     171      pdh.addCounter(NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_MEM_PAGE, C_SYSTEM_MEM_PAGE_DEFAULT), &memCmt); 
     172      pdh.addCounter(NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_UPTIME, C_SYSTEM_UPTIME_DEFAULT), &upTime); 
     173      pdh.addCounter(NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_CPU, C_SYSTEM_MEM_CPU_DEFAULT), &cpu); 
     174      try { 
     175        pdh.open(); 
     176      } catch (const PDH::PDHException &e) { 
     177        NSC_LOG_ERROR_STD(_T("Failed to open performance counters: ") + e.getError()); 
     178        bInit = false; 
     179      } 
    174180    } 
    175181  } 
     
    181187      std::list<std::wstring> errors; 
    182188      { 
    183         MutexLock mutex(mutexHandler); 
    184         if (!mutex.hasMutex())  
     189        ReadLock lock(&mutex_, true, 5000); 
     190        if (!lock.IsLocked())  
    185191          NSC_LOG_ERROR(_T("Failed to get Mutex!")); 
    186192        else { 
     
    210216 
    211217  { 
    212     MutexLock mutex(mutexHandler); 
    213     if (!mutex.hasMutex()) { 
     218    WriteLock lock(&mutex_, true, 5000); 
     219    if (!lock.IsLocked()) { 
    214220      NSC_LOG_ERROR(_T("Failed to get Mute when closing thread!")); 
    215221    } 
     
    235241  if (hStopEvent_ == NULL) 
    236242    NSC_LOG_ERROR(_T("Stop event is not created!")); 
    237   else 
    238     if (!SetEvent(hStopEvent_)) { 
     243  else if (!SetEvent(hStopEvent_)) { 
    239244      NSC_LOG_ERROR_STD(_T("SetStopEvent failed")); 
    240     } 
     245  } 
    241246} 
    242247/** 
     
    247252int PDHCollector::getCPUAvrage(std::wstring time) { 
    248253  unsigned int mseconds = strEx::stoui_as_time(time, checkIntervall_*100); 
    249   MutexLock mutex(mutexHandler); 
    250   if (!mutex.hasMutex()) { 
     254  ReadLock lock(&mutex_, true, 5000); 
     255  if (!lock.IsLocked()) { 
    251256    NSC_LOG_ERROR(_T("Failed to get Mutex!")); 
    252257    return -1; 
    253258  } 
    254   return static_cast<int>(cpu.getAvrage(mseconds / (checkIntervall_*100))); 
     259  try { 
     260    return static_cast<int>(cpu.getAvrage(mseconds / (checkIntervall_*100))); 
     261  } catch (PDHCollectors::PDHException &e) { 
     262    NSC_LOG_ERROR(_T("Failed to get (sub) Mutex!")); 
     263    return -1; 
     264  } 
    255265} 
    256266/** 
     
    261271*/ 
    262272long long PDHCollector::getUptime() { 
    263   MutexLock mutex(mutexHandler); 
    264   if (!mutex.hasMutex()) { 
     273  ReadLock lock(&mutex_, true, 5000); 
     274  if (!lock.IsLocked()) { 
    265275    NSC_LOG_ERROR(_T("Failed to get Mutex!")); 
    266276    return -1; 
     
    273283*/ 
    274284unsigned long long PDHCollector::getMemCommitLimit() { 
    275   MutexLock mutex(mutexHandler); 
    276   if (!mutex.hasMutex()) { 
     285  ReadLock lock(&mutex_, true, 5000); 
     286  if (!lock.IsLocked()) { 
    277287    NSC_LOG_ERROR(_T("Failed to get Mutex!")); 
    278288    return -1; 
     
    286296*/ 
    287297unsigned long long PDHCollector::getMemCommit() { 
    288   MutexLock mutex(mutexHandler); 
    289   if (!mutex.hasMutex()) { 
     298  ReadLock lock(&mutex_, true, 5000); 
     299  if (!lock.IsLocked()) { 
    290300    NSC_LOG_ERROR(_T("Failed to get Mutex!")); 
    291301    return -1; 
  • modules/CheckSystem/PDHCollector.h

    r47b843a r394f7a1  
    2323#include "PDHCollectors.h" 
    2424#include <thread.h> 
    25 #include <Mutex.h> 
     25#include <MutexRW.h> 
    2626 
    2727/** 
     
    4646class PDHCollector { 
    4747private: 
    48   MutexHandler mutexHandler; 
     48 
     49  MutexRW mutex_; 
    4950  HANDLE hStopEvent_; 
    5051  int checkIntervall_; 
    5152 
    52   PDHCollectors::StaticPDHCounterListener<unsigned __int64, PDHCollectors::format_large> memCmtLim; 
    53   PDHCollectors::StaticPDHCounterListener<unsigned __int64, PDHCollectors::format_large> memCmt; 
    54   PDHCollectors::StaticPDHCounterListener<__int64, PDHCollectors::format_large> upTime; 
    55   PDHCollectors::RoundINTPDHBufferListener<__int64, PDHCollectors::format_large> cpu; 
     53  PDHCollectors::StaticPDHCounterListener<unsigned __int64, PDHCollectors::format_large, PDHCollectors::PDHCounterNormalMutex> memCmtLim; 
     54  PDHCollectors::StaticPDHCounterListener<unsigned __int64, PDHCollectors::format_large, PDHCollectors::PDHCounterNormalMutex> memCmt; 
     55  PDHCollectors::StaticPDHCounterListener<__int64, PDHCollectors::format_large, PDHCollectors::PDHCounterNormalMutex> upTime; 
     56  PDHCollectors::RoundINTPDHBufferListener<__int64, PDHCollectors::format_large, PDHCollectors::PDHCounterNormalMutex> cpu; 
    5657 
    5758public: 
  • modules/NRPEListener/NRPEListener-2005.vcproj

    r3f69109 r394f7a1  
    14011401      </File> 
    14021402      <File 
    1403         RelativePath=".\NRPEPacket.cpp" 
    1404         > 
    1405       </File> 
    1406       <File 
    14071403        RelativePath="..\..\include\NSCHelper.cpp" 
    14081404        > 
     
    21042100      <File 
    21052101        RelativePath=".\NRPEListener.h" 
    2106         > 
    2107       </File> 
    2108       <File 
    2109         RelativePath=".\NRPEPacket.h" 
    21102102        > 
    21112103      </File> 
  • modules/NRPEListener/NRPEListener.cpp

    r7a156f4 r394f7a1  
    2424#include <time.h> 
    2525#include <config.h> 
    26 #include "NRPEPacket.h" 
    2726#include <msvc_wrappers.h> 
    2827 
     
    128127    return false; 
    129128  } 
     129  root_ = NSCModuleHelper::getBasePath(); 
    130130 
    131131  return true; 
     
    228228    return NSCModuleHelper::InjectSplitAndCommand(t.first, sTarget, '!', message, perf); 
    229229  } else if (cd.type == script) { 
    230     return executeNRPECommand(args, message, perf); 
     230    int result = process::executeProcess(root_, args, message, perf, timeout); 
     231    if (!NSCHelper::isNagiosReturnCode(result)) { 
     232      NSC_LOG_ERROR_STD(_T("The command (") + command.c_str() + _T(") returned an invalid return code: ") + strEx::itos(result)); 
     233      return NSCAPI::returnUNKNOWN; 
     234    } 
     235    return NSCHelper::int2nagios(result); 
    231236  } else if (cd.type == script_dir) { 
    232237    std::wstring args = arrayBuffer::arrayBuffer2string(char_args, argLen, _T(" ")); 
    233238    std::wstring cmd = scriptDirectory_ + command.c_str() + _T(" ") +args; 
    234     return executeNRPECommand(cmd, message, perf); 
     239    int result = process::executeProcess(root_, cmd, message, perf, timeout); 
     240    if (!NSCHelper::isNagiosReturnCode(result)) { 
     241      NSC_LOG_ERROR_STD(_T("The command (") + command.c_str() + _T(") returned an invalid return code: ") + strEx::itos(result)); 
     242      return NSCAPI::returnUNKNOWN; 
     243    } 
     244    return NSCHelper::int2nagios(result); 
    235245  } else { 
    236246    NSC_LOG_ERROR_STD(_T("Unknown script type: ") + command.c_str()); 
     
    239249 
    240250} 
    241 int NRPEListener::executeNRPECommand(std::wstring command, std::wstring &msg, std::wstring &perf) 
    242 { 
    243   NSCAPI::nagiosReturn result; 
    244   PROCESS_INFORMATION pi; 
    245   STARTUPINFO si; 
    246   HANDLE hChildOutR, hChildOutW, hChildInR, hChildInW; 
    247   SECURITY_ATTRIBUTES sec; 
    248   DWORD dwstate, dwexitcode; 
    249   int retval; 
    250  
    251  
    252   // Set up members of SECURITY_ATTRIBUTES structure.  
    253  
    254   sec.nLength = sizeof(SECURITY_ATTRIBUTES); 
    255   sec.bInheritHandle = TRUE; 
    256   sec.lpSecurityDescriptor = NULL; 
    257  
    258   // Create Pipes 
    259   CreatePipe(&hChildInR, &hChildInW, &sec, 0); 
    260   CreatePipe(&hChildOutR, &hChildOutW, &sec, 0); 
    261  
    262   // Set up members of STARTUPINFO structure.  
    263  
    264   ZeroMemory(&si, sizeof(STARTUPINFO)); 
    265   si.cb = sizeof(STARTUPINFO); 
    266   si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW; 
    267   si.hStdInput = hChildInR; 
    268   si.hStdOutput = hChildOutW; 
    269   si.hStdError = hChildOutW; 
    270   si.wShowWindow = SW_HIDE; 
    271  
    272  
    273   // CreateProcess doesn't work with a const command 
    274   TCHAR *cmd = new TCHAR[command.length()+1]; 
    275   wcsncpy_s(cmd, command.length()+1, command.c_str(), command.length()); 
    276   cmd[command.length()] = 0; 
    277   std::wstring root = NSCModuleHelper::getBasePath(); 
    278  
    279   // Create the child process.  
    280   BOOL processOK = CreateProcess(NULL, cmd,        // command line  
    281     NULL, // process security attributes  
    282     NULL, // primary thread security attributes  
    283     TRUE, // handles are inherited  
    284     0,    // creation flags  
    285     NULL, // use parent's environment  
    286     root.c_str(), // use parent's current directory  
    287     &si,  // STARTUPINFO pointer  
    288     &pi); // receives PROCESS_INFORMATION  
    289   delete [] cmd; 
    290  
    291   if (processOK) { 
    292     dwstate = WaitForSingleObject(pi.hProcess, 1000*timeout); 
    293     CloseHandle(hChildInR); 
    294     CloseHandle(hChildInW); 
    295     CloseHandle(hChildOutW); 
    296  
    297     if (dwstate == WAIT_TIMEOUT) { 
    298       TerminateProcess(pi.hProcess, 5); 
    299       msg = _T("The check (") + command + _T(") didn't respond within the timeout period (") + strEx::itos(timeout) + _T("s)!"); 
    300       result = NSCAPI::returnUNKNOWN; 
    301     } else { 
    302       DWORD dwread; 
    303       std::string str; 
    304 #define BUFF_SIZE 4096 
    305       char *buf = new char[BUFF_SIZE+1]; 
    306       do { 
    307         retval = ReadFile(hChildOutR, buf, BUFF_SIZE, &dwread, NULL); 
    308         if (retval == 0) 
    309           break; 
    310         if (dwread > BUFF_SIZE) 
    311           break; 
    312         buf[dwread] = 0; 
    313         str += buf; 
    314       } while (dwread == BUFF_SIZE); 
    315       delete [] buf; 
    316       if (str.empty()) { 
    317         msg = _T("No output available from command..."); 
    318       } else { 
    319         msg = strEx::string_to_wstring(str); 
    320         strEx::token t = strEx::getToken(msg, '|'); 
    321         msg = t.first; 
    322         std::wstring::size_type pos = msg.find_last_not_of(_T("\n\r ")); 
    323         if (pos != std::wstring::npos) { 
    324           if (pos == msg.size()) 
    325             msg = msg.substr(0,pos); 
    326           else 
    327             msg = msg.substr(0,pos+1); 
    328         } 
    329         perf = t.second; 
    330       } 
    331       if (GetExitCodeProcess(pi.hProcess, &dwexitcode) == 0) { 
    332         NSC_LOG_ERROR(_T("Failed to get commands (") + command + _T(") return code: ") + error::lookup::last_error()); 
    333         dwexitcode = NSCAPI::returnUNKNOWN; 
    334       } 
    335       if (!NSCHelper::isNagiosReturnCode(dwexitcode)) { 
    336         NSC_LOG_ERROR(_T("The command (") + command + _T(") returned an invalid return code: ") + strEx::itos(dwexitcode)); 
    337         dwexitcode = NSCAPI::returnUNKNOWN; 
    338       } 
    339       result = NSCHelper::int2nagios(dwexitcode); 
    340     } 
    341     CloseHandle(pi.hThread); 
    342     CloseHandle(pi.hProcess); 
    343     CloseHandle(hChildOutR); 
    344   } 
    345   else { 
    346     msg = _T("NRPE_NT failed to create process (") + command + _T("): ") + error::lookup::last_error(); 
    347     result = NSCAPI::returnUNKNOWN; 
    348     CloseHandle(hChildInR); 
    349     CloseHandle(hChildInW); 
    350     CloseHandle(hChildOutW); 
    351     CloseHandle(pi.hThread); 
    352     CloseHandle(pi.hProcess); 
    353     CloseHandle(hChildOutR); 
    354   } 
    355   return result; 
    356 } 
     251 
    357252void NRPEListener::onClose() 
    358253{} 
     
    400295        return; 
    401296      } 
    402       client->send(block); 
     297      int maxWait = socketTimeout_*10; 
     298      for (i=0;i<maxWait;i++) { 
     299        bool lastReadHasMore = false; 
     300        try { 
     301          if (client->canWrite()) 
     302            lastReadHasMore = client->sendAll(block); 
     303        } catch (simpleSocket::SocketException e) { 
     304          NSC_LOG_MESSAGE(_T("Could not send NRPE packet from socket :") + e.getMessage()); 
     305          client->close(); 
     306          return; 
     307        } 
     308        if (!lastReadHasMore) { 
     309          client->close(); 
     310          return; 
     311        } 
     312        Sleep(100); 
     313      } 
     314      if (i >= maxWait) { 
     315        NSC_LOG_ERROR_STD(_T("Timeout reading NRPE-packet (increase socket_timeout)")); 
     316        client->close(); 
     317        return; 
     318      } 
    403319    } 
    404320  } catch (simpleSocket::SocketException e) { 
     
    406322  } catch (NRPEException e) { 
    407323    NSC_LOG_ERROR_STD(_T("NRPEException: ") + e.getMessage()); 
     324  } catch (...) { 
     325    NSC_LOG_ERROR_STD(_T("Unhandled Exception in NRPE listner...")); 
    408326  } 
    409327  client->close(); 
     
    413331  if (p.getType() != NRPEPacket::queryPacket) { 
    414332    NSC_LOG_ERROR(_T("Request is not a query.")); 
    415     throw NRPEException(_T("Invalid query type")); 
     333    throw NRPEException(_T("Invalid query type: ") + strEx::itos(p.getType())); 
    416334  } 
    417335  if (p.getVersion() != NRPEPacket::version2) { 
     
    470388      ret = NSCAPI::returnUNKNOWN; 
    471389  } 
    472   if (msg.length() > buffer_length_) { 
     390  if (msg.length() >= buffer_length_) { 
    473391    NSC_LOG_ERROR(_T("Truncating returndata as it is bigger then NRPE allowes :(")); 
    474392    msg = msg.substr(0,buffer_length_-1); 
  • modules/NRPEListener/NRPEListener.h

    r7a156f4 r394f7a1  
    2424#include <SSLSocket.h> 
    2525#include <map> 
    26 #include "NRPEPacket.h" 
     26#include <nrpe/NRPEPacket.hpp> 
     27#include <execute_process.hpp> 
    2728 
    2829class NRPEListener : public simpleSocket::ListenerHandler { 
     
    4849  std::wstring scriptDirectory_; 
    4950  unsigned int buffer_length_; 
     51  std::wstring root_; 
    5052 
    5153public: 
  • modules/NSCAAgent/NSCAAgent.cpp

    r5ca3931 r394f7a1  
    4545 * @return  
    4646 */ 
    47 NSCAAgent::NSCAAgent() : pdhThread(_T("NSCAThread")) {} 
     47NSCAAgent::NSCAAgent() {} 
    4848/** 
    4949 * Default d-tor 
     
    5757 */ 
    5858bool NSCAAgent::loadModule() { 
    59   pdhThread.createThread(); 
    60   std::list<std::wstring> checks = NSCModuleHelper::getSettingsSection(NSCA_CMD_SECTION_TITLE); 
    61   int interval = NSCModuleHelper::getSettingsInt(NSCA_AGENT_SECTION_TITLE, NSCA_INTERVAL, NSCA_INTERVAL_DEFAULT); 
     59  //pdhThread.createThread(); 
     60  //std::list<std::wstring> checks = NSCModuleHelper::getSettingsSection(NSCA_CMD_SECTION_TITLE); 
     61  //int interval = NSCModuleHelper::getSettingsInt(NSCA_AGENT_SECTION_TITLE, NSCA_INTERVAL, NSCA_INTERVAL_DEFAULT); 
     62  int e_threads = NSCModuleHelper::getSettingsInt(NSCA_AGENT_SECTION_TITLE, NSCA_DEBUG_THREADS, NSCA_DEBUG_THREADS_DEFAULT); 
     63 
     64  for (int i=1;i<e_threads;i++) { 
     65    std::wstring id = _T("nsca_t_") + strEx::itos(i); 
     66    NSCAThreadImpl *thread = new NSCAThreadImpl(id); 
     67    extra_threads.push_back(thread); 
     68  } 
     69  for (std::list<NSCAThreadImpl*>::const_iterator cit=extra_threads.begin();cit != extra_threads.end(); ++cit) { 
     70    (*cit)->createThread(reinterpret_cast<LPVOID>(rand())); 
     71  } 
    6272   
    6373  return true; 
     
    6979 */ 
    7080bool NSCAAgent::unloadModule() { 
     81  /* 
    7182  if (!pdhThread.exitThread(20000)) { 
    7283    std::wcout << _T("MAJOR ERROR: Could not unload thread...") << std::endl; 
    7384    NSC_LOG_ERROR(_T("Could not exit the thread, memory leak and potential corruption may be the result...")); 
     85  } 
     86  */ 
     87  for (std::list<NSCAThreadImpl*>::iterator it=extra_threads.begin();it != extra_threads.end(); ++it) { 
     88    if (!(*it)->exitThread(20000)) { 
     89      std::wcout << _T("MAJOR ERROR: Could not unload thread...") << std::endl; 
     90      NSC_LOG_ERROR(_T("Could not exit the thread, memory leak and potential corruption may be the result...")); 
     91    } 
    7492  } 
    7593  return true; 
  • modules/NSCAAgent/NSCAAgent.h

    r5ca3931 r394f7a1  
    3131  CheckMemory memoryChecker; 
    3232  int processMethod_; 
    33   NSCAThreadImpl pdhThread; 
     33//  NSCAThreadImpl pdhThread; 
     34  std::list<NSCAThreadImpl*> extra_threads; 
    3435 
    3536public: 
  • modules/NSCAAgent/NSCAThread.cpp

    rc1fe385 r394f7a1  
    6060  NSCAPI::nagiosReturn ret = NSCModuleHelper::InjectCommand(cmd_.c_str(), args_.getLen(), args_.get(), msg, perf); 
    6161  result.service = alias_; 
    62   result.code = conv_code(ret); 
    6362  if (ret == NSCAPI::returnIgnored) { 
    6463    result.result = _T("Command was not found: ") + cmd_; 
     64    result.code = NSCAPI::returnUNKNOWN; 
    6565  } else if (ret == NSCAPI::returnInvalidBufferLen) { 
    6666    result.result = _T("Result was to long: ") + cmd_; 
     67    result.code = NSCAPI::returnUNKNOWN; 
    6768  } else { 
    6869    result.result = msg + _T("|") + perf; 
     70    result.code = conv_code(ret); 
    6971    if (result.result.length() >= NSCA_MAX_PLUGINOUTPUT_LENGTH) { 
    7072      NSC_LOG_ERROR(_T("NSCA return data truncated")); 
     
    7274    } 
    7375  } 
    74   NSC_LOG_MESSAGE_STD(_T("Result: ") + result.toString()); 
     76  //NSC_DEBUG_MSG_STD(_T("Result: ") + result.toString()); 
    7577  return result; 
    7678} 
     
    107109  } 
    108110 
     111  srand( reinterpret_cast<int>(lpParameter) ); 
     112 
    109113  DWORD waitStatus = 0; 
     114  int drift = (checkIntervall_*rand())/RAND_MAX ; 
     115  NSC_LOG_ERROR_STD(_T("Drifting: ") + strEx::itos(drift)); 
     116  waitStatus = WaitForSingleObject(hStopEvent_, drift*1000); 
     117  if (waitStatus != WAIT_TIMEOUT)  { 
     118    NSC_LOG_ERROR_STD(_T("Drift failed... strange...")); 
     119  } 
     120  NSC_LOG_ERROR_STD(_T("Done drifting: ") + strEx::itos(drift)); 
    110121  int remain = checkIntervall_; 
    111122  while (((waitStatus = WaitForSingleObject(hStopEvent_, remain*1000)) == WAIT_TIMEOUT)) { 
  • modules/NSCAAgent/nsca_enrypt.hpp

    rce57b1b r394f7a1  
    9292      /* generate an encryption/description key using the password */ 
    9393      int keysize=get_keySize(); 
    94       std::cout << "keysize: " << keysize << std::endl; 
    9594 
    9695      unsigned char *key = new unsigned char[keysize+1]; 
Note: See TracChangeset for help on using the changeset viewer.