source: nscp/modules/CheckExternalScripts/CheckExternalScripts.cpp @ 1d53fe0

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

2009-02-11 MickeM

  • Fixed so that performance data is always(?) renderd regardless of if we have bounds or not. This was primarily to fix issues where we have might not "unexpectedly" get performance data (might still be some issues here so let me know).

2009-02-06 MickeM

2009-02-05 MickeM

  • Fixed so the error message for "to small eventlog buffer" specifies the required size.
  • Changed so that event log buffer problems are "ignored in the result" (still logged in the error log).

2009-02-03 MickeM

  • Added support for changing the time when using NSCA with the time_delay in NSC.ini time_delay=+4h or time_delay=-1h etc should can now be used when system time not the same as NSCA time.

2009-01-30 MickeM

  • Added support for changing name and description of service from the /install command line NSClient++ /install [gui] [start] [service name] [description] NSClient++ /uninstall [gui] [stop] [service name] NSClient++ /start [gui] [service name] NSClient++ /stop [gui] [service name]

2009-01-28 MickeM

  • Slightly improved error handling around socket creation
  • Fixed some pretty minor issues with the SysTray module (uncreation as well as new boost build).

2009-01-25 MickeM

  • Fixed issue with checkVersion (#242)
  • Fixed spelling error (#244)
  • Fixed crash in CheckFile when a file was locked in exclusive mode (#254) + Improved error handling in all CheckDIsk/CheckFile checks. Should report errors better now.
  • Updated the config file a bit: remving "beta" from a bunch of modules no longer in beta. (#270) + Added more filter operatos to all numeric filters so they accept eq:, ne:, gt:, lt: in addition to =, >, <, <>, !, !=, in: (#269)

2009-01-23 MickeM

+ Added better support for numerical hit matching in the eventlog module. You can now use exact and detailed matching.

You can now use the following syntax:
CheckEventLog ... warn=ne:1 crit=eq:0 ...
To generate a warning if the number of hits are != 1 and a critical if the number of hits are = 0.
Other operators avalible are: =, >, <, <>, !, !=, eq:, ne:, gt:, lt:

  • Property mode set to 100644
File size: 8.8 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 "CheckExternalScripts.h"
23#include <strEx.h>
24#include <time.h>
25#include <config.h>
26#include <msvc_wrappers.h>
27
28CheckExternalScripts gCheckExternalScripts;
29
30BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
31{
32        NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call);
33        return TRUE;
34}
35
36CheckExternalScripts::CheckExternalScripts() {}
37CheckExternalScripts::~CheckExternalScripts() {}
38
39void CheckExternalScripts::addAllScriptsFrom(std::wstring path) {
40        std::wstring baseDir;
41        std::wstring::size_type pos = path.find_last_of('*');
42        if (pos == std::wstring::npos) {
43                path += _T("*.*");
44        }
45        WIN32_FIND_DATA wfd;
46        HANDLE hFind = FindFirstFile(path.c_str(), &wfd);
47        if (hFind != INVALID_HANDLE_VALUE) {
48                do {
49                        if ((wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY) {
50                                addCommand(wfd.cFileName);
51                        }
52                } while (FindNextFile(hFind, &wfd));
53        } else {
54                NSC_LOG_ERROR_STD(_T("No scripts found in path: ") + path);
55                return;
56        }
57        FindClose(hFind);
58}
59
60bool CheckExternalScripts::loadModule() {
61        timeout = NSCModuleHelper::getSettingsInt(EXTSCRIPT_SECTION_TITLE, EXTSCRIPT_SETTINGS_TIMEOUT ,EXTSCRIPT_SETTINGS_TIMEOUT_DEFAULT);
62        scriptDirectory_ = NSCModuleHelper::getSettingsString(EXTSCRIPT_SECTION_TITLE, EXTSCRIPT_SETTINGS_SCRIPTDIR ,EXTSCRIPT_SETTINGS_SCRIPTDIR_DEFAULT);
63        std::list<std::wstring>::const_iterator it;
64        std::list<std::wstring> commands = NSCModuleHelper::getSettingsSection(EXTSCRIPT_SCRIPT_SECTION_TITLE);
65        for (it = commands.begin(); it != commands.end(); ++it) {
66                if ((*it).empty())
67                        continue;
68                std::wstring s = NSCModuleHelper::getSettingsString(EXTSCRIPT_SCRIPT_SECTION_TITLE, (*it), _T(""));
69                if (s.empty()) {
70                        NSC_LOG_ERROR_STD(_T("Invalid command definition: ") + (*it));
71                } else {
72                        strEx::token tok = strEx::getToken(s, ' ', true);
73                        addCommand((*it).c_str(), tok.first, tok.second);
74                }
75        }
76
77        commands = NSCModuleHelper::getSettingsSection(EXTSCRIPT_ALIAS_SECTION_TITLE);
78        for (it = commands.begin(); it != commands.end(); ++it) {
79                if ((*it).empty())
80                        continue;
81                std::wstring s = NSCModuleHelper::getSettingsString(EXTSCRIPT_ALIAS_SECTION_TITLE, (*it), _T(""));
82                if (s.empty()) {
83                        NSC_LOG_ERROR_STD(_T("Invalid command definition: ") + (*it));
84                } else {
85                        strEx::token tok = strEx::getToken(s, ' ', true);
86                        addAlias((*it).c_str(), tok.first, tok.second);
87                }
88        }
89
90        if (!scriptDirectory_.empty()) {
91                addAllScriptsFrom(scriptDirectory_);
92        }
93        root_ = NSCModuleHelper::getBasePath();
94        return true;
95}
96bool CheckExternalScripts::unloadModule() {
97        return true;
98}
99
100
101bool CheckExternalScripts::hasCommandHandler() {
102        return true;
103}
104bool CheckExternalScripts::hasMessageHandler() {
105        return false;
106}
107
108
109NSCAPI::nagiosReturn CheckExternalScripts::handleCommand(const strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf) {
110        command_list::const_iterator cit = commands.find(command);
111        bool isAlias = false;
112        if (cit == commands.end()) {
113                cit = alias.find(command);
114                if (cit == alias.end())
115                        return NSCAPI::returnIgnored;
116                isAlias = true;
117        }
118
119        const command_data cd = (*cit).second;
120        std::wstring args = cd.arguments;
121        if (isAlias || NSCModuleHelper::getSettingsInt(EXTSCRIPT_SECTION_TITLE, EXTSCRIPT_SETTINGS_ALLOW_ARGUMENTS, EXTSCRIPT_SETTINGS_ALLOW_ARGUMENTS_DEFAULT) == 1) {
122                arrayBuffer::arrayList arr = arrayBuffer::arrayBuffer2list(argLen, char_args);
123                arrayBuffer::arrayList::const_iterator cit2 = arr.begin();
124                int i=1;
125
126                for (;cit2!=arr.end();cit2++,i++) {
127                        if (isAlias || NSCModuleHelper::getSettingsInt(EXTSCRIPT_SECTION_TITLE, EXTSCRIPT_SETTINGS_ALLOW_NASTY_META, EXTSCRIPT_SETTINGS_ALLOW_NASTY_META_DEFAULT) == 0) {
128                                if ((*cit2).find_first_of(NASTY_METACHARS) != std::wstring::npos) {
129                                        NSC_LOG_ERROR(_T("Request string contained illegal metachars!"));
130                                        return NSCAPI::returnIgnored;
131                                }
132                        }
133                        strEx::replace(args, _T("$ARG") + strEx::itos(i) + _T("$"), (*cit2));
134                }
135        }
136        if (isAlias) {
137                return NSCModuleHelper::InjectSplitAndCommand(cd.command, args, ' ', message, perf, true);
138        } else {
139                int result = process::executeProcess(root_, cd.command + _T(" ") + args, message, perf, timeout);
140                if (!NSCHelper::isNagiosReturnCode(result)) {
141                        NSC_LOG_ERROR_STD(_T("The command (") + cd.command + _T(") returned an invalid return code: ") + strEx::itos(result));
142                        return NSCAPI::returnUNKNOWN;
143                }
144                return NSCHelper::int2nagios(result);
145                /*
146        } else if (cd.type == script_dir) {
147                std::wstring args = arrayBuffer::arrayBuffer2string(char_args, argLen, _T(" "));
148                std::wstring cmd = scriptDirectory_ + command.c_str() + _T(" ") +args;
149                return executeNRPECommand(cmd, message, perf);
150        } else {
151                NSC_LOG_ERROR_STD(_T("Unknown script type: ") + command.c_str());
152                return NSCAPI::critical;
153                */
154        }
155
156}
157
158
159NSC_WRAPPERS_MAIN_DEF(gCheckExternalScripts);
160NSC_WRAPPERS_IGNORE_MSG_DEF();
161NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckExternalScripts);
162NSC_WRAPPERS_HANDLE_CONFIGURATION(gCheckExternalScripts);
163
164
165MODULE_SETTINGS_START(CheckExternalScripts, _T("NRPE Listener configuration"), _T("..."))
166
167PAGE(_T("NRPE Listsner configuration"))
168
169ITEM_EDIT_TEXT(_T("port"), _T("This is the port the CheckExternalScripts.dll will listen to."))
170ITEM_MAP_TO(_T("basic_ini_text_mapper"))
171OPTION(_T("section"), _T("NRPE"))
172OPTION(_T("key"), _T("port"))
173OPTION(_T("default"), _T("5666"))
174ITEM_END()
175
176ITEM_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."))
177ITEM_MAP_TO(_T("basic_ini_bool_mapper"))
178OPTION(_T("section"), _T("NRPE"))
179OPTION(_T("key"), _T("allow_arguments"))
180OPTION(_T("default"), _T("false"))
181OPTION(_T("true_value"), _T("1"))
182OPTION(_T("false_value"), _T("0"))
183ITEM_END()
184
185ITEM_CHECK_BOOL(_T("allow_nasty_meta_chars"), _T("This might have security implications (depending on what you do with the options)"))
186ITEM_MAP_TO(_T("basic_ini_bool_mapper"))
187OPTION(_T("section"), _T("NRPE"))
188OPTION(_T("key"), _T("allow_nasty_meta_chars"))
189OPTION(_T("default"), _T("false"))
190OPTION(_T("true_value"), _T("1"))
191OPTION(_T("false_value"), _T("0"))
192ITEM_END()
193
194ITEM_CHECK_BOOL(_T("use_ssl"), _T("This option will enable SSL encryption on the NRPE data socket (this increases security somwhat."))
195ITEM_MAP_TO(_T("basic_ini_bool_mapper"))
196OPTION(_T("section"), _T("NRPE"))
197OPTION(_T("key"), _T("use_ssl"))
198OPTION(_T("default"), _T("true"))
199OPTION(_T("true_value"), _T("1"))
200OPTION(_T("false_value"), _T("0"))
201ITEM_END()
202
203PAGE_END()
204ADVANCED_PAGE(_T("Access configuration"))
205
206ITEM_EDIT_OPTIONAL_LIST(_T("Allow connection from:"), _T("This is the hosts that will be allowed to poll performance data from the NRPE server."))
207OPTION(_T("disabledCaption"), _T("Use global settings (defined previously)"))
208OPTION(_T("enabledCaption"), _T("Specify hosts for NRPE server"))
209OPTION(_T("listCaption"), _T("Add all IP addresses (not hosts) which should be able to connect:"))
210OPTION(_T("separator"), _T(","))
211OPTION(_T("disabled"), _T(""))
212ITEM_MAP_TO(_T("basic_ini_text_mapper"))
213OPTION(_T("section"), _T("NRPE"))
214OPTION(_T("key"), _T("allowed_hosts"))
215OPTION(_T("default"), _T(""))
216ITEM_END()
217
218PAGE_END()
219MODULE_SETTINGS_END()
Note: See TracBrowser for help on using the repository browser.