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