| 1 | #include "stdafx.h"
|
|---|
| 2 |
|
|---|
| 3 | #include <strEx.h>
|
|---|
| 4 | #include "script_wrapper.hpp"
|
|---|
| 5 | #include "PythonScript.h"
|
|---|
| 6 |
|
|---|
| 7 | using namespace boost::python;
|
|---|
| 8 |
|
|---|
| 9 | boost::shared_ptr<script_wrapper::functions> script_wrapper::functions::instance;
|
|---|
| 10 |
|
|---|
| 11 | //extern PythonScript gPythonScript;
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 | void script_wrapper::log_msg(std::wstring x) {
|
|---|
| 15 | NSC_LOG_ERROR_STD(utf8::cvt<std::wstring>(x));
|
|---|
| 16 | }
|
|---|
| 17 | /*
|
|---|
| 18 | std::string script_wrapper::get_alias() {
|
|---|
| 19 | return utf8::cvt<std::string>(gPythonScript.get_alias());
|
|---|
| 20 | }
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 | void script_wrapper::log_exception() {
|
|---|
| 24 | PyErr_Print();
|
|---|
| 25 | boost::python::object sys(boost::python::handle<>(PyImport_ImportModule("sys")));
|
|---|
| 26 | boost::python::object err = sys.attr("stderr");
|
|---|
| 27 | std::string err_text = boost::python::extract<std::string>(err.attr("getvalue")());
|
|---|
| 28 | NSC_LOG_ERROR_STD(utf8::cvt<std::wstring>(err_text));
|
|---|
| 29 | PyErr_Clear();
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | void script_wrapper::function_wrapper::subscribe_simple_function(std::string channel, PyObject* callable) {
|
|---|
| 33 | functions::get()->simple_handler[channel] = callable;
|
|---|
| 34 | }
|
|---|
| 35 | void script_wrapper::function_wrapper::subscribe_function(std::string channel, PyObject* callable) {
|
|---|
| 36 | functions::get()->normal_handler[channel] = callable;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 | void script_wrapper::function_wrapper::register_simple_function(std::string name, PyObject* callable, std::string desc) {
|
|---|
| 41 | try {
|
|---|
| 42 | core->registerCommand(utf8::cvt<std::wstring>(name), utf8::cvt<std::wstring>(desc));
|
|---|
| 43 | functions::get()->simple_functions[name] = callable;
|
|---|
| 44 | } catch (...) {
|
|---|
| 45 | NSC_LOG_ERROR_STD(_T("Failed to register functions: ") + utf8::cvt<std::wstring>(name));
|
|---|
| 46 | }
|
|---|
| 47 | }
|
|---|
| 48 | void script_wrapper::function_wrapper::register_function(std::string name, PyObject* callable, std::string desc) {
|
|---|
| 49 | try {
|
|---|
| 50 | core->registerCommand(utf8::cvt<std::wstring>(name), utf8::cvt<std::wstring>(desc));
|
|---|
| 51 | functions::get()->normal_functions[name] = callable;
|
|---|
| 52 | } catch (...) {
|
|---|
| 53 | NSC_LOG_ERROR_STD(_T("Failed to register functions: ") + utf8::cvt<std::wstring>(name));
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 | void script_wrapper::function_wrapper::register_simple_cmdline(std::string name, PyObject* callable) {
|
|---|
| 57 | try {
|
|---|
| 58 | functions::get()->simple_cmdline[name] = callable;
|
|---|
| 59 | } catch (...) {
|
|---|
| 60 | NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(name));
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 | void script_wrapper::function_wrapper::register_cmdline(std::string name, PyObject* callable) {
|
|---|
| 64 | try {
|
|---|
| 65 | functions::get()->normal_cmdline[name] = callable;
|
|---|
| 66 | } catch (...) {
|
|---|
| 67 | NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(name));
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 | int script_wrapper::function_wrapper::exec(const std::string cmd, const std::string &request, std::string &response) const {
|
|---|
| 71 | try {
|
|---|
| 72 | functions::function_map_type::iterator it = functions::get()->normal_functions.find(cmd);
|
|---|
| 73 | if (it == functions::get()->normal_functions.end()) {
|
|---|
| 74 | NSC_LOG_ERROR_STD(_T("Failed to find python function: ") + utf8::cvt<std::wstring>(cmd));
|
|---|
| 75 | return NSCAPI::returnIgnored;
|
|---|
| 76 | }
|
|---|
| 77 | tuple ret = boost::python::call<tuple>(it->second, cmd, request);
|
|---|
| 78 | if (ret.ptr() == Py_None) {
|
|---|
| 79 | return NSCAPI::returnUNKNOWN;
|
|---|
| 80 | }
|
|---|
| 81 | int ret_code = NSCAPI::returnUNKNOWN;
|
|---|
| 82 | if (len(ret) > 0)
|
|---|
| 83 | ret_code = extract<int>(ret[0]);
|
|---|
| 84 | if (len(ret) > 1)
|
|---|
| 85 | response = extract<std::string>(ret[1]);
|
|---|
| 86 | return ret_code;
|
|---|
| 87 | } catch( error_already_set e) {
|
|---|
| 88 | log_exception();
|
|---|
| 89 | return NSCAPI::returnUNKNOWN;
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | int script_wrapper::function_wrapper::exec_simple(const std::string cmd, std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) const {
|
|---|
| 94 | try {
|
|---|
| 95 | functions::function_map_type::iterator it = functions::get()->simple_functions.find(cmd);
|
|---|
| 96 | if (it == functions::get()->simple_functions.end()) {
|
|---|
| 97 | NSC_LOG_ERROR_STD(_T("Failed to find python function: ") + utf8::cvt<std::wstring>(cmd));
|
|---|
| 98 | return NSCAPI::returnIgnored;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | boost::python::list l;
|
|---|
| 102 | BOOST_FOREACH(std::wstring a, arguments) {
|
|---|
| 103 | l.append(utf8::cvt<std::string>(a));
|
|---|
| 104 | }
|
|---|
| 105 | tuple ret = boost::python::call<tuple>(it->second, l);
|
|---|
| 106 | if (ret.ptr() == Py_None) {
|
|---|
| 107 | msg = _T("None");
|
|---|
| 108 | return NSCAPI::returnUNKNOWN;
|
|---|
| 109 | }
|
|---|
| 110 | int ret_code = NSCAPI::returnUNKNOWN;
|
|---|
| 111 | if (len(ret) > 0)
|
|---|
| 112 | ret_code = extract<int>(ret[0]);
|
|---|
| 113 | if (len(ret) > 1)
|
|---|
| 114 | msg = utf8::cvt<std::wstring>(extract<std::string>(ret[1]));
|
|---|
| 115 | if (len(ret) > 2)
|
|---|
| 116 | perf = utf8::cvt<std::wstring>(extract<std::string>(ret[2]));
|
|---|
| 117 | return ret_code;
|
|---|
| 118 | } catch( error_already_set e) {
|
|---|
| 119 | log_exception();
|
|---|
| 120 | msg = _T("Exception in: ") + utf8::cvt<std::wstring>(cmd);
|
|---|
| 121 | return NSCAPI::returnUNKNOWN;
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | bool script_wrapper::function_wrapper::has_function(const std::string command) {
|
|---|
| 126 | return functions::get()->normal_functions.find(command) != functions::get()->normal_functions.end();
|
|---|
| 127 | }
|
|---|
| 128 | bool script_wrapper::function_wrapper::has_simple(const std::string command) {
|
|---|
| 129 | return functions::get()->simple_functions.find(command) != functions::get()->simple_functions.end();
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | int script_wrapper::function_wrapper::handle_exec(const std::string cmd, const std::string &request, std::string &response) const {
|
|---|
| 133 | try {
|
|---|
| 134 | functions::function_map_type::iterator it = functions::get()->normal_cmdline.find(cmd);
|
|---|
| 135 | if (it == functions::get()->normal_cmdline.end()) {
|
|---|
| 136 | NSC_LOG_ERROR_STD(_T("Failed to find python function: ") + utf8::cvt<std::wstring>(cmd));
|
|---|
| 137 | return NSCAPI::returnIgnored;
|
|---|
| 138 | }
|
|---|
| 139 | tuple ret = boost::python::call<tuple>(it->second, cmd, request);
|
|---|
| 140 | if (ret.ptr() == Py_None) {
|
|---|
| 141 | return NSCAPI::returnUNKNOWN;
|
|---|
| 142 | }
|
|---|
| 143 | int ret_code = NSCAPI::returnUNKNOWN;
|
|---|
| 144 | if (len(ret) > 0)
|
|---|
| 145 | ret_code = extract<int>(ret[0]);
|
|---|
| 146 | if (len(ret) > 1)
|
|---|
| 147 | response = extract<std::string>(ret[1]);
|
|---|
| 148 | return ret_code;
|
|---|
| 149 | } catch( error_already_set e) {
|
|---|
| 150 | log_exception();
|
|---|
| 151 | return NSCAPI::returnUNKNOWN;
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | int script_wrapper::function_wrapper::handle_simple_exec(const std::string cmd, std::list<std::wstring> arguments, std::wstring &result) const {
|
|---|
| 156 | try {
|
|---|
| 157 | functions::function_map_type::iterator it = functions::get()->simple_cmdline.find(cmd);
|
|---|
| 158 | if (it == functions::get()->simple_cmdline.end()) {
|
|---|
| 159 | result = _T("Failed to find python function: ") + utf8::cvt<std::wstring>(cmd);
|
|---|
| 160 | NSC_LOG_ERROR_STD(result);
|
|---|
| 161 | return NSCAPI::returnIgnored;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | tuple ret = boost::python::call<tuple>(it->second, convert(arguments));
|
|---|
| 165 | if (ret.ptr() == Py_None) {
|
|---|
| 166 | result = _T("None");
|
|---|
| 167 | return NSCAPI::returnUNKNOWN;
|
|---|
| 168 | }
|
|---|
| 169 | int ret_code = NSCAPI::returnUNKNOWN;
|
|---|
| 170 | if (len(ret) > 0)
|
|---|
| 171 | ret_code = extract<int>(ret[0]);
|
|---|
| 172 | if (len(ret) > 1)
|
|---|
| 173 | result = utf8::cvt<std::wstring>(extract<std::string>(ret[1]));
|
|---|
| 174 | return ret_code;
|
|---|
| 175 | } catch( error_already_set e) {
|
|---|
| 176 | log_exception();
|
|---|
| 177 | result = _T("Exception in: ") + utf8::cvt<std::wstring>(cmd);
|
|---|
| 178 | return NSCAPI::returnUNKNOWN;
|
|---|
| 179 | }
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 | bool script_wrapper::function_wrapper::has_message_handler(const std::string channel) {
|
|---|
| 184 | return functions::get()->normal_handler.find(channel) != functions::get()->normal_handler.end();
|
|---|
| 185 | }
|
|---|
| 186 | bool script_wrapper::function_wrapper::has_simple_message_handler(const std::string channel) {
|
|---|
| 187 | return functions::get()->simple_handler.find(channel) != functions::get()->simple_handler.end();
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | int script_wrapper::function_wrapper::handle_message(const std::string channel, const std::string command, std::string &message) const {
|
|---|
| 191 | try {
|
|---|
| 192 | functions::function_map_type::iterator it = functions::get()->normal_handler.find(channel);
|
|---|
| 193 | if (it == functions::get()->normal_handler.end()) {
|
|---|
| 194 | NSC_LOG_ERROR_STD(_T("Failed to find python handler: ") + utf8::cvt<std::wstring>(channel));
|
|---|
| 195 | return NSCAPI::returnIgnored;
|
|---|
| 196 | }
|
|---|
| 197 | object ret = boost::python::call<object>(it->second, channel, command, message);
|
|---|
| 198 | if (ret.ptr() == Py_None) {
|
|---|
| 199 | return NSCAPI::returnUNKNOWN;
|
|---|
| 200 | }
|
|---|
| 201 | return extract<int>(ret);
|
|---|
| 202 | } catch( error_already_set e) {
|
|---|
| 203 | log_exception();
|
|---|
| 204 | return NSCAPI::returnUNKNOWN;
|
|---|
| 205 | }
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | int script_wrapper::function_wrapper::handle_simple_message(const std::string channel, const std::string command, int code, std::wstring &msg, std::wstring &perf) const {
|
|---|
| 209 | try {
|
|---|
| 210 | functions::function_map_type::iterator it = functions::get()->simple_handler.find(channel);
|
|---|
| 211 | if (it == functions::get()->simple_handler.end()) {
|
|---|
| 212 | NSC_LOG_ERROR_STD(_T("Failed to find python handler: ") + utf8::cvt<std::wstring>(channel));
|
|---|
| 213 | return NSCAPI::returnIgnored;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | object ret = boost::python::call<object>(it->second, channel, command, code, msg, perf);
|
|---|
| 217 | if (ret.ptr() == Py_None) {
|
|---|
| 218 | return NSCAPI::returnUNKNOWN;
|
|---|
| 219 | }
|
|---|
| 220 | return extract<int>(ret);
|
|---|
| 221 | } catch( error_already_set e) {
|
|---|
| 222 | log_exception();
|
|---|
| 223 | return NSCAPI::returnUNKNOWN;
|
|---|
| 224 | }
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 | bool script_wrapper::function_wrapper::has_cmdline(const std::string command) {
|
|---|
| 233 | return functions::get()->normal_cmdline.find(command) != functions::get()->normal_cmdline.end();
|
|---|
| 234 | }
|
|---|
| 235 | bool script_wrapper::function_wrapper::has_simple_cmdline(const std::string command) {
|
|---|
| 236 | return functions::get()->simple_cmdline.find(command) != functions::get()->simple_cmdline.end();
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | std::wstring script_wrapper::function_wrapper::get_commands() {
|
|---|
| 240 | std::wstring str;
|
|---|
| 241 | BOOST_FOREACH(const functions::function_map_type::value_type& i, functions::get()->normal_functions) {
|
|---|
| 242 | std::wstring tmp = utf8::cvt<std::wstring>(i.first);
|
|---|
| 243 | strEx::append_list(str, tmp, _T(", "));
|
|---|
| 244 | }
|
|---|
| 245 | BOOST_FOREACH(const functions::function_map_type::value_type& i, functions::get()->simple_functions) {
|
|---|
| 246 | std::wstring tmp = utf8::cvt<std::wstring>(i.first);
|
|---|
| 247 | strEx::append_list(str, tmp, _T(", "));
|
|---|
| 248 | }
|
|---|
| 249 | return str;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 | std::list<std::wstring> script_wrapper::convert(list lst) {
|
|---|
| 255 | std::list<std::wstring> ret;
|
|---|
| 256 | for (int i = 0;i<len(lst);i++)
|
|---|
| 257 | ret.push_back(utf8::cvt<std::wstring>(extract<std::string>(lst[i])));
|
|---|
| 258 | return ret;
|
|---|
| 259 | }
|
|---|
| 260 | list script_wrapper::convert(std::list<std::wstring> lst) {
|
|---|
| 261 | list ret;
|
|---|
| 262 | BOOST_FOREACH(std::wstring s, lst) {
|
|---|
| 263 | ret.append(utf8::cvt<std::string>(s));
|
|---|
| 264 | }
|
|---|
| 265 | return ret;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | void script_wrapper::command_wrapper::simple_submit(std::string channel, std::string command, status code, std::string message, std::string perf) {
|
|---|
| 269 | NSCAPI::nagiosReturn c = NSCAPI::returnUNKNOWN;
|
|---|
| 270 | if (code == OK)
|
|---|
| 271 | c = NSCAPI::returnOK;
|
|---|
| 272 | if (code == WARN)
|
|---|
| 273 | c = NSCAPI::returnWARN;
|
|---|
| 274 | if (code == CRIT)
|
|---|
| 275 | c = NSCAPI::returnCRIT;
|
|---|
| 276 | std::wstring wmessage = utf8::cvt<std::wstring>(message);
|
|---|
| 277 | std::wstring wperf = utf8::cvt<std::wstring>(perf);
|
|---|
| 278 | core->submit_simple_message(utf8::cvt<std::wstring>(channel), utf8::cvt<std::wstring>(command), c, wmessage, wperf);
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 | tuple script_wrapper::command_wrapper::simple_query(std::string command, list args) {
|
|---|
| 283 | std::wstring msg, perf;
|
|---|
| 284 | int ret = core->simple_query(utf8::cvt<std::wstring>(command), convert(args), msg, perf);
|
|---|
| 285 | return make_tuple(ret,utf8::cvt<std::string>(msg), utf8::cvt<std::string>(perf));
|
|---|
| 286 | }
|
|---|
| 287 | tuple script_wrapper::command_wrapper::query(std::string command, std::string request) {
|
|---|
| 288 | std::string response;
|
|---|
| 289 | int ret = core->query(utf8::cvt<std::wstring>(command), request, response);
|
|---|
| 290 | return make_tuple(ret,response);
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | object script_wrapper::command_wrapper::simple_exec(std::string command, list args) {
|
|---|
| 294 | try {
|
|---|
| 295 | std::list<std::wstring> result;
|
|---|
| 296 | int ret = core->exec_simple_command(utf8::cvt<std::wstring>(command), convert(args), result);
|
|---|
| 297 | return make_tuple(ret, convert(result));
|
|---|
| 298 | } catch (const std::exception &e) {
|
|---|
| 299 | NSC_LOG_ERROR_STD(_T("Failed to execute ") + utf8::cvt<std::wstring>(command) + _T(": ") + utf8::cvt<std::wstring>(e.what()));
|
|---|
| 300 | return object();
|
|---|
| 301 | } catch (...) {
|
|---|
| 302 | NSC_LOG_ERROR_STD(_T("Failed to execute ") + utf8::cvt<std::wstring>(command));
|
|---|
| 303 | return object();
|
|---|
| 304 | }
|
|---|
| 305 | }
|
|---|
| 306 | tuple script_wrapper::command_wrapper::exec(std::string command, std::string request) {
|
|---|
| 307 | std::string response;
|
|---|
| 308 | int ret = core->exec_command(utf8::cvt<std::wstring>(command), request, response);
|
|---|
| 309 | return make_tuple(ret, response);
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 | std::string script_wrapper::settings_wrapper::get_string(std::string path, std::string key, std::string def) {
|
|---|
| 314 | return utf8::cvt<std::string>(core->getSettingsString(utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(key), utf8::cvt<std::wstring>(def)));
|
|---|
| 315 | }
|
|---|
| 316 | void script_wrapper::settings_wrapper::set_string(std::string path, std::string key, std::string value) {
|
|---|
| 317 | core->SetSettingsString(utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(key), utf8::cvt<std::wstring>(value));
|
|---|
| 318 | }
|
|---|
| 319 | bool script_wrapper::settings_wrapper::get_bool(std::string path, std::string key, bool def) {
|
|---|
| 320 | return core->getSettingsBool(utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(key), def);
|
|---|
| 321 | }
|
|---|
| 322 | void script_wrapper::settings_wrapper::set_bool(std::string path, std::string key, bool value) {
|
|---|
| 323 | core->SetSettingsInt(utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(key), value);
|
|---|
| 324 | }
|
|---|
| 325 | int script_wrapper::settings_wrapper::get_int(std::string path, std::string key, int def) {
|
|---|
| 326 | return core->getSettingsInt(utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(key), def);
|
|---|
| 327 | }
|
|---|
| 328 | void script_wrapper::settings_wrapper::set_int(std::string path, std::string key, int value) {
|
|---|
| 329 | core->SetSettingsInt(utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(key), value);
|
|---|
| 330 | }
|
|---|
| 331 | std::list<std::string> script_wrapper::settings_wrapper::get_section(std::string path) {
|
|---|
| 332 | std::list<std::string> ret;
|
|---|
| 333 | BOOST_FOREACH(std::wstring s, core->getSettingsSection(utf8::cvt<std::wstring>(path))) {
|
|---|
| 334 | ret.push_back(utf8::cvt<std::string>(s));
|
|---|
| 335 | }
|
|---|
| 336 | return ret;
|
|---|
| 337 | }
|
|---|
| 338 | void script_wrapper::settings_wrapper::save() {
|
|---|
| 339 | core->settings_save();
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | NSCAPI::settings_type script_wrapper::settings_wrapper::get_type(std::string stype) {
|
|---|
| 343 | if (stype == "string" || stype == "str" || stype == "s")
|
|---|
| 344 | return NSCAPI::key_string;
|
|---|
| 345 | if (stype == "integer" || stype == "int" || stype == "i")
|
|---|
| 346 | return NSCAPI::key_integer;
|
|---|
| 347 | if (stype == "bool" || stype == "b")
|
|---|
| 348 | return NSCAPI::key_bool;
|
|---|
| 349 | NSC_LOG_ERROR_STD(_T("Invalid settings type"));
|
|---|
| 350 | return NSCAPI::key_string;
|
|---|
| 351 | }
|
|---|
| 352 | 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) {
|
|---|
| 353 | NSCAPI::settings_type type = get_type(stype);
|
|---|
| 354 | core->settings_register_key(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);
|
|---|
| 355 | }
|
|---|
| 356 | void script_wrapper::settings_wrapper::settings_register_path(std::string path, std::string title, std::string description) {
|
|---|
| 357 | core->settings_register_path(utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(title), utf8::cvt<std::wstring>(description), false);
|
|---|
| 358 | }
|
|---|