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