| 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 |
|
|---|
| 22 | #include "stdafx.h"
|
|---|
| 23 | #include "NSCAAgent.h"
|
|---|
| 24 | #include <utils.h>
|
|---|
| 25 | #include <list>
|
|---|
| 26 | #include <string>
|
|---|
| 27 |
|
|---|
| 28 | NSCAAgent gNSCAAgent;
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * DLL Entry point
|
|---|
| 32 | * @param hModule
|
|---|
| 33 | * @param ul_reason_for_call
|
|---|
| 34 | * @param lpReserved
|
|---|
| 35 | * @return
|
|---|
| 36 | */
|
|---|
| 37 | BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
|
|---|
| 38 | {
|
|---|
| 39 | NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call);
|
|---|
| 40 | return TRUE;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | /**
|
|---|
| 44 | * Default c-tor
|
|---|
| 45 | * @return
|
|---|
| 46 | */
|
|---|
| 47 | NSCAAgent::NSCAAgent() {}
|
|---|
| 48 | /**
|
|---|
| 49 | * Default d-tor
|
|---|
| 50 | * @return
|
|---|
| 51 | */
|
|---|
| 52 | NSCAAgent::~NSCAAgent() {}
|
|---|
| 53 | /**
|
|---|
| 54 | * Load (initiate) module.
|
|---|
| 55 | * Start the background collector thread and let it run until unloadModule() is called.
|
|---|
| 56 | * @return true
|
|---|
| 57 | */
|
|---|
| 58 | bool NSCAAgent::loadModule(NSCAPI::moduleLoadMode mode) {
|
|---|
| 59 | try {
|
|---|
| 60 |
|
|---|
| 61 | SETTINGS_REG_PATH(nsca::SECTION);
|
|---|
| 62 | SETTINGS_REG_PATH(nsca::SERVER_SECTION);
|
|---|
| 63 | SETTINGS_REG_PATH(nsca::CMD_SECTION);
|
|---|
| 64 |
|
|---|
| 65 | SETTINGS_REG_KEY_I(nsca::INTERVAL);
|
|---|
| 66 | SETTINGS_REG_KEY_S(nsca::HOSTNAME);
|
|---|
| 67 | SETTINGS_REG_KEY_S(nsca::SERVER_HOST);
|
|---|
| 68 | SETTINGS_REG_KEY_I(nsca::SERVER_PORT);
|
|---|
| 69 | SETTINGS_REG_KEY_I(nsca::ENCRYPTION);
|
|---|
| 70 | SETTINGS_REG_KEY_S(nsca::PASSWORD);
|
|---|
| 71 | SETTINGS_REG_KEY_I(nsca::THREADS);
|
|---|
| 72 | SETTINGS_REG_KEY_B(nsca::CACHE_HOST);
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 | } catch (NSCModuleHelper::NSCMHExcpetion &e) {
|
|---|
| 76 | NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_);
|
|---|
| 77 | } catch (...) {
|
|---|
| 78 | NSC_LOG_ERROR_STD(_T("Failed to register command."));
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | if (mode == NSCAPI::normalStart) {
|
|---|
| 82 | int e_threads = SETTINGS_GET_INT(nsca::THREADS);
|
|---|
| 83 |
|
|---|
| 84 | for (int i=0;i<e_threads;i++) {
|
|---|
| 85 | std::wstring id = _T("nsca_t_") + strEx::itos(i);
|
|---|
| 86 | NSCAThreadImpl *thread = new NSCAThreadImpl(id);
|
|---|
| 87 | extra_threads.push_back(thread);
|
|---|
| 88 | }
|
|---|
| 89 | for (std::list<NSCAThreadImpl*>::const_iterator cit=extra_threads.begin();cit != extra_threads.end(); ++cit) {
|
|---|
| 90 | (*cit)->createThread(reinterpret_cast<LPVOID>(rand()));
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | return true;
|
|---|
| 95 | }
|
|---|
| 96 | /**
|
|---|
| 97 | * Unload (terminate) module.
|
|---|
| 98 | * Attempt to stop the background processing thread.
|
|---|
| 99 | * @return true if successfully, false if not (if not things might be bad)
|
|---|
| 100 | */
|
|---|
| 101 | bool NSCAAgent::unloadModule() {
|
|---|
| 102 | /*
|
|---|
| 103 | if (!pdhThread.exitThread(20000)) {
|
|---|
| 104 | std::wcout << _T("MAJOR ERROR: Could not unload thread...") << std::endl;
|
|---|
| 105 | NSC_LOG_ERROR(_T("Could not exit the thread, memory leak and potential corruption may be the result..."));
|
|---|
| 106 | }
|
|---|
| 107 | */
|
|---|
| 108 | for (std::list<NSCAThreadImpl*>::iterator it=extra_threads.begin();it != extra_threads.end(); ++it) {
|
|---|
| 109 | if (!(*it)->exitThread(20000)) {
|
|---|
| 110 | std::wcout << _T("MAJOR ERROR: Could not unload thread...") << std::endl;
|
|---|
| 111 | NSC_LOG_ERROR(_T("Could not exit the thread, memory leak and potential corruption may be the result..."));
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|
| 114 | return true;
|
|---|
| 115 | }
|
|---|
| 116 | /**
|
|---|
| 117 | * Check if we have a command handler.
|
|---|
| 118 | * @return true (as we have a command handler)
|
|---|
| 119 | */
|
|---|
| 120 | bool NSCAAgent::hasCommandHandler() {
|
|---|
| 121 | return false;
|
|---|
| 122 | }
|
|---|
| 123 | /**
|
|---|
| 124 | * Check if we have a message handler.
|
|---|
| 125 | * @return false as we have no message handler
|
|---|
| 126 | */
|
|---|
| 127 | bool NSCAAgent::hasMessageHandler() {
|
|---|
| 128 | return false;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | int NSCAAgent::commandLineExec(const TCHAR* command, const unsigned int argLen, TCHAR** args) {
|
|---|
| 132 | return -1;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 | /**
|
|---|
| 137 | * Main command parser and delegator.
|
|---|
| 138 | * This also handles a lot of the simpler responses (though some are deferred to other helper functions)
|
|---|
| 139 | *
|
|---|
| 140 | *
|
|---|
| 141 | * @param command
|
|---|
| 142 | * @param argLen
|
|---|
| 143 | * @param **args
|
|---|
| 144 | * @return
|
|---|
| 145 | */
|
|---|
| 146 | NSCAPI::nagiosReturn NSCAAgent::handleCommand(const strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf) {
|
|---|
| 147 | return NSCAPI::returnIgnored;
|
|---|
| 148 | }
|
|---|
| 149 | std::wstring NSCAAgent::getCryptos() {
|
|---|
| 150 | std::wstring ret = _T("{");
|
|---|
| 151 | for (int i=0;i<LAST_ENCRYPTION_ID;i++) {
|
|---|
| 152 | if (nsca_encrypt::hasEncryption(i)) {
|
|---|
| 153 | std::wstring name;
|
|---|
| 154 | try {
|
|---|
| 155 | nsca_encrypt::any_encryption *core = nsca_encrypt::get_encryption_core(i);
|
|---|
| 156 | if (core == NULL)
|
|---|
| 157 | name = _T("Broken<NULL>");
|
|---|
| 158 | else
|
|---|
| 159 | name = core->getName();
|
|---|
| 160 | } catch (nsca_encrypt::encryption_exception &e) {
|
|---|
| 161 | name = e.getMessage();
|
|---|
| 162 | }
|
|---|
| 163 | if (ret.size() > 1)
|
|---|
| 164 | ret += _T(", ");
|
|---|
| 165 | ret += strEx::itos(i) + _T("=") + name;
|
|---|
| 166 | }
|
|---|
| 167 | }
|
|---|
| 168 | return ret + _T("}");
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 | NSC_WRAPPERS_MAIN_DEF(gNSCAAgent);
|
|---|
| 173 | NSC_WRAPPERS_IGNORE_MSG_DEF();
|
|---|
| 174 | NSC_WRAPPERS_HANDLE_CMD_DEF(gNSCAAgent);
|
|---|
| 175 | NSC_WRAPPERS_HANDLE_CONFIGURATION(gNSCAAgent);
|
|---|
| 176 | NSC_WRAPPERS_CLI_DEF(gNSCAAgent);
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 | MODULE_SETTINGS_START(NSCAAgent, _T("NRPE Listener configuration"), _T("..."))
|
|---|
| 181 |
|
|---|
| 182 | PAGE(_T("NRPE Listsner configuration"))
|
|---|
| 183 |
|
|---|
| 184 | ITEM_EDIT_TEXT(_T("port"), _T("This is the port the NRPEListener.dll will listen to."))
|
|---|
| 185 | ITEM_MAP_TO(_T("basic_ini_text_mapper"))
|
|---|
| 186 | OPTION(_T("section"), _T("NRPE"))
|
|---|
| 187 | OPTION(_T("key"), _T("port"))
|
|---|
| 188 | OPTION(_T("default"), _T("5666"))
|
|---|
| 189 | ITEM_END()
|
|---|
| 190 |
|
|---|
| 191 | ITEM_CHECK_BOOL(_T("allow_arguments"), _T("This option determines whether or not the NRPE daemon will allow clients to specify arguments to commands that are executed."))
|
|---|
| 192 | ITEM_MAP_TO(_T("basic_ini_bool_mapper"))
|
|---|
| 193 | OPTION(_T("section"), _T("NRPE"))
|
|---|
| 194 | OPTION(_T("key"), _T("allow_arguments"))
|
|---|
| 195 | OPTION(_T("default"), _T("false"))
|
|---|
| 196 | OPTION(_T("true_value"), _T("1"))
|
|---|
| 197 | OPTION(_T("false_value"), _T("0"))
|
|---|
| 198 | ITEM_END()
|
|---|
| 199 |
|
|---|
| 200 | ITEM_CHECK_BOOL(_T("allow_nasty_meta_chars"), _T("This might have security implications (depending on what you do with the options)"))
|
|---|
| 201 | ITEM_MAP_TO(_T("basic_ini_bool_mapper"))
|
|---|
| 202 | OPTION(_T("section"), _T("NRPE"))
|
|---|
| 203 | OPTION(_T("key"), _T("allow_nasty_meta_chars"))
|
|---|
| 204 | OPTION(_T("default"), _T("false"))
|
|---|
| 205 | OPTION(_T("true_value"), _T("1"))
|
|---|
| 206 | OPTION(_T("false_value"), _T("0"))
|
|---|
| 207 | ITEM_END()
|
|---|
| 208 |
|
|---|
| 209 | ITEM_CHECK_BOOL(_T("use_ssl"), _T("This option will enable SSL encryption on the NRPE data socket (this increases security somwhat."))
|
|---|
| 210 | ITEM_MAP_TO(_T("basic_ini_bool_mapper"))
|
|---|
| 211 | OPTION(_T("section"), _T("NRPE"))
|
|---|
| 212 | OPTION(_T("key"), _T("use_ssl"))
|
|---|
| 213 | OPTION(_T("default"), _T("true"))
|
|---|
| 214 | OPTION(_T("true_value"), _T("1"))
|
|---|
| 215 | OPTION(_T("false_value"), _T("0"))
|
|---|
| 216 | ITEM_END()
|
|---|
| 217 |
|
|---|
| 218 | PAGE_END()
|
|---|
| 219 | ADVANCED_PAGE(_T("Access configuration"))
|
|---|
| 220 |
|
|---|
| 221 | ITEM_EDIT_OPTIONAL_LIST(_T("Allow connection from:"), _T("This is the hosts that will be allowed to poll performance data from the NRPE server."))
|
|---|
| 222 | OPTION(_T("disabledCaption"), _T("Use global settings (defined previously)"))
|
|---|
| 223 | OPTION(_T("enabledCaption"), _T("Specify hosts for NRPE server"))
|
|---|
| 224 | OPTION(_T("listCaption"), _T("Add all IP addresses (not hosts) which should be able to connect:"))
|
|---|
| 225 | OPTION(_T("separator"), _T(","))
|
|---|
| 226 | OPTION(_T("disabled"), _T(""))
|
|---|
| 227 | ITEM_MAP_TO(_T("basic_ini_text_mapper"))
|
|---|
| 228 | OPTION(_T("section"), _T("NRPE"))
|
|---|
| 229 | OPTION(_T("key"), _T("allowed_hosts"))
|
|---|
| 230 | OPTION(_T("default"), _T(""))
|
|---|
| 231 | ITEM_END()
|
|---|
| 232 |
|
|---|
| 233 | PAGE_END()
|
|---|
| 234 | MODULE_SETTINGS_END()
|
|---|