source: nscp/service/NSClient++.h @ 81e420c

0.4.00.4.10.4.2
Last change on this file since 81e420c was 81e420c, checked in by Michael Medin <michael@…>, 21 months ago
  • Added support for loading same plugin twice (in different sessions)
  • Added preliminary support for routing passive checks
  • Property mode set to 100644
File size: 9.0 KB
Line 
1
2/**************************************************************************
3*   Copyright (C) 2004-2007 by Michael Medin <michael@medin.name>         *
4*                                                                         *
5*   This code is part of NSClient++ - http://trac.nakednuns.org/nscp      *
6*                                                                         *
7*   This program is free software; you can redistribute it and/or modify  *
8*   it under the terms of the GNU General Public License as published by  *
9*   the Free Software Foundation; either version 2 of the License, or     *
10*   (at your option) any later version.                                   *
11*                                                                         *
12*   This program is distributed in the hope that it will be useful,       *
13*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15*   GNU General Public License for more details.                          *
16*                                                                         *
17*   You should have received a copy of the GNU General Public License     *
18*   along with this program; if not, write to the                         *
19*   Free Software Foundation, Inc.,                                       *
20*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
21***************************************************************************/
22#pragma once
23#ifdef WIN32
24#include <com_helpers.hpp>
25#endif
26
27#include <types.hpp>
28#include <config.h>
29#include <service/system_service.hpp>
30
31#include "NSCPlugin.h"
32#include "commands.hpp"
33#include "channels.hpp"
34#include "routers.hpp"
35#include "logger.hpp"
36
37//#include <nsclient_session.hpp>
38
39
40class NSClientT;
41typedef service_helper::impl<NSClientT>::system_service NSClient;
42
43/**
44 * @ingroup NSClient++
45 * Main NSClient++ core class. This is the service core and as such is responsible for pretty much everything.
46 * It also acts as a broker for all plugins and other sub threads and such.
47 *
48 * @version 1.0
49 * first version
50 *
51 * @date 02-12-2005
52 *
53 * @author mickem
54 *
55 * @par license
56 * This code is absolutely free to use and modify. The code is provided "as is" with
57 * no expressed or implied warranty. The author accepts no liability if it causes
58 * any damage to your computer, causes your pet to fall ill, increases baldness
59 * or makes your car start emitting strange noises when you start it up.
60 * This code has no bugs, just undocumented features!
61 *
62 * @todo Plugininfy the socket somehow ?
63 * It is technically possible to make the socket a plug-in but would it be a good idea ?
64 *
65 * @bug
66 *
67 */
68class NSClientT : public nsclient::logger /*: public nsclient_session::session_handler_interface*/ {
69
70public:
71        struct plugin_info_type {
72                std::wstring dll;
73                std::wstring name;
74                std::wstring version;
75                std::wstring description;
76        };
77        typedef std::list<plugin_info_type> plugin_info_list;
78        nsclient::logging_queue::master logger_master_;
79private:
80
81        class NSException {
82                std::wstring what_;
83        public:
84                NSException(std::wstring what) : what_(what){}
85                std::wstring what() {
86                        return what_;
87                }
88        };
89
90        typedef boost::shared_ptr<NSCPlugin> plugin_type;
91        typedef std::vector<plugin_type> pluginList;
92        pluginList plugins_;
93        boost::filesystem::wpath basePath;
94        boost::timed_mutex internalVariables;
95        boost::shared_mutex m_mutexRW;
96
97        //boost::shared_mutex m_mutexRWcmdDescriptions;
98        //cmdMap cmdDescriptions_;
99        enum log_status {log_state_unknown, log_state_looking, log_state_debug, log_state_nodebug };
100        log_status debug_;
101        std::wstring context_;
102#ifdef WIN32
103        com_helper::initialize_com com_helper_;
104#endif
105        /*
106        std::auto_ptr<nsclient_session::shared_client_session> shared_client_;
107        std::auto_ptr<nsclient_session::shared_server_session> shared_server_;
108        */
109
110        bool enable_shared_session_;
111        nsclient::commands commands_;
112        nsclient::channels channels_;
113        nsclient::routers routers_;
114        unsigned int next_plugin_id_;
115        std::wstring service_name_;
116
117
118public:
119        typedef std::multimap<std::wstring,std::wstring> plugin_alias_list_type;
120        // c-tor, d-tor
121        NSClientT(void) : debug_(log_state_unknown), enable_shared_session_(false), commands_(this), channels_(this), routers_(this), next_plugin_id_(0), service_name_(DEFAULT_SERVICE_NAME) {
122                logger_master_.start_slave();
123        }
124        virtual ~NSClientT(void) {}
125        void enableDebug(bool debug = true) {
126                if (debug)
127                        debug_ = log_state_debug;
128                else
129                        debug_ = log_state_nodebug;
130        }
131
132        // Service helper functions
133        bool initCore(bool boot);
134        bool exitCore(bool boot);
135        void set_settings_context(std::wstring context) { context_ = context; }
136#ifdef WIN32x
137        static void WINAPI service_main_dispatch(DWORD dwArgc, LPTSTR *lpszArgv);
138        static void WINAPI service_ctrl_dispatch(DWORD dwCtrlCode);
139        static DWORD WINAPI service_ctrl_dispatch_ex(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext);
140#endif
141        void service_on_session_changed(DWORD dwSessionId, bool logon, DWORD dwEventType);
142
143
144        // Logger impl
145        void nsclient_log_error(std::string file, int line, std::wstring error);
146
147        // Service API
148        static NSClient* get_global_instance();
149        /*
150        void handle_error(unsigned int line, const char *file, std::wstring message) {
151                std::string s = nsclient::logger_helper::create_error(file, line, message);
152                reportMessage(s.c_str());
153        }
154        */
155        void handle_startup(std::wstring service_name);
156        void handle_shutdown(std::wstring service_name);
157#ifdef _WIN32
158        void handle_session_change(unsigned long dwSessionId, bool logon);
159#endif
160
161
162        // Member functions
163        boost::filesystem::wpath getBasePath(void);
164
165        NSCAPI::errorReturn reroute(std::wstring &channel, const wchar_t* command, std::string &buffer);
166        NSCAPI::errorReturn send_notification(const wchar_t* channel, const wchar_t* command, char* buffer, unsigned int buffer_len);
167        NSCAPI::nagiosReturn injectRAW(const wchar_t* command, std::string &request, std::string &response);
168        NSCAPI::nagiosReturn inject(std::wstring command, std::wstring arguments, std::wstring &msg, std::wstring & perf);
169//      std::wstring inject(const std::wstring buffer);
170        std::wstring execute(std::wstring password, std::wstring cmd, std::list<std::wstring> args);
171        void reportMessage(std::string data);
172        int simple_exec(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std::list<std::wstring> &resp);
173        NSCAPI::nagiosReturn exec_command(const wchar_t* raw_command, std::string &request, std::string &response);
174
175        struct service_controller {
176                std::wstring service;
177                service_controller(std::wstring service) : service(service) {}
178                service_controller(const service_controller & other) : service(other.service) {}
179                service_controller& operator=(const service_controller & other) {
180                        service = other.service;
181                        return *this;
182                }
183                void stop();
184                void start();
185                std::wstring get_service_name() {
186                        return service;
187                }
188                bool is_started();
189        };
190
191        service_controller get_service_control();
192
193        //plugin_type loadPlugin(const boost::filesystem::wpath plugin, std::wstring alias);
194        void loadPlugins(NSCAPI::moduleLoadMode mode);
195        void unloadPlugins(bool unloadLoggers);
196        std::wstring describeCommand(std::wstring command);
197        std::list<std::wstring> getAllCommandNames();
198        void registerCommand(unsigned int id, std::wstring cmd, std::wstring desc);
199        void startTrayIcons();
200        void startTrayIcon(DWORD dwSessionId);
201
202        bool logDebug();
203        void listPlugins();
204        plugin_info_list get_all_plugins();
205        plugin_alias_list_type find_all_plugins(bool active);
206        std::list<std::wstring> list_commands();
207
208        // Shared session interface:
209        void session_error(std::string file, unsigned int line, std::wstring msg);
210        void session_info(std::string file, unsigned int line, std::wstring msg);
211        void session_log_message(int msgType, const char* file, const int line, std::wstring message) {
212                std::string s = nsclient::logger_helper::create_info(file, line, message);
213                reportMessage(s.c_str());
214        }
215        int session_inject(std::wstring command, std::wstring arguments, wchar_t splitter, bool escape, std::wstring &msg, std::wstring & perf) {
216                return 0; // TODO: Readd this!!! inject(command, arguments, splitter, escape, msg, perf);
217        }
218//      std::pair<std::wstring,std::wstring> session_get_name() {
219//              return std::pair<std::wstring,std::wstring>(SZAPPNAME,SZVERSION);
220//      }
221
222        std::wstring expand_path(std::wstring file);
223        void set_console_log() {
224                logger_master_.set_console_log();
225        }
226
227
228        public:
229                void load_all_plugins(int mode);
230
231                static void log_debug(const char* file, const int line, std::wstring message);
232                static void log_error(const char* file, const int line, std::wstring message);
233                static void log_error(const char* file, const int line, std::string message);
234                static void log_info(const char* file, const int line, std::wstring message);
235
236
237
238        private:
239                plugin_type addPlugin(boost::filesystem::wpath file, std::wstring alias);
240};
241
242
243extern NSClient mainClient;     // Global core instance forward declaration.
244
245
246std::wstring Encrypt(std::wstring str, unsigned int algorithm = NSCAPI::encryption_xor);
247std::wstring Decrypt(std::wstring str, unsigned int algorithm = NSCAPI::encryption_xor);
Note: See TracBrowser for help on using the repository browser.