source: nscp/modules/NSCAAgent/NSCAAgent.cpp @ 1d4878e

0.4.00.4.10.4.2
Last change on this file since 1d4878e was f0eb62d, checked in by Michael Medin <michael@…>, 4 years ago

Refactored service into its own project

  • Property mode set to 100644
File size: 8.0 KB
Line 
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
28NSCAAgent gNSCAAgent;
29
30/**
31 * DLL Entry point
32 * @param hModule
33 * @param ul_reason_for_call
34 * @param lpReserved
35 * @return
36 */
37BOOL 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 */
47NSCAAgent::NSCAAgent() {}
48/**
49 * Default d-tor
50 * @return
51 */
52NSCAAgent::~NSCAAgent() {}
53/**
54 * Load (initiate) module.
55 * Start the background collector thread and let it run until unloadModule() is called.
56 * @return true
57 */
58bool 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 */
101bool 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 */
120bool 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 */
127bool NSCAAgent::hasMessageHandler() {
128        return false;
129}
130
131int 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 */
146NSCAPI::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}
149std::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
172NSC_WRAPPERS_MAIN_DEF(gNSCAAgent);
173NSC_WRAPPERS_IGNORE_MSG_DEF();
174NSC_WRAPPERS_HANDLE_CMD_DEF(gNSCAAgent);
175NSC_WRAPPERS_HANDLE_CONFIGURATION(gNSCAAgent);
176NSC_WRAPPERS_CLI_DEF(gNSCAAgent);
177
178
179
180MODULE_SETTINGS_START(NSCAAgent, _T("NRPE Listener configuration"), _T("..."))
181
182PAGE(_T("NRPE Listsner configuration"))
183
184ITEM_EDIT_TEXT(_T("port"), _T("This is the port the NRPEListener.dll will listen to."))
185ITEM_MAP_TO(_T("basic_ini_text_mapper"))
186OPTION(_T("section"), _T("NRPE"))
187OPTION(_T("key"), _T("port"))
188OPTION(_T("default"), _T("5666"))
189ITEM_END()
190
191ITEM_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."))
192ITEM_MAP_TO(_T("basic_ini_bool_mapper"))
193OPTION(_T("section"), _T("NRPE"))
194OPTION(_T("key"), _T("allow_arguments"))
195OPTION(_T("default"), _T("false"))
196OPTION(_T("true_value"), _T("1"))
197OPTION(_T("false_value"), _T("0"))
198ITEM_END()
199
200ITEM_CHECK_BOOL(_T("allow_nasty_meta_chars"), _T("This might have security implications (depending on what you do with the options)"))
201ITEM_MAP_TO(_T("basic_ini_bool_mapper"))
202OPTION(_T("section"), _T("NRPE"))
203OPTION(_T("key"), _T("allow_nasty_meta_chars"))
204OPTION(_T("default"), _T("false"))
205OPTION(_T("true_value"), _T("1"))
206OPTION(_T("false_value"), _T("0"))
207ITEM_END()
208
209ITEM_CHECK_BOOL(_T("use_ssl"), _T("This option will enable SSL encryption on the NRPE data socket (this increases security somwhat."))
210ITEM_MAP_TO(_T("basic_ini_bool_mapper"))
211OPTION(_T("section"), _T("NRPE"))
212OPTION(_T("key"), _T("use_ssl"))
213OPTION(_T("default"), _T("true"))
214OPTION(_T("true_value"), _T("1"))
215OPTION(_T("false_value"), _T("0"))
216ITEM_END()
217
218PAGE_END()
219ADVANCED_PAGE(_T("Access configuration"))
220
221ITEM_EDIT_OPTIONAL_LIST(_T("Allow connection from:"), _T("This is the hosts that will be allowed to poll performance data from the NRPE server."))
222OPTION(_T("disabledCaption"), _T("Use global settings (defined previously)"))
223OPTION(_T("enabledCaption"), _T("Specify hosts for NRPE server"))
224OPTION(_T("listCaption"), _T("Add all IP addresses (not hosts) which should be able to connect:"))
225OPTION(_T("separator"), _T(","))
226OPTION(_T("disabled"), _T(""))
227ITEM_MAP_TO(_T("basic_ini_text_mapper"))
228OPTION(_T("section"), _T("NRPE"))
229OPTION(_T("key"), _T("allowed_hosts"))
230OPTION(_T("default"), _T(""))
231ITEM_END()
232
233PAGE_END()
234MODULE_SETTINGS_END()
Note: See TracBrowser for help on using the repository browser.