source: nscp/NSCPlugin.cpp @ 5fda37e

0.4.00.4.10.4.2stable
Last change on this file since 5fda37e was c1d545e, checked in by Michael Medin <michael@…>, 5 years ago

2008-02-09 MickeM

+ New module CheckExternalScripts to handle 1, external script (similar to the old NRPE but in its own module)

  • Can Check batch/vbs/programs/*
  • Works with NSCA module (if you don't want to have NRPE at the same time)
  • Simpler syntax (discarded old and added new section for alias)
  • Started to add "sample alias" to ease initial setup and give some nice ideas (please provide me with feedback on them)
  • Property mode set to 100644
File size: 12.3 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#include "stdafx.h"
22#include "NSClient++.h"
23#include <error.hpp>
24/**
25 * Default c-tor
26 * Initializes the plug in name but does not load the actual plug in.<br>
27 * To load the plug in use function load() that loads an initializes the plug in.
28 *
29 * @param file The file (DLL) to load as a NSC plug in.
30 */
31NSCPlugin::NSCPlugin(const std::wstring file)
32        : file_(file)
33        ,hModule_(NULL)
34        ,fLoadModule(NULL)
35        ,fGetName(NULL)
36        ,fHasCommandHandler(NULL)
37        ,fUnLoadModule(NULL)
38        ,fHasMessageHandler(NULL)
39        ,fHandleMessage(NULL)
40        ,fGetDescription(NULL)
41        ,fGetConfigurationMeta(NULL)
42        ,fGetVersion(NULL)
43        ,fCommandLineExec(NULL)
44        ,bLoaded_(false)
45{
46}
47
48NSCPlugin::NSCPlugin(NSCPlugin &other)
49        :hModule_(NULL)
50        ,fLoadModule(NULL)
51        ,fGetName(NULL)
52        ,fHasCommandHandler(NULL)
53        ,fUnLoadModule(NULL)
54        ,fHasMessageHandler(NULL)
55        ,fHandleMessage(NULL)
56        ,fGetDescription(NULL)
57        ,fGetConfigurationMeta(NULL)
58        ,fGetVersion(NULL)
59        ,fCommandLineExec(NULL)
60        ,bLoaded_(false)
61{
62        if (other.bLoaded_) {
63                file_ = other.file_;
64                hModule_ = LoadLibrary(file_.c_str());
65                if (!hModule_)
66                        throw NSPluginException(file_, _T("Could not load library: ") + error::lookup::last_error());
67                loadRemoteProcs_();
68                if (!fLoadModule)
69                        throw NSPluginException(file_, _T("Critical error (fLoadModule)"));
70                bLoaded_ = other.bLoaded_;
71        }
72}
73
74/**
75 * Default d-tor
76 */
77NSCPlugin::~NSCPlugin() {
78        if (isLoaded())
79                unload();
80}
81/**
82 * Returns the name of the plug in.
83 *
84 * @return Name of the plug in.
85 *
86 * @throws NSPluginException if the module is not loaded.
87 */
88std::wstring NSCPlugin::getName() {
89        TCHAR *buffer = new TCHAR[1024];
90        if (!getName_(buffer, 1023)) {
91                throw NSPluginException(file_, _T("Could not get name"));
92        }
93        std::wstring ret = buffer;
94        delete [] buffer;
95        return ret;
96}
97std::wstring NSCPlugin::getDescription() {
98        TCHAR *buffer = new TCHAR[4096];
99        if (!getDescription_(buffer, 4095)) {
100                throw NSPluginException(file_, _T("Could not get description"));
101        }
102        std::wstring ret = buffer;
103        delete [] buffer;
104        return ret;
105}
106
107/**
108 * Loads the plug in (DLL) and initializes the plug in by calling NSLoadModule
109 *
110 * @throws NSPluginException when exceptions occur.
111 * Exceptions include but are not limited to: DLL fails to load, DLL is not a correct plug in.
112 */
113void NSCPlugin::load_dll() {
114        if (isLoaded())
115                throw NSPluginException(file_, _T("Module already loaded"));
116        hModule_ = LoadLibrary(file_.c_str());
117        if (!hModule_)
118                throw NSPluginException(file_, _T("Could not load library: ") + error::lookup::last_error());
119        loadRemoteProcs_();
120        bLoaded_ = true;
121}
122
123void NSCPlugin::load_plugin() {
124        if (!fLoadModule)
125                throw NSPluginException(file_, _T("Critical error (fLoadModule)"));
126        if (!fLoadModule())
127                throw NSPluginException(file_, _T("Could not load plug in"));
128}
129
130
131/**
132 * Get the plug in version.
133 *
134 * @bug Not implemented as of yet
135 *
136 * @param *major
137 * @param *minor
138 * @param *revision
139 * @return False
140 */
141bool NSCPlugin::getVersion(int *major, int *minor, int *revision) {
142        if (!isLoaded())
143                throw NSPluginException(file_, _T("Library is not loaded"));
144        if (!fGetVersion)
145                throw NSPluginException(file_, _T("Critical error (fGetVersion)"));
146        try {
147                return fGetVersion(major, minor, revision)?true:false;
148        } catch (...) {
149                throw NSPluginException(file_, _T("Unhandled exception in getVersion."));
150        }
151}
152/**
153 * Returns true if the plug in has a command handler.
154 * @return true if the plug in has a command handler.
155 * @throws NSPluginException if the module is not loaded.
156 */
157bool NSCPlugin::hasCommandHandler() {
158        if (!isLoaded())
159                throw NSPluginException(file_, _T("Module not loaded"));
160        try {
161                if (fHasCommandHandler())
162                        return true;
163                return false;
164        } catch (...) {
165                throw NSPluginException(file_, _T("Unhandled exception in hasCommandHandler."));
166        }
167}
168/**
169* Returns true if the plug in has a message (log) handler.
170* @return true if the plug in has a message (log) handler.
171* @throws NSPluginException if the module is not loaded.
172*/
173bool NSCPlugin::hasMessageHandler() {
174        if (!isLoaded())
175                throw NSPluginException(file_, _T("Module not loaded"));
176        try {
177                if (fHasMessageHandler())
178                        return true;
179                return false;
180        } catch (...) {
181                throw NSPluginException(file_, _T("Unhandled exception in hasMessageHandler."));
182        }
183}
184/**
185 * Allow for the plug in to handle a command from the input core.
186 *
187 * Plug ins may refuse to handle the plug in (if not applicable) by returning an empty string.
188 *
189 * @param command The command name (is a string encoded number for legacy commands)
190 * @param argLen The length of the argument buffer.
191 * @param **arguments The arguments for this command
192 * @param returnMessageBuffer Return buffer for plug in to store the result of the executed command.
193 * @param returnMessageBufferLen Size of returnMessageBuffer
194 * @param returnPerfBuffer Return buffer for performance data
195 * @param returnPerfBufferLen Size of returnPerfBuffer
196 * @return Status of execution. Could be error codes, buffer length messages etc.
197 * @throws NSPluginException if the module is not loaded.
198 */
199NSCAPI::nagiosReturn NSCPlugin::handleCommand(const TCHAR* command, const unsigned int argLen, TCHAR **arguments, TCHAR* returnMessageBuffer, unsigned int returnMessageBufferLen, TCHAR* returnPerfBuffer, unsigned int returnPerfBufferLen) {
200        if (!isLoaded())
201                throw NSPluginException(file_, _T("Library is not loaded"));
202        try {
203                return fHandleCommand(command, argLen, arguments, returnMessageBuffer, returnMessageBufferLen, returnPerfBuffer, returnPerfBufferLen);
204        } catch (...) {
205                throw NSPluginException(file_, _T("Unhandled exception in handleCommand."));
206        }
207}
208/**
209 * Handle a message from the core (or any other (or even potentially self) plug in).
210 * A message may be anything really errors, log messages etc.
211 *
212 * @param msgType Type of message (error, warning, debug, etc.)
213 * @param file The file that generated this message generally __FILE__.
214 * @param line The line in the file that generated the message generally __LINE__
215 * @throws NSPluginException if the module is not loaded.
216 */
217void NSCPlugin::handleMessage(int msgType, const TCHAR* file, const int line, const TCHAR *message) {
218        if (!fHandleMessage)
219                throw NSPluginException(file_, _T("Library is not loaded"));
220        try {
221                fHandleMessage(msgType, file, line, message);
222        } catch (...) {
223                throw NSPluginException(file_, _T("Unhandled exception in handleMessage."));
224        }
225}
226/**
227 * Unload the plug in
228 * @throws NSPluginException if the module is not loaded and/or cannot be unloaded (plug in remains loaded if so).
229 */
230void NSCPlugin::unload() {
231        if (!isLoaded())
232                throw NSPluginException(file_, _T("Library is not loaded"));
233        if (!fUnLoadModule)
234                throw NSPluginException(file_, _T("Critical error (fUnLoadModule)"));
235        try {
236                fUnLoadModule();
237        } catch (...) {
238                throw NSPluginException(file_, _T("Unhandled exception in handleMessage."));
239        }
240        FreeLibrary(hModule_);
241        hModule_ = NULL;
242        bLoaded_ = false;
243}
244bool NSCPlugin::getName_(TCHAR* buf, unsigned int buflen) {
245        if (fGetName == NULL)
246                throw NSPluginException(file_, _T("Critical error (fGetName)"));
247        try {
248                return fGetName(buf, buflen)?true:false;
249        } catch (...) {
250                throw NSPluginException(file_, _T("Unhandled exception in getName."));
251        }
252}
253bool NSCPlugin::getDescription_(TCHAR* buf, unsigned int buflen) {
254        if (fGetDescription == NULL)
255                throw NSPluginException(file_, _T("Critical error (fGetDescription)"));
256        try {
257                return fGetDescription(buf, buflen)?true:false;
258        } catch (...) {
259                throw NSPluginException(file_, _T("Unhandled exception in getDescription."));
260        }
261}
262/**
263 * Load all remote function pointers from the loaded module.
264 * These pointers are cached for "speed" which might (?) be dangerous if something changes.
265 * @throws NSPluginException if any of the function pointers fail to load.
266 * If NSPluginException  is thrown the loaded might remain partially loaded and crashes might occur if plug in is used in this state.
267 */
268void NSCPlugin::loadRemoteProcs_(void) {
269
270        fLoadModule = (lpLoadModule)GetProcAddress(hModule_, "NSLoadModule");
271        if (!fLoadModule)
272                throw NSPluginException(file_, _T("Could not load NSLoadModule"));
273
274        fModuleHelperInit = (lpModuleHelperInit)GetProcAddress(hModule_, "NSModuleHelperInit");
275        if (!fModuleHelperInit)
276                throw NSPluginException(file_, _T("Could not load NSModuleHelperInit"));
277
278        try {
279                fModuleHelperInit(NSAPILoader);
280        } catch (...) {
281                throw NSPluginException(file_, _T("Unhandled exception in getDescription."));
282        }
283       
284        fGetName = (lpGetName)GetProcAddress(hModule_, "NSGetModuleName");
285        if (!fGetName)
286                throw NSPluginException(file_, _T("Could not load NSGetModuleName"));
287
288        fGetVersion = (lpGetVersion)GetProcAddress(hModule_, "NSGetModuleVersion");
289        if (!fGetVersion)
290                throw NSPluginException(file_, _T("Could not load NSGetModuleVersion"));
291
292        fGetDescription = (lpGetDescription)GetProcAddress(hModule_, "NSGetModuleDescription");
293        if (!fGetDescription)
294                throw NSPluginException(file_, _T("Could not load NSGetModuleDescription"));
295
296        fHasCommandHandler = (lpHasCommandHandler)GetProcAddress(hModule_, "NSHasCommandHandler");
297        if (!fHasCommandHandler)
298                throw NSPluginException(file_, _T("Could not load NSHasCommandHandler"));
299
300        fHasMessageHandler = (lpHasMessageHandler)GetProcAddress(hModule_, "NSHasMessageHandler");
301        if (!fHasMessageHandler)
302                throw NSPluginException(file_, _T("Could not load NSHasMessageHandler"));
303
304        fHandleCommand = (lpHandleCommand)GetProcAddress(hModule_, "NSHandleCommand");
305        if (!fHandleCommand)
306                throw NSPluginException(file_, _T("Could not load NSHandleCommand"));
307
308        fHandleMessage = (lpHandleMessage)GetProcAddress(hModule_, "NSHandleMessage");
309        if (!fHandleMessage)
310                throw NSPluginException(file_, _T("Could not load NSHandleMessage"));
311
312        fUnLoadModule = (lpUnLoadModule)GetProcAddress(hModule_, "NSUnloadModule");
313        if (!fUnLoadModule)
314                throw NSPluginException(file_, _T("Could not load NSUnloadModule"));
315
316        fGetConfigurationMeta = (lpGetConfigurationMeta)GetProcAddress(hModule_, "NSGetConfigurationMeta");
317        fCommandLineExec = (lpCommandLineExec)GetProcAddress(hModule_, "NSCommandLineExec");
318}
319
320
321std::wstring NSCPlugin::getCongifurationMeta()
322{
323        TCHAR *buffer = new TCHAR[4097];
324        if (!getConfigurationMeta_(buffer, 4096)) {
325                throw NSPluginException(file_, _T("Could not get metadata"));
326        }
327        std::wstring ret = buffer;
328        delete [] buffer;
329        return ret;
330}
331bool NSCPlugin::getConfigurationMeta_(TCHAR* buf, unsigned int buflen) {
332        if (fGetConfigurationMeta == NULL)
333                throw NSPluginException(file_, _T("Critical error (getCongifurationMeta)"));
334        try {
335                return fGetConfigurationMeta(buflen, buf)?true:false;
336        } catch (...) {
337                throw NSPluginException(file_, _T("Unhandled exception in getConfigurationMeta."));
338        }
339}
340
341int NSCPlugin::commandLineExec(const TCHAR* command, const unsigned int argLen, TCHAR **arguments) {
342        if (fCommandLineExec== NULL)
343                throw NSPluginException(file_, _T("Module does not support CommandLineExec"));
344        try {
345                return fCommandLineExec(command, argLen, arguments);
346        } catch (...) {
347                throw NSPluginException(file_, _T("Unhandled exception in commandLineExec."));
348        }
349}
Note: See TracBrowser for help on using the repository browser.