| 1 | #include "stdafx.h"
|
|---|
| 2 |
|
|---|
| 3 | #include <strEx.h>
|
|---|
| 4 | #include "script_wrapper.hpp"
|
|---|
| 5 | #include "PythonScript.h"
|
|---|
| 6 | #include <nscapi/functions.hpp>
|
|---|
| 7 | #include <nscapi/nscapi_core_helper.hpp>
|
|---|
| 8 | #include <boost/thread.hpp>
|
|---|
| 9 |
|
|---|
| 10 | using namespace boost::python;
|
|---|
| 11 | namespace py = boost::python;
|
|---|
| 12 |
|
|---|
| 13 | boost::shared_ptr<script_wrapper::functions> script_wrapper::functions::instance;
|
|---|
| 14 |
|
|---|
| 15 | //extern PythonScript gPythonScript;
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 | script_wrapper::status script_wrapper::nagios_return_to_py(int code) {
|
|---|
| 19 | if (code == NSCAPI::returnOK)
|
|---|
| 20 | return OK;
|
|---|
| 21 | if (code == NSCAPI::returnWARN)
|
|---|
| 22 | return WARN;
|
|---|
| 23 | if (code == NSCAPI::returnCRIT)
|
|---|
| 24 | return CRIT;
|
|---|
| 25 | if (code == NSCAPI::returnUNKNOWN)
|
|---|
| 26 | return UNKNOWN;
|
|---|
| 27 | NSC_LOG_ERROR_STD(_T("Invalid return code: ") + strEx::itos(code));
|
|---|
| 28 | return UNKNOWN;
|
|---|
| 29 | }
|
|---|
| 30 | int script_wrapper::py_to_nagios_return(status code) {
|
|---|
| 31 | NSCAPI::nagiosReturn c = NSCAPI::returnUNKNOWN;
|
|---|
| 32 | if (code == OK)
|
|---|
| 33 | return NSCAPI::returnOK;
|
|---|
| 34 | if (code == WARN)
|
|---|
| 35 | return NSCAPI::returnWARN;
|
|---|
| 36 | if (code == CRIT)
|
|---|
| 37 | return NSCAPI::returnCRIT;
|
|---|
| 38 | if (code == UNKNOWN)
|
|---|
| 39 | return NSCAPI::returnUNKNOWN;
|
|---|
| 40 | NSC_LOG_ERROR_STD(_T("Invalid return code: ") + strEx::itos(c));
|
|---|
| 41 | return NSCAPI::returnUNKNOWN;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | std::string pystr(object o) {
|
|---|
| 46 | try {
|
|---|
| 47 | if (o.ptr() == Py_None)
|
|---|
| 48 | return "";
|
|---|
| 49 | if(PyUnicode_Check(o.ptr())) {
|
|---|
| 50 | std::string s = PyBytes_AsString(PyUnicode_AsEncodedString(o.ptr(), "utf-8", "Error"));
|
|---|
| 51 | return s;
|
|---|
| 52 | }
|
|---|
| 53 | return extract<std::string>(o);
|
|---|
| 54 | } catch (...) {
|
|---|
| 55 | NSC_LOG_ERROR(_T("Failed to convert python string"));
|
|---|
| 56 | return "Unable to convert python string";
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 | std::string pystr(boost::python::api::object_item o) {
|
|---|
| 60 | try {
|
|---|
| 61 | object po = o;
|
|---|
| 62 | return pystr(po);
|
|---|
| 63 | } catch (...) {
|
|---|
| 64 | NSC_LOG_ERROR(_T("Failed to convert python string"));
|
|---|
| 65 | return "Unable to convert python string";
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | object pystr(std::wstring str) {
|
|---|
| 70 | return boost::python::object(boost::python::handle<>(PyUnicode_FromString(utf8::cvt<std::string>(str).c_str())));
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | std::wstring pywstr(object o) {
|
|---|
| 74 | return utf8::cvt<std::wstring>(pystr(o));
|
|---|
| 75 | }
|
|---|
| 76 | std::wstring pywstr(boost::python::api::object_item o) {
|
|---|
| 77 | return utf8::cvt<std::wstring>(pystr(o));
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 | std::list<std::wstring> script_wrapper::convert(py::list lst) {
|
|---|
| 82 | std::list<std::wstring> ret;
|
|---|
| 83 | for (int i = 0;i<len(lst);i++) {
|
|---|
| 84 | try {
|
|---|
| 85 | extract<std::string> es(lst[i]);
|
|---|
| 86 | extract<long long> ei(lst[i]);
|
|---|
| 87 | if (es.check())
|
|---|
| 88 | ret.push_back(utf8::cvt<std::wstring>(es()));
|
|---|
| 89 | else if (ei.check())
|
|---|
| 90 | ret.push_back(strEx::itos(ei()));
|
|---|
| 91 | else
|
|---|
| 92 | NSC_LOG_ERROR_STD(_T("Failed to convert object in list"));
|
|---|
| 93 | } catch( error_already_set e) {
|
|---|
| 94 | log_exception();
|
|---|
| 95 | } catch (...) {
|
|---|
| 96 | NSC_LOG_ERROR_STD(_T("Failed to parse list"));
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 | return ret;
|
|---|
| 100 | }
|
|---|
| 101 | py::list script_wrapper::convert(std::list<std::wstring> lst) {
|
|---|
| 102 | py::list ret;
|
|---|
| 103 | BOOST_FOREACH(std::wstring s, lst) {
|
|---|
| 104 | ret.append(utf8::cvt<std::string>(s));
|
|---|
| 105 | }
|
|---|
| 106 | return ret;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 | void script_wrapper::log_msg(object x) {
|
|---|
| 111 | std::wstring msg = pywstr(x);
|
|---|
| 112 | {
|
|---|
| 113 | thread_unlocker unlocker;
|
|---|
| 114 | NSC_LOG_MESSAGE(msg);
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|
| 117 | void script_wrapper::log_error(object x) {
|
|---|
| 118 | std::wstring msg = pywstr(x);
|
|---|
| 119 | {
|
|---|
| 120 | thread_unlocker unlocker;
|
|---|
| 121 | NSC_LOG_ERROR_STD(msg);
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 | void script_wrapper::log_debug(object x) {
|
|---|
| 125 | std::wstring msg = pywstr(x);
|
|---|
| 126 | {
|
|---|
| 127 | thread_unlocker unlocker;
|
|---|
| 128 | NSC_DEBUG_MSG(msg);
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 | void script_wrapper::sleep(unsigned int ms) {
|
|---|
| 132 | {
|
|---|
| 133 | thread_unlocker unlocker;
|
|---|
| 134 | {
|
|---|
| 135 | boost::this_thread::sleep(boost::posix_time::milliseconds(ms));
|
|---|
| 136 |
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 | /*
|
|---|
| 141 | std::string script_wrapper::get_alias() {
|
|---|
| 142 | return utf8::cvt<std::string>(gPythonScript.get_alias());
|
|---|
| 143 | }
|
|---|
| 144 | */
|
|---|
| 145 |
|
|---|
| 146 | void script_wrapper::log_exception() {
|
|---|
| 147 | try {
|
|---|
| 148 | PyErr_Print();
|
|---|
| 149 | boost::python::object sys(boost::python::handle<>(PyImport_ImportModule("sys")));
|
|---|
| 150 | boost::python::object err = sys.attr("stderr");
|
|---|
| 151 | std::string err_text = boost::python::extract<std::string>(err.attr("getvalue")());
|
|---|
| 152 | NSC_LOG_ERROR_STD(utf8::cvt<std::wstring>(err_text));
|
|---|
| 153 | PyErr_Clear();
|
|---|
| 154 | } catch (const std::exception &e) {
|
|---|
| 155 | NSC_LOG_ERROR_STD(_T("Failed to parse error: ") + utf8::cvt<std::wstring>(e.what()));
|
|---|
| 156 | PyErr_Clear();
|
|---|
| 157 | } catch (...) {
|
|---|
| 158 | NSC_LOG_ERROR_STD(_T("Failed to parse python error"));
|
|---|
| 159 | PyErr_Clear();
|
|---|
| 160 | }
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | void script_wrapper::function_wrapper::subscribe_simple_function(std::string channel, PyObject* callable) {
|
|---|
| 164 | try {
|
|---|
| 165 | core->registerSubmissionListener(plugin_id, utf8::cvt<std::wstring>(channel));
|
|---|
| 166 | boost::python::handle<> h(boost::python::borrowed(callable));
|
|---|
| 167 | //return boost::python::object o(h);
|
|---|
| 168 | functions::get()->simple_handler[channel] = h;
|
|---|
| 169 | } catch (const std::exception &e) {
|
|---|
| 170 | NSC_LOG_ERROR_STD(_T("Failed to subscribe to channel ") + utf8::cvt<std::wstring>(channel) + _T(": ") + utf8::cvt<std::wstring>(e.what()));
|
|---|
| 171 | } catch (...) {
|
|---|
| 172 | NSC_LOG_ERROR_STD(_T("Failed to subscribe to channel ") + utf8::cvt<std::wstring>(channel));
|
|---|
| 173 | }
|
|---|
| 174 | }
|
|---|
| 175 | void script_wrapper::function_wrapper::subscribe_function(std::string channel, PyObject* callable) {
|
|---|
| 176 | try {
|
|---|
| 177 | core->registerSubmissionListener(plugin_id, utf8::cvt<std::wstring>(channel));
|
|---|
| 178 | boost::python::handle<> h(boost::python::borrowed(callable));
|
|---|
| 179 | functions::get()->normal_handler[channel] = h;
|
|---|
| 180 | } catch (const std::exception &e) {
|
|---|
| 181 | NSC_LOG_ERROR_STD(_T("Failed to subscribe to channel ") + utf8::cvt<std::wstring>(channel) + _T(": ") + utf8::cvt<std::wstring>(e.what()));
|
|---|
| 182 | } catch (...) {
|
|---|
| 183 | NSC_LOG_ERROR_STD(_T("Failed to subscribe to channel ") + utf8::cvt<std::wstring>(channel));
|
|---|
| 184 | }
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 | void script_wrapper::function_wrapper::register_simple_function(std::string name, PyObject* callable, std::string desc) {
|
|---|
| 189 | try {
|
|---|
| 190 | core->registerCommand(plugin_id, utf8::cvt<std::wstring>(name), utf8::cvt<std::wstring>(desc));
|
|---|
| 191 | boost::python::handle<> h(boost::python::borrowed(callable));
|
|---|
| 192 | functions::get()->simple_functions[name] = h;
|
|---|
| 193 | } catch (...) {
|
|---|
| 194 | NSC_LOG_ERROR_STD(_T("Failed to register functions: ") + utf8::cvt<std::wstring>(name));
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 | void script_wrapper::function_wrapper::register_function(std::string name, PyObject* callable, std::string desc) {
|
|---|
| 198 | try {
|
|---|
| 199 | core->registerCommand(plugin_id, utf8::cvt<std::wstring>(name), utf8::cvt<std::wstring>(desc));
|
|---|
| 200 | boost::python::handle<> h(boost::python::borrowed(callable));
|
|---|
| 201 | functions::get()->normal_functions[name] = h;
|
|---|
| 202 | } catch (...) {
|
|---|
| 203 | NSC_LOG_ERROR_STD(_T("Failed to register functions: ") + utf8::cvt<std::wstring>(name));
|
|---|
| 204 | }
|
|---|
| 205 | }
|
|---|
| 206 | void script_wrapper::function_wrapper::register_simple_cmdline(std::string name, PyObject* callable) {
|
|---|
| 207 | try {
|
|---|
| 208 | boost::python::handle<> h(boost::python::borrowed(callable));
|
|---|
| 209 | functions::get()->simple_cmdline[name] = h;
|
|---|
| 210 | } catch (...) {
|
|---|
| 211 | NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(name));
|
|---|
| 212 | }
|
|---|
| 213 | }
|
|---|
| 214 | void script_wrapper::function_wrapper::register_cmdline(std::string name, PyObject* callable) {
|
|---|
| 215 | try {
|
|---|
| 216 | boost::python::handle<> h(boost::python::borrowed(callable));
|
|---|
| 217 | functions::get()->normal_cmdline[name] = h;
|
|---|
| 218 | } catch (...) {
|
|---|
| 219 | NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(name));
|
|---|
| 220 | }
|
|---|
| 221 | }
|
|---|
| 222 | int script_wrapper::function_wrapper::handle_query(const std::string cmd, const std::string &request, std::string &response) const {
|
|---|
| 223 | try {
|
|---|
| 224 | functions::function_map_type::iterator it = functions::get()->normal_functions.find(cmd);
|
|---|
| 225 | if (it == functions::get()->normal_functions.end()) {
|
|---|
| 226 | NSC_LOG_ERROR_STD(_T("Failed to find python function: ") + utf8::cvt<std::wstring>(cmd));
|
|---|
| 227 | return NSCAPI::returnIgnored;
|
|---|
| 228 | }
|
|---|
| 229 | {
|
|---|
| 230 | thread_locker locker;
|
|---|
| 231 | try {
|
|---|
| 232 | tuple ret = boost::python::call<tuple>(boost::python::object(it->second).ptr(), cmd, request);
|
|---|
| 233 | if (ret.ptr() == Py_None) {
|
|---|
| 234 | return NSCAPI::returnUNKNOWN;
|
|---|
| 235 | }
|
|---|
| 236 | int ret_code = NSCAPI::returnUNKNOWN;
|
|---|
| 237 | if (len(ret) > 0)
|
|---|
| 238 | ret_code = extract<int>(ret[0]);
|
|---|
| 239 | if (len(ret) > 1)
|
|---|
| 240 | response = extract<std::string>(ret[1]);
|
|---|
| 241 | return ret_code;
|
|---|
| 242 | } catch( error_already_set e) {
|
|---|
| 243 | log_exception();
|
|---|
| 244 | return NSCAPI::returnUNKNOWN;
|
|---|
| 245 | }
|
|---|
| 246 | }
|
|---|
| 247 | } catch(...) {
|
|---|
| 248 | NSC_LOG_ERROR_STD(_T("Exception in ") + utf8::cvt<std::wstring>(cmd));
|
|---|
| 249 | return NSCAPI::returnUNKNOWN;
|
|---|
| 250 | }
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | int script_wrapper::function_wrapper::handle_simple_query(const std::string cmd, std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) const {
|
|---|
| 254 | try {
|
|---|
| 255 | functions::function_map_type::iterator it = functions::get()->simple_functions.find(cmd);
|
|---|
| 256 | if (it == functions::get()->simple_functions.end()) {
|
|---|
| 257 | NSC_LOG_ERROR_STD(_T("Failed to find python function: ") + utf8::cvt<std::wstring>(cmd));
|
|---|
| 258 | return NSCAPI::returnIgnored;
|
|---|
| 259 | }
|
|---|
| 260 | {
|
|---|
| 261 | thread_locker locker;
|
|---|
| 262 |
|
|---|
| 263 | try {
|
|---|
| 264 | py::list l;
|
|---|
| 265 | BOOST_FOREACH(std::wstring a, arguments) {
|
|---|
| 266 | l.append(utf8::cvt<std::string>(a));
|
|---|
| 267 | }
|
|---|
| 268 | object ret = boost::python::call<object>(boost::python::object(it->second).ptr(), l);
|
|---|
| 269 | if (ret.ptr() == Py_None) {
|
|---|
| 270 | msg = _T("None");
|
|---|
| 271 | return NSCAPI::returnUNKNOWN;
|
|---|
| 272 | }
|
|---|
| 273 | int ret_code = NSCAPI::returnUNKNOWN;
|
|---|
| 274 | if (len(ret) > 0)
|
|---|
| 275 | ret_code = extract<int>(ret[0]);
|
|---|
| 276 | if (len(ret) > 1)
|
|---|
| 277 | msg = pywstr(ret[1]);
|
|---|
| 278 | if (len(ret) > 2)
|
|---|
| 279 | perf = pywstr(ret[2]);
|
|---|
| 280 | return ret_code;
|
|---|
| 281 | } catch( error_already_set e) {
|
|---|
| 282 | log_exception();
|
|---|
| 283 | msg = _T("Exception in: ") + utf8::cvt<std::wstring>(cmd);
|
|---|
| 284 | return NSCAPI::returnUNKNOWN;
|
|---|
| 285 | }
|
|---|
| 286 | }
|
|---|
| 287 | } catch(const std::exception &e) {
|
|---|
| 288 | msg = _T("Exception in ") + utf8::cvt<std::wstring>(cmd) + _T(": ") + utf8::cvt<std::wstring>(e.what());
|
|---|
| 289 | return NSCAPI::returnUNKNOWN;
|
|---|
| 290 | } catch(...) {
|
|---|
| 291 | msg = _T("Exception in ") + utf8::cvt<std::wstring>(cmd);
|
|---|
| 292 | return NSCAPI::returnUNKNOWN;
|
|---|
| 293 | }
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | bool script_wrapper::function_wrapper::has_function(const std::string command) {
|
|---|
| 297 | return functions::get()->normal_functions.find(command) != functions::get()->normal_functions.end();
|
|---|
| 298 | }
|
|---|
| 299 | bool script_wrapper::function_wrapper::has_simple(const std::string command) {
|
|---|
| 300 | return functions::get()->simple_functions.find(command) != functions::get()->simple_functions.end();
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | int script_wrapper::function_wrapper::handle_exec(const std::string cmd, const std::string &request, std::string &response) const {
|
|---|
| 304 | try {
|
|---|
| 305 | functions::function_map_type::iterator it = functions::get()->normal_cmdline.find(cmd);
|
|---|
| 306 | if (it == functions::get()->normal_cmdline.end()) {
|
|---|
| 307 | NSC_LOG_ERROR_STD(_T("Failed to find python function: ") + utf8::cvt<std::wstring>(cmd));
|
|---|
| 308 | return NSCAPI::returnIgnored;
|
|---|
| 309 | }
|
|---|
| 310 | {
|
|---|
| 311 | thread_locker locker;
|
|---|
| 312 | try {
|
|---|
| 313 | tuple ret = boost::python::call<tuple>(boost::python::object(it->second).ptr(), cmd, request);
|
|---|
| 314 | if (ret.ptr() == Py_None) {
|
|---|
| 315 | return NSCAPI::returnUNKNOWN;
|
|---|
| 316 | }
|
|---|
| 317 | int ret_code = NSCAPI::returnUNKNOWN;
|
|---|
| 318 | if (len(ret) > 0)
|
|---|
| 319 | ret_code = extract<int>(ret[0]);
|
|---|
| 320 | if (len(ret) > 1)
|
|---|
| 321 | response = pystr(ret[1]);
|
|---|
| 322 | return ret_code;
|
|---|
| 323 | } catch( error_already_set e) {
|
|---|
| 324 | log_exception();
|
|---|
| 325 | return NSCAPI::returnUNKNOWN;
|
|---|
| 326 | }
|
|---|
| 327 | }
|
|---|
| 328 | } catch(const std::exception &e) {
|
|---|
| 329 | NSC_LOG_ERROR_STD(_T("Exception in ") + utf8::cvt<std::wstring>(cmd) + _T(": ") + utf8::cvt<std::wstring>(e.what()));
|
|---|
| 330 | return NSCAPI::returnUNKNOWN;
|
|---|
| 331 | } catch(...) {
|
|---|
| 332 | NSC_LOG_ERROR_STD(_T("Exception in ") + utf8::cvt<std::wstring>(cmd));
|
|---|
| 333 | return NSCAPI::returnUNKNOWN;
|
|---|
| 334 | }
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | int script_wrapper::function_wrapper::handle_simple_exec(const std::string cmd, std::list<std::wstring> arguments, std::wstring &result) const {
|
|---|
| 338 | try {
|
|---|
| 339 | functions::function_map_type::iterator it = functions::get()->simple_cmdline.find(cmd);
|
|---|
| 340 | if (it == functions::get()->simple_cmdline.end()) {
|
|---|
| 341 | result = _T("Failed to find python function: ") + utf8::cvt<std::wstring>(cmd);
|
|---|
| 342 | NSC_LOG_ERROR_STD(result);
|
|---|
| 343 | return NSCAPI::returnIgnored;
|
|---|
| 344 | }
|
|---|
| 345 | {
|
|---|
| 346 | thread_locker locker;
|
|---|
| 347 | try {
|
|---|
| 348 | tuple ret = boost::python::call<tuple>(boost::python::object(it->second).ptr(), convert(arguments));
|
|---|
| 349 | if (ret.ptr() == Py_None) {
|
|---|
| 350 | result = _T("None");
|
|---|
| 351 | return NSCAPI::returnUNKNOWN;
|
|---|
| 352 | }
|
|---|
| 353 | int ret_code = NSCAPI::returnUNKNOWN;
|
|---|
| 354 | if (len(ret) > 0)
|
|---|
| 355 | ret_code = extract<int>(ret[0]);
|
|---|
| 356 | if (len(ret) > 1)
|
|---|
| 357 | result = utf8::cvt<std::wstring>(extract<std::string>(ret[1]));
|
|---|
| 358 | return ret_code;
|
|---|
| 359 | } catch( error_already_set e) {
|
|---|
| 360 | log_exception();
|
|---|
| 361 | result = _T("Exception in: ") + utf8::cvt<std::wstring>(cmd);
|
|---|
| 362 | return NSCAPI::returnUNKNOWN;
|
|---|
| 363 | }
|
|---|
| 364 | }
|
|---|
| 365 | } catch(const std::exception &e) {
|
|---|
| 366 | result = _T("Exception in ") + utf8::cvt<std::wstring>(cmd) + _T(": ") + utf8::cvt<std::wstring>(e.what());
|
|---|
| 367 | return NSCAPI::returnUNKNOWN;
|
|---|
| 368 | } catch(...) {
|
|---|
| 369 | result = _T("Exception in ") + utf8::cvt<std::wstring>(cmd);
|
|---|
| 370 | return NSCAPI::returnUNKNOWN;
|
|---|
| 371 | }
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 |
|
|---|
| 375 | bool script_wrapper::function_wrapper::has_message_handler(const std::string channel) {
|
|---|
| 376 | return functions::get()->normal_handler.find(channel) != functions::get()->normal_handler.end();
|
|---|
| 377 | }
|
|---|
| 378 | bool script_wrapper::function_wrapper::has_simple_message_handler(const std::string channel) {
|
|---|
| 379 | return functions::get()->simple_handler.find(channel) != functions::get()->simple_handler.end();
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | int script_wrapper::function_wrapper::handle_message(const std::string channel, const std::string &request, std::string &response) const {
|
|---|
| 383 | try {
|
|---|
| 384 | functions::function_map_type::iterator it = functions::get()->normal_handler.find(channel);
|
|---|
| 385 | if (it == functions::get()->normal_handler.end()) {
|
|---|
| 386 | NSC_LOG_ERROR_STD(_T("Failed to find python handler: ") + utf8::cvt<std::wstring>(channel));
|
|---|
| 387 | return NSCAPI::returnIgnored;
|
|---|
| 388 | }
|
|---|
| 389 | {
|
|---|
| 390 | thread_locker locker;
|
|---|
| 391 | int ret_code = NSCAPI::returnIgnored;
|
|---|
| 392 | try {
|
|---|
| 393 | object ret = boost::python::call<object>(boost::python::object(it->second).ptr(), channel, request);
|
|---|
| 394 | if (ret.ptr() == Py_None) {
|
|---|
| 395 | return NSCAPI::returnIgnored;
|
|---|
| 396 | }
|
|---|
| 397 | if (len(ret) > 0)
|
|---|
| 398 | ret_code = extract<bool>(ret[0])?NSCAPI::isSuccess:NSCAPI::returnIgnored;
|
|---|
| 399 | if (len(ret) > 1)
|
|---|
| 400 | response = extract<std::string>(ret[1]);
|
|---|
| 401 | } catch( error_already_set e) {
|
|---|
| 402 | log_exception();
|
|---|
| 403 | return NSCAPI::returnUNKNOWN;
|
|---|
| 404 | }
|
|---|
| 405 | return ret_code;
|
|---|
| 406 | }
|
|---|
| 407 | } catch(const std::exception &e) {
|
|---|
| 408 | NSC_LOG_ERROR_STD(_T("Exception in ") + utf8::cvt<std::wstring>(channel) + _T(": ") + utf8::cvt<std::wstring>(e.what()));
|
|---|
| 409 | return NSCAPI::returnUNKNOWN;
|
|---|
| 410 | } catch(...) {
|
|---|
| 411 | NSC_LOG_ERROR_STD(_T("Exception in ") + utf8::cvt<std::wstring>(channel));
|
|---|
| 412 | return NSCAPI::returnUNKNOWN;
|
|---|
| 413 | }
|
|---|
| 414 | }
|
|---|
| 415 | int script_wrapper::function_wrapper::handle_simple_message(const std::string channel, const std::string source, const std::string command, int code, std::wstring &msg, std::wstring &perf) const {
|
|---|
| 416 | try {
|
|---|
| 417 | functions::function_map_type::iterator it = functions::get()->simple_handler.find(channel);
|
|---|
| 418 | if (it == functions::get()->simple_handler.end()) {
|
|---|
| 419 | NSC_LOG_ERROR_STD(_T("Failed to find python handler: ") + utf8::cvt<std::wstring>(channel));
|
|---|
| 420 | return NSCAPI::returnIgnored;
|
|---|
| 421 | }
|
|---|
| 422 | {
|
|---|
| 423 | thread_locker locker;
|
|---|
| 424 | try {
|
|---|
| 425 | object ret = boost::python::call<object>(boost::python::object(it->second).ptr(), channel, source, command, nagios_return_to_py(code), pystr(msg), utf8::cvt<std::string>(perf));
|
|---|
| 426 | int ret_code = NSCAPI::returnIgnored;
|
|---|
| 427 | if (ret.ptr() == Py_None) {
|
|---|
| 428 | ret_code = NSCAPI::isSuccess;
|
|---|
| 429 | } else {
|
|---|
| 430 | ret_code = extract<bool>(ret)?NSCAPI::isSuccess:NSCAPI::returnIgnored;
|
|---|
| 431 | }
|
|---|
| 432 | return ret_code;
|
|---|
| 433 | } catch( error_already_set e) {
|
|---|
| 434 | log_exception();
|
|---|
| 435 | return NSCAPI::hasFailed;
|
|---|
| 436 | }
|
|---|
| 437 | }
|
|---|
| 438 | } catch(const std::exception &e) {
|
|---|
| 439 | NSC_LOG_ERROR_STD(_T("Exception in ") + utf8::cvt<std::wstring>(channel) + _T(": ") + utf8::cvt<std::wstring>(e.what()));
|
|---|
| 440 | return NSCAPI::hasFailed;
|
|---|
| 441 | } catch(...) {
|
|---|
| 442 | NSC_LOG_ERROR_STD(_T("Exception in ") + utf8::cvt<std::wstring>(channel));
|
|---|
| 443 | return NSCAPI::hasFailed;
|
|---|
| 444 | }
|
|---|
| 445 | }
|
|---|
| 446 |
|
|---|
| 447 |
|
|---|
| 448 |
|
|---|
| 449 |
|
|---|
| 450 |
|
|---|
| 451 |
|
|---|
| 452 | bool script_wrapper::function_wrapper::has_cmdline(const std::string command) {
|
|---|
| 453 | return functions::get()->normal_cmdline.find(command) != functions::get()->normal_cmdline.end();
|
|---|
| 454 | }
|
|---|
| 455 | bool script_wrapper::function_wrapper::has_simple_cmdline(const std::string command) {
|
|---|
| 456 | return functions::get()->simple_cmdline.find(command) != functions::get()->simple_cmdline.end();
|
|---|
| 457 | }
|
|---|
| 458 |
|
|---|
| 459 | std::wstring script_wrapper::function_wrapper::get_commands() {
|
|---|
| 460 | std::wstring str;
|
|---|
| 461 | BOOST_FOREACH(const functions::function_map_type::value_type& i, functions::get()->normal_functions) {
|
|---|
| 462 | std::wstring tmp = utf8::cvt<std::wstring>(i.first);
|
|---|
| 463 | strEx::append_list(str, tmp, _T(", "));
|
|---|
| 464 | }
|
|---|
| 465 | BOOST_FOREACH(const functions::function_map_type::value_type& i, functions::get()->simple_functions) {
|
|---|
| 466 | std::wstring tmp = utf8::cvt<std::wstring>(i.first);
|
|---|
| 467 | strEx::append_list(str, tmp, _T(", "));
|
|---|
| 468 | }
|
|---|
| 469 | return str;
|
|---|
| 470 | }
|
|---|
| 471 |
|
|---|
| 472 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 473 | // Callouts from python into NSClient++
|
|---|
| 474 | //
|
|---|
| 475 | tuple script_wrapper::command_wrapper::simple_submit(std::string channel, std::string command, status code, std::string message, std::string perf) {
|
|---|
| 476 | NSCAPI::nagiosReturn c = py_to_nagios_return(code);
|
|---|
| 477 | std::wstring wmessage = utf8::cvt<std::wstring>(message);
|
|---|
| 478 | std::wstring wperf = utf8::cvt<std::wstring>(perf);
|
|---|
| 479 | std::wstring wchannel = utf8::cvt<std::wstring>(channel);
|
|---|
| 480 | std::wstring wcommand = utf8::cvt<std::wstring>(command);
|
|---|
| 481 | std::wstring wresp;
|
|---|
| 482 | bool ret = false;
|
|---|
| 483 | {
|
|---|
| 484 | thread_unlocker unlocker;
|
|---|
| 485 | ret = nscapi::core_helper::submit_simple_message(wchannel, wcommand, c, wmessage, wperf, wresp);
|
|---|
| 486 | }
|
|---|
| 487 | return make_tuple(ret,utf8::cvt<std::string>(wresp));
|
|---|
| 488 | }
|
|---|
| 489 | tuple script_wrapper::command_wrapper::submit(std::string channel, std::string request) {
|
|---|
| 490 | std::wstring wchannel = utf8::cvt<std::wstring>(channel);
|
|---|
| 491 | std::string response;
|
|---|
| 492 | int ret = 0;
|
|---|
| 493 | {
|
|---|
| 494 | thread_unlocker unlocker;
|
|---|
| 495 | ret = core->submit_message(wchannel, request, response);
|
|---|
| 496 | }
|
|---|
| 497 | std::wstring err;
|
|---|
| 498 | nscapi::functions::parse_simple_submit_response(response, err);
|
|---|
| 499 | return make_tuple(ret==NSCAPI::isSuccess,err);
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | bool script_wrapper::command_wrapper::reload(std::string module) {
|
|---|
| 503 | std::wstring wmodule = utf8::cvt<std::wstring>(module);
|
|---|
| 504 | int ret = 0;
|
|---|
| 505 | {
|
|---|
| 506 | thread_unlocker unlocker;
|
|---|
| 507 | ret = core->reload(wmodule);
|
|---|
| 508 | }
|
|---|
| 509 | return ret==NSCAPI::isSuccess;
|
|---|
| 510 | }
|
|---|
| 511 |
|
|---|
| 512 | std::string script_wrapper::command_wrapper::expand_path(std::string aPath) {
|
|---|
| 513 | thread_unlocker unlocker;
|
|---|
| 514 | return utf8::cvt<std::string>(core->expand_path(utf8::cvt<std::wstring>(aPath)));
|
|---|
| 515 | }
|
|---|
| 516 |
|
|---|
| 517 |
|
|---|
| 518 |
|
|---|
| 519 | tuple script_wrapper::command_wrapper::simple_query(std::string command, py::list args) {
|
|---|
| 520 | std::wstring msg, perf;
|
|---|
| 521 | const std::list<std::wstring> ws_argument = convert(args);
|
|---|
| 522 | int ret = 0;
|
|---|
| 523 | {
|
|---|
| 524 | thread_unlocker unlocker;
|
|---|
| 525 | ret = nscapi::core_helper::simple_query(utf8::cvt<std::wstring>(command), ws_argument, msg, perf);
|
|---|
| 526 | }
|
|---|
| 527 | return make_tuple(nagios_return_to_py(ret),utf8::cvt<std::string>(msg), utf8::cvt<std::string>(perf));
|
|---|
| 528 | }
|
|---|
| 529 | tuple script_wrapper::command_wrapper::query(std::string command, std::string request) {
|
|---|
| 530 | std::string response;
|
|---|
| 531 | int ret = 0;
|
|---|
| 532 | {
|
|---|
| 533 | thread_unlocker unlocker;
|
|---|
| 534 | ret = core->query(utf8::cvt<std::wstring>(command), request, response);
|
|---|
| 535 | }
|
|---|
| 536 | return make_tuple(ret,response);
|
|---|
| 537 | }
|
|---|
| 538 |
|
|---|
| 539 | tuple script_wrapper::command_wrapper::simple_exec(std::string target, std::string command, py::list args) {
|
|---|
| 540 | try {
|
|---|
| 541 | std::list<std::wstring> result;
|
|---|
| 542 | int ret = 0;
|
|---|
| 543 | const std::wstring ws_target = utf8::cvt<std::wstring>(target);
|
|---|
| 544 | const std::wstring ws_command = utf8::cvt<std::wstring>(command);
|
|---|
| 545 | const std::list<std::wstring> ws_argument = convert(args);
|
|---|
| 546 | {
|
|---|
| 547 | thread_unlocker unlocker;
|
|---|
| 548 | ret = nscapi::core_helper::exec_simple_command(ws_target, ws_command, ws_argument, result);
|
|---|
| 549 | }
|
|---|
| 550 | return make_tuple(ret, convert(result));
|
|---|
| 551 | } catch (const std::exception &e) {
|
|---|
| 552 | NSC_LOG_ERROR_STD(_T("Failed to execute ") + utf8::cvt<std::wstring>(command) + _T(": ") + utf8::cvt<std::wstring>(e.what()));
|
|---|
| 553 | return make_tuple(false,utf8::cvt<std::wstring>(e.what()));
|
|---|
| 554 | } catch (...) {
|
|---|
| 555 | NSC_LOG_ERROR_STD(_T("Failed to execute ") + utf8::cvt<std::wstring>(command));
|
|---|
| 556 | return make_tuple(false,utf8::cvt<std::wstring>(command));
|
|---|
| 557 | }
|
|---|
| 558 | }
|
|---|
| 559 | tuple script_wrapper::command_wrapper::exec(std::string target, std::string command, std::string request) {
|
|---|
| 560 | try {
|
|---|
| 561 | std::string response;
|
|---|
| 562 | int ret = 0;
|
|---|
| 563 | {
|
|---|
| 564 | thread_unlocker unlocker;
|
|---|
| 565 | ret = core->exec_command(utf8::cvt<std::wstring>(target), utf8::cvt<std::wstring>(command), request, response);
|
|---|
| 566 | }
|
|---|
| 567 | return make_tuple(ret, response);
|
|---|
| 568 | } catch (const std::exception &e) {
|
|---|
| 569 | NSC_LOG_ERROR_STD(_T("Failed to execute ") + utf8::cvt<std::wstring>(command) + _T(": ") + utf8::cvt<std::wstring>(e.what()));
|
|---|
| 570 | return make_tuple(false,utf8::cvt<std::wstring>(e.what()));
|
|---|
| 571 | } catch (...) {
|
|---|
| 572 | NSC_LOG_ERROR_STD(_T("Failed to execute ") + utf8::cvt<std::wstring>(command));
|
|---|
| 573 | return make_tuple(false,utf8::cvt<std::wstring>(command));
|
|---|
| 574 | }
|
|---|
| 575 | }
|
|---|
| 576 |
|
|---|
| 577 |
|
|---|
| 578 | std::string script_wrapper::settings_wrapper::get_string(std::string path, std::string key, std::string def) {
|
|---|
| 579 | return utf8::cvt<std::string>(core->getSettingsString(utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(key), utf8::cvt<std::wstring>(def)));
|
|---|
| 580 | }
|
|---|
| 581 | void script_wrapper::settings_wrapper::set_string(std::string path, std::string key, std::string value) {
|
|---|
| 582 | core->SetSettingsString(utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(key), utf8::cvt<std::wstring>(value));
|
|---|
| 583 | }
|
|---|
| 584 | bool script_wrapper::settings_wrapper::get_bool(std::string path, std::string key, bool def) {
|
|---|
| 585 | return core->getSettingsBool(utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(key), def);
|
|---|
| 586 | }
|
|---|
| 587 | void script_wrapper::settings_wrapper::set_bool(std::string path, std::string key, bool value) {
|
|---|
| 588 | core->SetSettingsInt(utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(key), value);
|
|---|
| 589 | }
|
|---|
| 590 | int script_wrapper::settings_wrapper::get_int(std::string path, std::string key, int def) {
|
|---|
| 591 | return core->getSettingsInt(utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(key), def);
|
|---|
| 592 | }
|
|---|
| 593 | void script_wrapper::settings_wrapper::set_int(std::string path, std::string key, int value) {
|
|---|
| 594 | core->SetSettingsInt(utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(key), value);
|
|---|
| 595 | }
|
|---|
| 596 | std::list<std::string> script_wrapper::settings_wrapper::get_section(std::string path) {
|
|---|
| 597 | std::list<std::string> ret;
|
|---|
| 598 | BOOST_FOREACH(std::wstring s, core->getSettingsSection(utf8::cvt<std::wstring>(path))) {
|
|---|
| 599 | ret.push_back(utf8::cvt<std::string>(s));
|
|---|
| 600 | }
|
|---|
| 601 | return ret;
|
|---|
| 602 | }
|
|---|
| 603 | void script_wrapper::settings_wrapper::save() {
|
|---|
| 604 | core->settings_save();
|
|---|
| 605 | }
|
|---|
| 606 |
|
|---|
| 607 | NSCAPI::settings_type script_wrapper::settings_wrapper::get_type(std::string stype) {
|
|---|
| 608 | if (stype == "string" || stype == "str" || stype == "s")
|
|---|
| 609 | return NSCAPI::key_string;
|
|---|
| 610 | if (stype == "integer" || stype == "int" || stype == "i")
|
|---|
| 611 | return NSCAPI::key_integer;
|
|---|
| 612 | if (stype == "bool" || stype == "b")
|
|---|
| 613 | return NSCAPI::key_bool;
|
|---|
| 614 | NSC_LOG_ERROR_STD(_T("Invalid settings type"));
|
|---|
| 615 | return NSCAPI::key_string;
|
|---|
| 616 | }
|
|---|
| 617 | void script_wrapper::settings_wrapper::settings_register_key(std::string path, std::string key, std::string stype, std::string title, std::string description, std::string defaultValue) {
|
|---|
| 618 | NSCAPI::settings_type type = get_type(stype);
|
|---|
| 619 | core->settings_register_key(plugin_id, utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(key), type, utf8::cvt<std::wstring>(title), utf8::cvt<std::wstring>(description), utf8::cvt<std::wstring>(defaultValue), false);
|
|---|
| 620 | }
|
|---|
| 621 | void script_wrapper::settings_wrapper::settings_register_path(std::string path, std::string title, std::string description) {
|
|---|
| 622 | core->settings_register_path(plugin_id, utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(title), utf8::cvt<std::wstring>(description), false);
|
|---|
| 623 | }
|
|---|