source: nscp/include/NSCHelper.h @ 047516e

0.4.00.4.10.4.2stable
Last change on this file since 047516e was 047516e, checked in by Michael Medin <michael@…>, 5 years ago

2008-02-08 MickeM

+ Added some more default catch handlers (on the "core" side of plugin-calls).

2008-02-07 MickeM

+ Added default catch handlers to all wrapped plugin calls.

  • Property mode set to 100644
File size: 16.6 KB
Line 
1/**************************************************************************
2*   Copyright (C) 2004-2007 by Michael Medin <michael@medin.name>         *
3*                                                                         *
4*   This code is part of NSClient++ - http://trac.nakednuns.org/nscp      *
5*                                                                         *
6*   This program is free software; you can redistribute it and/or modify  *
7*   it under the terms of the GNU General Public License as published by  *
8*   the Free Software Foundation; either version 2 of the License, or     *
9*   (at your option) any later version.                                   *
10*                                                                         *
11*   This program is distributed in the hope that it will be useful,       *
12*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14*   GNU General Public License for more details.                          *
15*                                                                         *
16*   You should have received a copy of the GNU General Public License     *
17*   along with this program; if not, write to the                         *
18*   Free Software Foundation, Inc.,                                       *
19*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20***************************************************************************/
21#pragma once
22
23#include <string>
24#include <list>
25#include <NSCAPI.h>
26#include <iostream>
27#include <charEx.h>
28#include <arrayBuffer.h>
29#include <windows.h>
30
31
32namespace NSCHelper
33{
34#ifdef DEBUG
35        NSCAPI::nagiosReturn wrapReturnString(TCHAR *buffer, unsigned int bufLen, std::wstring str, NSCAPI::nagiosReturn defaultReturnCode);
36        NSCAPI::errorReturn wrapReturnString(TCHAR *buffer, unsigned int bufLen, std::wstring str, NSCAPI::errorReturn defaultReturnCode);
37#else
38        int wrapReturnString(TCHAR *buffer, unsigned int bufLen, std::wstring str, int defaultReturnCode);
39#endif
40        std::wstring translateMessageType(NSCAPI::messageTypes msgType);
41        std::wstring translateReturn(NSCAPI::nagiosReturn returnCode);
42        NSCAPI::nagiosReturn maxState(NSCAPI::nagiosReturn a, NSCAPI::nagiosReturn b);
43
44        inline bool isNagiosReturnCode(NSCAPI::nagiosReturn code) {
45                if ( (code == NSCAPI::returnOK) || (code == NSCAPI::returnWARN) || (code == NSCAPI::returnCRIT) || (code == NSCAPI::returnUNKNOWN) )
46                        return true;
47                return false;
48        }
49        inline bool isMyNagiosReturn(NSCAPI::nagiosReturn code) {
50                return code == NSCAPI::returnCRIT || code == NSCAPI::returnOK || code == NSCAPI::returnWARN || code == NSCAPI::returnUNKNOWN
51                        || code == NSCAPI::returnInvalidBufferLen || code == NSCAPI::returnIgnored;
52        }
53
54#ifdef DEBUG
55        inline NSCAPI::nagiosReturn int2nagios(int code) {
56                if (code == 0)
57                        return NSCAPI::returnOK;
58                if (code == 1)
59                        return NSCAPI::returnWARN;
60                if (code == 2)
61                        return NSCAPI::returnCRIT;
62                if (code == 4)
63                        return NSCAPI::returnUNKNOWN;
64                throw "@fixme bad code";
65        }
66        inline int nagios2int(NSCAPI::nagiosReturn code) {
67                if (code == NSCAPI::returnOK)
68                        return 0;
69                if (code == NSCAPI::returnWARN)
70                        return 1;
71                if (code == NSCAPI::returnCRIT)
72                        return 2;
73                if (code == NSCAPI::returnUNKNOWN)
74                        return 4;
75                throw "@fixme bad code";
76        }
77#else
78        inline NSCAPI::nagiosReturn int2nagios(int code) {
79                return code;
80        }
81        inline int nagios2int(NSCAPI::nagiosReturn code) {
82                return code;
83        }
84#endif
85        inline void escalteReturnCodeToCRIT(NSCAPI::nagiosReturn &currentReturnCode) {
86                currentReturnCode = NSCAPI::returnCRIT;
87        }
88        inline void escalteReturnCodeToWARN(NSCAPI::nagiosReturn &currentReturnCode) {
89                if (currentReturnCode != NSCAPI::returnCRIT)
90                        currentReturnCode = NSCAPI::returnWARN;
91        }
92};
93
94namespace NSCModuleHelper
95{
96        class NSCMHExcpetion {
97        public:
98                std::wstring msg_;
99                NSCMHExcpetion(std::wstring msg) : msg_(msg) {}
100        };
101        // Types for the Callbacks into the main program
102        typedef NSCAPI::errorReturn (*lpNSAPIGetBasePath)(TCHAR*,unsigned int);
103        typedef NSCAPI::errorReturn (*lpNSAPIGetApplicationName)(TCHAR*,unsigned int);
104        typedef NSCAPI::errorReturn (*lpNSAPIGetApplicationVersionStr)(TCHAR*,unsigned int);
105        typedef NSCAPI::errorReturn (*lpNSAPIGetSettingsString)(const TCHAR*,const TCHAR*,const TCHAR*,TCHAR*,unsigned int);
106        typedef NSCAPI::errorReturn (*lpNSAPIGetSettingsInt)(const TCHAR*, const TCHAR*, int);
107        typedef NSCAPI::errorReturn (*lpNSAPIGetSettingsSection)(const TCHAR*, arrayBuffer::arrayBuffer*, unsigned int *);
108        typedef NSCAPI::errorReturn (*lpNSAPIReleaseSettingsSectionBuffer)(arrayBuffer::arrayBuffer*, unsigned int *);
109        typedef void (*lpNSAPIMessage)(int, const TCHAR*, const int, const TCHAR*);
110        typedef NSCAPI::errorReturn (*lpNSAPIStopServer)(void);
111        typedef NSCAPI::nagiosReturn (*lpNSAPIInject)(const TCHAR*, const unsigned int, TCHAR **, TCHAR *, unsigned int, TCHAR *, unsigned int);
112        typedef void* (*lpNSAPILoader)(TCHAR*);
113        typedef NSCAPI::boolReturn (*lpNSAPICheckLogMessages)(int);
114        typedef NSCAPI::errorReturn (*lpNSAPIEncrypt)(unsigned int, const TCHAR*, unsigned int, TCHAR*, unsigned int *);
115        typedef NSCAPI::errorReturn (*lpNSAPIDecrypt)(unsigned int, const TCHAR*, unsigned int, TCHAR*, unsigned int *);
116        typedef NSCAPI::errorReturn (*lpNSAPISetSettingsString)(const TCHAR*, const TCHAR*, const TCHAR*);
117        typedef NSCAPI::errorReturn (*lpNSAPISetSettingsInt)(const TCHAR*, const TCHAR*, int);
118        typedef NSCAPI::errorReturn (*lpNSAPIWriteSettings)(int);
119        typedef NSCAPI::errorReturn (*lpNSAPIReadSettings)(int);
120        typedef NSCAPI::errorReturn (*lpNSAPIRehash)(int);
121        typedef NSCAPI::errorReturn (*lpNSAPIDescribeCommand)(const TCHAR*,TCHAR*,unsigned int);
122        typedef NSCAPI::errorReturn (*lpNSAPIGetAllCommandNames)(arrayBuffer::arrayBuffer*, unsigned int *);
123        typedef NSCAPI::errorReturn (*lpNSAPIReleaseAllCommandNamessBuffer)(arrayBuffer::arrayBuffer*, unsigned int *);
124        typedef NSCAPI::errorReturn (*lpNSAPIRegisterCommand)(const TCHAR*,const TCHAR*);
125
126        // Helper functions for calling into the core
127        std::wstring getApplicationName(void);
128        std::wstring getApplicationVersionString(void);
129        std::list<std::wstring> getSettingsSection(std::wstring section);
130        std::wstring getSettingsString(std::wstring section, std::wstring key, std::wstring defaultValue);
131        int getSettingsInt(std::wstring section, std::wstring key, int defaultValue);
132        void Message(int msgType, std::wstring file, int line, std::wstring message);
133        NSCAPI::nagiosReturn InjectCommandRAW(const TCHAR* command, const unsigned int argLen, TCHAR **argument, TCHAR *returnMessageBuffer, unsigned int returnMessageBufferLen, TCHAR *returnPerfBuffer, unsigned int returnPerfBufferLen);
134        NSCAPI::nagiosReturn InjectCommand(const TCHAR* command, const unsigned int argLen, TCHAR **argument, std::wstring & message, std::wstring & perf);
135        NSCAPI::nagiosReturn InjectSplitAndCommand(const TCHAR* command, TCHAR* buffer, TCHAR splitChar, std::wstring & message, std::wstring & perf);
136        NSCAPI::nagiosReturn InjectSplitAndCommand(const std::wstring command, const std::wstring buffer, TCHAR splitChar, std::wstring & message, std::wstring & perf);
137        void StopService(void);
138        std::wstring getBasePath();
139        bool logDebug();
140        bool checkLogMessages(int type);
141        std::wstring Encrypt(std::wstring str, unsigned int algorithm = NSCAPI::xor);
142        std::wstring Decrypt(std::wstring str, unsigned int algorithm = NSCAPI::xor);
143        NSCAPI::errorReturn SetSettingsString(std::wstring section, std::wstring key, std::wstring value);
144        NSCAPI::errorReturn SetSettingsInt(std::wstring section, std::wstring key, int value);
145        NSCAPI::errorReturn WriteSettings(int type);
146        NSCAPI::errorReturn ReadSettings(int type);
147        NSCAPI::errorReturn Rehash(int flag);
148        std::list<std::wstring> getAllCommandNames();
149        std::wstring describeCommand(std::wstring command);
150        void registerCommand(std::wstring command, std::wstring description);
151};
152
153namespace NSCModuleWrapper {
154        struct module_version {
155                int major;
156                int minor;
157                int revision;
158        };
159
160        BOOL wrapDllMain(HANDLE hModule, DWORD ul_reason_for_call);
161        HINSTANCE getModule();
162
163        int wrapModuleHelperInit(NSCModuleHelper::lpNSAPILoader f);;
164        NSCAPI::errorReturn wrapGetModuleName(TCHAR* buf, unsigned int buflen, std::wstring str);
165        NSCAPI::errorReturn wrapGetConfigurationMeta(TCHAR* buf, unsigned int buflen, std::wstring str);
166        int wrapLoadModule(bool success);
167        NSCAPI::errorReturn wrapGetModuleVersion(int *major, int *minor, int *revision, module_version version);
168        NSCAPI::boolReturn wrapHasCommandHandler(bool has);
169        NSCAPI::boolReturn wrapHasMessageHandler(bool has);
170        int wrapUnloadModule(bool success);
171        NSCAPI::nagiosReturn wrapHandleCommand(NSCAPI::nagiosReturn retResult, const std::wstring retMessage, const std::wstring retPerformance, TCHAR *returnBufferMessage, unsigned int returnBufferMessageLen, TCHAR *returnBufferPerf, unsigned int returnBufferPerfLen);
172}
173
174//////////////////////////////////////////////////////////////////////////
175// Module wrappers (definitions)
176#define NSC_WRAPPERS_MAIN() \
177        extern "C" int NSModuleHelperInit(NSCModuleHelper::lpNSAPILoader f); \
178        extern int NSLoadModule(); \
179        extern int NSGetModuleName(TCHAR* buf, int buflen); \
180        extern int NSGetModuleDescription(TCHAR* buf, int buflen); \
181        extern int NSGetModuleVersion(int *major, int *minor, int *revision); \
182        extern NSCAPI::boolReturn NSHasCommandHandler(); \
183        extern NSCAPI::boolReturn NSHasMessageHandler(); \
184        extern void NSHandleMessage(int msgType, TCHAR* file, int line, TCHAR* message); \
185        extern NSCAPI::nagiosReturn NSHandleCommand(const TCHAR* IN_cmd, const unsigned int IN_argsLen, TCHAR **IN_args, \
186                TCHAR *OUT_retBufMessage, unsigned int IN_retBufMessageLen, TCHAR *OUT_retBufPerf, unsigned int IN_retBufPerfLen); \
187        extern int NSUnloadModule(); \
188        extern int NSGetConfigurationMeta(int IN_retBufLen, TCHAR *OUT_retBuf)
189
190#define NSC_WRAPPERS_CLI() \
191        extern int NSCommandLineExec(const TCHAR*,const unsigned int,TCHAR**)
192
193
194
195#define NSC_LOG_ERROR_STD(msg) NSC_LOG_ERROR(((std::wstring)msg).c_str())
196#define NSC_LOG_ERROR(msg) \
197        NSCModuleHelper::Message(NSCAPI::error, _T(__FILE__), __LINE__, msg)
198
199#define NSC_LOG_CRITICAL_STD(msg) NSC_LOG_CRITICAL(((std::wstring)msg).c_str())
200#define NSC_LOG_CRITICAL(msg) \
201        NSCModuleHelper::Message(NSCAPI::critical, _T(__FILE__), __LINE__, msg)
202
203#define NSC_LOG_MESSAGE_STD(msg) NSC_LOG_MESSAGE(((std::wstring)msg).c_str())
204#define NSC_LOG_MESSAGE(msg) \
205        NSCModuleHelper::Message(NSCAPI::log, _T(__FILE__), __LINE__, msg)
206
207//#define NSC_DEBUG_MSG_STD(msg) NSC_DEBUG_MSG(((std::wstring)msg).c_str())
208#define NSC_DEBUG_MSG_STD(msg) NSC_DEBUG_MSG((std::wstring)msg)
209#define NSC_DEBUG_MSG(msg) \
210        NSCModuleHelper::Message(NSCAPI::debug, _T(__FILE__), __LINE__, msg)
211
212/*
213#define NSC_DEBUG_MSG_STD(msg)
214#define NSC_DEBUG_MSG(msg)
215*/
216//////////////////////////////////////////////////////////////////////////
217// Message wrappers below this point
218
219#define NSC_WRAPPERS_MAIN_DEF(toObject) \
220        extern int NSModuleHelperInit(NSCModuleHelper::lpNSAPILoader f) { \
221                try { \
222                        return NSCModuleWrapper::wrapModuleHelperInit(f); \
223                } catch (...) { \
224                        NSC_LOG_CRITICAL(_T("Unknown exception in: wrapModuleHelperInit(...)")); \
225                        return NSCAPI::hasFailed; \
226                } \
227        } \
228        extern int NSLoadModule() { \
229                try { \
230                        return NSCModuleWrapper::wrapLoadModule(toObject.loadModule()); \
231                } catch (...) { \
232                        NSC_LOG_CRITICAL(_T("Unknown exception in: wrapLoadModule(...)")); \
233                        return NSCAPI::hasFailed; \
234                } \
235        } \
236        extern int NSGetModuleName(TCHAR* buf, int buflen) { \
237                try { \
238                        return NSCModuleWrapper::wrapGetModuleName(buf, buflen, toObject.getModuleName()); \
239                } catch (...) { \
240                        NSC_LOG_CRITICAL(_T("Unknown exception in: wrapGetModuleName(...)")); \
241                        return NSCAPI::hasFailed; \
242                } \
243        } \
244        extern int NSGetModuleDescription(TCHAR* buf, int buflen) { \
245                try { \
246                        return NSCModuleWrapper::wrapGetModuleName(buf, buflen, toObject.getModuleDescription()); \
247                } catch (...) { \
248                        NSC_LOG_CRITICAL(_T("Unknown exception in: wrapGetModuleName(...)")); \
249                        return NSCAPI::hasFailed; \
250                } \
251        } \
252        extern int NSGetModuleVersion(int *major, int *minor, int *revision) { \
253                try { \
254                        return NSCModuleWrapper::wrapGetModuleVersion(major, minor, revision, toObject.getModuleVersion()); \
255                } catch (...) { \
256                        NSC_LOG_CRITICAL(_T("Unknown exception in: wrapGetModuleVersion(...)")); \
257                        return NSCAPI::hasFailed; \
258                } \
259        } \
260        extern int NSUnloadModule() { \
261                try { \
262                        return NSCModuleWrapper::wrapUnloadModule(toObject.unloadModule()); \
263                } catch (...) { \
264                        NSC_LOG_CRITICAL(_T("Unknown exception in: wrapGetModuleVersion(...)")); \
265                        return NSCAPI::hasFailed; \
266                } \
267        }
268#define NSC_WRAPPERS_HANDLE_MSG_DEF(toObject) \
269        extern void NSHandleMessage(int msgType, TCHAR* file, int line, TCHAR* message) { \
270                try { \
271                        toObject.handleMessage(msgType, file, line, message); \
272                } catch (...) { \
273                        NSC_LOG_CRITICAL(_T("Unknown exception in: handleMessage(...)")); \
274                } \
275        } \
276        extern NSCAPI::boolReturn NSHasMessageHandler() { \
277                try { \
278                        return NSCModuleWrapper::wrapHasMessageHandler(toObject.hasMessageHandler()); \
279                } catch (...) { \
280                        NSC_LOG_CRITICAL(_T("Unknown exception in: wrapHasMessageHandler(...)")); \
281                        return NSCAPI::isfalse; \
282                } \
283        }
284#define NSC_WRAPPERS_IGNORE_MSG_DEF() \
285        extern void NSHandleMessage(int msgType, TCHAR* file, int line, TCHAR* message) {} \
286        extern NSCAPI::boolReturn NSHasMessageHandler() { return NSCAPI::isfalse; }
287#define NSC_WRAPPERS_HANDLE_CMD_DEF(toObject) \
288        extern NSCAPI::nagiosReturn NSHandleCommand(const TCHAR* IN_cmd, const unsigned int IN_argsLen, TCHAR **IN_args, \
289                                                                        TCHAR *OUT_retBufMessage, unsigned int IN_retBufMessageLen, TCHAR *OUT_retBufPerf, unsigned int IN_retBufPerfLen) \
290        { \
291                try { \
292                        std::wstring message, perf; \
293                        NSCAPI::nagiosReturn retCode = toObject.handleCommand(IN_cmd, IN_argsLen, IN_args, message, perf); \
294                        return NSCModuleWrapper::wrapHandleCommand(retCode, message, perf, OUT_retBufMessage, IN_retBufMessageLen, OUT_retBufPerf, IN_retBufPerfLen); \
295                } catch (...) { \
296                        NSC_LOG_CRITICAL(_T("Unknown exception in: wrapHandleCommand(...)")); \
297                        return NSCAPI::returnIgnored; \
298                } \
299        } \
300        extern NSCAPI::boolReturn NSHasCommandHandler() { \
301                try { \
302                        return NSCModuleWrapper::wrapHasCommandHandler(toObject.hasCommandHandler()); \
303                } catch (...) { \
304                        NSC_LOG_CRITICAL(_T("Unknown exception in: wrapHasCommandHandler(...)")); \
305                        return NSCAPI::isfalse; \
306                } \
307        }
308#define NSC_WRAPPERS_IGNORE_CMD_DEF() \
309        extern NSCAPI::nagiosReturn NSHandleCommand(const TCHAR* IN_cmd, const unsigned int IN_argsLen, TCHAR **IN_args, \
310                                                                        TCHAR *OUT_retBufMessage, unsigned int IN_retBufMessageLen, TCHAR *OUT_retBufPerf, unsigned int IN_retBufPerfLen) { \
311                return NSCAPI::returnIgnored; \
312        } \
313        extern NSCAPI::boolReturn NSHasCommandHandler() { return NSCAPI::isfalse; }
314
315
316#define NSC_WRAPPERS_HANDLE_CONFIGURATION(toObject) \
317        extern int NSGetConfigurationMeta(int IN_retBufLen, TCHAR *OUT_retBuf) \
318        { \
319                try { \
320                        return NSCModuleWrapper::wrapGetConfigurationMeta(OUT_retBuf, IN_retBufLen, toObject.getConfigurationMeta()); \
321                } catch (...) { \
322                        NSC_LOG_CRITICAL(_T("Unknown exception in: wrapGetConfigurationMeta(...)")); \
323                        return NSCAPI::hasFailed; \
324                } \
325        }
326
327#define NSC_WRAPPERS_CLI_DEF(toObject) \
328        extern int NSCommandLineExec(const TCHAR* command,const unsigned int argLen,TCHAR** args) { \
329                try { \
330                        return toObject.commandLineExec(command, argLen, args); \
331                } catch (...) { \
332                        NSC_LOG_CRITICAL(_T("Unknown exception in: commandLineExec(...)")); \
333                        return NSCAPI::hasFailed; \
334                } \
335        } \
336
337//////////////////////////////////////////////////////////////////////////
338#define MODULE_SETTINGS_START(class, name, description) \
339        std::wstring class::getConfigurationMeta() { \
340        return (std::wstring)_T("<module name=\"") + name + _T("\" description=\"") + description + _T("\">") \
341        _T("<pages>")
342
343
344#define ADVANCED_PAGE(title) \
345        _T("<page title=\"") title _T("\" advanced=\"true\">") \
346        _T("<items>")
347
348#define PAGE(title) \
349        _T("<page title=\"") title _T("\">") \
350        _T("<items>")
351
352#define ITEM_EDIT_TEXT(caption, description) \
353        _T("<item type=\"text\" caption=\"") caption _T("\" description=\"") description _T("\"><options>")
354
355#define ITEM_EDIT_OPTIONAL_LIST(caption, description) \
356        _T("<item type=\"optional_list\" caption=\"") caption _T("\" description=\"") description _T("\"><options>")
357
358#define ITEM_CHECK_BOOL(caption, description) \
359        _T("<item type=\"bool\" caption=\"") caption _T("\" description=\"") description _T("\"><options>")
360
361#define ITEM_MAP_TO(type) \
362        _T("</options><mapper type=\"") type _T("\">") \
363        _T("<options>")
364
365#define OPTION(key, value) \
366        _T("<option key=\"") key _T("\" value=\"") value _T("\"/>")
367
368#define ITEM_END() \
369        _T("</options>") \
370        _T("</mapper>") \
371        _T("</item>")
372
373#define PAGE_END() \
374        _T("</items>") \
375        _T("</page>")
376
377#define MODULE_SETTINGS_END() \
378                        _T("</pages>") \
379                _T("</module>"); \
380        }
381
Note: See TracBrowser for help on using the repository browser.