source: nscp/include/NSCHelper.cpp @ 1e62ed5

0.4.00.4.10.4.2stable
Last change on this file since 1e62ed5 was 1e62ed5, checked in by Michael Medin <michael@…>, 8 years ago

2005-05-15 MickeM

+ Added NRPE support for checkCounter
+ Updated documentation

  • Make check commands ignore case + Added CheckHelpers module to alter the result of various check and similar things

2005-05-14 MickeM

+ Added support for Volumes (CheckDisk)
+ Added support for checking all drives of a certain kind
+ Added support for altering filter (makes it posible to check removale drives)
+ Created webpage and better documentation

  • Property mode set to 100644
File size: 18.4 KB
Line 
1#include "stdafx.h"
2#include <NSCHelper.h>
3#include <assert.h>
4
5#define BUFF_LEN 4096
6
7
8#ifdef DEBUG
9/**
10* Wrap a return string.
11* This function copies a string to a char buffer making sure the buffer has the correct length.
12*
13* @param *buffer Buffer to copy the string to.
14* @param bufLen Length of the buffer
15* @param str Th string to copy
16* @return NSCAPI::success unless the buffer is to short then it will be NSCAPI::invalidBufferLen
17*/
18NSCAPI::nagiosReturn NSCHelper::wrapReturnString(char *buffer, unsigned int bufLen, std::string str, NSCAPI::nagiosReturn defaultReturnCode /* = NSCAPI::success */) {
19        if (str.length() >= bufLen)
20                return NSCAPI::returnInvalidBufferLen;
21        strncpy(buffer, str.c_str(), bufLen);
22        return defaultReturnCode;
23}
24/**
25* Wrap a return string.
26* This function copies a string to a char buffer making sure the buffer has the correct length.
27*
28* @param *buffer Buffer to copy the string to.
29* @param bufLen Length of the buffer
30* @param str Th string to copy
31* @return NSCAPI::success unless the buffer is to short then it will be NSCAPI::invalidBufferLen
32*/
33NSCAPI::errorReturn NSCHelper::wrapReturnString(char *buffer, unsigned int bufLen, std::string str, NSCAPI::errorReturn defaultReturnCode /* = NSCAPI::success */) {
34        if (str.length() >= bufLen)
35                return NSCAPI::isInvalidBufferLen;
36        strncpy(buffer, str.c_str(), bufLen);
37        return defaultReturnCode;
38}
39#else
40/**
41* Wrap a return string.
42* This function copies a string to a char buffer making sure the buffer has the correct length.
43*
44* @param *buffer Buffer to copy the string to.
45* @param bufLen Length of the buffer
46* @param str Th string to copy
47* @param defaultReturnCode The default return code
48* @return NSCAPI::success unless the buffer is to short then it will be NSCAPI::invalidBufferLen
49*/
50int NSCHelper::wrapReturnString(char *buffer, unsigned int bufLen, std::string str, int defaultReturnCode ) {
51        // @todo deprecate this
52        if (str.length() >= bufLen)
53                return NSCAPI::isInvalidBufferLen;
54        strncpy(buffer, str.c_str(), bufLen);
55        return defaultReturnCode;
56}
57#endif
58
59
60/**
61 * Translate a message type into a human readable string.
62 *
63 * @param msgType The message type
64 * @return A string representing the message type
65 */
66std::string NSCHelper::translateMessageType(NSCAPI::messageTypes msgType) {
67        switch (msgType) {
68                case NSCAPI::error:
69                        return "error";
70                case NSCAPI::critical:
71                        return "critical";
72                case NSCAPI::warning:
73                        return "warning";
74                case NSCAPI::log:
75                        return "message";
76                case NSCAPI::debug:
77                        return "debug";
78        }
79        return "unknown";
80}
81/**
82 * Translate a return code into the corresponding string
83 * @param returnCode
84 * @return
85 */
86std::string NSCHelper::translateReturn(NSCAPI::nagiosReturn returnCode) {
87        if (returnCode == NSCAPI::returnOK)
88                return "OK";
89        else if (returnCode == NSCAPI::returnCRIT)
90                return "CRITICAL";
91        else if (returnCode == NSCAPI::returnWARN)
92                return "WARNING";
93        else
94                return "UNKNOWN";
95}
96
97
98
99namespace NSCModuleHelper {
100        lpNSAPIGetBasePath fNSAPIGetBasePath = NULL;
101        lpNSAPIGetApplicationName fNSAPIGetApplicationName = NULL;
102        lpNSAPIGetApplicationVersionStr fNSAPIGetApplicationVersionStr = NULL;
103        lpNSAPIGetSettingsSection fNSAPIGetSettingsSection = NULL;
104        lpNSAPIGetSettingsString fNSAPIGetSettingsString = NULL;
105        lpNSAPIGetSettingsInt fNSAPIGetSettingsInt = NULL;
106        lpNSAPIMessage fNSAPIMessage = NULL;
107        lpNSAPIStopServer fNSAPIStopServer = NULL;
108        lpNSAPIInject fNSAPIInject = NULL;
109}
110
111//////////////////////////////////////////////////////////////////////////
112// Callbacks into the core
113//////////////////////////////////////////////////////////////////////////
114
115/**
116 * Callback to send a message through to the core
117 *
118 * @param msgType Message type (debug, warning, etc.)
119 * @param file File where message was generated (__FILE__)
120 * @param line Line where message was generated (__LINE__)
121 * @param message Message in human readable format
122 * @throws NSCMHExcpetion When core pointer set is unavailable.
123 */
124void NSCModuleHelper::Message(int msgType, std::string file, int line, std::string message) {
125        if (fNSAPIMessage)
126                return fNSAPIMessage(msgType, file.c_str(), line, message.c_str());
127        else
128                std::cout << "NSCore not loaded..." << std::endl << message << std::endl;
129}
130/**
131 * Inject a request command in the core (this will then be sent to the plug-in stack for processing)
132 * @param command Command to inject (password should not be included.
133 * @return The result (if any) of the command.
134 * @throws NSCMHExcpetion When core pointer set is unavailable or an unknown inject error occurs.
135 */
136
137/**
138 * Inject a request command in the core (this will then be sent to the plug-in stack for processing)
139 * @param command Command to inject
140 * @param argLen The length of the argument buffer
141 * @param **argument The argument buffer
142 * @param *returnMessageBuffer Buffer to hold the returned message
143 * @param returnMessageBufferLen Length of returnMessageBuffer
144 * @param *returnPerfBuffer Buffer to hold the returned performance data
145 * @param returnPerfBufferLen returnPerfBuffer
146 * @return The returned status of the command
147 */
148NSCAPI::nagiosReturn NSCModuleHelper::InjectCommandRAW(const char* command, const unsigned int argLen, char **argument, char *returnMessageBuffer, unsigned int returnMessageBufferLen, char *returnPerfBuffer, unsigned int returnPerfBufferLen)
149{
150        if (!fNSAPIInject)
151                throw NSCMHExcpetion("NSCore has not been initiated...");
152        return fNSAPIInject(command, argLen, argument, returnMessageBuffer, returnMessageBufferLen, returnPerfBuffer, returnPerfBufferLen);
153}
154/**
155 * Inject a request command in the core (this will then be sent to the plug-in stack for processing)
156 * @param command Command to inject (password should not be included.
157 * @param argLen The length of the argument buffer
158 * @param **argument The argument buffer
159 * @param message The return message buffer
160 * @param perf The return performance data buffer
161 * @return The return of the command
162 */
163NSCAPI::nagiosReturn NSCModuleHelper::InjectCommand(const char* command, const unsigned int argLen, char **argument, std::string & message, std::string & perf)
164{
165        if (!fNSAPIInject)
166                throw NSCMHExcpetion("NSCore has not been initiated...");
167        char *msgBuffer = new char[BUFF_LEN+1];
168        char *perfBuffer = new char[BUFF_LEN+1];
169        msgBuffer[0] = 0;
170        perfBuffer[0] = 0;
171        // @todo message here !
172        NSCAPI::nagiosReturn retC = InjectCommandRAW(command, argLen, argument, msgBuffer, BUFF_LEN, perfBuffer, BUFF_LEN);
173        switch (retC) {
174                case NSCAPI::returnIgnored:
175                        NSC_LOG_MESSAGE("No handler for this message.");
176                        break;
177                case NSCAPI::returnInvalidBufferLen:
178                        NSC_LOG_ERROR("Inject command resulted in an invalid buffer size.");
179                        break;
180                case NSCAPI::returnOK:
181                case NSCAPI::returnCRIT:
182                case NSCAPI::returnWARN:
183                case NSCAPI::returnUNKNOWN:
184                        message = msgBuffer;
185                        perf = perfBuffer;
186                        break;
187                default:
188                        throw NSCMHExcpetion("Unknown inject error.");
189        }
190        delete [] msgBuffer;
191        delete [] perfBuffer;
192        return retC;
193}
194/**
195 * A wrapper around the InjetCommand that is simpler to use.
196 * Parses a string by splitting and makes the array and also manages return buffers and such.
197 * @param command The command to execute
198 * @param buffer The buffer to split
199 * @param splitChar The char to use as splitter
200 * @param message The return message buffer
201 * @param perf The return performance data buffer
202 * @return The result of the command
203 */
204NSCAPI::nagiosReturn NSCModuleHelper::InjectSplitAndCommand(const char* command, char* buffer, char splitChar, std::string & message, std::string & perf)
205{
206        if (!fNSAPIInject)
207                throw NSCMHExcpetion("NSCore has not been initiated...");
208        unsigned int argLen = 0;
209        char ** aBuffer;
210        if (buffer)
211                aBuffer= arrayBuffer::split2arrayBuffer(buffer, splitChar, argLen);
212        else
213                aBuffer= arrayBuffer::createEmptyArrayBuffer(argLen);
214        NSCAPI::nagiosReturn ret = InjectCommand(command, argLen, aBuffer, message, perf);
215        arrayBuffer::destroyArrayBuffer(aBuffer, argLen);
216        return ret;
217}
218/**
219 * A wrapper around the InjetCommand that is simpler to use.
220 * @param command The command to execute
221 * @param buffer The buffer to split
222 * @param splitChar The char to use as splitter
223 * @param message The return message buffer
224 * @param perf The return performance data buffer
225 * @return The result of the command
226 */
227NSCAPI::nagiosReturn NSCModuleHelper::InjectSplitAndCommand(const std::string command, const std::string buffer, char splitChar, std::string & message, std::string & perf)
228{
229        if (!fNSAPIInject)
230                throw NSCMHExcpetion("NSCore has not been initiated...");
231        unsigned int argLen = 0;
232        char ** aBuffer;
233        if (buffer.empty())
234                aBuffer= arrayBuffer::createEmptyArrayBuffer(argLen);
235        else
236                aBuffer= arrayBuffer::split2arrayBuffer(buffer, splitChar, argLen);
237        NSCAPI::nagiosReturn ret = InjectCommand(command.c_str(), argLen, aBuffer, message, perf);
238        arrayBuffer::destroyArrayBuffer(aBuffer, argLen);
239        return ret;
240}
241/**
242 * Ask the core to shutdown (only works when run as a service, o/w does nothing ?
243 * @todo Check if this might cause damage if not run as a service.
244 */
245void NSCModuleHelper::StopService(void) {
246        if (fNSAPIStopServer)
247                fNSAPIStopServer();
248}
249/**
250 * Retrieve a string from the settings subsystem (INI-file)
251 * Might possibly be located in the registry in the future.
252 *
253 * @param section Section key (generally module specific, make sure this is "unique")
254 * @param key The key to retrieve
255 * @param defaultValue A default value (if no value is set in the settings file)
256 * @return the current value or defaultValue if no value is set.
257 * @throws NSCMHExcpetion When core pointer set is unavailable or an error occurs.
258 */
259std::string NSCModuleHelper::getSettingsString(std::string section, std::string key, std::string defaultValue) {
260        if (!fNSAPIGetSettingsString)
261                throw NSCMHExcpetion("NSCore has not been initiated...");
262        char *buffer = new char[BUFF_LEN+1];
263        if (fNSAPIGetSettingsString(section.c_str(), key.c_str(), defaultValue.c_str(), buffer, BUFF_LEN) != NSCAPI::isSuccess) {
264                delete [] buffer;
265                throw NSCMHExcpetion("Settings could not be retrieved.");
266        }
267        std::string ret = buffer;
268        delete [] buffer;
269        return ret;
270}
271/**
272 * Get a section of settings strings
273 * @param section The section to retrieve
274 * @return The keys in the section
275 */
276std::list<std::string> NSCModuleHelper::getSettingsSection(std::string section) {
277        if (!fNSAPIGetSettingsSection)
278                throw NSCMHExcpetion("NSCore has not been initiated...");
279        char ** aBuffer = NULL;
280        unsigned int argLen = 0;
281        if (fNSAPIGetSettingsSection(section.c_str(), &aBuffer, &argLen) != NSCAPI::isSuccess) {
282                throw NSCMHExcpetion("Settings could not be retrieved.");
283        }
284        std::list<std::string> ret = arrayBuffer::arrayBuffer2list(argLen, aBuffer);
285        arrayBuffer::destroyArrayBuffer(aBuffer, argLen);
286        return ret;
287}
288/**
289 * Retrieve an int from the settings subsystem (INI-file)
290 * Might possibly be located in the registry in the future.
291 *
292 * @param section Section key (generally module specific, make sure this is "unique")
293 * @param key The key to retrieve
294 * @param defaultValue A default value (if no value is set in the settings file)
295 * @return the current value or defaultValue if no value is set.
296 * @throws NSCMHExcpetion When core pointer set is unavailable.
297 */
298int NSCModuleHelper::getSettingsInt(std::string section, std::string key, int defaultValue) {
299        if (!fNSAPIGetSettingsInt)
300                throw NSCMHExcpetion("NSCore has not been initiated...");
301        return fNSAPIGetSettingsInt(section.c_str(), key.c_str(), defaultValue);
302}
303/**
304 * Returns the biggest of the two states
305 * STATE_UNKNOWN < STATE_OK < STATE_WARNING < STATE_CRITICAL
306 * @param a
307 * @param b
308 * @return
309 */
310NSCAPI::nagiosReturn NSCHelper::maxState(NSCAPI::nagiosReturn a, NSCAPI::nagiosReturn b)
311{
312        if (a == NSCAPI::returnCRIT || b == NSCAPI::returnCRIT)
313                return NSCAPI::returnCRIT;
314        else if (a == NSCAPI::returnWARN || b == NSCAPI::returnWARN)
315                return NSCAPI::returnWARN;
316        else if (a == NSCAPI::returnOK || b == NSCAPI::returnOK)
317                return NSCAPI::returnOK;
318        else if (a == NSCAPI::returnUNKNOWN || b == NSCAPI::returnUNKNOWN)
319                return NSCAPI::returnUNKNOWN;
320        return NSCAPI::returnUNKNOWN;
321}
322/**
323 * Retrieve the application name (in human readable format) from the core.
324 * @return A string representing the application name.
325 * @throws NSCMHExcpetion When core pointer set is unavailable or an unexpected error occurs.
326 */
327std::string NSCModuleHelper::getApplicationName() {
328        if (!fNSAPIGetApplicationName)
329                throw NSCMHExcpetion("NSCore has not been initiated...");
330        char *buffer = new char[BUFF_LEN+1];
331        if (fNSAPIGetApplicationName(buffer, BUFF_LEN) != NSCAPI::isSuccess) {
332                delete [] buffer;
333                throw NSCMHExcpetion("Application name could not be retrieved");
334        }
335        std::string ret = buffer;
336        delete [] buffer;
337        return ret;
338}
339/**
340 * Retrieve the directory root of the application from the core.
341 * @return A string representing the base path.
342 * @throws NSCMHExcpetion When core pointer set is unavailable or an unexpected error occurs.
343 */
344std::string NSCModuleHelper::getBasePath() {
345        if (!fNSAPIGetBasePath)
346                throw NSCMHExcpetion("NSCore has not been initiated...");
347        char *buffer = new char[BUFF_LEN+1];
348        if (fNSAPIGetBasePath(buffer, BUFF_LEN) != NSCAPI::isSuccess) {
349                delete [] buffer;
350                throw NSCMHExcpetion("Base path could not be retrieved");
351        }
352        std::string ret = buffer;
353        delete [] buffer;
354        return ret;
355}
356/**
357 * Retrieve the application version as a string (in human readable format) from the core.
358 * @return A string representing the application version.
359 * @throws NSCMHExcpetion When core pointer set is unavailable.
360 */
361std::string NSCModuleHelper::getApplicationVersionString() {
362        if (!fNSAPIGetApplicationVersionStr)
363                throw NSCMHExcpetion("NSCore has not been initiated...");
364        char *buffer = new char[BUFF_LEN+1];
365        int x = fNSAPIGetApplicationVersionStr(buffer, BUFF_LEN);
366        std::string ret = buffer;
367        delete [] buffer;
368        return ret;
369}
370
371namespace NSCModuleWrapper {
372        HINSTANCE hModule_ = NULL;
373}
374/**
375 * Used to help store the module handle (and possibly other things in the future)
376 * @param hModule cf. DllMain
377 * @param ul_reason_for_call cf. DllMain
378 * @return TRUE
379 */
380BOOL NSCModuleWrapper::wrapDllMain(HANDLE hModule, DWORD ul_reason_for_call)
381{
382        switch (ul_reason_for_call)
383        {
384        case DLL_PROCESS_ATTACH:
385        case DLL_THREAD_ATTACH:
386                hModule_ = (HINSTANCE)hModule;
387                break;
388        case DLL_THREAD_DETACH:
389        case DLL_PROCESS_DETACH:
390                break;
391        }
392        return TRUE;
393}
394/**
395 * Retrieve the module handle from the DllMain call.
396 * @return Module handle of this DLL
397 */
398HINSTANCE NSCModuleWrapper::getModule() {
399        return hModule_;
400}
401
402/**
403 * Wrapper function around the ModuleHelperInit call.
404 * This wrapper retrieves all pointers and stores them for future use.
405 * @param f A function pointer to a function that can be used to load function from the core.
406 * @return NSCAPI::success or NSCAPI::failure
407 */
408int NSCModuleWrapper::wrapModuleHelperInit(NSCModuleHelper::lpNSAPILoader f) {
409        NSCModuleHelper::fNSAPIGetApplicationName = (NSCModuleHelper::lpNSAPIGetApplicationName)f("NSAPIGetApplicationName");
410        NSCModuleHelper::fNSAPIGetApplicationVersionStr = (NSCModuleHelper::lpNSAPIGetApplicationVersionStr)f("NSAPIGetApplicationVersionStr");
411        NSCModuleHelper::fNSAPIGetSettingsInt = (NSCModuleHelper::lpNSAPIGetSettingsInt)f("NSAPIGetSettingsInt");
412        NSCModuleHelper::fNSAPIGetSettingsString = (NSCModuleHelper::lpNSAPIGetSettingsString)f("NSAPIGetSettingsString");
413        NSCModuleHelper::fNSAPIGetSettingsSection = (NSCModuleHelper::lpNSAPIGetSettingsSection)f("NSAPIGetSettingsSection");
414        NSCModuleHelper::fNSAPIMessage = (NSCModuleHelper::lpNSAPIMessage)f("NSAPIMessage");
415        NSCModuleHelper::fNSAPIStopServer = (NSCModuleHelper::lpNSAPIStopServer)f("NSAPIStopServer");
416        NSCModuleHelper::fNSAPIInject = (NSCModuleHelper::lpNSAPIInject)f("NSAPIInject");
417        NSCModuleHelper::fNSAPIGetBasePath = (NSCModuleHelper::lpNSAPIGetBasePath)f("NSAPIGetBasePath");
418        return NSCAPI::isSuccess;
419}
420/**
421* Wrap the GetModuleName function call
422* @param buf Buffer to store the module name
423* @param bufLen Length of buffer
424* @param str String to store inside the buffer
425* @return buffer copy status
426*/
427NSCAPI::errorReturn NSCModuleWrapper::wrapGetModuleName(char* buf, unsigned int bufLen, std::string str) {
428        return NSCHelper::wrapReturnString(buf, bufLen, str, NSCAPI::isSuccess);
429}
430/**
431 * Wrap the GetModuleVersion function call
432 * @param *major Major version number
433 * @param *minor Minor version number
434 * @param *revision Revision
435 * @param version version as a module_version
436 * @return NSCAPI::success
437 */
438NSCAPI::errorReturn NSCModuleWrapper::wrapGetModuleVersion(int *major, int *minor, int *revision, module_version version) {
439        *major = version.major;
440        *minor = version.minor;
441        *revision = version.revision;
442        return NSCAPI::isSuccess;
443}
444/**
445 * Wrap the HasCommandHandler function call
446 * @param has true if this module has a command handler
447 * @return NSCAPI::istrue or NSCAPI::isfalse
448 */
449NSCAPI::boolReturn NSCModuleWrapper::wrapHasCommandHandler(bool has) {
450        if (has)
451                return NSCAPI::istrue;
452        return NSCAPI::isfalse;
453}
454/**
455 * Wrap the HasMessageHandler function call
456 * @param has true if this module has a message handler
457 * @return NSCAPI::istrue or NSCAPI::isfalse
458 */
459NSCAPI::boolReturn NSCModuleWrapper::wrapHasMessageHandler(bool has) {
460        if (has)
461                return NSCAPI::istrue;
462        return NSCAPI::isfalse;
463}
464/**
465 * Wrap the HandleCommand call
466 * @param retResult The returned result
467 * @param retMessage The returned message
468 * @param retPerformance The returned performance data
469 * @param *returnBufferMessage The return message buffer
470 * @param returnBufferMessageLen The return message buffer length
471 * @param *returnBufferPerf The return perfomance data buffer
472 * @param returnBufferPerfLen The return perfomance data buffer length
473 * @return the return code
474 */
475NSCAPI::nagiosReturn NSCModuleWrapper::wrapHandleCommand(NSCAPI::nagiosReturn retResult, const std::string retMessage, const std::string retPerformance, char *returnBufferMessage, unsigned int returnBufferMessageLen, char *returnBufferPerf, unsigned int returnBufferPerfLen) {
476        if (retMessage.empty())
477                return NSCAPI::returnIgnored;
478        NSCAPI::nagiosReturn ret = NSCHelper::wrapReturnString(returnBufferMessage, returnBufferMessageLen, retMessage, retResult);
479        return NSCHelper::wrapReturnString(returnBufferPerf, returnBufferPerfLen, retPerformance, ret);
480}
481/**
482 * Wrap the NSLoadModule call
483 * @param success true if module load was successfully
484 * @return NSCAPI::success or NSCAPI::failed
485 */
486int NSCModuleWrapper::wrapLoadModule(bool success) {
487        if (success)
488                return NSCAPI::isSuccess;
489        return NSCAPI::hasFailed;
490}
491/**
492 * Wrap the NSUnloadModule call
493 * @param success true if module load was successfully
494 * @return NSCAPI::success or NSCAPI::failed
495 */
496int NSCModuleWrapper::wrapUnloadModule(bool success) {
497        if (success)
498                return NSCAPI::isSuccess;
499        return NSCAPI::hasFailed;
500}
501
502
503
Note: See TracBrowser for help on using the repository browser.