| 1 | #pragma once |
|---|
| 2 | |
|---|
| 3 | #include <string> |
|---|
| 4 | #include <list> |
|---|
| 5 | #include <NSCAPI.h> |
|---|
| 6 | #include <iostream> |
|---|
| 7 | #include <charEx.h> |
|---|
| 8 | |
|---|
| 9 | namespace NSCHelper |
|---|
| 10 | { |
|---|
| 11 | // int wrapReturnString(char *buffer, unsigned int bufLen, std::string str, int defaultReturnCode); |
|---|
| 12 | NSCAPI::nagiosReturn wrapReturnString(char *buffer, unsigned int bufLen, std::string str, NSCAPI::nagiosReturn defaultReturnCode); |
|---|
| 13 | NSCAPI::errorReturn wrapReturnString(char *buffer, unsigned int bufLen, std::string str, NSCAPI::errorReturn defaultReturnCode); |
|---|
| 14 | |
|---|
| 15 | std::list<std::string> arrayBuffer2list(const unsigned int argLen, char **argument); |
|---|
| 16 | char ** list2arrayBuffer(const std::list<std::string> lst, unsigned int &argLen); |
|---|
| 17 | char ** split2arrayBuffer(const char* buffer, char splitChar, unsigned int &argLen); |
|---|
| 18 | std::string arrayBuffer2string(char **argument, const unsigned int argLen, std::string join); |
|---|
| 19 | char ** createEmptyArrayBuffer(unsigned int &argLen); |
|---|
| 20 | void destroyArrayBuffer(char **argument, const unsigned int argLen); |
|---|
| 21 | |
|---|
| 22 | std::string translateMessageType(NSCAPI::messageTypes msgType); |
|---|
| 23 | std::string translateReturn(NSCAPI::nagiosReturn returnCode); |
|---|
| 24 | |
|---|
| 25 | /* |
|---|
| 26 | / * ************************************************************************** |
|---|
| 27 | * max_state(STATE_x, STATE_y) |
|---|
| 28 | * compares STATE_x to STATE_y and returns result based on the following |
|---|
| 29 | * STATE_UNKNOWN < STATE_OK < STATE_WARNING < STATE_CRITICAL |
|---|
| 30 | * |
|---|
| 31 | * Note that numerically the above does not hold |
|---|
| 32 | **************************************************************************** / |
|---|
| 33 | |
|---|
| 34 | int |
|---|
| 35 | max_state (int a, int b) |
|---|
| 36 | { |
|---|
| 37 | if (a == STATE_CRITICAL || b == STATE_CRITICAL) |
|---|
| 38 | return STATE_CRITICAL; |
|---|
| 39 | else if (a == STATE_WARNING || b == STATE_WARNING) |
|---|
| 40 | return STATE_WARNING; |
|---|
| 41 | else if (a == STATE_OK || b == STATE_OK) |
|---|
| 42 | return STATE_OK; |
|---|
| 43 | else if (a == STATE_UNKNOWN || b == STATE_UNKNOWN) |
|---|
| 44 | return STATE_UNKNOWN; |
|---|
| 45 | else if (a == STATE_DEPENDENT || b == STATE_DEPENDENT) |
|---|
| 46 | return STATE_DEPENDENT; |
|---|
| 47 | else |
|---|
| 48 | return max (a, b); |
|---|
| 49 | } |
|---|
| 50 | @bug Use this scheme instead!! |
|---|
| 51 | */ |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | inline void escalteReturnCode(NSCAPI::nagiosReturn ¤tReturnCode, NSCAPI::nagiosReturn newReturnCode) { |
|---|
| 55 | if (newReturnCode == NSCAPI::returnCRIT) |
|---|
| 56 | currentReturnCode = NSCAPI::returnCRIT; |
|---|
| 57 | else if ((newReturnCode == NSCAPI::returnWARN) && (currentReturnCode != NSCAPI::returnCRIT) ) |
|---|
| 58 | currentReturnCode = NSCAPI::returnWARN; |
|---|
| 59 | else if ((newReturnCode == NSCAPI::returnUNKNOWN) |
|---|
| 60 | && (currentReturnCode != NSCAPI::returnCRIT) |
|---|
| 61 | && (currentReturnCode != NSCAPI::returnWARN) ) |
|---|
| 62 | currentReturnCode = NSCAPI::returnUNKNOWN; |
|---|
| 63 | } |
|---|
| 64 | inline void escalteReturnCodeToCRIT(NSCAPI::nagiosReturn ¤tReturnCode) { |
|---|
| 65 | currentReturnCode = NSCAPI::returnCRIT; |
|---|
| 66 | } |
|---|
| 67 | inline void escalteReturnCodeToWARN(NSCAPI::nagiosReturn ¤tReturnCode) { |
|---|
| 68 | if (currentReturnCode != NSCAPI::returnCRIT) |
|---|
| 69 | currentReturnCode = NSCAPI::returnWARN; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | }; |
|---|
| 73 | |
|---|
| 74 | namespace NSCModuleHelper |
|---|
| 75 | { |
|---|
| 76 | class NSCMHExcpetion { |
|---|
| 77 | public: |
|---|
| 78 | std::string msg_; |
|---|
| 79 | NSCMHExcpetion(std::string msg) : msg_(msg) {} |
|---|
| 80 | }; |
|---|
| 81 | // Types for the Callbacks into the main program |
|---|
| 82 | typedef NSCAPI::errorReturn (*lpNSAPIGetBasePath)(char*,unsigned int); |
|---|
| 83 | typedef NSCAPI::errorReturn (*lpNSAPIGetApplicationName)(char*,unsigned int); |
|---|
| 84 | typedef NSCAPI::errorReturn (*lpNSAPIGetApplicationVersionStr)(char*,unsigned int); |
|---|
| 85 | typedef NSCAPI::errorReturn (*lpNSAPIGetSettingsString)(const char*,const char*,const char*,char*,unsigned int); |
|---|
| 86 | typedef NSCAPI::errorReturn (*lpNSAPIGetSettingsInt)(const char*, const char*, int); |
|---|
| 87 | typedef void (*lpNSAPIMessage)(int, const char*, const int, const char*); |
|---|
| 88 | typedef NSCAPI::errorReturn (*lpNSAPIStopServer)(void); |
|---|
| 89 | typedef NSCAPI::nagiosReturn (*lpNSAPIInject)(const char*, const unsigned int, char **, char *, unsigned int ); |
|---|
| 90 | typedef LPVOID (*lpNSAPILoader)(char*); |
|---|
| 91 | |
|---|
| 92 | // Helper functions for calling into the core |
|---|
| 93 | std::string getApplicationName(void); |
|---|
| 94 | std::string getApplicationVersionString(void); |
|---|
| 95 | std::string getSettingsString(std::string section, std::string key, std::string defaultValue); |
|---|
| 96 | int getSettingsInt(std::string section, std::string key, int defaultValue); |
|---|
| 97 | void Message(int msgType, std::string file, int line, std::string message); |
|---|
| 98 | NSCAPI::nagiosReturn InjectCommandRAW(const char* command, const unsigned int argLen, char **argument, char *returnBuffer, unsigned int returnBufferLen); |
|---|
| 99 | NSCAPI::nagiosReturn InjectCommand(const char* command, const unsigned int argLen, char **argument, std::string & message, std::string & perf); |
|---|
| 100 | NSCAPI::nagiosReturn InjectSplitAndCommand(const char* command, char* buffer, char splitChar, std::string & message, std::string & perf); |
|---|
| 101 | void StopService(void); |
|---|
| 102 | std::string getBasePath(); |
|---|
| 103 | }; |
|---|
| 104 | |
|---|
| 105 | namespace NSCModuleWrapper { |
|---|
| 106 | struct module_version { |
|---|
| 107 | int major; |
|---|
| 108 | int minor; |
|---|
| 109 | int revision; |
|---|
| 110 | }; |
|---|
| 111 | |
|---|
| 112 | BOOL wrapDllMain(HANDLE hModule, DWORD ul_reason_for_call); |
|---|
| 113 | HINSTANCE getModule(); |
|---|
| 114 | |
|---|
| 115 | int wrapModuleHelperInit(NSCModuleHelper::lpNSAPILoader f);; |
|---|
| 116 | NSCAPI::errorReturn wrapGetModuleName(char* buf, unsigned int buflen, std::string str); |
|---|
| 117 | int wrapLoadModule(bool success); |
|---|
| 118 | NSCAPI::errorReturn wrapGetModuleVersion(int *major, int *minor, int *revision, module_version version); |
|---|
| 119 | NSCAPI::boolReturn wrapHasCommandHandler(bool has); |
|---|
| 120 | NSCAPI::boolReturn wrapHasMessageHandler(bool has); |
|---|
| 121 | int wrapUnloadModule(bool success); |
|---|
| 122 | NSCAPI::nagiosReturn wrapHandleCommand(NSCAPI::nagiosReturn retResult, const std::string retMessage, const std::string retPerformance, char *returnBufferMessage, unsigned int returnBufferMessageLen, char *returnBufferPerf, unsigned int returnBufferPerfLen); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 126 | // Module wrappers (definitions) |
|---|
| 127 | #define NSC_WRAPPERS_MAIN() \ |
|---|
| 128 | extern "C" int NSModuleHelperInit(NSCModuleHelper::lpNSAPILoader f); \ |
|---|
| 129 | extern int NSLoadModule(); \ |
|---|
| 130 | extern int NSGetModuleName(char* buf, int buflen); \ |
|---|
| 131 | extern int NSGetModuleVersion(int *major, int *minor, int *revision); \ |
|---|
| 132 | extern NSCAPI::boolReturn NSHasCommandHandler(); \ |
|---|
| 133 | extern NSCAPI::boolReturn NSHasMessageHandler(); \ |
|---|
| 134 | extern void NSHandleMessage(int msgType, char* file, int line, char* message); \ |
|---|
| 135 | extern NSCAPI::nagiosReturn NSHandleCommand(const char* IN_cmd, const unsigned int IN_argsLen, char **IN_args, \ |
|---|
| 136 | char *OUT_retBufMessage, unsigned int IN_retBufMessageLen, char *OUT_retBufPerf, unsigned int IN_retBufPerfLen); \ |
|---|
| 137 | extern int NSUnloadModule(); |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | #define NSC_LOG_ERROR_STD(msg) NSC_LOG_ERROR(((std::string)msg).c_str()) |
|---|
| 142 | #define NSC_LOG_ERROR(msg) \ |
|---|
| 143 | NSCModuleHelper::Message(NSCAPI::error, __FILE__, __LINE__, msg) |
|---|
| 144 | |
|---|
| 145 | #define NSC_LOG_CRITICAL_STD(msg) NSC_LOG_CRITICAL(((std::string)msg).c_str()) |
|---|
| 146 | #define NSC_LOG_CRITICAL(msg) \ |
|---|
| 147 | NSCModuleHelper::Message(NSCAPI::critical, __FILE__, __LINE__, msg) |
|---|
| 148 | |
|---|
| 149 | #define NSC_LOG_MESSAGE_STD(msg) NSC_LOG_MESSAGE(((std::string)msg).c_str()) |
|---|
| 150 | #define NSC_LOG_MESSAGE(msg) \ |
|---|
| 151 | NSCModuleHelper::Message(NSCAPI::log, __FILE__, __LINE__, msg) |
|---|
| 152 | |
|---|
| 153 | #define NSC_DEBUG_MSG_STD(msg) NSC_DEBUG_MSG(((std::string)msg).c_str()) |
|---|
| 154 | #define NSC_DEBUG_MSG(msg) \ |
|---|
| 155 | NSCModuleHelper::Message(NSCAPI::debug, __FILE__, __LINE__, msg) |
|---|
| 156 | |
|---|
| 157 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 158 | // Message wrappers below this point |
|---|
| 159 | |
|---|
| 160 | #define NSC_WRAPPERS_MAIN_DEF(toObject) \ |
|---|
| 161 | extern int NSModuleHelperInit(NSCModuleHelper::lpNSAPILoader f) { \ |
|---|
| 162 | return NSCModuleWrapper::wrapModuleHelperInit(f); \ |
|---|
| 163 | } \ |
|---|
| 164 | extern int NSLoadModule() { \ |
|---|
| 165 | return NSCModuleWrapper::wrapLoadModule(toObject.loadModule()); \ |
|---|
| 166 | } \ |
|---|
| 167 | extern int NSGetModuleName(char* buf, int buflen) { \ |
|---|
| 168 | return NSCModuleWrapper::wrapGetModuleName(buf, buflen, toObject.getModuleName()); \ |
|---|
| 169 | } \ |
|---|
| 170 | extern int NSGetModuleVersion(int *major, int *minor, int *revision) { \ |
|---|
| 171 | return NSCModuleWrapper::wrapGetModuleVersion(major, minor, revision, toObject.getModuleVersion()); \ |
|---|
| 172 | } \ |
|---|
| 173 | extern int NSUnloadModule() { \ |
|---|
| 174 | return NSCModuleWrapper::wrapUnloadModule(toObject.unloadModule()); \ |
|---|
| 175 | } |
|---|
| 176 | #define NSC_WRAPPERS_HANDLE_MSG_DEF(toObject) \ |
|---|
| 177 | extern void NSHandleMessage(int msgType, char* file, int line, char* message) { \ |
|---|
| 178 | toObject.handleMessage(msgType, file, line, message); \ |
|---|
| 179 | } \ |
|---|
| 180 | extern NSCAPI::boolReturn NSHasMessageHandler() { \ |
|---|
| 181 | return NSCModuleWrapper::wrapHasMessageHandler(toObject.hasMessageHandler()); \ |
|---|
| 182 | } |
|---|
| 183 | #define NSC_WRAPPERS_IGNORE_MSG_DEF() \ |
|---|
| 184 | extern void NSHandleMessage(int msgType, char* file, int line, char* message) {} \ |
|---|
| 185 | extern NSCAPI::boolReturn NSHasMessageHandler() { return NSCAPI::isfalse; } |
|---|
| 186 | #define NSC_WRAPPERS_HANDLE_CMD_DEF(toObject) \ |
|---|
| 187 | extern NSCAPI::nagiosReturn NSHandleCommand(const char* IN_cmd, const unsigned int IN_argsLen, char **IN_args, \ |
|---|
| 188 | char *OUT_retBufMessage, unsigned int IN_retBufMessageLen, char *OUT_retBufPerf, unsigned int IN_retBufPerfLen) \ |
|---|
| 189 | { \ |
|---|
| 190 | std::string message, perf; \ |
|---|
| 191 | NSCAPI::nagiosReturn retCode = toObject.handleCommand(IN_cmd, IN_argsLen, IN_args, message, perf); \ |
|---|
| 192 | return NSCModuleWrapper::wrapHandleCommand(retCode, message, perf, OUT_retBufMessage, IN_retBufMessageLen, OUT_retBufPerf, IN_retBufPerfLen); \ |
|---|
| 193 | } \ |
|---|
| 194 | extern NSCAPI::boolReturn NSHasCommandHandler() { \ |
|---|
| 195 | return NSCModuleWrapper::wrapHasCommandHandler(toObject.hasCommandHandler()); \ |
|---|
| 196 | } |
|---|
| 197 | #define NSC_WRAPPERS_IGNORE_CMD_DEF() \ |
|---|
| 198 | extern NSCAPI::nagiosReturn NSHandleCommand(const char* IN_cmd, const unsigned int IN_argsLen, char **IN_args, \ |
|---|
| 199 | char *OUT_retBufMessage, unsigned int IN_retBufMessageLen, char *OUT_retBufPerf, unsigned int IN_retBufPerfLen) { \ |
|---|
| 200 | return NSCAPI::returnIgnored; \ |
|---|
| 201 | } \ |
|---|
| 202 | extern NSCAPI::boolReturn NSHasCommandHandler() { return NSCAPI::isfalse; } |
|---|