source: nscp/modules/PythonScript/script_wrapper.cpp @ d7e265d

0.4.00.4.10.4.2
Last change on this file since d7e265d was b9498ef, checked in by Michael Medin <michael@…>, 22 months ago

2011-08-15 MickeM

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