Changeset c0d7e82 in nscp
- Timestamp:
- 02/05/10 08:03:49 (3 years ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- f0e6036
- Parents:
- 3080680
- Files:
-
- 1 added
- 22 edited
-
build.cmake (modified) (2 diffs)
-
changelog (modified) (1 diff)
-
include/NSCAPI.h (modified) (1 diff)
-
include/NSCHelper.cpp (modified) (2 diffs)
-
include/NSCHelper.h (modified) (1 diff)
-
include/settings/macros.h (modified) (1 diff)
-
include/strEx.h (modified) (1 diff)
-
modules/CheckExternalScripts/CheckExternalScripts.h (modified) (2 diffs)
-
modules/FileLogger/CMakeLists.txt (added)
-
modules/FileLogger/FileLogger.cpp (modified) (10 diffs)
-
modules/FileLogger/FileLogger.def (modified) (1 diff)
-
modules/FileLogger/FileLogger.h (modified) (2 diffs)
-
modules/FileLogger/stdafx.h (modified) (1 diff)
-
modules/NRPEClient/NRPEClient.cpp (modified) (9 diffs)
-
modules/NRPEClient/NRPEClient.h (modified) (1 diff)
-
modules/Scheduler/Scheduler.cpp (modified) (4 diffs)
-
modules/Scheduler/Scheduler.h (modified) (1 diff)
-
modules/Scheduler/simple_scheduler.cpp (modified) (5 diffs)
-
modules/Scheduler/simple_scheduler.hpp (modified) (5 diffs)
-
service/NSClient++.cpp (modified) (4 diffs)
-
service/commands.hpp (modified) (6 diffs)
-
service/core_api.cpp (modified) (1 diff)
-
service/simple_client.hpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
build.cmake
r3080680 rc0d7e82 1 1 SET(Boost_DEBUG 1) 2 2 3 #set(Boost_USE_STATIC_LIBS ON)3 set(Boost_USE_STATIC_LIBS ON) 4 4 set(BOOST_USE_MULTITHREADED ON) 5 5 6 6 SET(BOOST_INCLUDEDIR c:/src/include/boost-1_39) 7 #SET(BOOST_LIBRARYDIR c:/src/lib/x86/)7 SET(BOOST_LIBRARYDIR c:/src/lib/x86/) 8 8 SET(BOOST_LIB_SUFFIX vc80-mt) 9 9 #SET(BOOST_LIB_VERSION 1.40) … … 17 17 SET(PROTOBUF_BINARYDIR C:/src/protobuf-2.3.0/vsprojects/Debug) 18 18 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 6 6 * Fixa dependonservice LanManWorkStation (old win) 7 7 * Fix RtlStringFromGUID problem on NT4 8 9 2010-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) 8 13 9 14 2010-01-31 MickeM -
include/NSCAPI.h
r7f9c823 rc0d7e82 87 87 88 88 // Various message Types 89 const int log = 0; // Log message89 const int log = 1; // Log message 90 90 const int error = -1; // Error (non critical) 91 91 const int critical = -10; // Critical error 92 const int warning = 1; // Warning93 const int debug = 666; // Debug message92 const int warning = 2; // Warning 93 const int debug = 10; // Debug message 94 94 95 95 typedef int messageTypes; // Message type -
include/NSCHelper.cpp
r2018659 rc0d7e82 118 118 ); 119 119 } 120 121 120 122 std::wstring NSCHelper::report::to_string(unsigned int report) { 121 123 std::wstring ret; … … 135 137 if (!ret.empty()) ret += _T(","); 136 138 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 149 unsigned 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 } 172 bool 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 183 std::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"); 137 204 } 138 205 return ret; -
include/NSCHelper.h
r920626f rc0d7e82 103 103 104 104 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 { 105 110 unsigned int parse(std::wstring str); 106 111 bool matches(unsigned int report, NSCAPI::nagiosReturn code); -
include/settings/macros.h
r3080680 rc0d7e82 429 429 DESCRIBE_SETTING_ADVANCED(DATEMASK, "DATEMASK", "The date format used when logging to a file."); 430 430 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 431 434 DEFINE_SETTING_B(DEBUG_LOG, LOG_SECTION, "debug", false); 432 435 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 741 741 742 742 #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 743 774 } 744 775 -
modules/CheckExternalScripts/CheckExternalScripts.h
r3080680 rc0d7e82 32 32 std::wstring command; 33 33 std::wstring arguments; 34 std::wstring to_string() { 35 return command + _T("(") + arguments + _T(")"); 36 } 34 37 }; 35 38 typedef std::map<std::wstring, command_data> command_list; … … 83 86 void addCommand(std::wstring key, std::wstring cmd, std::wstring args) { 84 87 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()); 86 91 } 87 92 void addAlias(std::wstring key, std::wstring cmd, std::wstring args) { 88 93 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()); 90 97 } 91 98 }; -
modules/FileLogger/FileLogger.cpp
rd5356c1 rc0d7e82 22 22 #include "stdafx.h" 23 23 #include "FileLogger.h" 24 25 #include <sys/timeb.h> 26 #include <time.h> 24 #include <boost/date_time.hpp> 25 #include <fstream> 27 26 #include <utils.h> 28 27 29 28 FileLogger 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 }36 29 37 30 FileLogger::FileLogger() : init_(false) { … … 40 33 } 41 34 35 #ifdef WIN32 42 36 #ifndef CSIDL_COMMON_APPDATA 43 37 #define CSIDL_COMMON_APPDATA 0x0023 … … 56 50 return FALSE; 57 51 } 52 #endif 58 53 59 54 std::wstring getFolder(std::wstring key) { … … 61 56 return NSCModuleHelper::getBasePath(); 62 57 } else { 58 #ifdef WIN32 63 59 if (key == _T("local-app-data")) { 64 60 TCHAR buf[MAX_PATH+1]; … … 66 62 return buf; 67 63 } 64 #endif 68 65 } 69 66 return NSCModuleHelper::getBasePath(); 70 67 } 71 std:: wstring FileLogger::getFileName() {68 std::string FileLogger::getFileName() { 72 69 if (file_.empty()) { 73 file_ = SETTINGS_GET_STRING(log::FILENAME);70 file_ = to_string(SETTINGS_GET_STRING(log::FILENAME)); 74 71 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('\\'); 79 76 if (pos != std::wstring::npos) { 80 77 //root = root.substr(0, pos); 81 78 } 82 file_ = root + _T("\\")+ file_;79 file_ = root + "\\" + file_; 83 80 } 84 81 } … … 95 92 SETTINGS_REG_KEY_S(log::FILENAME); 96 93 SETTINGS_REG_KEY_S(log::DATEMASK); 97 SETTINGS_REG_KEY_ B(log::DEBUG_LOG);94 SETTINGS_REG_KEY_S(log::LOG_MASK); 98 95 99 96 } catch (NSCModuleHelper::NSCMHExcpetion &e) { … … 104 101 105 102 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_)); 107 107 init_ = true; 108 108 std::wstring hello = _T("Starting to log for: ") + NSCModuleHelper::getApplicationName() + _T(" - ") + NSCModuleHelper::getApplicationVersionString(); 109 109 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_)); 111 111 return true; 112 112 } … … 120 120 return true; 121 121 } 122 /* 122 123 HANDLE openAppendOrNew(std::wstring file) { 123 124 DWORD numberOfBytesWritten = 0; … … 132 133 return hFile; 133 134 } 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 */ 148 136 149 137 void FileLogger::handleMessage(int msgType, TCHAR* file, int line, const TCHAR* message) { 150 138 if (!init_) { 151 std::wcout << _T("Discarding message (not initzialized yet")<< std::endl;139 std::wcout << _T("Discarding: ") << message << std::endl; 152 140 return; 153 141 } 154 TCHAR buffer[65]; 155 __time64_t ltime; 156 _time64( <ime ); 157 struct tm *today = _localtime64( <ime ); 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; 166 148 } 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; 171 154 } 172 155 156 std::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 164 NSC_WRAP_DLL(); 173 165 NSC_WRAPPERS_MAIN_DEF(gFileLogger); 174 166 NSC_WRAPPERS_HANDLE_MSG_DEF(gFileLogger); … … 176 168 NSC_WRAPPERS_HANDLE_CONFIGURATION(gFileLogger); 177 169 178 179 180 181 170 MODULE_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()209 171 MODULE_SETTINGS_END() -
modules/FileLogger/FileLogger.def
re26cfe0 rc0d7e82 13 13 NSGetConfigurationMeta 14 14 NSGetModuleDescription 15 NSDeleteBuffer -
modules/FileLogger/FileLogger.h
r9567d4b rc0d7e82 25 25 class FileLogger { 26 26 private: 27 std:: wstring file_;28 std:: wstring format_;27 std::string file_; 28 std::string format_; 29 29 bool init_; 30 int log_mask_; 30 31 31 32 public: … … 53 54 void handleMessage(int msgType, TCHAR* file, int line, const TCHAR* message); 54 55 int handleCommand(TCHAR* command, TCHAR **argument, TCHAR *returnBuffer, int returnBufferLen); 55 void writeEntry(std::wstring line);56 //void writeEntry(std::wstring line); 56 57 57 58 58 std::wstring getFileName(); 59 std::string getFileName(); 60 inline std::wstring FileLogger::get_formated_date(); 61 59 62 }; -
modules/FileLogger/stdafx.h
r7f9c823 rc0d7e82 22 22 #pragma once 23 23 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 27 28 #include <NSCAPI.h> 28 29 #include <NSCHelper.h> 29 30 #include <nsc_module_wrapper.hpp> 30 #include <string>31 #include <iostream>32 #include <fstream>33 31 34 32 #include <config.h> -
modules/NRPEClient/NRPEClient.cpp
r2018659 rc0d7e82 27 27 //#include <execute_process.hpp> 28 28 #include <strEx.h> 29 29 #include <boost/filesystem.hpp> 30 30 31 31 … … 57 57 NSC_LOG_ERROR_STD(_T("Failed to register command.")); 58 58 } 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 59 69 for (std::list<std::wstring>::const_iterator it = commands.begin(); it != commands.end(); ++it) { 60 70 NSC_DEBUG_MSG_STD(*it); … … 112 122 NSC_DEBUG_MSG_STD(_T("Added NRPE Client: ") + key.c_str() + _T(" = ") + command_data.toString()); 113 123 commands[key] = command_data; 124 125 NSCModuleHelper::registerCommand(key.c_str(), command_data.toString()); 126 114 127 } catch (boost::program_options::validation_error &e) { 115 128 NSC_LOG_ERROR_STD(_T("Could not parse: ") + key.c_str() + strEx::string_to_wstring(e.what())); … … 294 307 public: 295 308 nrpe_socket(boost::asio::io_service &io_service, std::wstring host, int port) : socket_(io_service) { 296 NSC_LOG_CRITICAL(_T("Looking up..."));297 309 tcp::resolver resolver(io_service); 298 310 tcp::resolver::query query(to_string(host), to_string(port)); … … 303 315 tcp::resolver::iterator end; 304 316 305 NSC_LOG_CRITICAL(_T("connecting..."));306 317 boost::system::error_code error = boost::asio::error::host_not_found; 307 318 while (error && endpoint_iterator != end) … … 312 323 socket_.connect(*endpoint_iterator++, error); 313 324 } 314 NSC_LOG_CRITICAL(_T("Connected..."));315 325 if (error) 316 326 throw boost::system::system_error(error); … … 342 352 NSC_LOG_CRITICAL(_T("Looking up...")); 343 353 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"); 346 356 //tcp::resolver::query query("test_server", "80"); 347 357 … … 370 380 371 381 void send(nrpe::packet &packet, boost::posix_time::seconds timeout) { 372 NSC_LOG_CRITICAL(_T("Writing..."));373 382 std::vector<char> buf(packet.get_packet_length()); 374 383 write_with_timeout(socket_, socket_.lowest_layer(), boost::asio::buffer(packet.create_buffer(), packet.get_packet_length()), timeout); 375 NSC_LOG_CRITICAL(_T("Written..."));376 384 } 377 385 nrpe::packet recv(const nrpe::packet &packet, boost::posix_time::seconds timeout) { 378 NSC_LOG_CRITICAL(_T("Reading..."));379 386 std::vector<char> buf(packet.get_packet_length()); 380 387 read_with_timeout(socket_, socket_.lowest_layer(), boost::asio::buffer(buf), timeout); 381 388 return nrpe::packet(&buf[0], buf.size(), packet.get_payload_length()); 382 NSC_LOG_CRITICAL(_T("Read..."));383 389 } 384 390 }; … … 388 394 boost::asio::ssl::context ctx(io_service, boost::asio::ssl::context::sslv23); 389 395 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_)); 391 397 ctx.set_verify_mode(boost::asio::ssl::context::verify_none); 392 398 nrpe_ssl_socket socket(io_service, ctx, host, port); -
modules/NRPEClient/NRPEClient.h
r2018659 rc0d7e82 87 87 command_list commands; 88 88 unsigned int buffer_length_; 89 std::wstring cert_; 89 90 90 91 public: -
modules/Scheduler/Scheduler.cpp
r2018659 rc0d7e82 50 50 51 51 scheduler_.set_threads(SETTINGS_GET_INT(scheduler::THREADS)); 52 NSC_DEBUG_MSG_STD(_T("Thread count: ") + to_wstring(scheduler_.get_threads())); 52 53 53 54 if (mode == NSCAPI::normalStart) { … … 73 74 74 75 } 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 */81 76 } catch (NSCModuleHelper::NSCMHExcpetion &e) { 82 77 NSC_LOG_ERROR_STD(_T("Exception in module Scheduler: ") + e.msg_); … … 106 101 item.channel = NSCModuleHelper::getSettingsString(detail_path, setting_keys::scheduler::CHANNEL, def.channel); 107 102 item.command = NSCModuleHelper::getSettingsString(detail_path, setting_keys::scheduler::COMMAND, item.command); 103 104 strEx::parse_command(item.command, item.command, item.arguments); 105 108 106 std::wstring report = NSCModuleHelper::getSettingsString(detail_path, setting_keys::scheduler::REPORT_MODE, NSCHelper::report::to_string(def.report)); 109 107 item.report = NSCHelper::report::parse(report); … … 117 115 scheduler_.stop(); 118 116 return true; 117 } 118 119 void Scheduler::on_error(std::wstring error) { 120 NSC_LOG_ERROR_STD(error); 119 121 } 120 122 -
modules/Scheduler/Scheduler.h
r1bfe6f0 rc0d7e82 42 42 scheduler::target read_defaut_schedule(std::wstring path); 43 43 void handle_schedule(scheduler::target item); 44 void on_error(std::wstring error); 44 45 45 46 std::wstring getModuleName() { -
modules/Scheduler/simple_scheduler.cpp
r6822839 rc0d7e82 2 2 3 3 #include <boost/bind.hpp> 4 4 #include <strEx.h> 5 5 #include <unicode_char.hpp> 6 6 … … 31 31 void simple_scheduler::start() { 32 32 running_ = true; 33 if (!queue_.empty()) 34 start_thread(); 33 start_thread(); 35 34 } 36 35 void simple_scheduler::stop() { 37 36 running_ = false; 38 //if (!thread_)39 // return;40 37 stop_requested_ = true; 41 38 threads_.interrupt_all(); 42 39 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 */50 40 } 51 41 … … 57 47 if (missing_threads > 0 && missing_threads <= thread_count_) { 58 48 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)); 61 51 } 62 52 } 53 threads_.create_thread(boost::bind(&simple_scheduler::watch_dog, this, 0)); 63 54 //thread_ = boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(&simple_scheduler::thread_proc, this))); 64 55 } 65 56 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) { 67 78 int iteration = 0; 68 79 schedule_queue_type::value_type instance; 69 80 while (!stop_requested_) { 70 81 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 } 73 87 74 88 try { 75 76 89 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)); 79 92 } 80 93 boost::thread::sleep((*instance).time); 81 94 } catch (boost::thread_interrupted &e) { 82 95 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)); 85 99 return; 100 } 86 101 continue; 87 102 } catch (...) { 88 103 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; 92 106 } 93 107 … … 100 114 reschedule(*item,now_time); 101 115 } catch (...) { 102 std::wcout << _T("UNKNOWN ERROR RUNING TASK: ") << std::endl;116 log_error(_T("UNKNOWN ERROR RUNING TASK: ")); 103 117 reschedule(*item); 104 118 } 105 119 } 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)); 107 121 } 108 122 } … … 120 134 instance.time = next; 121 135 if (!queue_.push(instance)) { 122 std::wcout << _T("ERROR") << std::endl;136 log_error(_T("ERROR")); 123 137 } 124 start_thread();138 idle_thread_cond_.notify_one(); 125 139 } 140 141 142 void simple_scheduler::log_error(std::wstring err) { 143 if (handler_) 144 handler_->on_error(err); 145 } 146 126 147 } 127 148 -
modules/Scheduler/simple_scheduler.hpp
r6822839 rc0d7e82 70 70 public: 71 71 virtual void handle_schedule(target item) = 0; 72 virtual void on_error(std::wstring error) = 0; 72 73 }; 73 74 struct schedule_instance { … … 129 130 schedule_queue_type queue_; 130 131 unsigned int thread_count_; 132 boost::mutex idle_thread_mutex_; 133 boost::condition_variable idle_thread_cond_; 134 131 135 132 136 … … 135 139 volatile bool running_; 136 140 boost::thread_group threads_; 137 //boost::shared_ptr<boost::thread> thread_;138 141 boost::mutex mutex_; 139 142 schedule_handler* handler_; 140 143 int error_threshold_; 141 144 public: 142 145 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) {} 144 147 ~simple_scheduler() {} 145 148 … … 163 166 start_thread(); 164 167 } 168 int get_threads() const { return thread_count_;} 165 169 166 170 167 171 private: 168 void thread_proc(); 172 void thread_proc(int id); 173 void watch_dog(int id); 169 174 170 175 void reschedule(target item); … … 172 177 void reschedule_wnext(target item, boost::posix_time::ptime next); 173 178 void start_thread(); 179 180 void log_error(std::wstring err); 174 181 175 182 inline boost::posix_time::ptime now() { -
service/NSClient++.cpp
r3080680 rc0d7e82 988 988 } 989 989 plugins_.insert(plugins_.end(), plugin); 990 commands_.add_plugin(plugin); 990 991 if (plugin->hasCommandHandler()) 991 992 commandHandlers_.insert(commandHandlers_.end(), plugin); … … 1046 1047 1047 1048 std::string args = to_string(arguments); 1049 1048 1050 boost::tokenizer<boost::escaped_list_separator<char> > tok(args, boost::escaped_list_separator<char>('\\', ' ', '\"')); 1049 1051 BOOST_FOREACH(string s, tok) … … 1107 1109 } 1108 1110 } else */{ 1111 /* 1109 1112 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 1110 1113 if (!readLock.owns_lock()) { … … 1112 1115 return NSCAPI::returnUNKNOWN; 1113 1116 } 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 } 1143 1134 } 1144 1135 } -
service/commands.hpp
r01a278b rc0d7e82 22 22 }; 23 23 24 private:25 24 typedef boost::shared_ptr<NSCPlugin> plugin_type; 26 25 typedef std::map<unsigned long,plugin_type> plugin_list_type; … … 29 28 30 29 30 private: 31 31 nsclient::logger *logger_; 32 32 plugin_list_type plugins_; … … 60 60 boost::unique_lock<boost::shared_mutex> writeLock(mutex_, boost::get_system_time() + boost::posix_time::seconds(10)); 61 61 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)); 63 63 return; 64 64 } … … 86 86 return; 87 87 } 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]; 90 93 } 91 94 … … 96 99 return _T("error: ") + command; 97 100 } 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); 99 103 if (cit == descriptions_.end()) 100 104 return _T("Command not found: ") + command; … … 120 124 if (!readLock.owns_lock()) { 121 125 log_error(__FILEW__, __LINE__, _T("Failed to get mutex: ") + command); 122 throw command_exception("Failed to get mutex t(commands::get)");126 throw command_exception("Failed to get mutex (commands::get)"); 123 127 } 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; 125 135 } 126 136 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 } 127 145 128 private: 146 inline std::wstring make_key(std::wstring key) { 147 return boost::algorithm::to_lower_copy(key); 148 } 129 149 void log_error(std::wstring file, int line, std::wstring error) { 130 150 if (logger_ != NULL) 131 151 logger_->nsclient_log_error(file, line, error); 132 152 } 153 154 inline bool have_plugin(unsigned long plugin_id) { 155 return !(plugins_.find(plugin_id) == plugins_.end()); 156 } 157 158 133 159 }; 134 160 } -
service/core_api.cpp
r2018659 rc0d7e82 279 279 try { 280 280 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; 281 284 } catch (...) { 282 285 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; 285 287 } 286 288 return NSCAPI::isSuccess; -
service/simple_client.hpp
r2018659 rc0d7e82 38 38 std::list<std::wstring> lst = core_->list_commands(); 39 39 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; 41 41 std::wcout << _T("Listing commands...Done") << std::endl; 42 42 } else if (s == _T("off") && buff == _T("debug ")) {
Note: See TracChangeset
for help on using the changeset viewer.








