Changeset c0d7e82 in nscp


Ignore:
Timestamp:
02/05/10 08:03:49 (3 years ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
f0e6036
Parents:
3080680
Message:
Files:
1 added
22 edited

Legend:

Unmodified
Added
Removed
  • build.cmake

    r3080680 rc0d7e82  
    11SET(Boost_DEBUG 1) 
    22 
    3 #set(Boost_USE_STATIC_LIBS   ON) 
     3set(Boost_USE_STATIC_LIBS   ON) 
    44set(BOOST_USE_MULTITHREADED ON) 
    55 
    66SET(BOOST_INCLUDEDIR c:/src/include/boost-1_39) 
    7 #SET(BOOST_LIBRARYDIR c:/src/lib/x86/) 
     7SET(BOOST_LIBRARYDIR c:/src/lib/x86/) 
    88SET(BOOST_LIB_SUFFIX vc80-mt) 
    99#SET(BOOST_LIB_VERSION 1.40) 
     
    1717SET(PROTOBUF_BINARYDIR C:/src/protobuf-2.3.0/vsprojects/Debug) 
    1818 
     19#SET(PROTOBUF_LIBRARYDIR C:/src/protobuf-2.3.0/vsprojects/Release) 
     20#SET(PROTOBUF_BINARYDIR C:/src/protobuf-2.3.0/vsprojects/Release) 
     21 
  • changelog

    r2018659 rc0d7e82  
    66 * Fixa dependonservice LanManWorkStation (old win) 
    77 * Fix RtlStringFromGUID problem on NT4 
     8 
     92010-02-05 
     10 * Multiple fixes all over the place 
     11 * Availible modules: CheckExternalScripts, CheckHelpers, FileLogger, NRPEClient, NRPEServer, Scheduler 
     12 ! Performance data is still missing (will be for a while yet) 
    813  
    9142010-01-31 MickeM 
  • include/NSCAPI.h

    r7f9c823 rc0d7e82  
    8787 
    8888  // Various message Types 
    89   const int log = 0;        // Log message 
     89  const int log = 1;        // Log message 
    9090  const int error = -1;     // Error (non critical) 
    9191  const int critical = -10;   // Critical error 
    92   const int warning = 1;      // Warning 
    93   const int debug = 666;      // Debug message 
     92  const int warning = 2;      // Warning 
     93  const int debug = 10;     // Debug message 
    9494 
    9595  typedef int messageTypes;   // Message type 
  • include/NSCHelper.cpp

    r2018659 rc0d7e82  
    118118    ); 
    119119} 
     120 
     121 
    120122std::wstring NSCHelper::report::to_string(unsigned int report) { 
    121123  std::wstring ret; 
     
    135137    if (!ret.empty()) ret += _T(","); 
    136138    ret += _T("unknown"); 
     139  } 
     140  return ret; 
     141} 
     142 
     143#define LOG_CRIT  0x10 
     144#define LOG_ERROR 0x08 
     145#define LOG_WARNING 0x04 
     146#define LOG_MSG   0x02 
     147#define LOG_DEBUG 0x01 
     148 
     149unsigned int NSCHelper::logging::parse(std::wstring str) { 
     150  unsigned int report = 0; 
     151  strEx::splitList lst = strEx::splitEx(str, _T(",")); 
     152 
     153  for (strEx::splitList::const_iterator key = lst.begin(); key != lst.end(); ++key) { 
     154    if (*key == _T("all")) { 
     155      report |= LOG_MSG|LOG_ERROR|LOG_CRIT|LOG_WARNING|LOG_DEBUG; 
     156    } else if (*key == _T("normal")) { 
     157        report |= LOG_MSG|LOG_ERROR|LOG_CRIT|LOG_WARNING; 
     158    } else if (*key == _T("log") || *key == _T("message") || *key == _T("info") || *key == _T("INFO")) { 
     159      report |= LOG_MSG; 
     160    } else if (*key == _T("error") || *key == _T("ERROR")) { 
     161      report |= LOG_ERROR; 
     162    } else if (*key == _T("critical") || *key == _T("CRITICAL")) { 
     163      report |= LOG_CRIT; 
     164    } else if (*key == _T("warning") || *key == _T("WARN")) { 
     165      report |= LOG_WARNING; 
     166    } else if (*key == _T("debug") || *key == _T("DEBUG")) { 
     167      report |= LOG_DEBUG; 
     168    } 
     169  } 
     170  return report; 
     171} 
     172bool NSCHelper::logging::matches(unsigned int report, NSCAPI::nagiosReturn code) { 
     173  return ( 
     174    (code == NSCAPI::critical && ((report&LOG_CRIT)==LOG_CRIT) ) || 
     175    (code == NSCAPI::error && ((report&LOG_ERROR)==LOG_ERROR) ) || 
     176    (code == NSCAPI::warning && ((report&LOG_WARNING)==LOG_WARNING) ) || 
     177    (code == NSCAPI::log && ((report&LOG_MSG)==LOG_MSG) ) || 
     178    (code == NSCAPI::debug && ((report&LOG_DEBUG)==LOG_DEBUG) ) || 
     179    ( (code != NSCAPI::critical) && (code != NSCAPI::error) && (code != NSCAPI::warning) && (code != NSCAPI::log) && (code != NSCAPI::debug) ) 
     180    ); 
     181} 
     182 
     183std::wstring NSCHelper::logging::to_string(unsigned int report) { 
     184  std::wstring ret; 
     185  if ((report&LOG_CRIT)!=0) { 
     186    if (!ret.empty()) ret += _T(","); 
     187    ret += _T("critical"); 
     188  } 
     189  if ((report&LOG_ERROR)!=0) { 
     190    if (!ret.empty()) ret += _T(","); 
     191    ret += _T("error"); 
     192  } 
     193  if ((report&LOG_WARNING)!=0) { 
     194    if (!ret.empty()) ret += _T(","); 
     195    ret += _T("warning"); 
     196  } 
     197  if ((report&LOG_MSG)!=0) { 
     198    if (!ret.empty()) ret += _T(","); 
     199    ret += _T("message"); 
     200  } 
     201  if ((report&LOG_DEBUG)!=0) { 
     202    if (!ret.empty()) ret += _T(","); 
     203    ret += _T("debug"); 
    137204  } 
    138205  return ret; 
  • include/NSCHelper.h

    r920626f rc0d7e82  
    103103 
    104104  namespace report { 
     105    unsigned int parse(std::wstring str); 
     106    bool matches(unsigned int report, NSCAPI::nagiosReturn code); 
     107    std::wstring to_string(unsigned int report); 
     108  } 
     109  namespace logging { 
    105110    unsigned int parse(std::wstring str); 
    106111    bool matches(unsigned int report, NSCAPI::nagiosReturn code); 
  • include/settings/macros.h

    r3080680 rc0d7e82  
    429429    DESCRIBE_SETTING_ADVANCED(DATEMASK, "DATEMASK", "The date format used when logging to a file."); 
    430430 
     431    DEFINE_SETTING_S(LOG_MASK, LOG_SECTION, "log mask", "normal"); 
     432    DESCRIBE_SETTING_ADVANCED(LOG_MASK, "LOG MASK", "The log mask information, error, warning, critical, debug"); 
     433 
    431434    DEFINE_SETTING_B(DEBUG_LOG, LOG_SECTION, "debug", false); 
    432435    DESCRIBE_SETTING_ADVANCED(DEBUG_LOG, "DEBUG LOGGING", "Enable debug logging can help track down errors and find problems but will impact overall perfoamnce negativly."); 
  • include/strEx.h

    r2018659 rc0d7e82  
    741741 
    742742#endif 
     743 
     744  template<typename T> 
     745  inline void parse_command(T &cmd_line, std::list<T> &args) { 
     746    boost::tokenizer<boost::escaped_list_separator<wchar_t>, T::const_iterator, T > tok(cmd_line, boost::escaped_list_separator<wchar_t>(L'\\', L' ', L'\"')); 
     747    BOOST_FOREACH(T s, tok) 
     748      args.push_back(s); 
     749  } 
     750  template<typename T> 
     751  inline void parse_command(T cmd_line, T &cmd, std::list<T> &args) { 
     752    boost::tokenizer<boost::escaped_list_separator<wchar_t>, T::const_iterator, T > tok(cmd_line, boost::escaped_list_separator<wchar_t>(L'\\', L' ', L'\"')); 
     753    bool first = true; 
     754    BOOST_FOREACH(T s, tok)  
     755    { 
     756      if (first) { 
     757        cmd = s; 
     758        first = false; 
     759      } else { 
     760        args.push_back(s); 
     761      } 
     762 
     763    } 
     764  } 
     765  /* 
     766  template<typename T = std::string> 
     767  inline void parse_command(T &string, std::list<T> &list) { 
     768    boost::tokenizer<boost::escaped_list_separator<char>, T::const_iterator, T > tok(string, boost::escaped_list_separator<char>('\\', ' ', '\"')); 
     769    BOOST_FOREACH(T s, tok) 
     770      list.push_back(s); 
     771  } 
     772  */ 
     773 
    743774} 
    744775 
  • modules/CheckExternalScripts/CheckExternalScripts.h

    r3080680 rc0d7e82  
    3232    std::wstring command; 
    3333    std::wstring arguments; 
     34    std::wstring to_string() { 
     35      return command + _T("(") + arguments + _T(")"); 
     36    } 
    3437  }; 
    3538  typedef std::map<std::wstring, command_data> command_list; 
     
    8386  void addCommand(std::wstring key, std::wstring cmd, std::wstring args) { 
    8487    boost::to_lower(key); 
    85     commands[key] = command_data(cmd, args); 
     88    command_data cd = command_data(cmd, args); 
     89    commands[key] = cd; 
     90    NSCModuleHelper::registerCommand(key, _T("Script: ") + cd.to_string()); 
    8691  } 
    8792  void addAlias(std::wstring key, std::wstring cmd, std::wstring args) { 
    8893    boost::to_lower(key); 
    89     alias[key] = command_data(cmd, args); 
     94    command_data cd = command_data(cmd, args); 
     95    alias[key] = cd; 
     96    NSCModuleHelper::registerCommand(key, _T("Alias for: ") + cd.to_string()); 
    9097  } 
    9198}; 
  • modules/FileLogger/FileLogger.cpp

    rd5356c1 rc0d7e82  
    2222#include "stdafx.h" 
    2323#include "FileLogger.h" 
    24  
    25 #include <sys/timeb.h> 
    26 #include <time.h> 
     24#include <boost/date_time.hpp> 
     25#include <fstream> 
    2726#include <utils.h> 
    2827 
    2928FileLogger gFileLogger; 
    30  
    31 BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved) 
    32 { 
    33   NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call); 
    34   return TRUE; 
    35 } 
    3629 
    3730FileLogger::FileLogger() : init_(false) { 
     
    4033} 
    4134 
     35#ifdef WIN32 
    4236#ifndef CSIDL_COMMON_APPDATA  
    4337#define CSIDL_COMMON_APPDATA 0x0023  
     
    5650  return FALSE; 
    5751} 
     52#endif 
    5853 
    5954std::wstring getFolder(std::wstring key) { 
     
    6156    return NSCModuleHelper::getBasePath(); 
    6257  } else { 
     58#ifdef WIN32 
    6359    if (key == _T("local-app-data")) { 
    6460      TCHAR buf[MAX_PATH+1]; 
     
    6662      return buf; 
    6763    } 
     64#endif 
    6865  } 
    6966  return NSCModuleHelper::getBasePath(); 
    7067} 
    71 std::wstring FileLogger::getFileName() { 
     68std::string FileLogger::getFileName() { 
    7269  if (file_.empty()) { 
    73     file_ = SETTINGS_GET_STRING(log::FILENAME); 
     70    file_ = to_string(SETTINGS_GET_STRING(log::FILENAME)); 
    7471    if (file_.empty()) 
    75       file_ = setting_keys::log::FILENAME_DEFAULT; 
    76     if (file_.find(_T("\\")) == std::wstring::npos) { 
    77       std::wstring root = getFolder(SETTINGS_GET_STRING(log::ROOT)); 
    78       std::wstring::size_type pos = root.find_last_not_of(L'\\'); 
     72      file_ = to_string(setting_keys::log::FILENAME_DEFAULT); 
     73    if (file_.find("\\") == std::wstring::npos) { 
     74      std::string root = to_string(getFolder(SETTINGS_GET_STRING(log::ROOT))); 
     75      std::string::size_type pos = root.find_last_not_of('\\'); 
    7976      if (pos != std::wstring::npos) { 
    8077        //root = root.substr(0, pos); 
    8178      } 
    82       file_ = root + _T("\\") + file_; 
     79      file_ = root + "\\" + file_; 
    8380    } 
    8481  } 
     
    9592    SETTINGS_REG_KEY_S(log::FILENAME); 
    9693    SETTINGS_REG_KEY_S(log::DATEMASK); 
    97     SETTINGS_REG_KEY_B(log::DEBUG_LOG); 
     94    SETTINGS_REG_KEY_S(log::LOG_MASK); 
    9895 
    9996  } catch (NSCModuleHelper::NSCMHExcpetion &e) { 
     
    104101 
    105102 
    106   format_ = SETTINGS_GET_STRING(log::DATEMASK); 
     103  format_ = to_string(SETTINGS_GET_STRING(log::DATEMASK)); 
     104  std::wstring log_mask = SETTINGS_GET_STRING(log::LOG_MASK); 
     105  log_mask_ = NSCHelper::logging::parse(log_mask); 
     106  NSC_LOG_MESSAGE_STD(_T("Using logmask: ") + NSCHelper::logging::to_string(log_mask_)); 
    107107  init_ = true; 
    108108  std::wstring hello = _T("Starting to log for: ") + NSCModuleHelper::getApplicationName() + _T(" - ") + NSCModuleHelper::getApplicationVersionString(); 
    109109  handleMessage(NSCAPI::log, __FILEW__, __LINE__, hello.c_str()); 
    110   NSC_LOG_MESSAGE_STD(_T("Log path is: ") + file_ ); 
     110  NSC_LOG_MESSAGE_STD(_T("Log path is: ") + to_wstring(file_)); 
    111111  return true; 
    112112} 
     
    120120  return true; 
    121121} 
     122/* 
    122123HANDLE openAppendOrNew(std::wstring file) { 
    123124  DWORD numberOfBytesWritten = 0; 
     
    132133  return hFile; 
    133134} 
    134 void FileLogger::writeEntry(std::wstring line) { 
    135   DWORD numberOfBytesWritten; 
    136   HANDLE hFile = openAppendOrNew(file_); 
    137   if (hFile == INVALID_HANDLE_VALUE) { 
    138     if (line.find(_T("can not log to file")) != std::wstring::npos) 
    139       NSC_LOG_MESSAGE_STD(_T("Failed to create log file and temporary log! (can not log to file): ") + file_ ); 
    140     return; 
    141   } 
    142   if (::SetFilePointer(hFile, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER) { 
    143     std::wcout << _T("Failed to move pointer to end of file...") << std::endl; 
    144   } 
    145   ::WriteFile(hFile, line.c_str(), (line.length())*(sizeof(TCHAR)), &numberOfBytesWritten, NULL); 
    146   ::CloseHandle(hFile); 
    147 } 
     135*/ 
    148136 
    149137void FileLogger::handleMessage(int msgType, TCHAR* file, int line, const TCHAR* message) { 
    150138  if (!init_) { 
    151     std::wcout << _T("Discarding message (not initzialized yet") << std::endl; 
     139    std::wcout << _T("Discarding: ") << message << std::endl; 
    152140    return; 
    153141  } 
    154   TCHAR buffer[65]; 
    155   __time64_t ltime; 
    156   _time64( &ltime ); 
    157   struct tm *today = _localtime64( &ltime ); 
    158   if (today) { 
    159     size_t len = wcsftime(buffer, 63, format_.c_str(), today); 
    160     if ((len < 1)||(len > 64)) 
    161       wcsncpy_s(buffer, 64, _T("???"), 63); 
    162     else 
    163       buffer[len] = 0; 
    164   } else { 
    165     wcsncpy_s(buffer, 64, _T("???"), 63); 
     142  if (!NSCHelper::logging::matches(log_mask_, msgType)) 
     143    return; 
     144 
     145  std::ofstream stream(file_.c_str(), std::ios::out|std::ios::app|std::ios::ate); 
     146  if (!stream) { 
     147    std::wcout << _T("File could not be opened, Discarding: ") << message << std::endl; 
    166148  } 
    167   writeEntry(std::wstring(buffer) + _T(": ") +  
    168     NSCHelper::translateMessageType(msgType) + _T(":") +  
    169     std::wstring(file) + _T(":") + strEx::itos(line) +_T(": ") +  
    170     message + _T("\r\n")); 
     149  stream << to_string(get_formated_date())  
     150    << (": ") << to_string(NSCHelper::translateMessageType(msgType)) 
     151    << (":") << to_string(std::wstring(file)) 
     152    <<(":") << to_string(line)  
     153    << (": ") << to_string(std::wstring(message)) << std::endl; 
    171154} 
    172155 
     156std::wstring FileLogger::get_formated_date() { 
     157  std::wstringstream ss; 
     158  boost::posix_time::time_facet *facet = new boost::posix_time::time_facet(format_.c_str()); 
     159  ss.imbue(locale(cout.getloc(), facet)); 
     160  ss << boost::posix_time::second_clock::local_time(); 
     161  return ss.str(); 
     162} 
     163 
     164NSC_WRAP_DLL(); 
    173165NSC_WRAPPERS_MAIN_DEF(gFileLogger); 
    174166NSC_WRAPPERS_HANDLE_MSG_DEF(gFileLogger); 
     
    176168NSC_WRAPPERS_HANDLE_CONFIGURATION(gFileLogger); 
    177169 
    178  
    179  
    180  
    181170MODULE_SETTINGS_START(FileLogger, _T("File logger configuration"),_T("...")) 
    182 PAGE(_T("Filelogger")) 
    183  
    184 ITEM_CHECK_BOOL(_T("Log debug messages"), _T("Enable this to log debug messages (when running with /test debuglog is always enabled)")) 
    185 ITEM_MAP_TO(_T("basic_ini_text_mapper")) 
    186 OPTION(_T("section"), _T("log")) 
    187 OPTION(_T("key"), _T("debug")) 
    188 OPTION(_T("default"), _T("false")) 
    189 OPTION(_T("true_value"), _T("1")) 
    190 OPTION(_T("false_value"), _T("0")) 
    191 ITEM_END() 
    192  
    193 ITEM_EDIT_TEXT(_T("Log file"), _T("This is the size of the buffer that stores CPU history.")) 
    194 OPTION(_T("unit"), _T("(relative to NSClient++ binary")) 
    195 ITEM_MAP_TO(_T("basic_ini_text_mapper")) 
    196 OPTION(_T("section"), _T("log")) 
    197 OPTION(_T("key"), _T("file")) 
    198 OPTION(_T("default"), _T("NSC.log")) 
    199 ITEM_END() 
    200  
    201 ITEM_EDIT_TEXT(_T("Date mask"), _T("The date/timeformat in the log file.")) 
    202 ITEM_MAP_TO(_T("basic_ini_text_mapper")) 
    203 OPTION(_T("section"), _T("log")) 
    204 OPTION(_T("key"), _T("date_mask")) 
    205 OPTION(_T("default"), _T("%Y-%m-%d %H:%M:%S")) 
    206 ITEM_END() 
    207  
    208 PAGE_END() 
    209171MODULE_SETTINGS_END() 
  • modules/FileLogger/FileLogger.def

    re26cfe0 rc0d7e82  
    1313  NSGetConfigurationMeta 
    1414  NSGetModuleDescription 
     15  NSDeleteBuffer 
  • modules/FileLogger/FileLogger.h

    r9567d4b rc0d7e82  
    2525class FileLogger { 
    2626private: 
    27   std::wstring file_; 
    28   std::wstring format_; 
     27  std::string file_; 
     28  std::string format_; 
    2929  bool init_; 
     30  int log_mask_; 
    3031 
    3132public: 
     
    5354  void handleMessage(int msgType, TCHAR* file, int line, const TCHAR* message); 
    5455  int handleCommand(TCHAR* command, TCHAR **argument, TCHAR *returnBuffer, int returnBufferLen); 
    55   void writeEntry(std::wstring line); 
     56  //void writeEntry(std::wstring line); 
    5657 
    5758 
    58   std::wstring getFileName(); 
     59  std::string getFileName(); 
     60  inline std::wstring FileLogger::get_formated_date(); 
     61 
    5962}; 
  • modules/FileLogger/stdafx.h

    r7f9c823 rc0d7e82  
    2222#pragma once 
    2323 
    24 #define WIN32_LEAN_AND_MEAN   // Exclude rarely-used stuff from Windows headers 
    25 // Windows Header Files: 
    26 #include <windows.h> 
     24#include <string> 
     25#include <iostream> 
     26#include <fstream> 
     27 
    2728#include <NSCAPI.h> 
    2829#include <NSCHelper.h> 
    2930#include <nsc_module_wrapper.hpp> 
    30 #include <string> 
    31 #include <iostream> 
    32 #include <fstream> 
    3331 
    3432#include <config.h> 
  • modules/NRPEClient/NRPEClient.cpp

    r2018659 rc0d7e82  
    2727//#include <execute_process.hpp> 
    2828#include <strEx.h> 
    29  
     29#include <boost/filesystem.hpp> 
    3030 
    3131 
     
    5757    NSC_LOG_ERROR_STD(_T("Failed to register command.")); 
    5858  } 
     59 
     60  boost::filesystem::wpath p = NSCModuleHelper::getBasePath() + std::wstring(_T("security/nrpe_dh_512.pem")); 
     61  cert_ = p.string(); 
     62  if (boost::filesystem::is_regular_file(p)) { 
     63    NSC_DEBUG_MSG_STD(_T("Using certificate: ") + cert_); 
     64  } else { 
     65    NSC_LOG_ERROR_STD(_T("Certificate not found: ") + cert_); 
     66  } 
     67 
     68 
    5969  for (std::list<std::wstring>::const_iterator it = commands.begin(); it != commands.end(); ++it) { 
    6070    NSC_DEBUG_MSG_STD(*it); 
     
    112122    NSC_DEBUG_MSG_STD(_T("Added NRPE Client: ") + key.c_str() + _T(" = ") + command_data.toString()); 
    113123    commands[key] = command_data; 
     124 
     125    NSCModuleHelper::registerCommand(key.c_str(), command_data.toString()); 
     126 
    114127  } catch (boost::program_options::validation_error &e) { 
    115128    NSC_LOG_ERROR_STD(_T("Could not parse: ") + key.c_str() + strEx::string_to_wstring(e.what())); 
     
    294307public: 
    295308  nrpe_socket(boost::asio::io_service &io_service, std::wstring host, int port) : socket_(io_service) { 
    296     NSC_LOG_CRITICAL(_T("Looking up...")); 
    297309    tcp::resolver resolver(io_service); 
    298310    tcp::resolver::query query(to_string(host), to_string(port)); 
     
    303315    tcp::resolver::iterator end; 
    304316 
    305     NSC_LOG_CRITICAL(_T("connecting...")); 
    306317    boost::system::error_code error = boost::asio::error::host_not_found; 
    307318    while (error && endpoint_iterator != end) 
     
    312323      socket_.connect(*endpoint_iterator++, error); 
    313324    } 
    314     NSC_LOG_CRITICAL(_T("Connected...")); 
    315325    if (error) 
    316326      throw boost::system::system_error(error); 
     
    342352    NSC_LOG_CRITICAL(_T("Looking up...")); 
    343353    tcp::resolver resolver(io_service); 
    344     //tcp::resolver::query query(to_string(host), to_string(port)); 
    345     tcp::resolver::query query("www.medin.name", "80"); 
     354    tcp::resolver::query query(to_string(host), to_string(port)); 
     355    //tcp::resolver::query query("www.medin.name", "80"); 
    346356    //tcp::resolver::query query("test_server", "80"); 
    347357 
     
    370380 
    371381  void send(nrpe::packet &packet, boost::posix_time::seconds timeout) { 
    372     NSC_LOG_CRITICAL(_T("Writing...")); 
    373382    std::vector<char> buf(packet.get_packet_length()); 
    374383    write_with_timeout(socket_, socket_.lowest_layer(), boost::asio::buffer(packet.create_buffer(), packet.get_packet_length()), timeout); 
    375     NSC_LOG_CRITICAL(_T("Written...")); 
    376384  } 
    377385  nrpe::packet recv(const nrpe::packet &packet, boost::posix_time::seconds timeout) { 
    378     NSC_LOG_CRITICAL(_T("Reading...")); 
    379386    std::vector<char> buf(packet.get_packet_length()); 
    380387    read_with_timeout(socket_, socket_.lowest_layer(), boost::asio::buffer(buf), timeout); 
    381388    return nrpe::packet(&buf[0], buf.size(), packet.get_payload_length()); 
    382     NSC_LOG_CRITICAL(_T("Read...")); 
    383389  } 
    384390}; 
     
    388394  boost::asio::ssl::context ctx(io_service, boost::asio::ssl::context::sslv23); 
    389395  SSL_CTX_set_cipher_list(ctx.impl(), "ADH"); 
    390   ctx.use_tmp_dh_file("d:\\nrpe_512.pem"); 
     396  ctx.use_tmp_dh_file(to_string(cert_)); 
    391397  ctx.set_verify_mode(boost::asio::ssl::context::verify_none); 
    392398  nrpe_ssl_socket socket(io_service, ctx, host, port); 
  • modules/NRPEClient/NRPEClient.h

    r2018659 rc0d7e82  
    8787  command_list commands; 
    8888  unsigned int buffer_length_; 
     89  std::wstring cert_; 
    8990 
    9091public: 
  • modules/Scheduler/Scheduler.cpp

    r2018659 rc0d7e82  
    5050 
    5151    scheduler_.set_threads(SETTINGS_GET_INT(scheduler::THREADS)); 
     52    NSC_DEBUG_MSG_STD(_T("Thread count: ") + to_wstring(scheduler_.get_threads())); 
    5253 
    5354    if (mode == NSCAPI::normalStart) { 
     
    7374 
    7475    } 
    75 /* 
    76     add_schedule(_T("test: FIRST")); 
    77     for (int i=0;i<1000;i++) 
    78       add_schedule(_T("test: ") + to_wstring(i)); 
    79     add_schedule(_T("test: LAST")); 
    80     */ 
    8176  } catch (NSCModuleHelper::NSCMHExcpetion &e) { 
    8277    NSC_LOG_ERROR_STD(_T("Exception in module Scheduler: ") + e.msg_); 
     
    106101  item.channel = NSCModuleHelper::getSettingsString(detail_path, setting_keys::scheduler::CHANNEL, def.channel); 
    107102  item.command = NSCModuleHelper::getSettingsString(detail_path, setting_keys::scheduler::COMMAND, item.command); 
     103 
     104  strEx::parse_command(item.command, item.command, item.arguments); 
     105 
    108106  std::wstring report = NSCModuleHelper::getSettingsString(detail_path, setting_keys::scheduler::REPORT_MODE, NSCHelper::report::to_string(def.report)); 
    109107  item.report = NSCHelper::report::parse(report); 
     
    117115  scheduler_.stop(); 
    118116  return true; 
     117} 
     118 
     119void Scheduler::on_error(std::wstring error) { 
     120  NSC_LOG_ERROR_STD(error); 
    119121} 
    120122 
  • modules/Scheduler/Scheduler.h

    r1bfe6f0 rc0d7e82  
    4242  scheduler::target read_defaut_schedule(std::wstring path); 
    4343  void handle_schedule(scheduler::target item); 
     44  void on_error(std::wstring error); 
    4445 
    4546  std::wstring getModuleName() { 
  • modules/Scheduler/simple_scheduler.cpp

    r6822839 rc0d7e82  
    22 
    33#include <boost/bind.hpp> 
    4  
     4#include <strEx.h> 
    55#include <unicode_char.hpp> 
    66 
     
    3131  void simple_scheduler::start() { 
    3232    running_ = true; 
    33     if (!queue_.empty()) 
    34       start_thread(); 
     33    start_thread(); 
    3534  } 
    3635  void simple_scheduler::stop() { 
    3736    running_ = false; 
    38     //if (!thread_) 
    39     //  return; 
    4037    stop_requested_ = true; 
    4138    threads_.interrupt_all(); 
    4239    threads_.join_all(); 
    43     /* 
    44     if (!threads.join_all(boost::posix_time::seconds(5))) { 
    45       std::wcout << _T("FAILED TO TERMINATE!!!") << std::endl; 
    46     } else { 
    47       std::wcout << _T("THREAD TERMINATED NICELY!") << std::endl; 
    48     } 
    49     */ 
    5040  } 
    5141 
     
    5747    if (missing_threads > 0 && missing_threads <= thread_count_) { 
    5848      for (int i=0;i<missing_threads;i++) { 
    59         std::wcout << _T("***START_THREAD***") << std::endl; 
    60         threads_.create_thread(boost::bind(&simple_scheduler::thread_proc, this)); 
     49        //std::wcout << _T("***START_THREAD: ") << threads_.size() << std::endl; 
     50        threads_.create_thread(boost::bind(&simple_scheduler::thread_proc, this, i)); 
    6151      } 
    6252    } 
     53    threads_.create_thread(boost::bind(&simple_scheduler::watch_dog, this, 0)); 
    6354    //thread_ = boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(&simple_scheduler::thread_proc, this))); 
    6455  } 
    6556 
    66   void simple_scheduler::thread_proc() { 
     57  void simple_scheduler::watch_dog(int id) { 
     58 
     59    schedule_queue_type::value_type instance; 
     60    while(!stop_requested_) { 
     61      instance = queue_.top(); 
     62      if (instance) { 
     63        boost::posix_time::time_duration off = now() - (*instance).time; 
     64        if (off.total_seconds() > error_threshold_) { 
     65          log_error(_T("NOONE IS HANDLING scheduled item ") + to_wstring((*instance).schedule_id) + _T(" ") + to_wstring(off.total_seconds()) + _T(" seconds to late from thread ") + to_wstring(id)); 
     66        } 
     67      } else { 
     68        log_error(_T("Nothing is scheduled to run")); 
     69      } 
     70 
     71      // add support for checking queue length 
     72      boost::thread::sleep(boost::get_system_time() + boost::posix_time::seconds(5)); 
     73    } 
     74 
     75  } 
     76 
     77  void simple_scheduler::thread_proc(int id) { 
    6778    int iteration = 0; 
    6879    schedule_queue_type::value_type instance; 
    6980    while (!stop_requested_) { 
    7081      instance = queue_.pop(); 
    71       if (!instance) 
    72         return; 
     82      if (!instance) { 
     83        boost::unique_lock<boost::mutex> lock(idle_thread_mutex_); 
     84        idle_thread_cond_.wait(lock); 
     85        continue; 
     86      } 
    7387 
    7488      try { 
    75  
    7689        boost::posix_time::time_duration off = now() - (*instance).time; 
    77         if (off.total_seconds() > 0) { 
    78           std::wcout << _T("MISSED IT!") << off.total_seconds() << std::endl; 
     90        if (off.total_seconds() > error_threshold_) { 
     91          log_error(_T("Ran scheduled item ") + to_wstring((*instance).schedule_id) + _T(" ") + to_wstring(off.total_seconds()) + _T(" seconds to late from thread ") + to_wstring(id)); 
    7992        } 
    8093        boost::thread::sleep((*instance).time); 
    8194      } catch (boost::thread_interrupted  &e) { 
    8295        if (!queue_.push(*instance)) 
    83           std::wcout << _T("ERROR") << std::endl; 
    84         if (stop_requested_) 
     96          log_error(_T("ERROR")); 
     97        if (stop_requested_) { 
     98          log_error(_T("Terminating thread: ") + to_wstring(id)); 
    8599          return; 
     100        } 
    86101        continue; 
    87102      } catch (...) { 
    88103        if (!queue_.push(*instance)) 
    89           std::wcout << _T("ERROR") << std::endl; 
    90         std::wcout << _T("ERROR!!!") << std::endl; 
    91         return; 
     104          log_error(_T("ERROR")); 
     105        continue; 
    92106      } 
    93107 
     
    100114          reschedule(*item,now_time); 
    101115        } catch (...) { 
    102           std::wcout << _T("UNKNOWN ERROR RUNING TASK: ") << std::endl; 
     116          log_error(_T("UNKNOWN ERROR RUNING TASK: ")); 
    103117          reschedule(*item); 
    104118        } 
    105119      } else { 
    106         std::wcout << _T("Task not found: ") << (*instance).schedule_id << std::endl; 
     120        log_error(_T("Task not found: ") + to_wstring((*instance).schedule_id)); 
    107121      } 
    108122    } 
     
    120134    instance.time = next; 
    121135    if (!queue_.push(instance)) { 
    122       std::wcout << _T("ERROR") << std::endl; 
     136      log_error(_T("ERROR")); 
    123137    } 
    124     start_thread(); 
     138    idle_thread_cond_.notify_one(); 
    125139  } 
     140 
     141 
     142  void simple_scheduler::log_error(std::wstring err) { 
     143    if (handler_) 
     144      handler_->on_error(err); 
     145  } 
     146 
    126147} 
    127148 
  • modules/Scheduler/simple_scheduler.hpp

    r6822839 rc0d7e82  
    7070  public: 
    7171    virtual void handle_schedule(target item) = 0; 
     72    virtual void on_error(std::wstring error) = 0; 
    7273  }; 
    7374  struct schedule_instance { 
     
    129130    schedule_queue_type queue_; 
    130131    unsigned int thread_count_; 
     132    boost::mutex idle_thread_mutex_; 
     133    boost::condition_variable idle_thread_cond_; 
     134 
    131135 
    132136 
     
    135139    volatile bool running_; 
    136140    boost::thread_group threads_; 
    137     //boost::shared_ptr<boost::thread> thread_; 
    138141    boost::mutex mutex_; 
    139142    schedule_handler* handler_; 
    140  
     143    int error_threshold_; 
    141144  public: 
    142145 
    143     simple_scheduler() : target_id_(0), stop_requested_(false), running_(false), thread_count_(10), handler_(NULL) {} 
     146    simple_scheduler() : target_id_(0), stop_requested_(false), running_(false), thread_count_(10), handler_(NULL), error_threshold_(5) {} 
    144147    ~simple_scheduler() {} 
    145148 
     
    163166      start_thread(); 
    164167    } 
     168    int get_threads() const { return thread_count_;} 
    165169 
    166170 
    167171  private: 
    168     void thread_proc(); 
     172    void thread_proc(int id); 
     173    void watch_dog(int id); 
    169174 
    170175    void reschedule(target item); 
     
    172177    void reschedule_wnext(target item, boost::posix_time::ptime next); 
    173178    void start_thread(); 
     179 
     180    void log_error(std::wstring err); 
    174181 
    175182    inline boost::posix_time::ptime now() { 
  • service/NSClient++.cpp

    r3080680 rc0d7e82  
    988988    } 
    989989    plugins_.insert(plugins_.end(), plugin); 
     990    commands_.add_plugin(plugin); 
    990991    if (plugin->hasCommandHandler()) 
    991992      commandHandlers_.insert(commandHandlers_.end(), plugin); 
     
    10461047 
    10471048    std::string args = to_string(arguments); 
     1049 
    10481050    boost::tokenizer<boost::escaped_list_separator<char> > tok(args, boost::escaped_list_separator<char>('\\', ' ', '\"')); 
    10491051    BOOST_FOREACH(string s, tok) 
     
    11071109    } 
    11081110  } else */{ 
     1111    /* 
    11091112    boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 
    11101113    if (!readLock.owns_lock()) { 
     
    11121115      return NSCAPI::returnUNKNOWN; 
    11131116    } 
    1114     for (pluginList::size_type i = 0; i < commandHandlers_.size(); i++) { 
    1115       try { 
    1116         NSCAPI::nagiosReturn c = commandHandlers_[i]->handleCommand(command, request, response); 
    1117         switch (c) { 
    1118           case NSCAPI::returnInvalidBufferLen: 
    1119             LOG_ERROR_CORE(_T("UNKNOWN: Return buffer to small to handle this command.")); 
    1120             return c; 
    1121           case NSCAPI::returnIgnored: 
    1122             break; 
    1123           case NSCAPI::returnOK: 
    1124           case NSCAPI::returnWARN: 
    1125           case NSCAPI::returnCRIT: 
    1126           case NSCAPI::returnUNKNOWN: 
    1127             LOG_DEBUG_STD(_T("Result ") + std::wstring(command) + _T(": ") + NSCHelper::translateReturn(c) + _T(" {{{") + strEx::strip_hex(to_wstring(response)) + _T("}}}")); 
    1128             return c; 
    1129           default: 
    1130             LOG_ERROR_CORE_STD(_T("Unknown error from handleCommand: ") + strEx::itos(c) + _T(" the injected command was: ") + (std::wstring)command); 
    1131             return c; 
    1132         } 
    1133       } catch(const NSPluginException& e) { 
    1134         LOG_ERROR_CORE_STD(_T("Exception raised: ") + e.error_ + _T(" in module: ") + e.file_); 
    1135         return NSCAPI::returnCRIT; 
    1136       } catch(...) { 
    1137         LOG_ERROR_CORE(_T("Unknown exception raised in module")); 
    1138         return NSCAPI::returnCRIT; 
    1139       } 
    1140     } 
    1141     LOG_MESSAGE_STD(_T("No handler for command: '") + command + _T("'")); 
    1142     return NSCAPI::returnIgnored; 
     1117    */ 
     1118    try { 
     1119      nsclient::commands::plugin_type plugin = commands_.get(command); 
     1120      if (!plugin) { 
     1121        LOG_ERROR_CORE(_T("No handler for command: ") + std::wstring(command) + _T(" avalible commands: ") + commands_.to_wstring()); 
     1122        return NSCAPI::returnIgnored; 
     1123      } 
     1124      NSCAPI::nagiosReturn c = plugin->handleCommand(command, request, response); 
     1125      LOG_DEBUG_STD(_T("Result ") + std::wstring(command) + _T(": ") + NSCHelper::translateReturn(c) + _T(" {{{") + strEx::strip_hex(to_wstring(response)) + _T("}}}")); 
     1126      return c; 
     1127    } catch (nsclient::commands::command_exception &e) { 
     1128      LOG_ERROR_CORE(_T("No handler for command: ") + std::wstring(command) + _T(": ") + to_wstring(e.what())); 
     1129      return NSCAPI::returnIgnored; 
     1130    } catch (...) { 
     1131      LOG_ERROR_CORE(_T("Error handling command: ") + std::wstring(command)); 
     1132      return NSCAPI::returnIgnored; 
     1133    } 
    11431134  } 
    11441135} 
  • service/commands.hpp

    r01a278b rc0d7e82  
    2222    }; 
    2323 
    24   private: 
    2524    typedef boost::shared_ptr<NSCPlugin> plugin_type; 
    2625    typedef std::map<unsigned long,plugin_type> plugin_list_type; 
     
    2928 
    3029 
     30  private: 
    3131    nsclient::logger *logger_; 
    3232    plugin_list_type plugins_; 
     
    6060      boost::unique_lock<boost::shared_mutex> writeLock(mutex_, boost::get_system_time() + boost::posix_time::seconds(10)); 
    6161      if (!writeLock.owns_lock()) { 
    62         log_error(__FILEW__, __LINE__, _T("Failed to get mutex in remove_plugin for plugin id: ") + to_wstring(id)); 
     62        log_error(__FILEW__, __LINE__, _T("Failed to get mutex in remove_plugin for plugin id: ") + ::to_wstring(id)); 
    6363        return; 
    6464      } 
     
    8686        return; 
    8787      } 
    88       descriptions_[cmd] = desc; 
    89       commands_[cmd] = plugins_[plugin_id]; 
     88      std::wstring lc = make_key(cmd); 
     89      if (!have_plugin(plugin_id)) 
     90        throw command_exception("Failed to find plugin: " + ::to_string(plugin_id)); 
     91      descriptions_[lc] = desc; 
     92      commands_[lc] = plugins_[plugin_id]; 
    9093    } 
    9194 
     
    9699        return _T("error: ") + command; 
    97100      } 
    98       description_list_type::const_iterator cit = descriptions_.find(command); 
     101      std::wstring lc = make_key(command); 
     102      description_list_type::const_iterator cit = descriptions_.find(lc); 
    99103      if (cit == descriptions_.end()) 
    100104        return _T("Command not found: ") + command; 
     
    120124      if (!readLock.owns_lock()) { 
    121125        log_error(__FILEW__, __LINE__, _T("Failed to get mutex: ") + command); 
    122         throw command_exception("Failed to get mutext (commands::get)"); 
     126        throw command_exception("Failed to get mutex (commands::get)"); 
    123127      } 
    124       commands_[command]; 
     128      std::wstring lc = make_key(command); 
     129      command_list_type::iterator cit = commands_.find(lc); 
     130      if (cit == commands_.end()) { 
     131        std::wcout << _T("NOT FOUND") << std::endl; 
     132        return plugin_type(); 
     133      } 
     134      return (*cit).second; 
    125135    } 
    126136 
     137    std::wstring to_wstring() { 
     138      std::wstring ret; 
     139      BOOST_FOREACH(std::wstring str, list()) { 
     140        if (!ret.empty()) ret += _T(", "); 
     141        ret += str; 
     142      } 
     143      return ret; 
     144    } 
    127145 
    128   private: 
     146    inline std::wstring make_key(std::wstring key) { 
     147      return boost::algorithm::to_lower_copy(key); 
     148    } 
    129149    void log_error(std::wstring file, int line, std::wstring error) { 
    130150      if (logger_ != NULL) 
    131151        logger_->nsclient_log_error(file, line, error); 
    132152    } 
     153 
     154    inline bool have_plugin(unsigned long plugin_id) { 
     155      return !(plugins_.find(plugin_id) == plugins_.end()); 
     156    } 
     157 
     158 
    133159  }; 
    134160} 
  • service/core_api.cpp

    r2018659 rc0d7e82  
    279279  try { 
    280280    mainClient.registerCommand(id, cmd, desc); 
     281  } catch (nsclient::commands::command_exception &e) { 
     282    LOG_ERROR_STD(_T("Exception registrying command: ") + ::to_wstring(e.what()) + _T(", from: ") + to_wstring(id)); 
     283    return NSCAPI::isfalse; 
    281284  } catch (...) { 
    282285    LOG_ERROR_STD(_T("Unknown exception registrying command: ") + std::wstring(cmd) + _T(", from: ") + to_wstring(id)); 
    283     return NSCAPI::isSuccess; 
    284  
     286    return NSCAPI::isfalse; 
    285287  } 
    286288  return NSCAPI::isSuccess; 
  • service/simple_client.hpp

    r2018659 rc0d7e82  
    3838          std::list<std::wstring> lst = core_->list_commands(); 
    3939          for (std::list<std::wstring>::const_iterator cit = lst.begin(); cit!=lst.end();++cit) 
    40             std::wcout << *cit << std::endl; 
     40            std::wcout << *cit << _T(": ") << core_->describeCommand(*cit) << std::endl; 
    4141          std::wcout << _T("Listing commands...Done") << std::endl; 
    4242        } else if (s == _T("off") && buff == _T("debug ")) { 
Note: See TracChangeset for help on using the changeset viewer.