| 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 | NSC_WRAPPERS_MAIN();
|
|---|
| 23 | #include <map>
|
|---|
| 24 | #include <error.hpp>
|
|---|
| 25 | #include <execute_process.hpp>
|
|---|
| 26 |
|
|---|
| 27 | class CheckExternalScripts : public nscapi::impl::simple_plugin {
|
|---|
| 28 | private:
|
|---|
| 29 | struct command_data {
|
|---|
| 30 | command_data() {}
|
|---|
| 31 | command_data(std::wstring alias, std::wstring command_, std::wstring arguments_) : alias(alias), command(command_) {
|
|---|
| 32 | parser_arguments(arguments_);
|
|---|
| 33 | }
|
|---|
| 34 | void parser_arguments(std::wstring args) {
|
|---|
| 35 | try {
|
|---|
| 36 | boost::tokenizer<boost::escaped_list_separator<wchar_t>, std::wstring::const_iterator, std::wstring>
|
|---|
| 37 | tok(args, boost::escaped_list_separator<wchar_t>(L'\\', L' ', L'\"'));
|
|---|
| 38 | BOOST_FOREACH(std::wstring s, tok) {
|
|---|
| 39 | arguments.push_back(s);
|
|---|
| 40 | }
|
|---|
| 41 | } catch (const std::exception &e) {
|
|---|
| 42 | NSC_LOG_ERROR(_T("Failed to parse arguments for command '") + alias + _T("', using old split string method: ") + utf8::to_unicode(e.what()));
|
|---|
| 43 | strEx::splitList list = strEx::splitEx(args, _T(" "));
|
|---|
| 44 | BOOST_FOREACH(std::wstring s, list) {
|
|---|
| 45 | arguments.push_back(s);
|
|---|
| 46 | }
|
|---|
| 47 | }
|
|---|
| 48 | }
|
|---|
| 49 | std::wstring get_argument() {
|
|---|
| 50 | std::wstring args;
|
|---|
| 51 | BOOST_FOREACH(std::wstring s, arguments) {
|
|---|
| 52 | if (!args.empty())
|
|---|
| 53 | args += _T(" ");
|
|---|
| 54 | args += s;
|
|---|
| 55 | }
|
|---|
| 56 | return args;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | std::wstring alias;
|
|---|
| 60 | std::wstring command;
|
|---|
| 61 | std::list<std::wstring> arguments;
|
|---|
| 62 | std::wstring to_string() {
|
|---|
| 63 | std::wstring args;
|
|---|
| 64 | BOOST_FOREACH(std::wstring s, arguments) {
|
|---|
| 65 | if (!args.empty())
|
|---|
| 66 | args += _T(" ");
|
|---|
| 67 | args += s;
|
|---|
| 68 | }
|
|---|
| 69 | return command + _T("(") + get_argument() + _T(")");
|
|---|
| 70 | }
|
|---|
| 71 | };
|
|---|
| 72 | typedef std::map<std::wstring, command_data> command_list;
|
|---|
| 73 | command_list commands;
|
|---|
| 74 | command_list alias;
|
|---|
| 75 | unsigned int timeout;
|
|---|
| 76 | std::wstring scriptDirectory_;
|
|---|
| 77 | std::wstring root_;
|
|---|
| 78 | bool allowArgs_;
|
|---|
| 79 | bool allowNasty_;
|
|---|
| 80 | std::map<std::wstring,std::wstring> wrappings_;
|
|---|
| 81 |
|
|---|
| 82 | public:
|
|---|
| 83 | CheckExternalScripts();
|
|---|
| 84 | virtual ~CheckExternalScripts();
|
|---|
| 85 | // Module calls
|
|---|
| 86 | bool loadModule();
|
|---|
| 87 | bool loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode);
|
|---|
| 88 | bool unloadModule();
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 | static std::wstring getModuleName() {
|
|---|
| 92 | return _T("Check External Scripts");
|
|---|
| 93 | }
|
|---|
| 94 | static nscapi::plugin_wrapper::module_version getModuleVersion() {
|
|---|
| 95 | nscapi::plugin_wrapper::module_version version = {0, 0, 1 };
|
|---|
| 96 | return version;
|
|---|
| 97 | }
|
|---|
| 98 | static std::wstring getModuleDescription() {
|
|---|
| 99 | return _T("A simple wrapper to run external scripts and batch files.");
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | bool hasCommandHandler();
|
|---|
| 103 | bool hasMessageHandler();
|
|---|
| 104 | NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response);
|
|---|
| 105 | //NSCAPI::nagiosReturn handleCommand(const std::wstring command, std::list<std::wstring> arguments, std::wstring &message, std::wstring &perf);
|
|---|
| 106 | std::wstring getConfigurationMeta();
|
|---|
| 107 |
|
|---|
| 108 | private:
|
|---|
| 109 | class NRPEException {
|
|---|
| 110 | std::wstring error_;
|
|---|
| 111 | public:
|
|---|
| 112 | NRPEException(std::wstring s) {
|
|---|
| 113 | error_ = s;
|
|---|
| 114 | }
|
|---|
| 115 | std::wstring getMessage() {
|
|---|
| 116 | return error_;
|
|---|
| 117 | }
|
|---|
| 118 | };
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 | private:
|
|---|
| 122 | void addAllScriptsFrom(std::wstring path);
|
|---|
| 123 | void add_command(std::wstring key, std::wstring command) {
|
|---|
| 124 | strEx::token tok = strEx::getToken(command, ' ', true);
|
|---|
| 125 | add_command(key.c_str(), command, _T("Script: ") + tok.first);
|
|---|
| 126 | }
|
|---|
| 127 | void add_command(std::wstring key, std::wstring command, std::wstring alias) {
|
|---|
| 128 | strEx::token tok = strEx::getToken(command, ' ', true);
|
|---|
| 129 | boost::to_lower(key);
|
|---|
| 130 | command_data cd = command_data(key, tok.first, tok.second);
|
|---|
| 131 | commands[key.c_str()] = cd;
|
|---|
| 132 | register_command(key.c_str(), alias);
|
|---|
| 133 | }
|
|---|
| 134 | void add_alias(std::wstring key, std::wstring command) {
|
|---|
| 135 | strEx::token tok = strEx::getToken(command, ' ', true);
|
|---|
| 136 | boost::to_lower(key);
|
|---|
| 137 | command_data cd = command_data(key, tok.first, tok.second);
|
|---|
| 138 | alias[key.c_str()] = cd;
|
|---|
| 139 | register_command(key.c_str(), _T("Alias for: ") + cd.to_string());
|
|---|
| 140 | }
|
|---|
| 141 | void add_wrapping(std::wstring key, std::wstring command) {
|
|---|
| 142 | strEx::token tok = strEx::getToken(command, ' ', true);
|
|---|
| 143 | std::wstring::size_type pos = tok.first.find_last_of(_T("."));
|
|---|
| 144 | std::wstring type;
|
|---|
| 145 | if (pos != std::wstring::npos)
|
|---|
| 146 | type = tok.first.substr(pos+1);
|
|---|
| 147 |
|
|---|
| 148 | std::wstring tpl = wrappings_[type];
|
|---|
| 149 |
|
|---|
| 150 | strEx::replace(tpl, _T("%SCRIPT%"), tok.first);
|
|---|
| 151 | strEx::replace(tpl, _T("%ARGS%"), tok.second);
|
|---|
| 152 |
|
|---|
| 153 | add_command(key,tpl,command);
|
|---|
| 154 | }
|
|---|
| 155 | };
|
|---|
| 156 |
|
|---|