Changeset 04ef932 in nscp for modules


Ignore:
Timestamp:
08/10/11 07:37:24 (22 months ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
54ac968
Parents:
e11d494
Message:

2011-08-10

  • Fixed so it builds and runs on linux (but parser had issues so disabled som grammar rules whichneeds to be enabled again)
  • Added a lot of freatures and cleand up the PythonScript module
  • Started to merge som features from PythonScript back to Lua script


2011-08-07

  • Fixed a lot of issues with PythonScript module adding suport for alias and "raw command processing"
  • Fixed issue with loading plugins and aliases as well as duplicate plugin detection


2011-08-01

Location:
modules
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • modules/CheckNSCP/CheckNSCP.cpp

    re11d494 r04ef932  
    2222 
    2323#include <file_helpers.hpp> 
     24#include <unicode_char.hpp> 
    2425 
    2526#include <settings/client/settings_client.hpp> 
     
    8586 
    8687int CheckNSCP::get_crashes(std::wstring &last_crash) { 
     88#ifdef WIN32 
    8789  if (!file_helpers::checks::is_directory(crashFolder)) { 
    8890    return 0; 
     
    109111    last_crash = last_file; 
    110112  return count; 
     113#else 
     114  return 0; 
     115#endif 
    111116} 
    112117 
     
    125130  std::wstring last_crash; 
    126131  int crash_count = get_crashes(last_crash); 
    127   if (crash_count > 0) 
    128     strEx::append_list(msg, strEx::itos(crash_count) + _T(" crash(es), last crash: ") + last_crash, _T(", ")); 
     132  if (crash_count > 0){ 
     133    std::wstring tmp = strEx::itos(crash_count) + _T(" crash(es), last crash: ") + last_crash; 
     134    strEx::append_list(msg, tmp, _T(", ")); 
     135  } 
    129136 
    130137  std::wstring last_error; 
    131138  int err_count = get_errors(last_error); 
    132   if (err_count > 0) 
    133     strEx::append_list(msg, strEx::itos(err_count) + _T(" error(s), last error: ") + last_error, _T(", ")); 
     139  if (err_count > 0) { 
     140    std::wstring tmp = strEx::itos(err_count) + _T(" error(s), last error: ") + last_error; 
     141    strEx::append_list(msg, tmp, _T(", ")); 
     142  } 
    134143 
    135144  if (msg.empty()) 
  • modules/LUAScript/LUAScript.cpp

    rd66ccee r04ef932  
    4646  try { 
    4747 
     48    root_ = get_core()->getBasePath(); 
     49 
    4850    sh::settings_registry settings(get_settings_proxy()); 
    4951    settings.set_alias(alias, _T("lua")); 
     
    5254      (_T("LUA SCRIPT SECTION"), _T("Section for the LUAScripts module.")) 
    5355 
    54       (_T("scripts"), sh::fun_values_path(boost::bind(&LUAScript::loadScript, this, _1)),  
     56      (_T("scripts"), sh::fun_values_path(boost::bind(&LUAScript::loadScript, this, _1, _2)),  
    5557      _T("LUA SCRIPTS SECTION"), _T("A list of scripts available to run from the LuaSCript module.")) 
    5658      ; 
     
    6264//      addAllScriptsFrom(scriptDirectory_); 
    6365//    } 
    64     root_ = get_core()->getBasePath(); 
     66 
     67 
     68 
     69 
     70    BOOST_FOREACH(script_container &script, scripts_) { 
     71      try { 
     72        boost::shared_ptr<script_wrapper::lua_script> instance = boost::shared_ptr<script_wrapper::lua_script>(new script_wrapper::lua_script(script)); 
     73        instance->pre_load(this); 
     74        instances_.push_back(instance); 
     75      } catch (script_wrapper::LUAException e) { 
     76        NSC_LOG_ERROR_STD(_T("Could not load script ") + script.to_wstring() + _T(": ") + e.getMessage()); 
     77      } 
     78    } 
    6579 
    6680    //  } catch (nrpe::server::nrpe_exception &e) { 
     
    8195 
    8296void LUAScript::register_command(script_wrapper::lua_script* script, std::wstring command, std::wstring function) { 
    83   NSC_LOG_MESSAGE(_T("Script loading: ") + script->get_script() + _T(": ") + command); 
     97  NSC_LOG_MESSAGE(_T("Script loading: ") + script->get_wscript() + _T(": ") + command); 
    8498  commands_[command] = lua_func(script, function); 
    8599} 
    86100 
    87 bool LUAScript::loadScript(const std::wstring file) { 
     101boost::optional<boost::filesystem::wpath> LUAScript::find_file(std::wstring file) { 
     102  std::list<boost::filesystem::wpath> checks; 
     103  checks.push_back(file); 
     104  checks.push_back(root_ / _T("scripts") / _T("lua") / file); 
     105  checks.push_back(root_ / _T("scripts") / file); 
     106  checks.push_back(root_ / _T("lua") / file); 
     107  checks.push_back(root_ / file); 
     108  BOOST_FOREACH(boost::filesystem::wpath c, checks) { 
     109    NSC_DEBUG_MSG_STD(_T("Looking for: ") + c.string()); 
     110    if (boost::filesystem::exists(c)) 
     111      return boost::optional<boost::filesystem::wpath>(c); 
     112  } 
     113  NSC_LOG_ERROR(_T("Script not found: ") + file); 
     114  return boost::optional<boost::filesystem::wpath>(); 
     115} 
     116 
     117bool LUAScript::loadScript(std::wstring alias, std::wstring file) { 
    88118  try { 
    89     std::wstring file_ = file; 
     119    if (file.empty()) { 
     120      file = alias; 
     121      alias = _T(""); 
     122    } 
    90123 
    91     if (!file_helpers::checks::exists(file_)) { 
    92       file_ = root_ + file; 
    93       if (!file_helpers::checks::exists(file_)) { 
    94         NSC_LOG_ERROR(_T("Script not found: ") + file + _T(" (") + file_ + _T(")")); 
    95         return false; 
    96       } 
    97     } 
    98     NSC_DEBUG_MSG_STD(_T("Loading script: ") + file + _T(" (") + file_ + _T(")")); 
    99     script_wrapper::lua_script *script = new script_wrapper::lua_script(file_); 
    100     script->pre_load(this); 
    101     scripts_.push_back(script); 
     124    boost::optional<boost::filesystem::wpath> ofile = find_file(file); 
     125    if (!ofile) 
     126      return false; 
     127    script_container::push(scripts_, alias, *ofile); 
     128    NSC_DEBUG_MSG_STD(_T("Adding script: ") + ofile->string() + _T(" as ") + alias + _T(")")); 
    102129    return true; 
    103   } catch (script_wrapper::LUAException e) { 
    104     NSC_LOG_ERROR_STD(_T("Could not load script: ") + file + _T(", ") + e.getMessage()); 
    105130  } catch (...) { 
    106131    NSC_LOG_ERROR_STD(_T("Could not load script: (Unknown exception) ") + file); 
     
    111136 
    112137bool LUAScript::unloadModule() { 
    113   for (script_list::const_iterator cit = scripts_.begin(); cit != scripts_.end() ; ++cit) { 
    114     delete (*cit); 
    115   } 
    116   scripts_.clear(); 
     138  instances_.clear(); 
    117139  return true; 
    118140} 
     
    129151  bool error = false; 
    130152  commands_.clear(); 
    131   for (script_list::const_iterator cit = scripts_.begin(); cit != scripts_.end() ; ++cit) { 
     153  for (script_list::const_iterator cit = instances_.begin(); cit != instances_.end() ; ++cit) { 
    132154    try { 
    133155      (*cit)->reload(this); 
    134156    } catch (script_wrapper::LUAException e) { 
    135157      error = true; 
    136       message += _T("Exception when reloading script: ") + (*cit)->get_script() + _T(": ") + e.getMessage(); 
    137       NSC_LOG_ERROR_STD(_T("Exception when reloading script: ") + (*cit)->get_script() + _T(": ") + e.getMessage()); 
     158      message += _T("Exception when reloading script: ") + (*cit)->get_wscript() + _T(": ") + e.getMessage(); 
     159      NSC_LOG_ERROR_STD(_T("Exception when reloading script: ") + (*cit)->get_wscript() + _T(": ") + e.getMessage()); 
    138160    } catch (...) { 
    139161      error = true; 
    140       message += _T("Unhandeled Exception when reloading script: ") + (*cit)->get_script(); 
    141       NSC_LOG_ERROR_STD(_T("Unhandeled Exception when reloading script: ") + (*cit)->get_script()); 
     162      message += _T("Unhandeled Exception when reloading script: ") + (*cit)->get_wscript(); 
     163      NSC_LOG_ERROR_STD(_T("Unhandeled Exception when reloading script: ") + (*cit)->get_wscript()); 
    142164    } 
    143165  } 
  • modules/LUAScript/LUAScript.h

    r3bdaf18 r04ef932  
    2626#include "script_wrapper.hpp" 
    2727 
     28#include <boost/optional.hpp> 
     29 
     30#include <scripts/functions.hpp> 
     31 
     32 
    2833class LUAScript : public nscapi::impl::SimpleCommand, public script_wrapper::lua_handler, public nscapi::impl::simple_plugin { 
    2934private: 
     
    4146  }; 
    4247 
     48  script_container::list_type scripts_; 
     49 
    4350  typedef std::map<std::wstring,lua_func> cmd_list; 
    44   typedef std::list<script_wrapper::lua_script*> script_list; 
     51  typedef std::list<boost::shared_ptr<script_wrapper::lua_script> > script_list; 
    4552 
    4653  cmd_list commands_; 
    47   script_list scripts_; 
    48   std::wstring root_; 
     54  script_list instances_; 
     55  boost::filesystem::wpath root_; 
    4956 
    5057public: 
     
    7178  bool hasCommandHandler(); 
    7279  bool hasMessageHandler(); 
    73   bool loadScript(const std::wstring script); 
     80  boost::optional<boost::filesystem::wpath> find_file(std::wstring file); 
     81  bool loadScript(std::wstring alias, std::wstring file); 
    7482  NSCAPI::nagiosReturn handleCommand(const std::wstring command, std::list<std::wstring> arguments, std::wstring &message, std::wstring &perf); 
    7583  //NSCAPI::nagiosReturn RunLUA(const unsigned int argLen, wchar_t **char_args, std::wstring &message, std::wstring &perf); 
  • modules/LUAScript/script_wrapper.hpp

    rd66ccee r04ef932  
    1010#include "luna.h" 
    1111 
     12#include <scripts/functions.hpp> 
    1213 
    1314namespace script_wrapper { 
     
    304305  class lua_script { 
    305306    Lua_State L; 
    306     std::wstring script_; 
    307   public: 
    308     lua_script(const std::wstring file) : script_(file) { 
     307    std::string script_; 
     308    std::string alias_; 
     309  public: 
     310    lua_script(const script_container &script) : script_(utf8::cvt<std::string>(script.script.string())), alias_(utf8::cvt<std::string>(script.alias)) { 
    309311      load(); 
    310312    } 
     
    312314      luaL_openlibs(L); 
    313315      nsclient_wrapper::luaopen(L); 
    314       //Luna<Account>::Register(L); 
    315       //lua_register(L, "register_command", register_command); 
    316  
    317       if (luaL_loadfile(L, strEx::wstring_to_string(script_).c_str()) != 0) { 
    318         throw LUAException(_T("Failed to load script: ") + script_ + _T(": ") + s2w(lua_tostring(L, -1))); 
    319       } 
    320  
    321     } 
    322     std::wstring get_script() const { 
     316      if (luaL_loadfile(L, script_.c_str()) != 0) { 
     317        throw LUAException(_T("Failed to load script: ") + get_wscript() + _T(": ") + s2w(lua_tostring(L, -1))); 
     318      } 
     319 
     320    } 
     321    std::wstring get_wscript() const { 
     322      return utf8::cvt<std::wstring>(script_); 
     323    } 
     324    std::string get_script() const { 
    323325      return script_; 
    324326    } 
     
    333335      lua_manager::set_script(L, this); 
    334336      if (lua_pcall(L, 0, 0, 0) != 0) { 
    335         throw LUAException(_T("Failed to parse script: ") + script_ + _T(": ") + s2w(lua_tostring(L, -1))); 
     337        throw LUAException(_T("Failed to parse script: ") + get_wscript() + _T(": ") + s2w(lua_tostring(L, -1))); 
    336338      } 
    337339    } 
     
    368370      if (!lua_isfunction(L, -1)) { 
    369371        lua_pop(L, 1); // remove function from LUA stack 
    370         throw LUAException(_T("Failed to run script: ") + script_ + _T(": Function not found: handle")); 
     372        throw LUAException(_T("Failed to run script: ") + get_wscript() + _T(": Function not found: handle")); 
    371373      } 
    372374      lua_pushstring(L, w2s(cmd).c_str());  
     
    382384      if (lua_pcall(L, 2, LUA_MULTRET, 0) != 0) { 
    383385        std::wstring err = strEx::string_to_wstring(lua_tostring(L, -1)); 
    384         NSC_LOG_ERROR_STD(_T("Failed to call main function in script: ") + script_ + _T(": ") + err); 
     386        NSC_LOG_ERROR_STD(_T("Failed to call main function in script: ") + get_wscript() + _T(": ") + err); 
    385387        lua_pop(L, 1); // remove error message 
    386388        return NSCAPI::returnUNKNOWN; 
  • modules/NRPEClient/CMakeLists.txt

    rd66ccee r04ef932  
    3030 
    3131add_library(${TARGET} MODULE ${SRCS}) 
     32INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR}) 
    3233 
    3334target_link_libraries(${TARGET} 
  • modules/NRPEServer/CMakeLists.txt

    rd66ccee r04ef932  
    4444 
    4545add_library(${TARGET} MODULE ${SRCS}) 
     46INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR}) 
    4647 
    4748target_link_libraries(${TARGET} 
  • modules/NSCAAgent/CMakeLists.txt

    rd66ccee r04ef932  
    3838 
    3939add_library(${TARGET} MODULE ${SRCS}) 
     40INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR}) 
    4041 
    4142target_link_libraries(${TARGET} 
Note: See TracChangeset for help on using the changeset viewer.