Changeset 523576e in nscp
- Timestamp:
- 02/19/12 22:44:34 (15 months ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- 72eea1f
- Parents:
- c710cc7
- Files:
-
- 26 edited
-
changelog (modified) (1 diff)
-
helpers/installers/installer/Product.wxs (modified) (2 diffs)
-
include/execute_process_w32.hpp (modified) (1 diff)
-
include/nsca/nsca_packet.hpp (modified) (2 diffs)
-
include/nsca/nsca_socket.hpp (modified) (3 diffs)
-
include/nscapi/functions.hpp (modified) (1 diff)
-
include/pdh/enumerations.hpp (modified) (2 diffs)
-
include/settings/settings_ini.hpp (modified) (3 diffs)
-
include/strEx.h (modified) (1 diff)
-
modules/CheckExternalScripts/CheckExternalScripts.cpp (modified) (4 diffs)
-
modules/CheckExternalScripts/CheckExternalScripts.h (modified) (1 diff)
-
modules/CheckExternalScripts/commands.hpp (modified) (1 diff)
-
modules/CheckSystem/CheckSystem.cpp (modified) (11 diffs)
-
modules/CheckSystem/CheckSystem.def (modified) (1 diff)
-
modules/CheckSystem/CheckSystem.h (modified) (2 diffs)
-
modules/FileLogger/FileLogger.cpp (modified) (10 diffs)
-
modules/FileLogger/FileLogger.h (modified) (3 diffs)
-
modules/NSCAClient/NSCAClient.cpp (modified) (1 diff)
-
modules/Scheduler/Scheduler.cpp (modified) (1 diff)
-
modules/Scheduler/schedules.hpp (modified) (1 diff)
-
scripts/python/test_w32_system.py (modified) (3 diffs)
-
service/NSClient++.cpp (modified) (1 diff)
-
service/cli_parser.hpp (modified) (7 diffs)
-
service/logger.hpp (modified) (7 diffs)
-
version.hpp (modified) (1 diff)
-
version.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
changelog
rc710cc7 r523576e 4 4 * Fixa dependonservice LanManWorkStation (old win) 5 5 * Fix RtlStringFromGUID problem on NT4 6 7 2012-02-19 MickeM 8 * Added new unit context on command line for running simple unit test cases: 9 nscp unit --language python --script test_nsca 10 * Added dependency on Tcpip 11 * Dropped OS bit-match requirement so now you can install any version on x64 plattform. 12 * Added so you can specify multiple counters for CheckCounter and poll them all in one go (#387) 13 * Added test cases (a bit flaky) for counters. 14 * Added ${host} and ${domain} to hostname settings option which might be used to construct your own hostname. (#428) 15 Not 100% sure about domain as on my box the "hostname" is only a name ie. has no domain... 16 17 2012-02-18 MickeM 18 * Fixed so too long log messages are trunacated instead of discarded 19 * Added option to truncate logfile if to large (#358) 20 [/settings/file logger] 21 max size = 1000 22 * Added keyword to reload (settings) to reload settings store (ie. flushing the cache) (#249). 23 * Added ability to load all files in a given directory (#366) 24 25 2012-02-17 MickeM 26 * Tweaked a bit in regards to ExternalScripts so it works better and handles errors better (#367) 27 * Fixed default report mode in 0.4.0 (#290) 28 * Made NSCA Client handle timestamps like send_nsca (ie. ignoring local time) 6 29 7 30 2012-02-16 MickeM -
helpers/installers/installer/Product.wxs
r2906cda r523576e 30 30 31 31 <?if "$(var.arch)" = "x64"?> 32 <Condition Message="Win32 bit version not supported (by installer) on x64 bit platform (get x64 bit version instead)">VersionNT64</Condition>33 <?else?>34 32 <Condition Message="x64 bit version not supported (by installer) on Win32 bit platform (get Win32 bit version instead)">NOT VersionNT64</Condition> 35 33 <?endif?> … … 50 48 Type="ownProcess" Start="auto" ErrorControl="normal" Interactive="no" 51 49 Description="Monitoring agent for nagios (and others) used to respond to status queries" 52 Arguments="service --run --name [SERVICE_NAME]" /> 50 Arguments="service --run --name [SERVICE_NAME]"> 51 <ServiceDependency Id="Tcpip" /> 52 </ServiceInstall> 53 53 <ServiceControl Id="StartSWCNSCP" Name="[SERVICE_NAME]" Start="install" Stop="both" Wait="yes" Remove="uninstall" /> 54 54 <RemoveFolder Id="RemoveMenuShortcuts" Directory="ProgramMenuDir" On="uninstall" /> -
include/execute_process_w32.hpp
rc710cc7 r523576e 111 111 112 112 dwAvail = 0; 113 if (!::PeekNamedPipe(hChildOutR, NULL, 0, NULL, &dwAvail, NULL)) 114 NSC_LOG_ERROR_STD(_T("Failed to peek buffer: ") + error::lookup::last_error()); 115 if (dwAvail > 0) 113 if (::PeekNamedPipe(hChildOutR, NULL, 0, NULL, &dwAvail, NULL) && dwAvail > 0) 116 114 str += readFromFile(buffer, hChildOutR); 117 115 msg = strEx::string_to_wstring(str); -
include/nsca/nsca_packet.hpp
rb8b2e3d r523576e 165 165 } 166 166 167 void get_buffer(std::string &buffer ) const {167 void get_buffer(std::string &buffer, int servertime=0) const { 168 168 nsca::data::data_packet *data = reinterpret_cast<nsca::data::data_packet*>(&*buffer.begin()); 169 169 if (buffer.size() < get_packet_length()) … … 171 171 172 172 data->packet_version=swap_bytes::hton<int16_t>(nsca::data::version3); 173 data->timestamp=swap_bytes::hton<u_int32_t>(time); 173 if (servertime != 0) 174 data->timestamp=swap_bytes::hton<u_int32_t>(servertime); 175 else 176 data->timestamp=swap_bytes::hton<u_int32_t>(time); 174 177 data->return_code = swap_bytes::hton<int16_t>(code); 175 178 data->crc32_value= swap_bytes::hton<u_int32_t>(0); -
include/nsca/nsca_socket.hpp
rc5ec0c8 r523576e 18 18 boost::asio::io_service &io_service_; 19 19 nsca_encrypt crypt_inst; 20 int time; 20 21 public: 21 22 typedef boost::asio::basic_socket<tcp,boost::asio::stream_socket_service<tcp> > basic_socket_type; 22 23 23 24 public: 24 socket(boost::asio::io_service &io_service) : io_service_(io_service) {25 socket(boost::asio::io_service &io_service) : io_service_(io_service), time(0) { 25 26 socket_.reset(new tcp::socket(io_service_)); 26 27 } … … 72 73 } 73 74 std::string buffer = crypt_inst.get_rand_buffer(packet.get_packet_length()); 74 packet.get_buffer(buffer );75 packet.get_buffer(buffer, time); 75 76 crypt_inst.encrypt_buffer(buffer); 76 77 NSC_DEBUG_MSG(_T("Sending data: ") + strEx::itos(buffer.size())); … … 90 91 nsca::iv_packet iv_packet(std::string(buf.begin(), buf.end())); 91 92 std::string iv = iv_packet.get_iv(); 93 time = iv_packet.get_time(); 92 94 NSC_DEBUG_MSG(_T("Encrypting using when sending: ") + utf8::cvt<std::wstring>(nsca::nsca_encrypt::helpers::encryption_to_string(encryption_method)) + _T(" and ") + utf8::cvt<std::wstring>(password)); 93 95 crypt_inst.encrypt_init(password, encryption_method, iv); -
include/nscapi/functions.hpp
r0f7b655 r523576e 885 885 Plugin::Common_PerformanceData_FloatValue fval = perfData.float_value(); 886 886 887 ss << fval.value();887 ss << strEx::s::itos_non_sci(fval.value()); 888 888 if (fval.has_unit()) 889 889 ss << fval.unit(); -
include/pdh/enumerations.hpp
rf0e6036 r523576e 43 43 struct Object { 44 44 std::wstring name; 45 std::string error; 45 46 Instances instances; 46 47 Counters counters; … … 71 72 delete [] szObjectBuffer; 72 73 73 for (Objects::iterator it = ret.begin(); it != ret.end(); ++it) {74 BOOST_FOREACH(Object &o, ret) { 74 75 DWORD dwCounterBufLen = 0; 75 76 TCHAR* szCounterBuffer = NULL; 76 77 DWORD dwInstanceBufLen = 0; 77 78 TCHAR* szInstanceBuffer = NULL; 78 status = PDH::PDHFactory::get_impl()->PdhEnumObjectItems(NULL, NULL, (*it).name.c_str(), szCounterBuffer, &dwCounterBufLen, szInstanceBuffer, &dwInstanceBufLen, dwDetailLevel, 0); 79 if (!status.is_more_data()) { 80 szCounterBuffer = new TCHAR[dwCounterBufLen+1024]; 81 szInstanceBuffer = new TCHAR[dwInstanceBufLen+1024]; 79 try { 80 status = PDH::PDHFactory::get_impl()->PdhEnumObjectItems(NULL, NULL, o.name.c_str(), szCounterBuffer, &dwCounterBufLen, szInstanceBuffer, &dwInstanceBufLen, dwDetailLevel, 0); 81 if (status.is_more_data()) { 82 szCounterBuffer = new TCHAR[dwCounterBufLen+1024]; 83 szInstanceBuffer = new TCHAR[dwInstanceBufLen+1024]; 82 84 83 status = PDH::PDHFactory::get_impl()->PdhEnumObjectItems(NULL, NULL, (*it).name.c_str(), szCounterBuffer, &dwCounterBufLen, szInstanceBuffer, &dwInstanceBufLen, dwDetailLevel, 0); 84 if (status.is_error()) { 85 status = PDH::PDHFactory::get_impl()->PdhEnumObjectItems(NULL, NULL, o.name.c_str(), szCounterBuffer, &dwCounterBufLen, szInstanceBuffer, &dwInstanceBufLen, dwDetailLevel, 0); 86 if (status.is_error()) { 87 delete [] szCounterBuffer; 88 delete [] szInstanceBuffer; 89 throw PDHException(_T("PdhEnumObjectItems failed when trying to retrieve buffer for ") + o.name, status); 90 } 91 92 if (dwCounterBufLen > 0) { 93 cp=szCounterBuffer; 94 while(*cp != '\0') { 95 Counter c; 96 c.name = cp; 97 o.counters.push_back(c); 98 cp += lstrlen(cp)+1; 99 } 100 } 101 if (dwInstanceBufLen > 0) { 102 cp=szInstanceBuffer; 103 while(*cp != '\0') { 104 Instance i; 105 i.name = cp; 106 o.instances.push_back(i); 107 cp += lstrlen(cp)+1; 108 } 109 } 85 110 delete [] szCounterBuffer; 86 111 delete [] szInstanceBuffer; 87 throw PDHException(_T("PdhEnumObjectItems failed when trying to retrieve buffer for ") + (*it).name, status);88 112 } 89 90 if (dwCounterBufLen > 0) { 91 cp=szCounterBuffer; 92 while(*cp != '\0') { 93 Counter o; 94 o.name = cp; 95 (*it).counters.push_back(o); 96 cp += lstrlen(cp)+1; 97 } 98 } 99 if (dwInstanceBufLen > 0) { 100 cp=szInstanceBuffer; 101 while(*cp != '\0') { 102 Instance o; 103 o.name = cp; 104 (*it).instances.push_back(o); 105 cp += lstrlen(cp)+1; 106 } 107 } 108 delete [] szCounterBuffer; 109 delete [] szInstanceBuffer; 113 } catch (std::exception &e) { 114 o.error = e.what(); 115 } catch (...) { 116 o.error = "Exception fetching data"; 110 117 } 111 118 } -
include/settings/settings_ini.hpp
r53c1a6e r523576e 218 218 if (is_loaded_) 219 219 return; 220 if (boost::filesystem::is_directory(get_file_name())) { 221 222 boost::filesystem::wdirectory_iterator it(get_file_name()), eod; 223 224 BOOST_FOREACH(boost::filesystem::wpath const &p, std::make_pair(it, eod)) { 225 add_child(_T("ini:///") + p.string()); 226 } 227 } 220 228 if (!file_exists()) { 221 229 is_loaded_ = true; … … 249 257 if (filename_.empty()) { 250 258 filename_ = get_file_from_context(); 251 //filename_ = get_core()->get_base() / boost::filesystem::wpath(get_core()->get_boot_string(get_context(), _T("file"), _T("nsclient.ini"))); 259 if (filename_.size() > 0) { 260 if (boost::filesystem::is_regular(filename_)) { 261 } else if (boost::filesystem::is_regular(filename_.substr(1))) { 262 filename_ = filename_.substr(1); 263 } else if (boost::filesystem::is_directory(filename_)) { 264 } else if (boost::filesystem::is_directory(filename_.substr(1))) { 265 filename_ = filename_.substr(1); 266 } 267 } 252 268 get_core()->get_logger()->debug(__FILE__, __LINE__, _T("Reading INI settings from: ") + filename_); 253 269 } … … 265 281 std::wstring file = url.host + url.path; 266 282 std::wstring tmp = core->expand_path(file); 267 return boost::filesystem::is_regular(tmp); 283 if (tmp.size()>1 && tmp[0] == '/') { 284 if (boost::filesystem::is_regular(tmp) || boost::filesystem::is_directory(tmp)) 285 return true; 286 tmp = tmp.substr(1); 287 } 288 return boost::filesystem::is_regular(tmp) || boost::filesystem::is_directory(tmp); 268 289 } 269 290 -
include/strEx.h
rf33c12f r523576e 271 271 return ss.str(); 272 272 } 273 inline std::string itos_non_sci(double i) { 274 std::stringstream ss; 275 if (i < 10) 276 ss.precision(20); 277 ss << std::noshowpoint << std::fixed << i; 278 std::string s = ss.str(); 279 std::string::size_type pos = s.find_last_not_of('0'); 280 if (pos == std::wstring::npos) 281 return s; 282 if (s[pos] != '.') 283 pos++; 284 return s.substr(0, pos); 285 } 273 286 } 274 287 -
modules/CheckExternalScripts/CheckExternalScripts.cpp
rc710cc7 r523576e 81 81 82 82 settings.alias().add_path_to_settings() 83 84 (_T("wrappings"), sh::wstring_map_path(&wrappings_) 85 , _T("EXTERNAL SCRIPT WRAPPINGS SECTION"), _T("A list of templates for wrapped scripts")) 86 87 ; 88 89 settings.register_all(); 90 settings.notify(); 91 settings.clear(); 92 93 settings.alias().add_path_to_settings() 83 94 (_T("EXTERNAL SCRIPT SECTION"), _T("Section for external scripts configuration options (CheckExternalScripts).")) 84 95 … … 86 97 _T("EXTERNAL SCRIPT SCRIPT SECTION"), _T("A list of scripts available to run from the CheckExternalScripts module. Syntax is: <command>=<script> <arguments>")) 87 98 88 (_T("wrappings"), sh::wstring_map_path(&wrappings_)89 , _T("EXTERNAL SCRIPT WRAPPINGS SECTION"), _T("A list of templates for wrapped scripts"))90 91 99 (_T("alias"), sh::fun_values_path(boost::bind(&CheckExternalScripts::add_alias, this, _1, _2)), 92 100 _T("EXTERNAL SCRIPT ALIAS SECTION"), _T("A list of aliases available. An alias is an internal command that has been \"wrapped\" (to add arguments). Be careful so you don't create loops (ie check_loop=check_a, check_a=check_loop)")) 101 102 (_T("wrapped scripts"), sh::fun_values_path(boost::bind(&CheckExternalScripts::add_wrapping, this, _1, _2)), 103 _T("EXTERNAL SCRIPT WRAPPED SCRIPTS SECTION"), _T("A list of wrappped scripts (ie. using the template mechanism)")) 93 104 94 105 ; … … 108 119 ; 109 120 110 settings.register_all();111 settings.notify();112 settings.clear();113 114 settings.alias().add_path_to_settings()115 116 (_T("wrapped scripts"), sh::fun_values_path(boost::bind(&CheckExternalScripts::add_wrapping, this, _1, _2)),117 _T("EXTERNAL SCRIPT WRAPPED SCRIPTS SECTION"), _T("A list of wrappped scripts (ie. using the template mechanism)"))118 119 ;120 121 settings.register_all(); 121 122 settings.notify(); … … 201 202 } 202 203 } 204 } else if (!allowArgs_ && data.args.size() > 0) { 205 NSC_LOG_ERROR_STD(_T("Arguments not allowed in CheckExternalScripts set /settings/external scripts/allow arguments=true")) 203 206 } 204 207 -
modules/CheckExternalScripts/CheckExternalScripts.h
rc710cc7 r523576e 90 90 91 91 std::wstring tpl = wrappings_[type]; 92 if (tpl.empty() && type == _T("vbs")) 93 tpl = _T("cscript.exe //T:30 //NoLogo scripts\\lib\\wrapper.vbs %SCRIPT% %ARGS%"); 94 else if (tpl.empty() && type == _T("ps1")) 95 tpl = _T("cmd /c echo scripts\\%SCRIPT% %ARGS%; exit($lastexitcode) | powershell.exe -command -"); 96 else if (tpl.empty() && type == _T("bat")) 97 tpl = _T("scripts\\%SCRIPT% %ARGS%"); 98 if (tpl.empty()) { 99 NSC_LOG_ERROR(_T("Failed to find wrapping for type: ") + type); 100 } else { 101 strEx::replace(tpl, _T("%SCRIPT%"), tok.first); 102 strEx::replace(tpl, _T("%ARGS%"), tok.second); 92 103 93 strEx::replace(tpl, _T("%SCRIPT%"), tok.first); 94 strEx::replace(tpl, _T("%ARGS%"), tok.second); 95 96 add_command(key,tpl); 104 add_command(key,tpl); 105 } 97 106 } 98 107 }; -
modules/CheckExternalScripts/commands.hpp
rc710cc7 r523576e 89 89 NSC_LOG_ERROR(_T("Failed to parse arguments for command '") + alias + _T("', using old split string method: ") + utf8::to_unicode(e.what())); 90 90 strEx::splitList list = strEx::splitEx(str, _T(" ")); 91 if (list.size() > 0) { 92 command = list.front(); 93 list.pop_front(); 94 } 95 arguments.clear(); 91 96 BOOST_FOREACH(std::wstring s, list) { 92 97 arguments.push_back(s); -
modules/CheckSystem/CheckSystem.cpp
rc5ec0c8 r523576e 205 205 } 206 206 207 int CheckSystem::commandLineExec(const wchar_t* command,const unsigned int argLen,wchar_t** args) {208 if (command == NULL) {207 int CheckSystem::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 208 if (command == _T("help")) { 209 209 std::wcerr << _T("Usage: ... CheckSystem <command>") << std::endl; 210 210 std::wcerr << _T("Commands: debugpdh, listpdh, pdhlookup, pdhmatch, pdhobject") << std::endl; 211 211 return -1; 212 212 } 213 if ( _wcsicmp(command, _T("debugpdh")) == 0) {213 if (command == _T("debugpdh")) { 214 214 PDH::Enumerations::Objects lst; 215 215 try { … … 334 334 } 335 335 } 336 } else if (_wcsicmp(command, _T("listpdh")) == 0) { 336 } else if (command == _T("listpdh")) { 337 bool porcelain = arguments.size() > 0 && arguments.front() == _T("--porcelain"); 337 338 PDH::Enumerations::Objects lst; 338 339 try { 339 340 lst = PDH::Enumerations::EnumObjects(); 340 341 } catch (const PDH::PDHException e) { 341 std::wcout << _T("Service enumeration failed: ") << e.getError(); 342 return 0; 343 } 344 for (PDH::Enumerations::Objects::iterator it = lst.begin();it!=lst.end();++it) { 345 if ((*it).instances.size() > 0) { 346 for (PDH::Enumerations::Instances::const_iterator it2 = (*it).instances.begin();it2!=(*it).instances.end();++it2) { 347 for (PDH::Enumerations::Counters::const_iterator it3 = (*it).counters.begin();it3!=(*it).counters.end();++it3) { 348 std::wcout << _T("\\") << (*it).name << _T("(") << (*it2).name << _T(")\\") << (*it3).name << std::endl;; 342 result = _T("ERROR: Service enumeration failed: ") + e.getError(); 343 return NSCAPI::returnUNKNOWN; 344 } 345 std::wstringstream ss; 346 BOOST_FOREACH(PDH::Enumerations::Object &obj, lst) { 347 if (!obj.error.empty()) { 348 ss << "error," << obj.name << "," << utf8::to_unicode(obj.error) << _T("\n"); 349 } else if (obj.instances.size() > 0) { 350 BOOST_FOREACH(const PDH::Enumerations::Instance &inst, obj.instances) { 351 BOOST_FOREACH(const PDH::Enumerations::Counter &count, obj.counters) { 352 if (porcelain) 353 ss << "counter," << obj.name << _T(",") << inst.name << _T(",") << count.name << _T("\n"); 354 else 355 ss << _T("\\") << obj.name << _T("(") << inst.name << _T(")\\") << count.name << _T("\n"); 349 356 } 350 357 } 351 358 } else { 352 for (PDH::Enumerations::Counters::const_iterator it2 = (*it).counters.begin();it2!=(*it).counters.end();++it2) { 353 std::wcout << _T("\\") << (*it).name << _T("\\") << (*it2).name << std::endl;; 359 BOOST_FOREACH(const PDH::Enumerations::Counter &count, obj.counters) { 360 if (porcelain) 361 ss << obj.name << _T(",") << count.name << _T("\n"); 362 else 363 ss << _T("\\") << obj.name << _T("\\") << count.name << _T("\n"); 354 364 } 355 365 } 356 366 } 357 } else if (_wcsicmp(command, _T("pdhlookup")) == 0) { 367 result = ss.str(); 368 return NSCAPI::returnOK; 369 } else if (command == _T("pdhlookup")) { 358 370 try { 359 std::wstring name = array_buffer::arrayBuffer2string(args, argLen, _T(" ")); 360 if (name.empty()) { 371 if (arguments.size() == 0) { 361 372 NSC_LOG_ERROR_STD(_T("Need to specify counter index name!")); 362 373 return 0; 363 374 } 375 std::wstring name = arguments.front(); 364 376 DWORD dw = PDH::PDHResolver::lookupIndex(name); 365 377 NSC_LOG_MESSAGE_STD(_T("--+--[ Lookup Result ]----------------------------------------")); … … 370 382 return 0; 371 383 } 372 } else if ( _wcsicmp(command, _T("pdhmatch")) == 0) {384 } else if (command == _T("pdhmatch")) { 373 385 try { 374 std::wstring name = array_buffer::arrayBuffer2string(args, argLen, _T(" ")); 375 if (name.empty()) { 376 NSC_LOG_ERROR_STD(_T("Need to specify counter pattern!")); 386 if (arguments.size() == 0) { 387 NSC_LOG_ERROR_STD(_T("Need to specify counter index name!")); 377 388 return 0; 378 389 } 390 std::wstring name = arguments.front(); 379 391 std::list<std::wstring> list = PDH::PDHResolver::PdhExpandCounterPath(name.c_str()); 380 392 NSC_LOG_MESSAGE_STD(_T("--+--[ Lookup Result ]----------------------------------------")); … … 387 399 return 0; 388 400 } 389 } else if ( _wcsicmp(command, _T("pdhobject")) == 0) {401 } else if (command == _T("pdhobject")) { 390 402 try { 391 std::wstring name = array_buffer::arrayBuffer2string(args, argLen, _T(" ")); 392 if (name.empty()) { 393 NSC_LOG_ERROR_STD(_T("Need to specify counter pattern!")); 403 if (arguments.size() == 0) { 404 NSC_LOG_ERROR_STD(_T("Need to specify counter index name!")); 394 405 return 0; 395 406 } 407 std::wstring name = arguments.front(); 396 408 PDH::Enumerations::pdh_object_details list = PDH::Enumerations::EnumObjectInstances(name.c_str()); 397 409 NSC_LOG_MESSAGE_STD(_T("--+--[ Lookup Result ]----------------------------------------")); … … 1195 1207 } 1196 1208 1209 template<class T> 1210 class PerfDataContainer : public checkHolders::CheckContainer<T> { 1211 private: 1212 typedef PDHCollectors::StaticPDHCounterListener<double, PDH_FMT_DOUBLE> counter_type; 1213 typedef boost::shared_ptr<counter_type> ptr_lsnr_type; 1214 ptr_lsnr_type cDouble; 1215 public: 1216 1217 PerfDataContainer() : CheckContainer<T>() {} 1218 1219 PerfDataContainer(const PerfDataContainer &other) : CheckContainer<T>(other), cDouble(other.cDouble) {} 1220 const PerfDataContainer& operator =(const PerfDataContainer &other) { 1221 *((CheckContainer<T>*)this) = other; 1222 cDouble = other.cDouble; 1223 return *this; 1224 } 1225 ptr_lsnr_type get_listener() { 1226 if (!cDouble) 1227 cDouble = ptr_lsnr_type(new counter_type()); 1228 return cDouble; 1229 } 1230 }; 1197 1231 1198 1232 /** … … 1210 1244 NSCAPI::nagiosReturn CheckSystem::checkCounter(std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) 1211 1245 { 1212 typedef checkHolders::CheckContainer<checkHolders::MaxMinBoundsDouble> CounterContainer;1246 typedef PerfDataContainer<checkHolders::MaxMinBoundsDouble> CounterContainer; 1213 1247 1214 1248 if (arguments.empty()) { … … 1260 1294 return NSCAPI::returnUNKNOWN; 1261 1295 } 1262 1263 for (std::list<CounterContainer>::const_iterator cit = counters.begin(); cit != counters.end(); ++cit) { 1264 CounterContainer counter = (*cit); 1296 PDH::PDHQuery pdh; 1297 1298 bool has_counter = false; 1299 BOOST_FOREACH(CounterContainer &counter, counters) { 1265 1300 try { 1266 1267 1268 double value = 0;1269 1301 if (counter.data.find('\\') == std::wstring::npos) { 1270 PDHCollector *pObject = pdhThread.getThread(); 1271 if (!pObject) { 1272 msg = _T("ERROR: PDH Collection thread not running."); 1273 return NSCAPI::returnUNKNOWN; 1274 } 1302 /* 1275 1303 value = pObject->get_double(counter.data); 1276 1304 if (value == -1) { … … 1278 1306 return NSCAPI::returnUNKNOWN; 1279 1307 } 1308 */ 1280 1309 } else { 1281 1310 std::wstring tstr; … … 1293 1322 } 1294 1323 } 1295 PDH::PDHQuery pdh;1296 typedef boost::shared_ptr<PDHCollectors::StaticPDHCounterListener<double, PDH_FMT_DOUBLE> > ptr_lsnr_type;1297 ptr_lsnr_type cDouble(new PDHCollectors::StaticPDHCounterListener<double, PDH_FMT_DOUBLE>());1298 //boost::shared_ptr<PDHCollectors::StaticPDHCounterListener<double, PDH_FMT_DOUBLE> > cDouble;1299 1324 if (!extra_format.empty()) { 1300 1325 boost::char_separator<wchar_t> sep(_T(",")); … … 1313 1338 } 1314 1339 } 1315 c Double->set_extra_format(flags);1340 counter.get_listener()->set_extra_format(flags); 1316 1341 } 1317 pdh.addCounter(counter.data, cDouble); 1318 pdh.open(); 1319 if (bCheckAverages) { 1320 pdh.collect(); 1321 Sleep(1000); 1342 pdh.addCounter(counter.data, counter.get_listener()); 1343 has_counter = true; 1344 } 1345 } catch (const PDH::PDHException e) { 1346 NSC_LOG_ERROR_STD(_T("ERROR: ") + e.getError() + _T(" (") + counter.getAlias() + _T("|") + counter.data + _T(")")); 1347 if (bNSClient) 1348 msg = _T("0"); 1349 else 1350 msg = static_cast<std::wstring>(_T("ERROR: ")) + e.getError()+ _T(" (") + counter.getAlias() + _T("|") + counter.data + _T(")"); 1351 return NSCAPI::returnUNKNOWN; 1352 } 1353 } 1354 if (has_counter) { 1355 try { 1356 pdh.open(); 1357 if (bCheckAverages) { 1358 pdh.collect(); 1359 Sleep(1000); 1360 } 1361 pdh.collect(); 1362 pdh.gatherData(); 1363 pdh.close(); 1364 } catch (const PDH::PDHException e) { 1365 NSC_LOG_ERROR_STD(_T("ERROR: ") + e.getError()); 1366 if (bNSClient) 1367 msg = _T("0"); 1368 else 1369 msg = _T("ERROR: ") + e.getError(); 1370 return NSCAPI::returnUNKNOWN; 1371 } 1372 } 1373 BOOST_FOREACH(CounterContainer &counter, counters) { 1374 try { 1375 double value = 0; 1376 if (counter.data.find('\\') == std::wstring::npos) { 1377 PDHCollector *pObject = pdhThread.getThread(); 1378 if (!pObject) { 1379 msg = _T("ERROR: PDH Collection thread not running."); 1380 return NSCAPI::returnUNKNOWN; 1322 1381 } 1323 pdh.gatherData(); 1324 pdh.close(); 1325 value = cDouble->getValue(); 1326 } 1327 1382 value = pObject->get_double(counter.data); 1383 if (value == -1) { 1384 msg = _T("ERROR: Failed to get counter value: ") + counter.data; 1385 return NSCAPI::returnUNKNOWN; 1386 } 1387 } else { 1388 value = counter.get_listener()->getValue(); 1389 } 1328 1390 1329 1391 if (bNSClient) { … … 1752 1814 NSC_WRAPPERS_IGNORE_MSG_DEF(); 1753 1815 NSC_WRAPPERS_HANDLE_CMD_DEF(); 1754 //NSC_WRAPPERS_CLI_DEF(gCheckSystem); 1755 1816 NSC_WRAPPERS_CLI_DEF(); -
modules/CheckSystem/CheckSystem.def
r291548e r523576e 13 13 NSUnloadModule 14 14 NSGetModuleDescription 15 NSCommandLineExec 15 16 NSDeleteBuffer -
modules/CheckSystem/CheckSystem.h
ra14aa07 r523576e 28 28 NSC_WRAPPERS_CLI(); 29 29 30 class CheckSystem : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin {30 class CheckSystem : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec { 31 31 private: 32 32 CheckMemory memoryChecker; … … 77 77 bool hasMessageHandler(); 78 78 NSCAPI::nagiosReturn handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf); 79 int commandLineExec(const wchar_t* command,const unsigned int argLen,wchar_t** args);79 NSCAPI::nagiosReturn commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result); 80 80 81 81 NSCAPI::nagiosReturn checkCPU(std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf); -
modules/FileLogger/FileLogger.cpp
r8013c0c r523576e 27 27 28 28 #include <boost/date_time.hpp> 29 #include <boost/filesystem.hpp> 29 30 30 31 #include "FileLogger.h" … … 34 35 #include <settings/client/settings_client.hpp> 35 36 #include <settings/macros.h> 37 #include <protobuf/plugin.pb.h> 36 38 37 39 namespace sh = nscapi::settings_helper; … … 40 42 } 41 43 FileLogger::~FileLogger() { 42 }43 namespace setting_keys {44 45 namespace log {46 DEFINE_PATH(SECTION, LOG_SECTION);47 //DESCRIBE_SETTING_ADVANCED(SECTION, "LOG SECTION", "Configure loggning properties.");48 49 DEFINE_SETTING_S(FILENAME, LOG_SECTION, "file", "nsclient.log");50 //DESCRIBE_SETTING_ADVANCED(FILENAME, "SYNTAX", "The file to write log data to. If no directory is used this is relative to the NSClient++ binary.");51 52 DEFINE_SETTING_S(ROOT, LOG_SECTION, "root", "auto");53 //DESCRIBE_SETTING_ADVANCED(ROOT, "TODO", "TODO");54 55 DEFINE_SETTING_S(DATEMASK, LOG_SECTION, "date format", "%Y-%m-%d %H:%M:%S");56 //DESCRIBE_SETTING_ADVANCED(DATEMASK, "DATEMASK", "The date format used when logging to a file.");57 58 DEFINE_SETTING_S(LOG_MASK, LOG_SECTION, "log mask", "normal");59 //DESCRIBE_SETTING_ADVANCED(LOG_MASK, "LOG MASK", "The log mask information, error, warning, critical, debug");60 61 DEFINE_SETTING_B(DEBUG_LOG, LOG_SECTION, "debug", false);62 //DESCRIBE_SETTING_ADVANCED(DEBUG_LOG, "DEBUG LOGGING", "Enable debug logging can help track down errors and find problems but will impact overall perfoamnce negativly.");63 }64 44 } 65 45 … … 101 81 file_ = utf8::cvt<std::string>(cfg_file_); 102 82 if (file_.empty()) 103 file_ = utf8::cvt<std::string>(setting_keys::log::FILENAME_DEFAULT);83 file_ = "nsclient.log"; 104 84 if (file_.find("\\") == std::wstring::npos) { 105 85 std::string root = utf8::cvt<std::string>(getFolder(cfg_root_)); … … 123 103 124 104 sh::settings_registry settings(get_settings_proxy()); 125 settings.set_alias(_T(" log"), alias);105 settings.set_alias(_T("file logger"), alias); 126 106 127 107 settings.alias().add_path_to_settings() … … 129 109 ; 130 110 131 settings.alias().add_key_to_settings() 132 //(_T("debug"), sh::bool_key(&debug_, false), 133 //_T("DEBUG LOGGING"), _T("Enable debug logging can help track down errors and find problems but will impact overall performance negativly.")) 134 135 (_T("log mask"), sh::wstring_key(&log_mask, _T("false")), 136 _T("LOG MASK"), _T("The log mask information, error, warning, critical, debug")) 111 settings.alias().add_parent(_T("/settings/log")).add_key_to_settings() 112 (_T("level"), sh::wstring_key(&log_mask, _T("info")), 113 _T("LOG LEVEL"), _T("The log level info, error, warning, critical, debug")) 137 114 138 115 (_T("root"), sh::wstring_key(&cfg_root_, _T("auto")), … … 144 121 (_T("date format"), sh::string_key(&format_, "%Y-%m-%d %H:%M:%S"), 145 122 _T("DATEMASK"), _T("The size of the buffer to use when getting messages this affects the speed and maximum size of messages you can recieve.")) 123 124 (_T("max size"), sh::int_key(&max_size_, 0), 125 _T("MAXIMUM FILE SIZE"), _T("When file size reaches this it will be truncated to 50% if set to 0 (default) truncation will be disabled")) 146 126 ; 147 127 … … 166 146 init_ = true; 167 147 std::string hello = "Starting to log for: " + utf8::cvt<std::string>(GET_CORE()->getApplicationName()) + " - " + utf8::cvt<std::string>(GET_CORE()->getApplicationVersionString()); 168 handleMessage(NSCAPI::log_level::log, __FILE__, __LINE__, hello); 169 handleMessage(NSCAPI::log_level::log, __FILE__, __LINE__, "Log path is: " + file_); 148 log(NSCAPI::log_level::log, __FILE__, __LINE__, hello); 170 149 return true; 171 150 } … … 202 181 */ 203 182 204 void FileLogger::handleMessage(int msgType, const std::string file, int line, std::string message) { 183 void FileLogger::handleMessageRAW(std::string data) { 184 try { 185 Plugin::LogEntry message; 186 message.ParseFromString(data); 187 188 for (int i=0;i<message.entry_size();i++) { 189 Plugin::LogEntry::Entry msg = message.entry(i); 190 log(msg.level(), msg.file(), msg.line(), msg.message()); 191 } 192 } catch (std::exception &e) { 193 std::cout << "Failed to parse data from: " << strEx::strip_hex(data) << e.what() << std::endl;; 194 } catch (...) { 195 std::cout << "Failed to parse data from: " << strEx::strip_hex(data) << std::endl;; 196 } 197 } 198 void FileLogger::log(int msgType, const std::string file, int line, std::string message) { 205 199 if (!init_) { 206 200 std::wcout << _T("Discarding: ") << utf8::cvt<std::wstring>(message) << std::endl; … … 210 204 return; 211 205 206 if (max_size_ != 0 && boost::filesystem::exists(file_.c_str()) && boost::filesystem::file_size(file_.c_str()) > max_size_) { 207 int target_size = max_size_*0.7; 208 char *tmpBuffer = new char[target_size+1]; 209 try { 210 std::ifstream ifs(file_.c_str()); 211 ifs.seekg(-target_size, std::ios_base::end); // One call to find it. . . 212 ifs.read(tmpBuffer, target_size); 213 ifs.close(); 214 std::ofstream ofs(file_.c_str(), std::ios::trunc); 215 ofs.write(tmpBuffer, target_size); 216 } catch (...) { 217 std::cout << "Failed to truncate log file: " << file_ << std::endl;; 218 } 219 delete [] tmpBuffer; 220 } 212 221 std::ofstream stream(file_.c_str(), std::ios::out|std::ios::app|std::ios::ate); 213 222 if (!stream) { -
modules/FileLogger/FileLogger.h
r81e420c r523576e 25 25 NSC_WRAPPERS_MAIN(); 26 26 27 class FileLogger : public nscapi::impl::simple_plugin , public nscapi::impl::simple_log_handler{27 class FileLogger : public nscapi::impl::simple_plugin { 28 28 private: 29 29 std::string file_; … … 33 33 std::wstring cfg_file_; 34 34 std::wstring cfg_root_; 35 int max_size_; 35 36 36 37 public: … … 57 58 bool hasCommandHandler(); 58 59 bool hasMessageHandler(); 59 void handleMessage(int msgType, const std::string file, int line, std::string message); 60 void handleMessageRAW(std::string data); 61 void log(int msgType, const std::string file, int line, std::string message); 60 62 int handleCommand(wchar_t* command, wchar_t **argument, wchar_t *returnBuffer, int returnBufferLen); 61 63 //void writeEntry(std::wstring line); -
modules/NSCAClient/NSCAClient.cpp
r0f7b655 r523576e 100 100 if (hostname_ == "auto") { 101 101 hostname_ = boost::asio::ip::host_name(); 102 } else { 103 std::pair<std::string,std::string> dn = strEx::split<std::string>(boost::asio::ip::host_name(), "."); 104 105 try { 106 boost::asio::io_service svc; 107 boost::asio::ip::tcp::resolver resolver (svc); 108 boost::asio::ip::tcp::resolver::query query (boost::asio::ip::host_name(), ""); 109 boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve (query), end; 110 111 std::string s; 112 while (iter != end) { 113 s += iter->host_name(); 114 s += " - "; 115 s += iter->endpoint().address().to_string(); 116 iter++; 117 } 118 } catch (exception& e) { 119 cerr << "Error: " << e.what() << endl; 120 } 121 122 123 strEx::replace(hostname_, "${host}", dn.first); 124 strEx::replace(hostname_, "${domain}", dn.second); 102 125 } 103 126 } catch (nscapi::nscapi_exception &e) { -
modules/Scheduler/Scheduler.cpp
r76540c3 r523576e 130 130 std::string result; 131 131 get_core()->submit_message(item.channel, response, result); 132 } else { 133 NSC_DEBUG_MSG(_T("Filter not matched for: ") + item.alias + _T(" so nothing is reported")); 132 134 } 133 135 } catch (nscapi::nscapi_exception &e) { -
modules/Scheduler/schedules.hpp
r76540c3 r523576e 110 110 static void read_object(boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object) { 111 111 object.set_command(object.value); 112 if (object.alias == _T("default")) 112 if (object.alias == _T("default")) { 113 113 object.set_duration(_T("5m")); 114 object.set_report(_T("all")); 115 } 114 116 115 117 nscapi::settings_helper::settings_registry settings(proxy); -
scripts/python/test_w32_system.py
r0f7b655 r523576e 1 from NSCP import Settings, Registry, Core, log, status, log_error, sleep 1 2 from NSCP import Settings, Registry, Core, log, status, log_error, sleep 2 3 from test_helper import BasicTest, TestResult, Callable, setup_singleton, install_testcases, init_testcases, shutdown_testcases 3 4 from types import * 5 import random 4 6 import subprocess 5 7 … … 47 49 return result 48 50 49 def test_one_proc(self):51 def run_test_proc(self): 50 52 result = TestResult('Checking CheckProcState') 51 53 … … 67 69 return result 68 70 71 def run_test_counters(self): 72 result = TestResult('Checking CheckCounter') 73 (result_code, result_message) = core.simple_exec('any', 'listpdh', ['--porcelain']) 74 count = 0 75 data = [] 76 for m in result_message: 77 data = m.splitlines() 78 count = len(data) 79 result.add_message(count > 0, 'Managed to retrieve counters: %d'%count) 80 81 counters = [] 82 for x in range(1,10): 83 str = random.choice(data) 84 lst = str.split(',') 85 found = True 86 if len(lst) == 4: 87 counter = '\\%s(%s)\\%s'%(lst[1], lst[2], lst[3]) 88 elif len(lst) == 3: 89 counter = '\\%s\\%s'%(lst[1], lst[2]) 90 else: 91 result.add_message(False, 'Invalid counter found: %s'%lst) 92 found = False 93 if found: 94 (retcode, retmessage, retperf) = core.simple_query('CheckCounter', ['ShowAll', 'MaxWarn=10', 'Counter:001=%s'%counter]) 95 result.add_message(retcode != status.UNKNOWN, 'Queried normal: %s'%counter) 96 result.add_message(len(retmessage) > 0, 'Queried normal (got message): %s'%retmessage) 97 result.add_message(len(retperf) > 0, 'Queried normal (got perf): %s'%retperf) 98 if retcode != status.UNKNOWN: 99 counters.append('Counter:%d=%s'%(x, counter)) 100 101 args = ['ShowAll', 'MaxWarn=10'] 102 args.extend(counters) 103 (retcode, retmessage, retperf) = core.simple_query('CheckCounter', args) 104 result.add_message(retcode != status.UNKNOWN, 'Queried normal list of %d counters'%len(counters)) 105 result.add_message(len(retmessage) > 0, 'Queried normal (got message): %s'%retmessage) 106 result.add_message(len(retperf) > 0, 'Queried normal (got perf): %s'%retperf) 107 result.add_message(len(counters) == len(retperf.split(' ')), 'Got all responses: %d'%len(counters)) 108 return result 109 69 110 def run_test(self): 70 result = TestResult('Testing process checks on windows 32 bit systems') 71 result.extend(self.test_one_proc()) 111 result = TestResult('Testing W32 systems') 112 #result.add(self.run_test_proc()) 113 result.add(self.run_test_counters()) 72 114 return result 73 115 -
service/NSClient++.cpp
re396b2f r523576e 954 954 955 955 NSCAPI::errorReturn NSClientT::reload(const std::wstring module) { 956 if (module == _T("service")) { 956 if (module == _T("settings")) { 957 try { 958 settings_manager::get_settings()->clear_cache(); 959 return NSCAPI::isSuccess; 960 } catch(const std::exception &e) { 961 LOG_ERROR_CORE_STD(_T("Exception raised when reloading: ") + utf8::to_unicode(e.what())); 962 } catch(...) { 963 LOG_ERROR_CORE_STD(_T("Exception raised when reloading: UNKNOWN")); 964 } 965 } else if (module == _T("service")) { 957 966 try { 958 967 stop_unload_plugins_pre(); -
service/cli_parser.hpp
rc5ec0c8 r523576e 14 14 po::options_description client; 15 15 po::options_description common; 16 po::options_description unittest; 16 17 17 18 bool help; … … 28 29 , service("Service Options") 29 30 , client("Client Options") 31 , unittest("Unittest Options") 30 32 , help(false) 31 33 , version(false) … … 33 35 root.add_options() 34 36 ("help", po::bool_switch(&help), "produce help message") 35 /*36 ("settings-help", "Produce help message for the various settings related options")37 ("service-help", "Produce help message for the various settings related service management")38 ("client-help", "Produce help message for the various settings related client")39 ("test-help", "Produce help message for the various settings related client")40 */41 /*42 ("settings", "Enter settings mode and handle settings related commands")43 ("service", "Enter service mode and handle service related commands")44 ("client", "Enter client mode and handle client related commands")45 ("test", "Start test and debug mode")46 */47 37 ("version", po::bool_switch(&version), "Show version information") 48 38 ; … … 88 78 ("raw-argument", po::wvalue<std::vector<std::wstring> >(), "List of arguments (does not get -- prefixed)") 89 79 ; 80 unittest.add_options() 81 ("language,l", po::value<std::wstring>()->implicit_value(_T("")), "Language tests are written in") 82 ("argument,a", po::wvalue<std::vector<std::wstring> >(), "List of arguments (gets -- prefixed automatically)") 83 ("raw-argument", po::wvalue<std::vector<std::wstring> >(), "List of arguments (does not get -- prefixed)") 84 ; 90 85 91 86 } … … 120 115 handlers["test"] = boost::bind(&cli_parser::parse_test, this, _1, _2); 121 116 handlers["help"] = boost::bind(&cli_parser::parse_help, this, _1, _2); 117 handlers["unit"] = boost::bind(&cli_parser::parse_unittest, this, _1, _2); 122 118 return handlers; 123 119 } … … 340 336 } 341 337 342 int parse_client(int argc, wchar_t* argv[], std::wstring module = _T("")) { 343 try { 344 po::options_description all("Allowed options (client)"); 345 all.add(common).add(client); 346 347 po::positional_options_description p; 348 p.add("arguments", -1); 349 350 po::variables_map vm; 351 po::wparsed_options parsed = 352 po::wcommand_line_parser(argc, argv).options(all).allow_unregistered().run(); 353 po::store(parsed, vm); 354 po::notify(vm); 355 356 if (process_common_options("client", all)) 357 return 1; 358 359 std::wstring command, combined_query; 360 enum modes { exec, query, submit, none, combined}; 361 modes mode = none; 362 363 if (vm.count("exec")) { 364 command = vm["exec"].as<std::wstring>(); 365 mode = exec; 366 if (vm.count("query")) { 367 combined_query = vm["query"].as<std::wstring>(); 368 mode = combined; 369 } 370 } else if (vm.count("query")) { 371 command = vm["query"].as<std::wstring>(); 372 mode = query; 373 } else if (vm.count("submit")) { 374 command = vm["submit"].as<std::wstring>(); 375 mode = submit; 376 } 377 378 if (vm.count("module")) 379 module = vm["module"].as<std::wstring>(); 380 381 bool boot = false; 382 if (vm.count("boot")) 383 boot = true; 384 385 std::vector<std::wstring> kvp_args; 386 if (vm.count("argument")) 387 kvp_args = vm["argument"].as<std::vector<std::wstring> >(); 388 389 std::vector<std::wstring> arguments = po::collect_unrecognized(parsed.options, po::include_positional); 390 391 BOOST_FOREACH(std::wstring s, kvp_args) { 392 std::wstring::size_type pos = s.find(L'='); 393 if (pos == std::wstring::npos) 394 arguments.push_back(_T("--") + s); 395 else { 396 arguments.push_back(_T("--") + s.substr(0,pos)); 397 arguments.push_back(s.substr(pos+1)); 398 } 399 } 400 401 if (vm.count("raw-argument")) 402 kvp_args = vm["raw-argument"].as<std::vector<std::wstring> >(); 403 BOOST_FOREACH(std::wstring s, kvp_args) { 404 std::wstring::size_type pos = s.find(L'='); 405 if (pos == std::wstring::npos) 406 arguments.push_back(s); 407 else { 408 arguments.push_back(s.substr(0,pos)); 409 arguments.push_back(s.substr(pos+1)); 410 } 411 } 412 338 struct client_arguments { 339 std::wstring command, combined_query, module; 340 std::vector<std::wstring> arguments; 341 enum modes { exec, query, submit, none, combined}; 342 modes mode; 343 bool boot; 344 client_arguments() : mode(none), boot(false) {} 345 346 void debug() { 413 347 if (mainClient.should_log(NSCAPI::log_level::debug)) { 414 348 mainClient.log_info(__FILE__, __LINE__, _T("Module: ") + module); 415 349 mainClient.log_info(__FILE__, __LINE__, _T("Command: ") + command); 350 mainClient.log_info(__FILE__, __LINE__, _T("Extra Query: ") + combined_query); 416 351 mainClient.log_info(__FILE__, __LINE__, _T("Mode: ") + strEx::itos(mode)); 417 352 mainClient.log_info(__FILE__, __LINE__, _T("Boot: ") + strEx::itos(boot)); … … 424 359 } 425 360 361 } 362 }; 363 int parse_client(int argc, wchar_t* argv[], std::wstring module_ = _T("")) { 364 try { 365 client_arguments args; 366 367 args.module = module_; 368 po::options_description all("Allowed options (client)"); 369 all.add(common).add(client); 370 371 po::positional_options_description p; 372 p.add("arguments", -1); 373 374 po::variables_map vm; 375 po::wparsed_options parsed = 376 po::wcommand_line_parser(argc, argv).options(all).allow_unregistered().run(); 377 po::store(parsed, vm); 378 po::notify(vm); 379 380 if (process_common_options("client", all)) 381 return 1; 382 383 384 if (vm.count("exec")) { 385 args.command = vm["exec"].as<std::wstring>(); 386 args.mode = client_arguments::exec; 387 if (vm.count("query")) { 388 args.combined_query = vm["query"].as<std::wstring>(); 389 args.mode = client_arguments::combined; 390 } 391 } else if (vm.count("query")) { 392 args.command = vm["query"].as<std::wstring>(); 393 args.mode = client_arguments::query; 394 } else if (vm.count("submit")) { 395 args.command = vm["submit"].as<std::wstring>(); 396 args.mode = client_arguments::submit; 397 } 398 399 if (vm.count("module")) 400 args.module = vm["module"].as<std::wstring>(); 401 402 if (vm.count("boot")) 403 args.boot = true; 404 405 std::vector<std::wstring> kvp_args; 406 if (vm.count("argument")) 407 kvp_args = vm["argument"].as<std::vector<std::wstring> >(); 408 409 args.arguments = po::collect_unrecognized(parsed.options, po::include_positional); 410 411 BOOST_FOREACH(std::wstring s, kvp_args) { 412 std::wstring::size_type pos = s.find(L'='); 413 if (pos == std::wstring::npos) 414 args.arguments.push_back(_T("--") + s); 415 else { 416 args.arguments.push_back(_T("--") + s.substr(0,pos)); 417 args.arguments.push_back(s.substr(pos+1)); 418 } 419 } 420 421 if (vm.count("raw-argument")) 422 kvp_args = vm["raw-argument"].as<std::vector<std::wstring> >(); 423 BOOST_FOREACH(std::wstring s, kvp_args) { 424 std::wstring::size_type pos = s.find(L'='); 425 if (pos == std::wstring::npos) 426 args.arguments.push_back(s); 427 else { 428 args.arguments.push_back(s.substr(0,pos)); 429 args.arguments.push_back(s.substr(pos+1)); 430 } 431 } 432 return exec_client_mode(args); 433 } catch(const std::exception & e) { 434 std::wcerr << _T("Client: Unable to parse command line: ") << utf8::to_unicode(e.what()) << std::endl; 435 return 1; 436 } catch(...) { 437 std::wcerr << _T("Client: Unable to parse command line: UNKNOWN") << std::endl; 438 return 1; 439 } 440 } 441 442 int parse_unittest(int argc, wchar_t* argv[]) { 443 try { 444 client_arguments args; 445 settings_store = _T("dummy"); 446 po::options_description all("Allowed options (client)"); 447 all.add(common).add(unittest); 448 449 po::positional_options_description p; 450 p.add("arguments", -1); 451 452 po::variables_map vm; 453 po::wparsed_options parsed = 454 po::wcommand_line_parser(argc, argv).options(all).allow_unregistered().run(); 455 po::store(parsed, vm); 456 po::notify(vm); 457 458 if (process_common_options("unitest", all)) 459 return 1; 460 461 462 if (vm.count("language")) { 463 std::wstring lang = vm["language"].as<std::wstring>(); 464 if (lang == _T("python") || lang == _T("py")) { 465 args.command = _T("python-script"); 466 args.combined_query = _T("py_unittest"); 467 args.mode = client_arguments::combined; 468 args.module = _T("PythonScript"); 469 } else { 470 std::wcerr << _T("Unknown language: ") << lang << std::endl; 471 return 1; 472 } 473 } else { 474 args.command = _T("python-script"); 475 args.combined_query = _T("py_unittest"); 476 args.mode = client_arguments::combined; 477 args.module = _T("PythonScript"); 478 } 479 480 std::vector<std::wstring> kvp_args; 481 if (vm.count("argument")) 482 kvp_args = vm["argument"].as<std::vector<std::wstring> >(); 483 484 args.arguments = po::collect_unrecognized(parsed.options, po::include_positional); 485 486 BOOST_FOREACH(std::wstring s, kvp_args) { 487 std::wstring::size_type pos = s.find(L'='); 488 if (pos == std::wstring::npos) 489 args.arguments.push_back(_T("--") + s); 490 else { 491 args.arguments.push_back(_T("--") + s.substr(0,pos)); 492 args.arguments.push_back(s.substr(pos+1)); 493 } 494 } 495 496 if (vm.count("raw-argument")) 497 kvp_args = vm["raw-argument"].as<std::vector<std::wstring> >(); 498 BOOST_FOREACH(std::wstring s, kvp_args) { 499 std::wstring::size_type pos = s.find(L'='); 500 if (pos == std::wstring::npos) 501 args.arguments.push_back(s); 502 else { 503 args.arguments.push_back(s.substr(0,pos)); 504 args.arguments.push_back(s.substr(pos+1)); 505 } 506 } 507 return exec_client_mode(args); 508 } catch(const std::exception & e) { 509 std::wcerr << _T("Client: Unable to parse command line: ") << utf8::to_unicode(e.what()) << std::endl; 510 return 1; 511 } catch(...) { 512 std::wcerr << _T("Client: Unable to parse command line: UNKNOWN") << std::endl; 513 return 1; 514 } 515 } 516 517 int exec_client_mode(client_arguments &args) { 518 try { 519 args.debug(); 520 426 521 core_->boot_init(); 427 if ( module.empty())522 if (args.module.empty()) 428 523 core_->boot_load_all_plugins(); 429 524 else 430 core_->boot_load_plugin( module);431 core_->boot_start_plugins( boot);525 core_->boot_load_plugin(args.module); 526 core_->boot_start_plugins(args.boot); 432 527 int ret = 0; 433 528 std::list<std::wstring> resp; 434 if ( mode ==none) {435 mode =exec;529 if (args.mode == client_arguments::none) { 530 args.mode = client_arguments::exec; 436 531 std::wcerr << _T("Since no mode was specified assuming --exec (other options are --query and --submit)") << std::endl; 437 532 } 438 if ( mode ==query) {439 ret = mainClient.simple_query( module, command,arguments, resp);440 } else if ( mode == exec || mode ==combined) {441 ret = mainClient.simple_exec( module, command,arguments, resp);533 if (args.mode == client_arguments::query) { 534 ret = mainClient.simple_query(args.module, args.command, args.arguments, resp); 535 } else if (args.mode == client_arguments::exec || args.mode == client_arguments::combined) { 536 ret = mainClient.simple_exec(args.module, args.command, args.arguments, resp); 442 537 if (ret == NSCAPI::returnIgnored) { 443 538 ret = 1; 444 std::wcout << _T("Command not found (by module): ") << command << std::endl;445 resp.push_back(_T("Command not found: ") + command);446 mainClient.simple_exec( module, _T("help"),arguments, resp);447 } else if ( mode ==combined) {539 std::wcout << _T("Command not found (by module): ") << args.command << std::endl; 540 resp.push_back(_T("Command not found: ") + args.command); 541 mainClient.simple_exec(args.module, _T("help"), args.arguments, resp); 542 } else if (args.mode == client_arguments::combined) { 448 543 if (ret == NSCAPI::returnOK) { 449 544 mainClient.reload(_T("service")); 450 ret = mainClient.simple_query( module, combined_query,arguments, resp);545 ret = mainClient.simple_query(args.module, args.combined_query, args.arguments, resp); 451 546 } else { 452 547 std::wcerr << _T("Failed to execute command, will not attempt query") << std::endl; 453 548 } 454 549 } 455 } else if ( mode ==submit) {550 } else if (args.mode == client_arguments::submit) { 456 551 std::wcerr << _T("--submit is currently not supported (but you can use --exec submit which is technically the same)") << std::endl; 457 552 } else { -
service/logger.hpp
r8013c0c r523576e 51 51 msg->set_line(line); 52 52 msg->set_message(utf8::cvt<std::string>(logMessage)); 53 if (!message.SerializeToString(&str)) { 54 return "Failed to generate message"; 55 } 56 return str; 53 return message.SerializeAsString(); 57 54 } catch (std::exception &e) { 58 return std::string("Failed to generate message: ") + e.what();55 std::cout << "Failed to generate message: " << e.what() << std::endl;; 59 56 } catch (...) { 60 return "Failed to generate message";61 } 62 // return to_string(logMessage);57 std::cout << "Failed to generate message: " << std::endl;; 58 } 59 return ""; 63 60 } 64 61 }; … … 86 83 87 84 public: 88 slave() : mq_(ip::open_only,queue_name.c_str()), plugins_loaded_(false), console_log_(false) { 89 #ifdef WIN32 90 // if(!SetConsoleOutputCP(CP_UTF8)) { // 65001 91 // std::cerr << "Failed to set console output mode!\n"; 92 // } 93 #endif 94 } 85 slave() : mq_(ip::open_only,queue_name.c_str()), plugins_loaded_(false), console_log_(false) {} 95 86 96 87 void add_plugin(plugin_type plugin) { … … 206 197 207 198 #ifdef WIN32 208 s += _T("\n"); 199 //s += _T("\n"); 200 std::wcout << s << std::endl; 201 /* 209 202 HANDLE const consout = GetStdHandle(STD_OUTPUT_HANDLE); 210 203 DWORD nNumberOfCharsWritten; … … 213 206 cerr << "WriteConsole failed with << " << err << "!\n"; 214 207 } 208 */ 215 209 #else 216 210 std::wcout << s << std::endl; … … 310 304 } 311 305 306 void truncate_entries(Plugin::LogEntry &message) { 307 if (message.entry_size() == 1) 308 return; 309 Plugin::LogEntry::Entry e = message.entry(0); 310 message.clear_entry(); 311 message.add_entry()->CopyFrom(e); 312 //message.mutable_entry()->add(e); 313 } 314 315 void truncate_string(Plugin::LogEntry &message) { 316 if (message.entry_size() == 0) 317 return; 318 std::string str = message.mutable_entry(0)->message(); 319 if (str.size() > max_message_size*0.7) 320 str = str.substr(0, max_message_size*0.7); 321 message.mutable_entry(0)->set_message("TRUNCATED(" + str + "...)"); 322 } 323 312 324 void log(std::string data) { 313 325 if (!mq_) { … … 316 328 } 317 329 if (data.size() >= max_message_size) { 318 log_fatal_error("Message to large to fit buffer: " + to_string(data.size()) + " > " + to_string(max_message_size)); 319 return; 330 Plugin::LogEntry message; 331 message.ParseFromString(data); 332 truncate_entries(message); 333 data = message.SerializeAsString(); 334 if (data.size() >= max_message_size) { 335 truncate_string(message); 336 data = message.SerializeAsString(); 337 } 338 if (data.size() >= max_message_size) { 339 log_fatal_error("Message to large to fit buffer: " + to_string(data.size()) + " > " + to_string(max_message_size)); 340 return; 341 } 342 data = message.SerializeAsString(); 320 343 } 321 344 try { … … 330 353 OutputDebugString(strEx::string_to_wstring(message).c_str()); 331 354 #endif 332 std::cout << " TODO: " << message << std::endl;355 std::cout << "FATAL ERROR: " << message << std::endl; 333 356 } 334 357 -
version.hpp
r2906cda r523576e 1 1 #ifndef VERSION_HPP 2 2 #define VERSION_HPP 3 #define PRODUCTVER 0,4,0,13 44 #define STRPRODUCTVER "0,4,0,13 4"5 #define STRPRODUCTDATE "2012-02-1 3"3 #define PRODUCTVER 0,4,0,135 4 #define STRPRODUCTVER "0,4,0,135" 5 #define STRPRODUCTDATE "2012-02-19" 6 6 #endif // VERSION_HPP -
version.txt
r2906cda r523576e 1 1 version=0.4.0 2 build=13 43 date=2012-02-1 32 build=135 3 date=2012-02-19
Note: See TracChangeset
for help on using the changeset viewer.








