Changeset 4b1e6fe in nscp


Ignore:
Timestamp:
08/30/11 23:08:13 (21 months ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
1307e3f5
Parents:
60e6c18
Message:
  • Fixed uncaught exception in test client
  • Added protocol buffers to source (needs to be added to installer as well)
  • Added example python protocol buffer handler
  • Added lib folder to python scripting (and automagicall load)
Files:
21 added
21 edited

Legend:

Unmodified
Added
Removed
  • build/cmake/NSCPPBuffer.cmake

    rafd42f1 r4b1e6fe  
    9191    SET(PB_TARGET_INCLUDE ${INCL}) 
    9292    #configure_file(${ABS_FIL}.h.in ${ABS_FIL}.h) 
     93    file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/scripts/python/lib) 
    9394 
    9495      ADD_CUSTOM_COMMAND( 
    95         OUTPUT ${${VAR}} ${INCL} 
     96        OUTPUT ${${VAR}} ${INCL}  ${PROJECT_BINARY_DIR}/scripts/python/lib/${FIL_WE}_pb2.py 
    9697        COMMAND  ${PROTOBUF_PROTOC_EXECUTABLE} 
    97         ARGS --cpp_out  ${CMAKE_CURRENT_BINARY_DIR} --proto_path ${CMAKE_CURRENT_SOURCE_DIR} ${ABS_FIL} 
     98        ARGS --cpp_out  ${CMAKE_CURRENT_BINARY_DIR} --python_out ${PROJECT_BINARY_DIR}/scripts/python/lib --proto_path ${CMAKE_CURRENT_SOURCE_DIR} ${ABS_FIL} 
    9899        DEPENDS ${ABS_FIL} 
    99100        COMMENT "Running protocol buffer compiler on ${FIL} - ${PROTOBUF_PROTOC_EXECUTABLE}" VERBATIM ) 
  • changelog

    r60e6c18 r4b1e6fe  
    55 * Fixa dependonservice LanManWorkStation (old win) 
    66 * Fix RtlStringFromGUID problem on NT4 
     7 
     82011-08-30 MickeM 
     9 * Fixed uncaught exception in test client 
     10 * Added protocol buffers to source (needs to be added to installer as well) 
     11 * Added example python protocol buffer handler 
     12 * Added lib folder to python scripting (and automagicall load) 
    713 
    8142011-08-29 MickeM 
  • include/NSCAPI.h

    rb9498ef r4b1e6fe  
    114114  class nscapi_exception : public std::exception { 
    115115  public: 
    116     std::wstring msg_; 
    117     nscapi_exception(std::wstring msg) : msg_(msg) {} 
     116    std::string msg_; 
     117    nscapi_exception(std::wstring msg) : msg_(utf8::cvt<std::string>(msg)) {} 
    118118 
    119119 
    120     virtual ~nscapi_exception() throw() {} 
    121     std::string what() { 
    122       return utf8::cvt<std::string>(msg_); 
     120    ~nscapi_exception() throw() {} 
     121    const char* what() const throw() { 
     122      return msg_.c_str(); 
     123    } 
     124    const std::wstring wwhat() const throw() { 
     125      return utf8::cvt<std::wstring>(msg_); 
    123126    } 
    124127  }; 
  • include/nscapi/functions.hpp

    rd7e265d r4b1e6fe  
    180180 
    181181 
    182       if (message.payload_size() != 1) { 
     182      if (message.payload_size() == 0) { 
     183        return; 
     184      } else if (message.payload_size() > 1) { 
    183185        throw nscapi_exception(_T("Whoops, invalid payload size (for now)")); 
    184186      } 
  • include/nscapi/macros.hpp

    r8840f09 r4b1e6fe  
    8282  try { \ 
    8383  return GET_PLUGIN()->wrapLoadModule(toObject.loadModuleEx(alias, mode)); \ 
    84     } catch (nscapi::nscapi_exception e) { \ 
    85     NSC_LOG_CRITICAL(_T("NSCMHE in: wrapLoadModule: " + e.msg_)); \ 
    86     return NSCAPI::hasFailed; \ 
    8784    } catch (...) { \ 
    8885    NSC_LOG_CRITICAL(_T("Unknown exception in: wrapLoadModule(...)")); \ 
     
    9390  try { \ 
    9491  return GET_PLUGIN()->wrapLoadModule(toObject.loadModule()); \ 
    95     } catch (nscapi::nscapi_exception e) { \ 
    96     NSC_LOG_CRITICAL(_T("NSCMHE in: wrapLoadModule: " + e.msg_)); \ 
    97     return NSCAPI::hasFailed; \ 
    9892    } catch (...) { \ 
    9993    NSC_LOG_CRITICAL(_T("Unknown exception in: wrapLoadModule(...)")); \ 
  • include/settings/client/settings_client.hpp

    rfe75eff r4b1e6fe  
    593593                v->key->notify(core_, v->path, v->key_name); 
    594594            } 
    595           } catch (const nscapi::nscapi_exception &e) { 
    596             core_->err(__FILE__, __LINE__, _T("Failed to notify ") + v->key_name + _T(": ") + e.msg_); 
    597595          } catch (const std::exception &e) { 
    598596            core_->err(__FILE__, __LINE__, _T("Failed to notify ") + v->key_name + _T(": ") + utf8::cvt<std::wstring>(e.what())); 
     
    605603            if (v->path) 
    606604              v->path->notify(core_, v->path_name); 
    607           } catch (const nscapi::nscapi_exception &e) { 
    608             core_->err(__FILE__, __LINE__, _T("Failed to notify ") + v->path_name + _T(": ") + e.msg_); 
    609605          } catch (const std::exception &e) { 
    610606            core_->err(__FILE__, __LINE__, _T("Failed to notify ") + v->path_name + _T(": ") + utf8::cvt<std::wstring>(e.what())); 
  • modules/CheckDisk/CheckDisk.cpp

    rfe75eff r4b1e6fe  
    7272      _T("SHOW ERRORS"), _T("")) 
    7373      ; 
     74  } catch (nscapi::nscapi_exception &e) { 
     75    NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(e.what())); 
     76    return false; 
    7477  } catch (std::exception &e) { 
    75     NSC_LOG_ERROR_STD(_T("Exception caught: ") + utf8::cvt<std::wstring>(e.what())); 
    76     return false; 
    77   } catch (nscapi::nscapi_exception &e) { 
    78     NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_); 
     78    NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 
    7979    return false; 
    8080  } catch (...) { 
  • modules/CheckEventLog/CheckEventLog.cpp

    rfe75eff r4b1e6fe  
    9898    settings.notify(); 
    9999 
     100  } catch (nscapi::nscapi_exception &e) { 
     101    NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(e.what())); 
     102    return false; 
    100103  } catch (std::exception &e) { 
    101     NSC_LOG_ERROR_STD(_T("Exception caught: ") + utf8::cvt<std::wstring>(e.what())); 
    102     return false; 
    103   } catch (nscapi::nscapi_exception &e) { 
    104     NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_); 
     104    NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 
    105105    return false; 
    106106  } catch (...) { 
  • modules/CheckHelpers/CheckHelpers.cpp

    rfe75eff r4b1e6fe  
    5656    get_core()->registerCommand(_T("CheckVersion"), _T("Just return the nagios version (along with OK status).")); 
    5757  } catch (nscapi::nscapi_exception &e) { 
    58     NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_); 
     58    NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(e.what())); 
     59    return false; 
     60  } catch (std::exception &e) { 
     61    NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 
     62    return false; 
    5963  } catch (...) { 
    6064    NSC_LOG_ERROR_STD(_T("Failed to register command.")); 
     65    return false; 
    6166  } 
    6267  return true; 
  • modules/CheckNSCP/CheckNSCP.cpp

    rfe75eff r4b1e6fe  
    5454    get_core()->registerCommand(_T("check_nscp"), _T("Check the internal healt of NSClient++.")); 
    5555  } catch (nscapi::nscapi_exception &e) { 
    56     NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_); 
     56    NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(e.what())); 
     57    return false; 
     58  } catch (std::exception &e) { 
     59    NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 
     60    return false; 
    5761  } catch (...) { 
    5862    NSC_LOG_ERROR_STD(_T("Failed to register command.")); 
     63    return false; 
    5964  } 
    6065  return true; 
  • modules/CheckSystem/CheckSystem.cpp

    rfe75eff r4b1e6fe  
    147147    get_core()->registerCommand(_T("checkSingleRegEntry"), _T("Check registry key")); 
    148148  } catch (nscapi::nscapi_exception &e) { 
    149     NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_); 
     149    NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(e.what())); 
     150    return false; 
     151  } catch (std::exception &e) { 
     152    NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 
     153    return false; 
    150154  } catch (...) { 
    151155    NSC_LOG_ERROR_STD(_T("Failed to register command.")); 
     156    return false; 
    152157  } 
    153158 
  • modules/CheckTaskSched/CheckTaskSched.cpp

    rfe75eff r4b1e6fe  
    5252    SETTINGS_REG_KEY_S(task_scheduler::SYNTAX); 
    5353  } catch (nscapi::nscapi_exception &e) { 
    54     NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_); 
     54    NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(e.what())); 
     55    return false; 
     56  } catch (std::exception &e) { 
     57    NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 
     58    return false; 
    5559  } catch (...) { 
    5660    NSC_LOG_ERROR_STD(_T("Failed to register command.")); 
     61    return false; 
    5762  } 
    5863  syntax = SETTINGS_GET_STRING(task_scheduler::SYNTAX); 
  • modules/CheckTaskSched2/CheckTaskSched2.cpp

    rfe75eff r4b1e6fe  
    5151    SETTINGS_REG_KEY_S(task_scheduler::SYNTAX); 
    5252  } catch (nscapi::nscapi_exception &e) { 
    53     NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_); 
     53    NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(e.what())); 
     54    return false; 
     55  } catch (std::exception &e) { 
     56    NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 
     57    return false; 
    5458  } catch (...) { 
    5559    NSC_LOG_ERROR_STD(_T("Failed to register command.")); 
     60    return false; 
    5661  } 
    5762  syntax = SETTINGS_GET_STRING(task_scheduler::SYNTAX); 
  • modules/CheckWMI/CheckWMI.cpp

    rfe75eff r4b1e6fe  
    7676 
    7777  } catch (nscapi::nscapi_exception &e) { 
    78     NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_); 
     78    NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(e.what())); 
     79  } catch (std::exception &e) { 
     80    NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 
    7981  } catch (...) { 
    8082    NSC_LOG_ERROR_STD(_T("Failed to register command.")); 
     
    104106 
    105107  } catch (nscapi::nscapi_exception &e) { 
    106     NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_); 
     108    NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(e.what())); 
     109    return false; 
     110  } catch (std::exception &e) { 
     111    NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 
     112    return false; 
    107113  } catch (...) { 
    108114    NSC_LOG_ERROR_STD(_T("Failed to register command.")); 
     115    return false; 
    109116  } 
    110117  return true; 
  • modules/FileLogger/FileLogger.cpp

    r7ec3dd1 r4b1e6fe  
    155155    getFileName(); 
    156156 
     157  } catch (nscapi::nscapi_exception &e) { 
     158    NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(e.what())); 
     159    return false; 
    157160  } catch (std::exception &e) { 
    158161    NSC_LOG_ERROR_STD(_T("Exception caught: ") + utf8::cvt<std::wstring>(e.what())); 
    159     return false; 
    160   } catch (nscapi::nscapi_exception &e) { 
    161     NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_); 
    162162    return false; 
    163163  } catch (...) { 
  • modules/NSCAAgent/NSCAAgent.cpp

    rb9498ef r4b1e6fe  
    108108 
    109109  } catch (nscapi::nscapi_exception &e) { 
    110     NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_); 
     110    NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(e.what())); 
    111111    return false; 
    112112  } catch (std::exception &e) { 
  • modules/PythonScript/PythonScript.cpp

    rd7e265d r4b1e6fe  
    136136    PyRun_SimpleString("import sys"); 
    137137    PyRun_SimpleString("sys.stderr = cStringIO.StringIO()"); 
    138  
     138    boost::filesystem::wpath path = GET_CORE()->getBasePath(); 
     139    path /= _T("scripts"); 
     140    path /= _T("python"); 
     141    path /= _T("lib"); 
     142    PyRun_SimpleString(("sys.path.append('" + utf8::cvt<std::string>(path.string()) + "')").c_str()); 
    139143    object ignored = exec_file(scriptfile.c_str(), localDict, localDict);  
    140144  } catch( error_already_set e) { 
  • modules/Scheduler/Scheduler.cpp

    r60e6c18 r4b1e6fe  
    6868 
    6969  } catch (nscapi::nscapi_exception &e) { 
    70     NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_); 
     70    NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(e.what())); 
    7171    return false; 
    7272  } catch (std::exception &e) { 
    73     NSC_LOG_ERROR_STD(_T("Exception caught: ") + utf8::cvt<std::wstring>(e.what())); 
     73    NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 
    7474    return false; 
    7575  } catch (...) { 
     
    8484    } 
    8585  } catch (nscapi::nscapi_exception &e) { 
    86     NSC_LOG_ERROR_STD(_T("Exception in module Scheduler: ") + e.msg_); 
     86    NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(e.what())); 
     87    return false; 
     88  } catch (std::exception &e) { 
     89    NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 
    8790    return false; 
    8891  } catch (...) { 
     
    161164    } 
    162165  } catch (nscapi::nscapi_exception &e) { 
    163     NSC_LOG_ERROR_STD(_T("Exception handling: ") + item.alias + _T(": ") + e.msg_); 
     166    NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(e.what())); 
     167    scheduler_.remove_task(item.id); 
     168  } catch (std::exception &e) { 
     169    NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 
    164170    scheduler_.remove_task(item.id); 
    165171  } catch (...) { 
  • scripts/CMakeLists.txt

    r04ef932 r4b1e6fe  
    55FILE (GLOB scripts_PS1 "*.ps1") 
    66FILE (GLOB scripts_LIB "lib/*.vbs") 
    7 FILE (GLOB scripts_PYT "python/*.py") 
     7IF(WIN32) 
     8FILE (GLOB_RECURSE scripts_PYT "python/*.py") 
     9ELSE(WIN32) 
     10FILE (GLOB scripts_PYT "python/*.py" "python/lib/*.py") 
     11ENDIF(WIN32) 
    812FILE (GLOB scripts_LUA "lua/*.lua") 
    913 
     
    2529FOREACH(file ${scripts_PYT}) 
    2630  get_filename_component(filename ${file} NAME) 
    27   #MESSAGE(STATUS " + lib - ${filename}") 
    28   copy_single_file("script python" python/${filename} scripts/python) 
     31  get_filename_component(filepath ${file} PATH) 
     32  file(RELATIVE_PATH relpath ${CMAKE_CURRENT_SOURCE_DIR} "${filepath}") 
     33  STRING(REPLACE "/" "_" alias "${relpath}") 
     34  STRING(REPLACE "." "_" alias "${alias}") 
     35  #MESSAGE(STATUS " + python - ${filename} - ${relpath} - ${alias}") 
     36  copy_single_file("python_${alias}" ${relpath}/${filename} scripts/${relpath}) 
    2937ENDFOREACH(file ${scripts_PYT}) 
    3038 
  • scripts/python/test.py

    r2c95d22 r4b1e6fe  
    11from NSCP import Settings, Registry, Core, log, status, get_alias 
     2#import sys 
     3#sys.path.append('D:/source/nscp/build/x64/scripts/python/include') 
     4 
     5import plugin_pb2 
    26 
    37core = Core.get() 
     8 
     9prefix = 'py_' 
    410 
    511def get_help(arguments): 
     
    1622  log('Data: %d %s %s'%(code, message, perf)) 
    1723 
    18 prefix = 'py_' 
    1924def test(arguments): 
    2025  global prefix 
     
    3136  if perf != "'args'=2": 
    3237    return (status.CRITICAL, "Test failed: -%s-"%perf) 
    33   return (status.OK, 'Tests ok') 
     38     
     39  message = plugin_pb2.QueryRequestMessage() 
     40   
     41  message.header.type = plugin_pb2.Common.Header.QUERY_REQUEST 
     42  message.header.version = plugin_pb2.Common.VERSION_1 
     43 
     44  payload = message.payload.add() 
     45  payload.command = "%snormal"%prefix 
     46  payload.arguments.append("hello") 
     47 
     48  (retcode, buffer) = core.query("%snormal"%prefix, message.SerializeToString()) 
     49  message = plugin_pb2.QueryResponseMessage() 
     50  message.ParseFromString(buffer) 
     51  if message.payload[0].perf[0].alias != "args": 
     52    return (status.CRITICAL, "Test failed: -%s-"%message.payload[0].perf[0].alias) 
     53  if message.payload[0].perf[0].float_value.value != 1: 
     54    return (status.CRITICAL, "Test failed: -%s-"%message.payload[0].perf[0].float_value.value) 
     55   
     56  return (status.OK, 'All tests ok') 
    3457 
    3558def normal(arguments): 
     
    5881    log('Got argument: %s'%a) 
    5982   
     83def simple_pb(command, buffer): 
     84  message = plugin_pb2.QueryRequestMessage() 
     85  message.ParseFromString(buffer) 
     86  for p in message.payload: 
     87    log('Command: %s'%p.command) 
     88    for a in p.arguments: 
     89      log('Arg: %s'%a) 
     90   
     91  response = plugin_pb2.QueryResponseMessage() 
     92  response.header.type = plugin_pb2.Common.Header.QUERY_RESPONSE 
     93  response.header.version = plugin_pb2.Common.VERSION_1 
     94 
     95  payload = response.payload.add() 
     96  payload.command = command 
     97  payload.result = plugin_pb2.Common.OK 
     98  payload.message = 'This is more difficult...' 
     99 
     100  return (status.OK, response.SerializeToString()) 
     101 
    60102def init(alias): 
    61103  global prefix 
     
    77119  reg.simple_function('%snom'%prefix, no_msg, 'No performance data') 
    78120  reg.simple_function('%snor'%prefix, no_ret, 'No performance data') 
     121   
     122  reg.function('%spb'%prefix, simple_pb, 'Simple protocolbuffer sample') 
     123   
    79124 
    80125  reg.simple_cmdline('help', get_help) 
     
    83128  reg.simple_subscription('%stest'%prefix, test_channel) 
    84129 
    85   (ret, list) = core.simple_submit('%stest'%prefix, 'test.py', status.WARNING, 'hello', '') 
     130  core.simple_submit('%stest'%prefix, 'test.py', status.WARNING, 'hello', '') 
    86131   
    87132  (ret, list) = core.simple_exec('%stest'%prefix, ['a', 'b', 'c']) 
     
    98143 
    99144  log('Saving configuration...') 
    100   conf.save() 
     145  #conf.save() 
    101146 
    102147def shutdown(): 
  • service/simple_client.hpp

    r04ef932 r4b1e6fe  
    6464          throw "test"; 
    6565        } else { 
    66           strEx::token t = strEx::getToken(s, ' '); 
    67           std::wstring msg, perf; 
    68           NSCAPI::nagiosReturn ret = core_->inject(t.first, t.second, msg, perf); 
    69           if (ret == NSCAPI::returnIgnored) { 
    70             log(_T("No handler for command: ") + t.first); 
    71           } else { 
    72             log(nscapi::plugin_helper::translateReturn(ret) + _T(":") + msg); 
    73             if (!perf.empty()) 
    74               log(_T(" Perfoamcen data: ") + perf); 
     66          try { 
     67            strEx::token t = strEx::getToken(s, ' '); 
     68            std::wstring msg, perf; 
     69            NSCAPI::nagiosReturn ret = core_->inject(t.first, t.second, msg, perf); 
     70            if (ret == NSCAPI::returnIgnored) { 
     71              log(_T("No handler for command: ") + t.first); 
     72            } else { 
     73              log(nscapi::plugin_helper::translateReturn(ret) + _T(":") + msg); 
     74              if (!perf.empty()) 
     75                log(_T(" Performance data: ") + perf); 
     76            } 
     77          } catch (const nscapi::nscapi_exception &e) { 
     78            log(_T("NSCAPI Exception: ") + utf8::cvt<std::wstring>(e.what())); 
     79          } catch (const std::exception &e) { 
     80            log(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 
     81          } catch (...) { 
     82            log(_T("Unknown exception")); 
    7583          } 
    7684        } 
Note: See TracChangeset for help on using the changeset viewer.