source: nscp/NSCPlugin.cpp @ 99e4d8f

0.4.00.4.10.4.2stable
Last change on this file since 99e4d8f was 99e4d8f, checked in by Michael Medin <michael@…>, 6 years ago

2007-11-23 MickeM

  • Converted to unicode (damn sometimes I HATE C++) + Added support for escaping " on the /test syntax so now you can do: CheckWMI MaxCrit=3 "MinWarn=1" "Query:load=Select * from win32_Processor"

2007-11-22 MickeM

  • Fixed so the "default path" is correct even when running as a service (issue: #96)
  • Property mode set to 100644
File size: 11.1 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        return fGetVersion(major, minor, revision)?true:false;
147}
148/**
149 * Returns true if the plug in has a command handler.
150 * @return true if the plug in has a command handler.
151 * @throws NSPluginException if the module is not loaded.
152 */
153bool NSCPlugin::hasCommandHandler() {
154        if (!isLoaded())
155                throw NSPluginException(file_, _T("Module not loaded"));
156        if (fHasCommandHandler())
157                return true;
158        return false;
159}
160/**
161* Returns true if the plug in has a message (log) handler.
162* @return true if the plug in has a message (log) handler.
163* @throws NSPluginException if the module is not loaded.
164*/
165bool NSCPlugin::hasMessageHandler() {
166        if (!isLoaded())
167                throw NSPluginException(file_, _T("Module not loaded"));
168        if (fHasMessageHandler())
169                return true;
170        return false;
171}
172/**
173 * Allow for the plug in to handle a command from the input core.
174 *
175 * Plug ins may refuse to handle the plug in (if not applicable) by returning an empty string.
176 *
177 * @param command The command name (is a string encoded number for legacy commands)
178 * @param argLen The length of the argument buffer.
179 * @param **arguments The arguments for this command
180 * @param returnMessageBuffer Return buffer for plug in to store the result of the executed command.
181 * @param returnMessageBufferLen Size of returnMessageBuffer
182 * @param returnPerfBuffer Return buffer for performance data
183 * @param returnPerfBufferLen Sixe of returnPerfBuffer
184 * @return Status of execution. Could be error codes, buffer length messages etc.
185 * @throws NSPluginException if the module is not loaded.
186 */
187NSCAPI::nagiosReturn NSCPlugin::handleCommand(const TCHAR* command, const unsigned int argLen, TCHAR **arguments, TCHAR* returnMessageBuffer, unsigned int returnMessageBufferLen, TCHAR* returnPerfBuffer, unsigned int returnPerfBufferLen) {
188        if (!isLoaded())
189                throw NSPluginException(file_, _T("Library is not loaded"));
190        return fHandleCommand(command, argLen, arguments, returnMessageBuffer, returnMessageBufferLen, returnPerfBuffer, returnPerfBufferLen);
191}
192/**
193 * Handle a message from the core (or any other (or even potentially self) plug in).
194 * A message may be anything really errors, log messages etc.
195 *
196 * @param msgType Type of message (error, warning, debug, etc.)
197 * @param file The file that generated this message generally __FILE__.
198 * @param line The line in the file that generated the message generally __LINE__
199 * @throws NSPluginException if the module is not loaded.
200 */
201void NSCPlugin::handleMessage(int msgType, const TCHAR* file, const int line, const TCHAR *message) {
202        if (!fHandleMessage)
203                throw NSPluginException(file_, _T("Library is not loaded"));
204        fHandleMessage(msgType, file, line, message);
205}
206/**
207 * Unload the plug in
208 * @throws NSPluginException if the module is not loaded and/or cannot be unloaded (plug in remains loaded if so).
209 */
210void NSCPlugin::unload() {
211        if (!isLoaded())
212                throw NSPluginException(file_, _T("Library is not loaded"));
213        if (!fUnLoadModule)
214                throw NSPluginException(file_, _T("Critical error (fUnLoadModule)"));
215        fUnLoadModule();
216        FreeLibrary(hModule_);
217        hModule_ = NULL;
218        bLoaded_ = false;
219}
220bool NSCPlugin::getName_(TCHAR* buf, unsigned int buflen) {
221        if (fGetName == NULL)
222                throw NSPluginException(file_, _T("Critical error (fGetName)"));
223        return fGetName(buf, buflen)?true:false;
224}
225bool NSCPlugin::getDescription_(TCHAR* buf, unsigned int buflen) {
226        if (fGetDescription == NULL)
227                throw NSPluginException(file_, _T("Critical error (fGetDescription)"));
228        return fGetDescription(buf, buflen)?true:false;
229}
230/**
231 * Load all remote function pointers from the loaded module.
232 * These pointers are cached for "speed" which might (?) be dangerous if something changes.
233 * @throws NSPluginException if any of the function pointers fail to load.
234 * If NSPluginException  is thrown the loaded might remain partially loaded and crashes might occur if plug in is used in this state.
235 */
236void NSCPlugin::loadRemoteProcs_(void) {
237
238        fLoadModule = (lpLoadModule)GetProcAddress(hModule_, "NSLoadModule");
239        if (!fLoadModule)
240                throw NSPluginException(file_, _T("Could not load NSLoadModule"));
241
242        fModuleHelperInit = (lpModuleHelperInit)GetProcAddress(hModule_, "NSModuleHelperInit");
243        if (!fModuleHelperInit)
244                throw NSPluginException(file_, _T("Could not load NSModuleHelperInit"));
245
246        fModuleHelperInit(NSAPILoader);
247       
248        fGetName = (lpGetName)GetProcAddress(hModule_, "NSGetModuleName");
249        if (!fGetName)
250                throw NSPluginException(file_, _T("Could not load NSGetModuleName"));
251
252        fGetVersion = (lpGetVersion)GetProcAddress(hModule_, "NSGetModuleVersion");
253        if (!fGetVersion)
254                throw NSPluginException(file_, _T("Could not load NSGetModuleVersion"));
255
256        fGetDescription = (lpGetDescription)GetProcAddress(hModule_, "NSGetModuleDescription");
257        if (!fGetDescription)
258                throw NSPluginException(file_, _T("Could not load NSGetModuleDescription"));
259
260        fHasCommandHandler = (lpHasCommandHandler)GetProcAddress(hModule_, "NSHasCommandHandler");
261        if (!fHasCommandHandler)
262                throw NSPluginException(file_, _T("Could not load NSHasCommandHandler"));
263
264        fHasMessageHandler = (lpHasMessageHandler)GetProcAddress(hModule_, "NSHasMessageHandler");
265        if (!fHasMessageHandler)
266                throw NSPluginException(file_, _T("Could not load NSHasMessageHandler"));
267
268        fHandleCommand = (lpHandleCommand)GetProcAddress(hModule_, "NSHandleCommand");
269        if (!fHandleCommand)
270                throw NSPluginException(file_, _T("Could not load NSHandleCommand"));
271
272        fHandleMessage = (lpHandleMessage)GetProcAddress(hModule_, "NSHandleMessage");
273        if (!fHandleMessage)
274                throw NSPluginException(file_, _T("Could not load NSHandleMessage"));
275
276        fUnLoadModule = (lpUnLoadModule)GetProcAddress(hModule_, "NSUnloadModule");
277        if (!fUnLoadModule)
278                throw NSPluginException(file_, _T("Could not load NSUnloadModule"));
279
280        fGetConfigurationMeta = (lpGetConfigurationMeta)GetProcAddress(hModule_, "NSGetConfigurationMeta");
281        fCommandLineExec = (lpCommandLineExec)GetProcAddress(hModule_, "NSCommandLineExec");
282}
283
284
285std::wstring NSCPlugin::getCongifurationMeta()
286{
287        TCHAR *buffer = new TCHAR[4097];
288        if (!getConfigurationMeta_(buffer, 4096)) {
289                throw NSPluginException(file_, _T("Could not get metadata"));
290        }
291        std::wstring ret = buffer;
292        delete [] buffer;
293        return ret;
294}
295bool NSCPlugin::getConfigurationMeta_(TCHAR* buf, unsigned int buflen) {
296        if (fGetConfigurationMeta == NULL)
297                throw NSPluginException(file_, _T("Critical error (getCongifurationMeta)"));
298        return fGetConfigurationMeta(buflen, buf)?true:false;
299}
300
301int NSCPlugin::commandLineExec(const TCHAR* command, const unsigned int argLen, TCHAR **arguments) {
302        if (fCommandLineExec== NULL)
303                throw NSPluginException(file_, _T("Module does not support CommandLineExec"));
304        return fCommandLineExec(command, argLen, arguments);
305}
Note: See TracBrowser for help on using the repository browser.