| 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 | #pragma once
|
|---|
| 22 |
|
|---|
| 23 | #include <boost/tuple/tuple.hpp>
|
|---|
| 24 |
|
|---|
| 25 | #include <client/command_line_parser.hpp>
|
|---|
| 26 | #include <nscapi/targets.hpp>
|
|---|
| 27 |
|
|---|
| 28 | #include <nsca/nsca_packet.hpp>
|
|---|
| 29 |
|
|---|
| 30 | NSC_WRAPPERS_MAIN();
|
|---|
| 31 | NSC_WRAPPERS_CLI();
|
|---|
| 32 | NSC_WRAPPERS_CHANNELS();
|
|---|
| 33 |
|
|---|
| 34 | namespace po = boost::program_options;
|
|---|
| 35 |
|
|---|
| 36 | class NSCAAgent : public nscapi::impl::simple_plugin {
|
|---|
| 37 | private:
|
|---|
| 38 |
|
|---|
| 39 | std::wstring channel_;
|
|---|
| 40 | std::wstring target_path;
|
|---|
| 41 | std::string hostname_;
|
|---|
| 42 | bool cacheNscaHost_;
|
|---|
| 43 | long time_delta_;
|
|---|
| 44 |
|
|---|
| 45 | nscapi::target_handler targets;
|
|---|
| 46 | client::command_manager commands;
|
|---|
| 47 |
|
|---|
| 48 | struct connection_data {
|
|---|
| 49 | std::string password;
|
|---|
| 50 | std::string encryption;
|
|---|
| 51 | std::string host, port;
|
|---|
| 52 | std::string sender_hostname;
|
|---|
| 53 | int timeout;
|
|---|
| 54 | int buffer_length;
|
|---|
| 55 | int time_delta;
|
|---|
| 56 |
|
|---|
| 57 | connection_data(nscapi::functions::destination_container recipient, nscapi::functions::destination_container sender) {
|
|---|
| 58 | timeout = recipient.get_int_data("timeout", 30);
|
|---|
| 59 | buffer_length = recipient.get_int_data("payload length", 512);
|
|---|
| 60 | password = recipient.get_string_data("password");
|
|---|
| 61 | encryption = recipient.get_string_data("encryption");
|
|---|
| 62 | std::string tmp = recipient.get_string_data("time offset");
|
|---|
| 63 | if (!tmp.empty())
|
|---|
| 64 | time_delta = strEx::stol_as_time_sec(recipient.get_string_data("time offset"));
|
|---|
| 65 | else
|
|---|
| 66 | time_delta = 0;
|
|---|
| 67 | net::url url = recipient.get_url(5667);
|
|---|
| 68 | host = url.host;
|
|---|
| 69 | port = url.get_port();
|
|---|
| 70 | sender_hostname = sender.get_string_data("host");
|
|---|
| 71 | }
|
|---|
| 72 | unsigned int get_encryption() {
|
|---|
| 73 | return nsca::nsca_encrypt::helpers::encryption_to_int(encryption);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | std::wstring to_wstring() {
|
|---|
| 77 | std::wstringstream ss;
|
|---|
| 78 | ss << _T("host: ") << utf8::cvt<std::wstring>(host);
|
|---|
| 79 | ss << _T(", port: ") << utf8::cvt<std::wstring>(port);
|
|---|
| 80 | ss << _T(", timeout: ") << timeout;
|
|---|
| 81 | ss << _T(", buffer_length: ") << buffer_length;
|
|---|
| 82 | ss << _T(", time_delta: ") << time_delta;
|
|---|
| 83 | ss << _T(", password: ") << utf8::cvt<std::wstring>(password);
|
|---|
| 84 | ss << _T(", encryption: ") << utf8::cvt<std::wstring>(encryption);
|
|---|
| 85 | return ss.str();
|
|---|
| 86 | }
|
|---|
| 87 | };
|
|---|
| 88 |
|
|---|
| 89 | struct clp_handler_impl : public client::clp_handler, client::target_lookup_interface {
|
|---|
| 90 |
|
|---|
| 91 | NSCAAgent *instance;
|
|---|
| 92 | clp_handler_impl(NSCAAgent *instance) : instance(instance) {}
|
|---|
| 93 |
|
|---|
| 94 | int query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply);
|
|---|
| 95 | int submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply);
|
|---|
| 96 | int exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply);
|
|---|
| 97 |
|
|---|
| 98 | virtual nscapi::functions::destination_container lookup_target(std::wstring &id) {
|
|---|
| 99 | nscapi::functions::destination_container ret;
|
|---|
| 100 | nscapi::target_handler::optarget t = instance->targets.find_target(id);
|
|---|
| 101 | if (t) {
|
|---|
| 102 | if (!t->alias.empty())
|
|---|
| 103 | ret.id = utf8::cvt<std::string>(t->alias);
|
|---|
| 104 | if (!t->host.empty())
|
|---|
| 105 | ret.host = utf8::cvt<std::string>(t->host);
|
|---|
| 106 | if (t->has_option("address"))
|
|---|
| 107 | ret.address = utf8::cvt<std::string>(t->options[_T("address")]);
|
|---|
| 108 | else
|
|---|
| 109 | ret.address = utf8::cvt<std::string>(t->host);
|
|---|
| 110 | BOOST_FOREACH(const nscapi::target_handler::target::options_type::value_type &kvp, t->options) {
|
|---|
| 111 | ret.data[utf8::cvt<std::string>(kvp.first)] = utf8::cvt<std::string>(kvp.second);
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|
| 114 | return ret;
|
|---|
| 115 | }
|
|---|
| 116 | };
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 | public:
|
|---|
| 120 | NSCAAgent();
|
|---|
| 121 | virtual ~NSCAAgent();
|
|---|
| 122 | // Module calls
|
|---|
| 123 | bool loadModule();
|
|---|
| 124 | bool loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode);
|
|---|
| 125 | bool unloadModule();
|
|---|
| 126 |
|
|---|
| 127 | /**
|
|---|
| 128 | * Return the module name.
|
|---|
| 129 | * @return The module name
|
|---|
| 130 | */
|
|---|
| 131 | static std::wstring getModuleName() {
|
|---|
| 132 | #ifdef HAVE_LIBCRYPTOPP
|
|---|
| 133 | return _T("NSCAClient");
|
|---|
| 134 | #else
|
|---|
| 135 | return _T("NSCAClient (without encryption support)");
|
|---|
| 136 | #endif
|
|---|
| 137 | }
|
|---|
| 138 | /**
|
|---|
| 139 | * Module version
|
|---|
| 140 | * @return module version
|
|---|
| 141 | */
|
|---|
| 142 | static nscapi::plugin_wrapper::module_version getModuleVersion() {
|
|---|
| 143 | nscapi::plugin_wrapper::module_version version = {0, 4, 0 };
|
|---|
| 144 | return version;
|
|---|
| 145 | }
|
|---|
| 146 | static std::wstring getModuleDescription() {
|
|---|
| 147 | return std::wstring(_T("Passive check support (needs NSCA on nagios server).\nAvalible crypto are: ")) + getCryptos();
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | bool hasCommandHandler() { return true; };
|
|---|
| 151 | bool hasMessageHandler() { return true; };
|
|---|
| 152 | bool hasNotificationHandler() { return true; };
|
|---|
| 153 | NSCAPI::nagiosReturn handleRAWNotification(const wchar_t* channel, std::string request, std::string &response);
|
|---|
| 154 | NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response);
|
|---|
| 155 | NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response);
|
|---|
| 156 |
|
|---|
| 157 | private:
|
|---|
| 158 | boost::tuple<int,std::wstring> send(connection_data data, const std::list<nsca::packet> packets);
|
|---|
| 159 | void add_options(po::options_description &desc, connection_data &command_data);
|
|---|
| 160 | static connection_data parse_header(const ::Plugin::Common_Header &header);
|
|---|
| 161 |
|
|---|
| 162 | private:
|
|---|
| 163 | void add_local_options(po::options_description &desc, client::configuration::data_type data);
|
|---|
| 164 | void setup(client::configuration &config);
|
|---|
| 165 | void add_command(std::wstring key, std::wstring args);
|
|---|
| 166 | void add_target(std::wstring key, std::wstring args);
|
|---|
| 167 |
|
|---|
| 168 | static std::wstring getCryptos();
|
|---|
| 169 | void set_delay(std::wstring key) {
|
|---|
| 170 | time_delta_ = strEx::stol_as_time_sec(key, 1);
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | };
|
|---|