| 1 | // CheckEventLog.cpp : Defines the entry point for the DLL application. |
|---|
| 2 | // |
|---|
| 3 | |
|---|
| 4 | #include "stdafx.h" |
|---|
| 5 | #include "CheckHelpers.h" |
|---|
| 6 | #include <strEx.h> |
|---|
| 7 | #include <time.h> |
|---|
| 8 | #include <utils.h> |
|---|
| 9 | |
|---|
| 10 | CheckHelpers gCheckHelpers; |
|---|
| 11 | |
|---|
| 12 | BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) |
|---|
| 13 | { |
|---|
| 14 | NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call); |
|---|
| 15 | return TRUE; |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | CheckHelpers::CheckHelpers() { |
|---|
| 19 | } |
|---|
| 20 | CheckHelpers::~CheckHelpers() { |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | bool CheckHelpers::loadModule() { |
|---|
| 25 | return true; |
|---|
| 26 | } |
|---|
| 27 | bool CheckHelpers::unloadModule() { |
|---|
| 28 | return true; |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | std::string CheckHelpers::getModuleName() { |
|---|
| 32 | return "CheckHelpers Various helper function to extend other checks."; |
|---|
| 33 | } |
|---|
| 34 | NSCModuleWrapper::module_version CheckHelpers::getModuleVersion() { |
|---|
| 35 | NSCModuleWrapper::module_version version = {0, 3, 0 }; |
|---|
| 36 | return version; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | bool CheckHelpers::hasCommandHandler() { |
|---|
| 40 | return true; |
|---|
| 41 | } |
|---|
| 42 | bool CheckHelpers::hasMessageHandler() { |
|---|
| 43 | return false; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | NSCAPI::nagiosReturn CheckHelpers::handleCommand(const strEx::blindstr command, const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) { |
|---|
| 47 | if (command == "CheckAlwaysOK") { |
|---|
| 48 | if (argLen < 2) { |
|---|
| 49 | msg = "ERROR: Missing arguments."; |
|---|
| 50 | return NSCAPI::returnUNKNOWN; |
|---|
| 51 | } |
|---|
| 52 | NSCModuleHelper::InjectCommand(char_args[0], argLen-1, &char_args[1], msg, perf); |
|---|
| 53 | return NSCAPI::returnOK; |
|---|
| 54 | } else if (command == "CheckAlwaysCRITICAL") { |
|---|
| 55 | if (argLen < 2) { |
|---|
| 56 | msg = "ERROR: Missing arguments."; |
|---|
| 57 | return NSCAPI::returnUNKNOWN; |
|---|
| 58 | } |
|---|
| 59 | NSCModuleHelper::InjectCommand(char_args[0], argLen-1, &char_args[1], msg, perf); |
|---|
| 60 | return NSCAPI::returnCRIT; |
|---|
| 61 | } else if (command == "CheckAlwaysWARNING") { |
|---|
| 62 | if (argLen < 2) { |
|---|
| 63 | msg = "ERROR: Missing arguments."; |
|---|
| 64 | return NSCAPI::returnUNKNOWN; |
|---|
| 65 | } |
|---|
| 66 | NSCModuleHelper::InjectCommand(char_args[0], argLen-1, &char_args[1], msg, perf); |
|---|
| 67 | return NSCAPI::returnWARN; |
|---|
| 68 | } else if (command == "CheckMultiple") { |
|---|
| 69 | return checkMultiple(argLen, char_args, msg, perf); |
|---|
| 70 | } |
|---|
| 71 | return NSCAPI::returnIgnored; |
|---|
| 72 | } |
|---|
| 73 | NSCAPI::nagiosReturn CheckHelpers::checkMultiple(const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) |
|---|
| 74 | { |
|---|
| 75 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; |
|---|
| 76 | std::list<std::string> args = arrayBuffer::arrayBuffer2list(argLen, char_args); |
|---|
| 77 | if (args.empty()) { |
|---|
| 78 | msg = "Missing argument(s)."; |
|---|
| 79 | return NSCAPI::returnCRIT; |
|---|
| 80 | } |
|---|
| 81 | typedef std::pair<std::string, std::list<std::string> > sub_command; |
|---|
| 82 | std::list<sub_command> commands; |
|---|
| 83 | sub_command currentCommand; |
|---|
| 84 | std::list<std::string>::const_iterator cit; |
|---|
| 85 | for (cit=args.begin();cit!=args.end();++cit) { |
|---|
| 86 | std::string arg = *cit; |
|---|
| 87 | std::pair<std::string,std::string> p = strEx::split(arg,"="); |
|---|
| 88 | if (p.first == "command") { |
|---|
| 89 | if (!currentCommand.first.empty()) |
|---|
| 90 | commands.push_back(currentCommand); |
|---|
| 91 | currentCommand.first = p.second; |
|---|
| 92 | currentCommand.second.clear(); |
|---|
| 93 | } else { |
|---|
| 94 | currentCommand.second.push_back(*cit); |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | if (!currentCommand.first.empty()) |
|---|
| 98 | commands.push_back(currentCommand); |
|---|
| 99 | std::list<sub_command>::iterator cit2; |
|---|
| 100 | for (cit2 = commands.begin(); cit2 != commands.end(); ++cit2) { |
|---|
| 101 | unsigned int length = 0; |
|---|
| 102 | char ** args = arrayBuffer::list2arrayBuffer((*cit2).second, length); |
|---|
| 103 | std::string tMsg, tPerf; |
|---|
| 104 | NSCAPI::nagiosReturn tRet = NSCModuleHelper::InjectCommand((*cit2).first.c_str(), length, args, tMsg, tPerf); |
|---|
| 105 | arrayBuffer::destroyArrayBuffer(args, length); |
|---|
| 106 | returnCode = NSCHelper::maxState(returnCode, tRet); |
|---|
| 107 | if (!msg.empty()) |
|---|
| 108 | msg += ", "; |
|---|
| 109 | msg += tMsg; |
|---|
| 110 | perf += tPerf; |
|---|
| 111 | } |
|---|
| 112 | return returnCode; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | NSC_WRAPPERS_MAIN_DEF(gCheckHelpers); |
|---|
| 117 | NSC_WRAPPERS_IGNORE_MSG_DEF(); |
|---|
| 118 | NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckHelpers); |
|---|