source: nscp/service/NSClient++.h @ 98113da

0.4.00.4.10.4.2
Last change on this file since 98113da was 98113da, checked in by Michael Medin <michael@…>, 20 months ago
  • Real-time CheckEventLog working (still only for one, and I think application log)
  • Added python tests to validfate that Real-time eventlog is working.
  • Fixed som defects here and there (now builds on Linux again)
  • Fixed so it builds in "debug mode"
  • Fixed issue in grammar which caused infiniate loop in som cases
  • Fixed so error rendering in eventlog works with "infininate number of argumnets"
  • Added support for targeting execs (in API)
  • Fixed some invalid return messages
  • Streamlined submissions wrappers to be more inline with "other wrappers"
  • Fixed a myrriad of minor python script bugs
  • Added sleep command (which sometimes causes issues so use with care)
  • Property mode set to 100644
File size: 9.2 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 boot_init();
134        bool boot_load_plugins(bool boot);
135        bool exitCore(bool boot);
136        void set_settings_context(std::wstring context) { context_ = context; }
137#ifdef WIN32x
138        static void WINAPI service_main_dispatch(DWORD dwArgc, LPTSTR *lpszArgv);
139        static void WINAPI service_ctrl_dispatch(DWORD dwCtrlCode);
140        static DWORD WINAPI service_ctrl_dispatch_ex(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext);
141#endif
142        void service_on_session_changed(DWORD dwSessionId, bool logon, DWORD dwEventType);
143
144
145        // Logger impl
146        void nsclient_log_error(std::string file, int line, std::wstring error);
147
148        // Service API
149        static NSClient* get_global_instance();
150        /*
151        void handle_error(unsigned int line, const char *file, std::wstring message) {
152                std::string s = nsclient::logger_helper::create_error(file, line, message);
153                reportMessage(s.c_str());
154        }
155        */
156        void handle_startup(std::wstring service_name);
157        void handle_shutdown(std::wstring service_name);
158#ifdef _WIN32
159        void handle_session_change(unsigned long dwSessionId, bool logon);
160#endif
161
162
163        // Member functions
164        boost::filesystem::wpath getBasePath(void);
165
166        NSCAPI::errorReturn reroute(std::wstring &channel, std::string &buffer);
167        NSCAPI::errorReturn send_notification(const wchar_t* channel, std::string &request, std::string &response);
168        NSCAPI::nagiosReturn injectRAW(const wchar_t* command, std::string &request, std::string &response);
169        NSCAPI::nagiosReturn inject(std::wstring command, std::wstring arguments, std::wstring &msg, std::wstring & perf);
170//      std::wstring inject(const std::wstring buffer);
171        std::wstring execute(std::wstring password, std::wstring cmd, std::list<std::wstring> args);
172        void reportMessage(std::string data);
173        int simple_exec(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std::list<std::wstring> &resp);
174        NSCAPI::nagiosReturn exec_command(const wchar_t* target, const wchar_t* raw_command, std::string &request, std::string &response);
175        NSCAPI::errorReturn register_submission_listener(unsigned int plugin_id, const wchar_t* channel);
176        NSCAPI::errorReturn register_routing_listener(unsigned int plugin_id, const wchar_t* channel);
177
178        NSCAPI::errorReturn reload(const wchar_t *module);
179
180        struct service_controller {
181                std::wstring service;
182                service_controller(std::wstring service) : service(service) {}
183                service_controller(const service_controller & other) : service(other.service) {}
184                service_controller& operator=(const service_controller & other) {
185                        service = other.service;
186                        return *this;
187                }
188                void stop();
189                void start();
190                std::wstring get_service_name() {
191                        return service;
192                }
193                bool is_started();
194        };
195
196        service_controller get_service_control();
197
198        //plugin_type loadPlugin(const boost::filesystem::wpath plugin, std::wstring alias);
199        void loadPlugins(NSCAPI::moduleLoadMode mode);
200        void unloadPlugins(bool unloadLoggers);
201        std::wstring describeCommand(std::wstring command);
202        std::list<std::wstring> getAllCommandNames();
203        void registerCommand(unsigned int id, std::wstring cmd, std::wstring desc);
204        void startTrayIcons();
205        void startTrayIcon(DWORD dwSessionId);
206
207        bool logDebug();
208        void listPlugins();
209        plugin_info_list get_all_plugins();
210        plugin_alias_list_type find_all_plugins(bool active);
211        std::list<std::wstring> list_commands();
212
213        // Shared session interface:
214        void session_error(std::string file, unsigned int line, std::wstring msg);
215        void session_info(std::string file, unsigned int line, std::wstring msg);
216        void session_log_message(int msgType, const char* file, const int line, std::wstring message) {
217                std::string s = nsclient::logger_helper::create_info(file, line, message);
218                reportMessage(s.c_str());
219        }
220        int session_inject(std::wstring command, std::wstring arguments, wchar_t splitter, bool escape, std::wstring &msg, std::wstring & perf) {
221                return 0; // TODO: Readd this!!! inject(command, arguments, splitter, escape, msg, perf);
222        }
223//      std::pair<std::wstring,std::wstring> session_get_name() {
224//              return std::pair<std::wstring,std::wstring>(SZAPPNAME,SZVERSION);
225//      }
226
227        std::wstring expand_path(std::wstring file);
228        void set_console_log() {
229                logger_master_.set_console_log();
230        }
231
232
233        public:
234                void load_all_plugins(int mode);
235
236                static void log_debug(const char* file, const int line, std::wstring message);
237                static void log_error(const char* file, const int line, std::wstring message);
238                static void log_error(const char* file, const int line, std::string message);
239                static void log_info(const char* file, const int line, std::wstring message);
240
241
242
243        private:
244                plugin_type addPlugin(boost::filesystem::wpath file, std::wstring alias);
245};
246
247
248extern NSClient mainClient;     // Global core instance forward declaration.
249
250
251std::wstring Encrypt(std::wstring str, unsigned int algorithm = NSCAPI::encryption_xor);
252std::wstring Decrypt(std::wstring str, unsigned int algorithm = NSCAPI::encryption_xor);
Note: See TracBrowser for help on using the repository browser.