Changeset 232


Ignore:
Timestamp:
01/17/10 12:29:46 (2 years ago)
Author:
mickem
Message:

Finnished Scheduler and added basic "Notify" support (no sinks yet)

Location:
trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/NSCHelper.cpp

    r226 r232  
    8383 
    8484 
     85#define REPORT_ERROR  0x01 
     86#define REPORT_WARNING  0x02 
     87#define REPORT_UNKNOWN  0x04 
     88#define REPORT_OK   0x08 
     89 
     90unsigned 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} 
     108bool 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} 
     117std::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 
    85139/** 
    86140 * Translate a message type into a human readable string. 
     
    151205  lpNSAPIExit fNSAPIExit = NULL; 
    152206  lpNSAPIInject fNSAPIInject = NULL; 
     207  lpNSAPINotify fNSAPINotify = NULL; 
    153208  lpNSAPICheckLogMessages fNSAPICheckLogMessages = NULL; 
    154209  lpNSAPIEncrypt fNSAPIEncrypt = NULL; 
     
    225280  return fNSAPIInject(command, argLen, argument, returnMessageBuffer, returnMessageBufferLen, returnPerfBuffer, returnPerfBufferLen); 
    226281} 
     282 
     283NSCAPI::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 
    227289/** 
    228290 * Inject a request command in the core (this will then be sent to the plug-in stack for processing) 
     
    734796  //NSCModuleHelper::fNSAPIExit = (NSCModuleHelper::lpNSAPIExit)f(_T("NSAPIExit")); 
    735797  NSCModuleHelper::fNSAPIInject = (NSCModuleHelper::lpNSAPIInject)f(_T("NSAPIInject")); 
     798  NSCModuleHelper::fNSAPINotify = (NSCModuleHelper::lpNSAPINotify)f(_T("NSAPINotify")); 
    736799  NSCModuleHelper::fNSAPIGetBasePath = (NSCModuleHelper::lpNSAPIGetBasePath)f(_T("NSAPIGetBasePath")); 
    737800  NSCModuleHelper::fNSAPICheckLogMessages = (NSCModuleHelper::lpNSAPICheckLogMessages)f(_T("NSAPICheckLogMessages")); 
  • trunk/include/NSCHelper.h

    r219 r232  
    9494    if (currentReturnCode != NSCAPI::returnCRIT) 
    9595      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); 
    96102  } 
    97103}; 
     
    124130  typedef NSCAPI::errorReturn (*lpNSAPIExit)(void); 
    125131  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 
    127135  typedef NSCAPI::boolReturn (*lpNSAPICheckLogMessages)(int); 
    128136  typedef NSCAPI::errorReturn (*lpNSAPIEncrypt)(unsigned int, const wchar_t*, unsigned int, wchar_t*, unsigned int *); 
     
    158166  NSCAPI::nagiosReturn InjectCommand(const wchar_t* command, const unsigned int argLen, wchar_t **argument, std::wstring & message, std::wstring & perf); 
    159167  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); 
    160169  NSCAPI::nagiosReturn InjectSplitAndCommand(const wchar_t* command, wchar_t* buffer, wchar_t splitChar, std::wstring & message, std::wstring & perf); 
    161170  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  
    382382    return value * smallest_unit; 
    383383  } 
     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  } 
    384402 
    385403  inline unsigned long long stoi64_as_time(std::wstring time, unsigned int smallest_unit = 1000) { 
  • trunk/modules/Scheduler/Scheduler.cpp

    r230 r232  
    5252 
    5353    if (mode == NSCAPI::normalStart) { 
     54      scheduler_.set_handler(this); 
    5455      scheduler_.start(); 
    5556    } 
    5657 
    5758    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); 
    5960    std::list<std::wstring> items = NSCModuleHelper::getSettingsSection(setting_keys::scheduler::SCHEDULES_SECTION_PATH); 
    6061 
    6162    for (std::list<std::wstring>::const_iterator cit = items.begin(); cit != items.end(); ++cit) { 
    6263      found = true; 
    63       add_schedule(*cit, def); 
     64      add_schedule(*cit, NSCModuleHelper::getSettingsString(setting_keys::scheduler::SCHEDULES_SECTION_PATH, *cit, _T("")), def); 
    6465    } 
    6566 
     
    8889} 
    8990 
    90 scheduler::target Scheduler::read_schedule(std::wstring path) { 
     91scheduler::target Scheduler::read_defaut_schedule(std::wstring path) { 
    9192  scheduler::target item; 
    9293  item.channel = NSCModuleHelper::getSettingsString(path, setting_keys::scheduler::CHANNEL, setting_keys::scheduler::CHANNEL_DEFAULT); 
    9394  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); 
    9897  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)); 
    10099  return item; 
    101100} 
    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; 
     101void 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); 
    114114} 
    115115 
    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 } 
    130116bool Scheduler::unloadModule() { 
     117  scheduler_.unset_handler(); 
    131118  scheduler_.stop(); 
    132119  return true; 
    133120} 
     121 
     122void 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 
    134140 
    135141NSC_WRAP_DLL(); 
  • trunk/modules/Scheduler/Scheduler.h

    r230 r232  
    2626 
    2727 
    28 class Scheduler { 
     28class Scheduler : public scheduler::schedule_handler { 
    2929private: 
    3030  scheduler::simple_scheduler scheduler_; 
     
    3939 
    4040 
    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); 
    4444 
    4545  std::wstring getModuleName() { 
  • trunk/modules/Scheduler/simple_scheduler.cpp

    r230 r232  
    9393      if (item) { 
    9494        try { 
    95           execute(*item); 
     95          if (handler_) 
     96            handler_->handle_schedule(*item); 
    9697          reschedule(*item,now_time); 
    9798        } catch (...) { 
     
    120121    start_thread(); 
    121122  } 
    122   void simple_scheduler::execute(target item) { 
    123     //std::wcout << _T("Running: ") << item.command << std::endl; 
    124   } 
    125  
    126123} 
    127124 
  • trunk/modules/Scheduler/simple_scheduler.hpp

    r230 r232  
    99#include <boost/date_time/local_time/local_time.hpp> 
    1010#include <boost/optional.hpp> 
     11#include <unicode_char.hpp> 
    1112 
    1213namespace scheduler { 
     14 
    1315 
    1416  class task_not_found { 
     
    2123  public: 
    2224    int id; 
     25    std::wstring alias; 
     26 
    2327    std::wstring command; 
    2428    std::list<std::wstring> arguments; 
    2529    std::wstring tag; 
     30 
    2631    boost::posix_time::time_duration duration; 
    2732    std::wstring  channel; 
    28     //std::wstring duration; 
    29  
     33    unsigned int report; 
    3034 
    3135    void set_duration(boost::posix_time::time_duration duration_) { 
     
    3741    {} 
    3842     
    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) {} 
    4046    target& operator=(target const& other) { 
     47      id = other.id; 
     48      alias = other.alias; 
     49 
    4150      command = other.command; 
     51      arguments = other.arguments; 
     52      tag = other.tag; 
     53 
    4254      duration = other.duration; 
    43       id = other.id; 
     55      report = other.report; 
     56      channel = other.channel; 
    4457      return *this; 
    4558    } 
    4659    ~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; 
    4769  }; 
    4870  struct schedule_instance { 
     
    112134    //boost::shared_ptr<boost::thread> thread_; 
    113135    boost::mutex mutex_; 
     136    schedule_handler* handler_; 
    114137 
    115138  public: 
    116139 
    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) {} 
    118141    ~simple_scheduler() {} 
    119142 
     143 
     144    void set_handler(schedule_handler* handler) { 
     145      handler_ = handler; 
     146    } 
     147    void unset_handler() { 
     148      handler_ = NULL; 
     149    } 
    120150 
    121151    int add_task(target item); 
     
    138168    void reschedule(target item, boost::posix_time::ptime now); 
    139169    void reschedule_wnext(target item, boost::posix_time::ptime next); 
    140     void execute(target item); 
    141170    void start_thread(); 
    142171 
  • trunk/service/core_api.cpp

    r219 r232  
    413413  if (wcscasecmp(buffer, _T("NSAPISettingsSave")) == 0) 
    414414    return reinterpret_cast<LPVOID>(&NSAPISettingsSave); 
     415  if (wcscasecmp(buffer, _T("NSAPINotify")) == 0) 
     416    return reinterpret_cast<LPVOID>(&NSAPINotify); 
    415417 
    416418  LOG_ERROR_STD(_T("Function not found: ") + buffer); 
     
    418420} 
    419421 
    420  
     422NSCAPI::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  
    5858NSCAPI::errorReturn NSAPIReleasePluginList(int,NSCAPI::plugin_info*[]); 
    5959NSCAPI::errorReturn NSAPISettingsSave(void); 
     60NSCAPI::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.