Changeset 232
- Timestamp:
- 01/17/10 12:29:46 (2 years ago)
- Location:
- trunk
- Files:
-
- 9 edited
-
include/NSCHelper.cpp (modified) (4 diffs)
-
include/NSCHelper.h (modified) (3 diffs)
-
include/strEx.h (modified) (1 diff)
-
modules/Scheduler/Scheduler.cpp (modified) (2 diffs)
-
modules/Scheduler/Scheduler.h (modified) (2 diffs)
-
modules/Scheduler/simple_scheduler.cpp (modified) (2 diffs)
-
modules/Scheduler/simple_scheduler.hpp (modified) (5 diffs)
-
service/core_api.cpp (modified) (2 diffs)
-
service/core_api.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/NSCHelper.cpp
r226 r232 83 83 84 84 85 #define REPORT_ERROR 0x01 86 #define REPORT_WARNING 0x02 87 #define REPORT_UNKNOWN 0x04 88 #define REPORT_OK 0x08 89 90 unsigned int NSCHelper::report::parse(std::wstring str) { 91 unsigned int report = 0; 92 strEx::splitList lst = strEx::splitEx(str, _T(",")); 93 for (strEx::splitList::const_iterator key = lst.begin(); key != lst.end(); ++key) { 94 if (*key == _T("all")) { 95 report |= REPORT_ERROR|REPORT_OK|REPORT_UNKNOWN|REPORT_WARNING; 96 } else if (*key == _T("error") || *key == _T("err") || *key == _T("critical") || *key == _T("crit")) { 97 report |= REPORT_ERROR; 98 } else if (*key == _T("warning") || *key == _T("warn")) { 99 report |= REPORT_WARNING; 100 } else if (*key == _T("unknown")) { 101 report |= REPORT_UNKNOWN; 102 } else if (*key == _T("ok")) { 103 report |= REPORT_OK; 104 } 105 } 106 return report; 107 } 108 bool NSCHelper::report::matches(unsigned int report, NSCAPI::nagiosReturn code) { 109 return ( 110 (code == NSCAPI::returnOK && (report&REPORT_OK)==REPORT_OK) || 111 (code == NSCAPI::returnCRIT && ((report&REPORT_ERROR)==REPORT_ERROR) ) || 112 (code == NSCAPI::returnWARN && ((report&REPORT_WARNING)==REPORT_WARNING) ) || 113 (code == NSCAPI::returnUNKNOWN && ((report&REPORT_UNKNOWN)==REPORT_UNKNOWN) ) || 114 ( (code != NSCAPI::returnOK) && (code != NSCAPI::returnCRIT) && (code != NSCAPI::returnWARN) && (code != NSCAPI::returnUNKNOWN) ) 115 ); 116 } 117 std::wstring NSCHelper::report::to_string(unsigned int report) { 118 std::wstring ret; 119 if ((report&REPORT_OK)!=0) { 120 if (!ret.empty()) ret += _T(","); 121 ret += _T("ok"); 122 } 123 if ((report&REPORT_WARNING)!=0) { 124 if (!ret.empty()) ret += _T(","); 125 ret += _T("warning"); 126 } 127 if ((report&REPORT_ERROR)!=0) { 128 if (!ret.empty()) ret += _T(","); 129 ret += _T("critical"); 130 } 131 if ((report&REPORT_UNKNOWN)!=0) { 132 if (!ret.empty()) ret += _T(","); 133 ret += _T("unknown"); 134 } 135 return ret; 136 } 137 138 85 139 /** 86 140 * Translate a message type into a human readable string. … … 151 205 lpNSAPIExit fNSAPIExit = NULL; 152 206 lpNSAPIInject fNSAPIInject = NULL; 207 lpNSAPINotify fNSAPINotify = NULL; 153 208 lpNSAPICheckLogMessages fNSAPICheckLogMessages = NULL; 154 209 lpNSAPIEncrypt fNSAPIEncrypt = NULL; … … 225 280 return fNSAPIInject(command, argLen, argument, returnMessageBuffer, returnMessageBufferLen, returnPerfBuffer, returnPerfBufferLen); 226 281 } 282 283 NSCAPI::errorReturn NSCModuleHelper::NotifyChannel(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn code, std::wstring message, std::wstring perf) { 284 if (!fNSAPINotify) 285 throw NSCMHExcpetion(_T("NSCore has not been initiated...")); 286 return fNSAPINotify(channel.c_str(), command.c_str(), code, message.c_str(), perf.c_str()); 287 } 288 227 289 /** 228 290 * Inject a request command in the core (this will then be sent to the plug-in stack for processing) … … 734 796 //NSCModuleHelper::fNSAPIExit = (NSCModuleHelper::lpNSAPIExit)f(_T("NSAPIExit")); 735 797 NSCModuleHelper::fNSAPIInject = (NSCModuleHelper::lpNSAPIInject)f(_T("NSAPIInject")); 798 NSCModuleHelper::fNSAPINotify = (NSCModuleHelper::lpNSAPINotify)f(_T("NSAPINotify")); 736 799 NSCModuleHelper::fNSAPIGetBasePath = (NSCModuleHelper::lpNSAPIGetBasePath)f(_T("NSAPIGetBasePath")); 737 800 NSCModuleHelper::fNSAPICheckLogMessages = (NSCModuleHelper::lpNSAPICheckLogMessages)f(_T("NSAPICheckLogMessages")); -
trunk/include/NSCHelper.h
r219 r232 94 94 if (currentReturnCode != NSCAPI::returnCRIT) 95 95 currentReturnCode = NSCAPI::returnWARN; 96 } 97 98 namespace report { 99 unsigned int parse(std::wstring str); 100 bool matches(unsigned int report, NSCAPI::nagiosReturn code); 101 std::wstring to_string(unsigned int report); 96 102 } 97 103 }; … … 124 130 typedef NSCAPI::errorReturn (*lpNSAPIExit)(void); 125 131 typedef NSCAPI::nagiosReturn (*lpNSAPIInject)(const wchar_t*, const unsigned int, wchar_t **, wchar_t *, unsigned int, wchar_t *, unsigned int); 126 132 133 typedef NSCAPI::errorReturn (*lpNSAPINotify)(const wchar_t*, const wchar_t*, NSCAPI::nagiosReturn, const wchar_t*, const wchar_t*); 134 127 135 typedef NSCAPI::boolReturn (*lpNSAPICheckLogMessages)(int); 128 136 typedef NSCAPI::errorReturn (*lpNSAPIEncrypt)(unsigned int, const wchar_t*, unsigned int, wchar_t*, unsigned int *); … … 158 166 NSCAPI::nagiosReturn InjectCommand(const wchar_t* command, const unsigned int argLen, wchar_t **argument, std::wstring & message, std::wstring & perf); 159 167 NSCAPI::nagiosReturn InjectCommand(const wchar_t* command, std::list<std::wstring> argument, std::wstring & message, std::wstring & perf); 168 NSCAPI::errorReturn NotifyChannel(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn code, std::wstring message, std::wstring perf); 160 169 NSCAPI::nagiosReturn InjectSplitAndCommand(const wchar_t* command, wchar_t* buffer, wchar_t splitChar, std::wstring & message, std::wstring & perf); 161 170 NSCAPI::nagiosReturn InjectSplitAndCommand(const std::wstring command, const std::wstring buffer, wchar_t splitChar, std::wstring & message, std::wstring & perf, bool escape = false); -
trunk/include/strEx.h
r230 r232 382 382 return value * smallest_unit; 383 383 } 384 inline unsigned stoui_as_time_sec(std::wstring time, unsigned int smallest_unit = 1) { 385 std::wstring::size_type p = time.find_first_of(_T("sSmMhHdDwW")); 386 std::wstring::size_type pend = time.find_first_not_of(_T("0123456789")); 387 unsigned int value = boost::lexical_cast<unsigned int>(pend==std::wstring::npos?time:time.substr(0,pend).c_str()); 388 if (p == std::wstring::npos) 389 return value * smallest_unit; 390 else if ( (time[p] == 's') || (time[p] == 'S') ) 391 return value; 392 else if ( (time[p] == 'm') || (time[p] == 'M') ) 393 return value * 60; 394 else if ( (time[p] == 'h') || (time[p] == 'H') ) 395 return value * 60 * 60; 396 else if ( (time[p] == 'd') || (time[p] == 'D') ) 397 return value * 24 * 60 * 60; 398 else if ( (time[p] == 'w') || (time[p] == 'W') ) 399 return value * 7 * 24 * 60 * 60; 400 return value * smallest_unit; 401 } 384 402 385 403 inline unsigned long long stoi64_as_time(std::wstring time, unsigned int smallest_unit = 1000) { -
trunk/modules/Scheduler/Scheduler.cpp
r230 r232 52 52 53 53 if (mode == NSCAPI::normalStart) { 54 scheduler_.set_handler(this); 54 55 scheduler_.start(); 55 56 } 56 57 57 58 bool found = false; 58 scheduler::target def = read_ schedule(setting_keys::scheduler::DEFAULT_SCHEDULE_SECTION_PATH);59 scheduler::target def = read_defaut_schedule(setting_keys::scheduler::DEFAULT_SCHEDULE_SECTION_PATH); 59 60 std::list<std::wstring> items = NSCModuleHelper::getSettingsSection(setting_keys::scheduler::SCHEDULES_SECTION_PATH); 60 61 61 62 for (std::list<std::wstring>::const_iterator cit = items.begin(); cit != items.end(); ++cit) { 62 63 found = true; 63 add_schedule(*cit, def);64 add_schedule(*cit, NSCModuleHelper::getSettingsString(setting_keys::scheduler::SCHEDULES_SECTION_PATH, *cit, _T("")), def); 64 65 } 65 66 … … 88 89 } 89 90 90 scheduler::target Scheduler::read_ schedule(std::wstring path) {91 scheduler::target Scheduler::read_defaut_schedule(std::wstring path) { 91 92 scheduler::target item; 92 93 item.channel = NSCModuleHelper::getSettingsString(path, setting_keys::scheduler::CHANNEL, setting_keys::scheduler::CHANNEL_DEFAULT); 93 94 item.command = NSCModuleHelper::getSettingsString(path, setting_keys::scheduler::COMMAND, setting_keys::scheduler::COMMAND_PATH); 94 /* 95 std::wstring report = SETTINGS_GET_STRING(scheduler::REPORT_MODE); 96 item.report = parse_report_string(report); 97 */ 95 std::wstring report = NSCModuleHelper::getSettingsString(path, setting_keys::scheduler::REPORT_MODE, setting_keys::scheduler::REPORT_MODE_PATH); 96 item.report = NSCHelper::report::parse(report); 98 97 std::wstring duration = NSCModuleHelper::getSettingsString(path, setting_keys::scheduler::INTERVAL, setting_keys::scheduler::INTERVAL_DEFAULT); 99 item.duration = boost::posix_time::seconds(strEx::stoui_as_time (duration));98 item.duration = boost::posix_time::seconds(strEx::stoui_as_time_sec(duration, 1)); 100 99 return item; 101 100 } 102 scheduler::target Scheduler::read_schedule(std::wstring path, scheduler::target def) { 103 scheduler::target item; 104 105 item.channel = NSCModuleHelper::getSettingsString(path, setting_keys::scheduler::CHANNEL, def.channel); 106 item.command = NSCModuleHelper::getSettingsString(path, setting_keys::scheduler::COMMAND, def.command); 107 /* 108 std::wstring report = NSCModuleHelper::getSettingsString(path, setting_keys::scheduler::REPORT_MODE, def.report); 109 item.report = parse_report_string(report); 110 */ 111 std::wstring duration = NSCModuleHelper::getSettingsString(path, setting_keys::scheduler::INTERVAL, to_wstring(def.duration.total_seconds()) + _T("s")); 112 item.duration = boost::posix_time::seconds(strEx::stoui_as_time(duration)); 113 return item; 101 void Scheduler::add_schedule(std::wstring alias, std::wstring command, scheduler::target def) { 102 scheduler::target item; 103 std::wstring detail_path = setting_keys::scheduler::SCHEDULES_SECTION_PATH + _T("/") + alias; 104 item.alias = alias; 105 item.command = command; 106 item.channel = NSCModuleHelper::getSettingsString(detail_path, setting_keys::scheduler::CHANNEL, def.channel); 107 item.command = NSCModuleHelper::getSettingsString(detail_path, setting_keys::scheduler::COMMAND, item.command); 108 std::wstring report = NSCModuleHelper::getSettingsString(detail_path, setting_keys::scheduler::REPORT_MODE, NSCHelper::report::to_string(def.report)); 109 item.report = NSCHelper::report::parse(report); 110 std::wstring duration = NSCModuleHelper::getSettingsString(detail_path, setting_keys::scheduler::INTERVAL, to_wstring(def.duration.total_seconds()) + _T("s")); 111 item.duration = boost::posix_time::seconds(strEx::stoui_as_time_sec(duration, 1)); 112 //std::wcout << _T("Added: ") << item.to_string() << std::endl; 113 scheduler_.add_task(item); 114 114 } 115 115 116 void Scheduler::add_schedule(std::wstring command, scheduler::target def) {117 NSC_DEBUG_MSG_STD(_T("Adding scheduled command: ") + command);118 scheduler::target item = read_schedule(setting_keys::scheduler::SCHEDULES_SECTION_PATH + _T("/") + command, def);119 120 // std::wstring report = SETTINGS_GET_STRING(scheduler::REPORT_MODE);121 // report_ = parse_report_string(report);122 123 item.command = command;124 item.set_duration(boost::posix_time::seconds(5));125 scheduler_.add_task(item);126 /*127 std::wcout << _T("*** DURATION ") << item.duration << _T(" ***") << std::endl;128 */129 }130 116 bool Scheduler::unloadModule() { 117 scheduler_.unset_handler(); 131 118 scheduler_.stop(); 132 119 return true; 133 120 } 121 122 void Scheduler::handle_schedule(scheduler::target item) { 123 try { 124 std::wstring msg, perf; 125 NSCAPI::nagiosReturn code = NSCModuleHelper::InjectCommand(item.command.c_str(), item.arguments, msg, perf); 126 if (NSCHelper::report::matches(item.report, code)) { 127 NSCModuleHelper::NotifyChannel(item.channel, item.alias, code, msg, perf); 128 } 129 } catch (NSCModuleHelper::NSCMHExcpetion &e) { 130 NSC_LOG_ERROR_STD(_T("Exception handling: ") + item.alias + _T(": ") + e.msg_); 131 scheduler_.remove_task(item.id); 132 } catch (...) { 133 NSC_LOG_ERROR_STD(_T("Unknown Exception handling: ") + item.alias); 134 scheduler_.remove_task(item.id); 135 } 136 } 137 138 139 134 140 135 141 NSC_WRAP_DLL(); -
trunk/modules/Scheduler/Scheduler.h
r230 r232 26 26 27 27 28 class Scheduler {28 class Scheduler : public scheduler::schedule_handler { 29 29 private: 30 30 scheduler::simple_scheduler scheduler_; … … 39 39 40 40 41 void add_schedule(std::wstring command, scheduler::target def);42 scheduler::target read_ schedule(std::wstring path, scheduler::target def);43 scheduler::target read_schedule(std::wstring path);41 void add_schedule(std::wstring alias, std::wstring command, scheduler::target def); 42 scheduler::target read_defaut_schedule(std::wstring path); 43 void handle_schedule(scheduler::target item); 44 44 45 45 std::wstring getModuleName() { -
trunk/modules/Scheduler/simple_scheduler.cpp
r230 r232 93 93 if (item) { 94 94 try { 95 execute(*item); 95 if (handler_) 96 handler_->handle_schedule(*item); 96 97 reschedule(*item,now_time); 97 98 } catch (...) { … … 120 121 start_thread(); 121 122 } 122 void simple_scheduler::execute(target item) {123 //std::wcout << _T("Running: ") << item.command << std::endl;124 }125 126 123 } 127 124 -
trunk/modules/Scheduler/simple_scheduler.hpp
r230 r232 9 9 #include <boost/date_time/local_time/local_time.hpp> 10 10 #include <boost/optional.hpp> 11 #include <unicode_char.hpp> 11 12 12 13 namespace scheduler { 14 13 15 14 16 class task_not_found { … … 21 23 public: 22 24 int id; 25 std::wstring alias; 26 23 27 std::wstring command; 24 28 std::list<std::wstring> arguments; 25 29 std::wstring tag; 30 26 31 boost::posix_time::time_duration duration; 27 32 std::wstring channel; 28 //std::wstring duration; 29 33 unsigned int report; 30 34 31 35 void set_duration(boost::posix_time::time_duration duration_) { … … 37 41 {} 38 42 39 target(const target &other) : id(other.id), command(other.command), duration(other.duration) {} 43 target(const target &other) : id(other.id), alias(other.alias) 44 , command(other.command), arguments(other.arguments), tag(other.tag) 45 , duration(other.duration), report(other.report), channel(other.channel) {} 40 46 target& operator=(target const& other) { 47 id = other.id; 48 alias = other.alias; 49 41 50 command = other.command; 51 arguments = other.arguments; 52 tag = other.tag; 53 42 54 duration = other.duration; 43 id = other.id; 55 report = other.report; 56 channel = other.channel; 44 57 return *this; 45 58 } 46 59 ~target() {} 60 std::wstring to_string() { 61 std::wstringstream ss; 62 ss << alias << _T("[") << id << _T("] = {command: ") << command << _T(", channel") << channel << _T("}"); 63 return ss.str(); 64 } 65 }; 66 class schedule_handler { 67 public: 68 virtual void handle_schedule(target item) = 0; 47 69 }; 48 70 struct schedule_instance { … … 112 134 //boost::shared_ptr<boost::thread> thread_; 113 135 boost::mutex mutex_; 136 schedule_handler* handler_; 114 137 115 138 public: 116 139 117 simple_scheduler() : target_id_(0), stop_requested_(false), running_(false), thread_count_(10) {}140 simple_scheduler() : target_id_(0), stop_requested_(false), running_(false), thread_count_(10), handler_(NULL) {} 118 141 ~simple_scheduler() {} 119 142 143 144 void set_handler(schedule_handler* handler) { 145 handler_ = handler; 146 } 147 void unset_handler() { 148 handler_ = NULL; 149 } 120 150 121 151 int add_task(target item); … … 138 168 void reschedule(target item, boost::posix_time::ptime now); 139 169 void reschedule_wnext(target item, boost::posix_time::ptime next); 140 void execute(target item);141 170 void start_thread(); 142 171 -
trunk/service/core_api.cpp
r219 r232 413 413 if (wcscasecmp(buffer, _T("NSAPISettingsSave")) == 0) 414 414 return reinterpret_cast<LPVOID>(&NSAPISettingsSave); 415 if (wcscasecmp(buffer, _T("NSAPINotify")) == 0) 416 return reinterpret_cast<LPVOID>(&NSAPINotify); 415 417 416 418 LOG_ERROR_STD(_T("Function not found: ") + buffer); … … 418 420 } 419 421 420 422 NSCAPI::errorReturn NSAPINotify(const wchar_t* channel, const wchar_t* command, NSCAPI::nagiosReturn code, const wchar_t* message, const wchar_t* perf) { 423 LOG_ERROR_STD(_T("TODO: implment channels: ") + std::wstring(command)); 424 return NSCAPI::hasFailed; 425 } 426 -
trunk/service/core_api.h
r219 r232 58 58 NSCAPI::errorReturn NSAPIReleasePluginList(int,NSCAPI::plugin_info*[]); 59 59 NSCAPI::errorReturn NSAPISettingsSave(void); 60 NSCAPI::errorReturn NSAPINotify(const wchar_t*, const wchar_t*, NSCAPI::nagiosReturn, const wchar_t*, const wchar_t*);
Note: See TracChangeset
for help on using the changeset viewer.







