source: nscp/modules/PythonScript/script_wrapper.hpp @ a14aa07

0.4.00.4.10.4.2
Last change on this file since a14aa07 was a14aa07, checked in by Michael Medin <michael@…>, 21 months ago
  • Fixed several issues in PythonScript
  • PythonScript supports loading multiple instances as well as scripts
  • Added some basic channel test to test.py script (the idea is that this will become "unit tests" eventually)
  • Property mode set to 100644
File size: 5.9 KB
Line 
1#pragma once
2
3#include <boost/python.hpp>
4
5namespace script_wrapper {
6        using namespace boost::python;
7       
8        enum status {
9                OK = NSCAPI::returnOK,
10                WARN = NSCAPI::returnWARN,
11                CRIT = NSCAPI::returnCRIT,
12                UNKNOWN = NSCAPI::returnUNKNOWN,
13        };
14
15        status nagios_return_to_py(int code);
16        int py_to_nagios_return(status code);
17
18        void log_exception();
19        void log_msg(std::wstring x);
20        //std::string get_alias();
21
22        std::list<std::wstring> convert(boost::python::list lst);
23        boost::python::list convert(std::list<std::wstring> lst);
24
25
26        struct functions {
27                typedef std::map<std::string,boost::python::handle<> > function_map_type;
28                function_map_type simple_functions;
29                function_map_type normal_functions;
30
31                function_map_type simple_cmdline;
32                function_map_type normal_cmdline;
33
34                function_map_type simple_handler;
35                function_map_type normal_handler;
36
37                static boost::shared_ptr<functions> instance;
38                static boost::shared_ptr<functions> get() {
39                        if (!instance)
40                                instance = boost::shared_ptr<functions>(new functions());
41                        return instance;
42                }
43                static void destroy() {
44                        instance.reset();
45                }
46        };
47
48        struct function_wrapper {
49        private:
50                nscapi::core_wrapper* core;
51                unsigned int plugin_id;
52                function_wrapper() {}
53        public:
54                function_wrapper(const function_wrapper &other) : core(other.core) {}
55                function_wrapper& operator=(const function_wrapper &other) {
56                        core = other.core;
57                        return *this;
58                }
59                function_wrapper(nscapi::core_wrapper* core, unsigned int plugin_id) : core(core), plugin_id(plugin_id) {}
60                typedef std::map<std::string,PyObject*> function_map_type;
61                //typedef boost::python::tuple simple_return;
62
63
64                static boost::shared_ptr<function_wrapper> create(unsigned int plugin_id) {
65                        return boost::shared_ptr<function_wrapper>(new function_wrapper(nscapi::plugin_singleton->get_core(), plugin_id));
66                }
67
68                void register_simple_cmdline(std::string name, PyObject* callable);
69                void register_cmdline(std::string name, PyObject* callable);
70                void register_simple_function(std::string name, PyObject* callable, std::string desc);
71                void register_function(std::string name, PyObject* callable, std::string desc);
72                void subscribe_function(std::string channel, PyObject* callable);
73                void subscribe_simple_function(std::string channel, PyObject* callable);
74                int handle_simple_query(const std::string wcmd, std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) const;
75                int handle_query(const std::string wcmd, const std::string &request, std::string &response) const;
76                bool has_function(const std::string command);
77                bool has_simple(const std::string command);
78
79                int handle_simple_exec(const std::string wcmd, std::list<std::wstring> arguments, std::wstring &result) const;
80                int handle_exec(const std::string wcmd, const std::string &request, std::string &response) const;
81                bool has_cmdline(const std::string command);
82                bool has_simple_cmdline(const std::string command);
83
84
85                int handle_simple_message(const std::string channel, const std::string wcmd, int code, std::wstring &msg, std::wstring &perf) const;
86                int handle_message(const std::string channel, const std::string wcmd, std::string &message) const;
87                bool has_message_handler(const std::string command);
88                bool has_simple_message_handler(const std::string command);
89
90                std::wstring get_commands();
91        };
92        struct command_wrapper {
93        private:
94                nscapi::core_wrapper* core;
95        public:
96                command_wrapper() : core(NULL) {}
97                command_wrapper(const command_wrapper &other) : core(other.core) {}
98                command_wrapper& operator=(const command_wrapper &other) {
99                        core = other.core;
100                        return *this;
101                }
102                command_wrapper(nscapi::core_wrapper* core) : core(core) {}
103
104        public:
105                static boost::shared_ptr<command_wrapper> create() {
106                        NSC_DEBUG_MSG_STD(_T("<<<CREATING NEW>>>"));
107                        return boost::shared_ptr<command_wrapper>(new command_wrapper(nscapi::plugin_singleton->get_core()));
108                }
109
110                tuple simple_query(std::string command, boost::python::list args);
111                tuple query(std::string command, std::string request);
112                object simple_exec(std::string command, boost::python::list args);
113                tuple exec(std::string command, std::string request);
114                void simple_submit(std::string channel, std::string command, status code, std::string message, std::string perf);
115                void submit(std::string channel, std::string command, std::string request);
116        };
117
118        struct settings_wrapper {
119        private:
120                nscapi::core_wrapper* core;
121        public:
122                settings_wrapper() : core(NULL) {}
123                settings_wrapper(const settings_wrapper &other) : core(other.core) {}
124                settings_wrapper& operator=(const settings_wrapper &other) {
125                        core = other.core;
126                        return *this;
127                }
128                settings_wrapper(nscapi::core_wrapper* core) : core(core) {}
129
130        public:
131                static boost::shared_ptr<settings_wrapper> create() {
132                        return boost::shared_ptr<settings_wrapper>(new settings_wrapper(nscapi::plugin_singleton->get_core()));
133                }
134               
135
136                std::string get_string(std::string path, std::string key, std::string def = "");
137                void set_string(std::string path, std::string key, std::string value);
138                bool get_bool(std::string path, std::string key, bool def);
139                void set_bool(std::string path, std::string key, bool value);
140                int get_int(std::string path, std::string key, int def);
141                void set_int(std::string path, std::string key, int value);
142                std::list<std::string> get_section(std::string path);
143                void save();
144                NSCAPI::settings_type get_type(std::string stype);
145                void settings_register_key(std::string path, std::string key, std::string stype, std::string title, std::string description, std::string defaultValue);
146                void settings_register_path(std::string path, std::string title, std::string description);
147        };
148
149
150        class PyInitializer {
151        public:  PyInitializer()  { Py_Initialize(); } 
152                         ~PyInitializer() { Py_Finalize(); }
153        private: 
154                PyInitializer(const PyInitializer &); 
155                PyInitializer & operator=(const PyInitializer &);
156        };
157
158
159
160
161}
Note: See TracBrowser for help on using the repository browser.