| 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 | #include "stdafx.h" |
|---|
| 22 | #include "CheckHelpers.h" |
|---|
| 23 | #include <strEx.h> |
|---|
| 24 | #include <time.h> |
|---|
| 25 | #include <utils.h> |
|---|
| 26 | |
|---|
| 27 | CheckHelpers gCheckHelpers; |
|---|
| 28 | |
|---|
| 29 | BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) |
|---|
| 30 | { |
|---|
| 31 | NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call); |
|---|
| 32 | return TRUE; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | CheckHelpers::CheckHelpers() { |
|---|
| 36 | } |
|---|
| 37 | CheckHelpers::~CheckHelpers() { |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | bool CheckHelpers::loadModule(NSCAPI::moduleLoadMode mode) { |
|---|
| 42 | try { |
|---|
| 43 | NSCModuleHelper::registerCommand(_T("CheckAlwaysOK"), _T("Run another check and regardless of its return code return OK.")); |
|---|
| 44 | NSCModuleHelper::registerCommand(_T("CheckAlwaysCRITICAL"), _T("Run another check and regardless of its return code return CRIT.")); |
|---|
| 45 | NSCModuleHelper::registerCommand(_T("CheckAlwaysWARNING"), _T("Run another check and regardless of its return code return WARN.")); |
|---|
| 46 | NSCModuleHelper::registerCommand(_T("CheckMultiple"), _T("Run more then one check and return the worst state.")); |
|---|
| 47 | } catch (NSCModuleHelper::NSCMHExcpetion &e) { |
|---|
| 48 | NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_); |
|---|
| 49 | } catch (...) { |
|---|
| 50 | NSC_LOG_ERROR_STD(_T("Failed to register command.")); |
|---|
| 51 | } |
|---|
| 52 | return true; |
|---|
| 53 | } |
|---|
| 54 | bool CheckHelpers::unloadModule() { |
|---|
| 55 | return true; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | bool CheckHelpers::hasCommandHandler() { |
|---|
| 59 | return true; |
|---|
| 60 | } |
|---|
| 61 | bool CheckHelpers::hasMessageHandler() { |
|---|
| 62 | return false; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | NSCAPI::nagiosReturn CheckHelpers::handleCommand(const strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf) { |
|---|
| 66 | if (command == _T("CheckAlwaysOK")) { |
|---|
| 67 | if (argLen < 2) { |
|---|
| 68 | msg = _T("ERROR: Missing arguments."); |
|---|
| 69 | return NSCAPI::returnUNKNOWN; |
|---|
| 70 | } |
|---|
| 71 | NSCModuleHelper::InjectCommand(char_args[0], argLen-1, &char_args[1], msg, perf); |
|---|
| 72 | return NSCAPI::returnOK; |
|---|
| 73 | } else if (command == _T("CheckAlwaysCRITICAL")) { |
|---|
| 74 | if (argLen < 2) { |
|---|
| 75 | msg = _T("ERROR: Missing arguments."); |
|---|
| 76 | return NSCAPI::returnUNKNOWN; |
|---|
| 77 | } |
|---|
| 78 | NSCModuleHelper::InjectCommand(char_args[0], argLen-1, &char_args[1], msg, perf); |
|---|
| 79 | return NSCAPI::returnCRIT; |
|---|
| 80 | } else if (command == _T("CheckAlwaysWARNING")) { |
|---|
| 81 | if (argLen < 2) { |
|---|
| 82 | msg = _T("ERROR: Missing arguments."); |
|---|
| 83 | return NSCAPI::returnUNKNOWN; |
|---|
| 84 | } |
|---|
| 85 | NSCModuleHelper::InjectCommand(char_args[0], argLen-1, &char_args[1], msg, perf); |
|---|
| 86 | return NSCAPI::returnWARN; |
|---|
| 87 | } else if (command == _T("CheckMultiple")) { |
|---|
| 88 | return checkMultiple(argLen, char_args, msg, perf); |
|---|
| 89 | } |
|---|
| 90 | return NSCAPI::returnIgnored; |
|---|
| 91 | } |
|---|
| 92 | NSCAPI::nagiosReturn CheckHelpers::checkMultiple(const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf) |
|---|
| 93 | { |
|---|
| 94 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; |
|---|
| 95 | std::list<std::wstring> args = arrayBuffer::arrayBuffer2list(argLen, char_args); |
|---|
| 96 | if (args.empty()) { |
|---|
| 97 | msg = _T("Missing argument(s)."); |
|---|
| 98 | return NSCAPI::returnCRIT; |
|---|
| 99 | } |
|---|
| 100 | typedef std::pair<std::wstring, std::list<std::wstring> > sub_command; |
|---|
| 101 | std::list<sub_command> commands; |
|---|
| 102 | sub_command currentCommand; |
|---|
| 103 | std::list<std::wstring>::const_iterator cit; |
|---|
| 104 | for (cit=args.begin();cit!=args.end();++cit) { |
|---|
| 105 | std::wstring arg = *cit; |
|---|
| 106 | std::pair<std::wstring,std::wstring> p = strEx::split(arg,_T("=")); |
|---|
| 107 | if (p.first == _T("command")) { |
|---|
| 108 | if (!currentCommand.first.empty()) |
|---|
| 109 | commands.push_back(currentCommand); |
|---|
| 110 | currentCommand.first = p.second; |
|---|
| 111 | currentCommand.second.clear(); |
|---|
| 112 | } else { |
|---|
| 113 | currentCommand.second.push_back(*cit); |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | if (!currentCommand.first.empty()) |
|---|
| 117 | commands.push_back(currentCommand); |
|---|
| 118 | std::list<sub_command>::iterator cit2; |
|---|
| 119 | for (cit2 = commands.begin(); cit2 != commands.end(); ++cit2) { |
|---|
| 120 | unsigned int length = 0; |
|---|
| 121 | TCHAR ** args = arrayBuffer::list2arrayBuffer((*cit2).second, length); |
|---|
| 122 | std::wstring tMsg, tPerf; |
|---|
| 123 | NSCAPI::nagiosReturn tRet = NSCModuleHelper::InjectCommand((*cit2).first.c_str(), length, args, tMsg, tPerf); |
|---|
| 124 | arrayBuffer::destroyArrayBuffer(args, length); |
|---|
| 125 | returnCode = NSCHelper::maxState(returnCode, tRet); |
|---|
| 126 | if (!msg.empty()) |
|---|
| 127 | msg += _T(", "); |
|---|
| 128 | msg += tMsg; |
|---|
| 129 | perf += tPerf; |
|---|
| 130 | } |
|---|
| 131 | return returnCode; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | NSC_WRAPPERS_MAIN_DEF(gCheckHelpers); |
|---|
| 136 | NSC_WRAPPERS_IGNORE_MSG_DEF(); |
|---|
| 137 | NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckHelpers); |
|---|