| 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 "NSCAClient.h"
|
|---|
| 23 |
|
|---|
| 24 | #include <utils.h>
|
|---|
| 25 | #include <strEx.h>
|
|---|
| 26 |
|
|---|
| 27 | #include <nsca/nsca_enrypt.hpp>
|
|---|
| 28 | #include <nsca/nsca_packet.hpp>
|
|---|
| 29 | #include <nsca/nsca_socket.hpp>
|
|---|
| 30 |
|
|---|
| 31 | #include <settings/client/settings_client.hpp>
|
|---|
| 32 |
|
|---|
| 33 | namespace sh = nscapi::settings_helper;
|
|---|
| 34 |
|
|---|
| 35 | /**
|
|---|
| 36 | * Default c-tor
|
|---|
| 37 | * @return
|
|---|
| 38 | */
|
|---|
| 39 | NSCAAgent::NSCAAgent() {}
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * Default d-tor
|
|---|
| 43 | * @return
|
|---|
| 44 | */
|
|---|
| 45 | NSCAAgent::~NSCAAgent() {}
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * Load (initiate) module.
|
|---|
| 49 | * Start the background collector thread and let it run until unloadModule() is called.
|
|---|
| 50 | * @return true
|
|---|
| 51 | */
|
|---|
| 52 | bool NSCAAgent::loadModule() {
|
|---|
| 53 | return false;
|
|---|
| 54 | }
|
|---|
| 55 | /*
|
|---|
| 56 | DEFINE_SETTING_S(REPORT_MODE, NSCA_SERVER_SECTION, "report", "all");
|
|---|
| 57 | DESCRIBE_SETTING(REPORT_MODE, "REPORT MODE", "What to report to the server (any of the following: all, critical, warning, unknown, ok)");
|
|---|
| 58 | */
|
|---|
| 59 |
|
|---|
| 60 | bool NSCAAgent::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) {
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 | std::wstring encryption, password, nscahost;
|
|---|
| 64 | std::string delay;
|
|---|
| 65 | unsigned int timeout = 30, payload_length = 512, nscaport = 5666;
|
|---|
| 66 | try {
|
|---|
| 67 | sh::settings_registry settings(get_settings_proxy());
|
|---|
| 68 | settings.set_alias(_T("NSCA"), alias, _T("client"));
|
|---|
| 69 | target_path = settings.alias().get_settings_path(_T("targets"));
|
|---|
| 70 |
|
|---|
| 71 | settings.alias().add_path_to_settings()
|
|---|
| 72 | (_T("NSCA CLIENT SECTION"), _T("Section for NSCA passive check module."))
|
|---|
| 73 |
|
|---|
| 74 | (_T("handlers"), sh::fun_values_path(boost::bind(&NSCAAgent::add_command, this, _1, _2)),
|
|---|
| 75 | _T("CLIENT HANDLER SECTION"), _T(""))
|
|---|
| 76 |
|
|---|
| 77 | (_T("targets"), sh::fun_values_path(boost::bind(&NSCAAgent::add_target, this, _1, _2)),
|
|---|
| 78 | _T("REMOTE TARGET DEFINITIONS"), _T(""))
|
|---|
| 79 | ;
|
|---|
| 80 |
|
|---|
| 81 | settings.alias().add_key_to_settings()
|
|---|
| 82 | (_T("hostname"), sh::string_key(&hostname_),
|
|---|
| 83 | _T("HOSTNAME"), _T("The host name of this host if set to blank (default) the windows name of the computer will be used."))
|
|---|
| 84 |
|
|---|
| 85 | (_T("channel"), sh::wstring_key(&channel_, _T("NSCA")),
|
|---|
| 86 | _T("CHANNEL"), _T("The channel to listen to."))
|
|---|
| 87 |
|
|---|
| 88 | (_T("delay"), sh::string_fun_key<std::wstring>(boost::bind(&NSCAAgent::set_delay, this, _1), _T("0")),
|
|---|
| 89 | _T("DELAY"), _T(""))
|
|---|
| 90 | ;
|
|---|
| 91 |
|
|---|
| 92 | settings.alias().add_path_to_settings(_T("server"))
|
|---|
| 93 | (_T("NSCA SERVER"), _T("Configure the NSCA server to report to."))
|
|---|
| 94 | ;
|
|---|
| 95 |
|
|---|
| 96 | settings.alias().add_key_to_settings(_T("targets/default"))
|
|---|
| 97 |
|
|---|
| 98 | (_T("timeout"), sh::uint_key(&timeout, 30),
|
|---|
| 99 | _T("TIMEOUT"), _T("Timeout when reading packets on incoming sockets. If the data has not arrived withint this time we will bail out."))
|
|---|
| 100 |
|
|---|
| 101 | (_T("host"), sh::wstring_key(&nscahost),
|
|---|
| 102 | _T("NSCA HOST"), _T("The NSCA server to report results to."))
|
|---|
| 103 |
|
|---|
| 104 | (_T("port"), sh::uint_key(&nscaport, 5667),
|
|---|
| 105 | _T("NSCA PORT"), _T("The NSCA server port"))
|
|---|
| 106 |
|
|---|
| 107 | (_T("encryption"), sh::wstring_key(&encryption, _T("aes")),
|
|---|
| 108 | _T("ENCRYPTION METHOD"), _T("Number corresponding to the various encryption algorithms (see the wiki). Has to be the same as the server or it wont work at all."))
|
|---|
| 109 |
|
|---|
| 110 | (_T("password"), sh::wstring_key(&password),
|
|---|
| 111 | _T("PASSWORD"), _T("The password to use. Again has to be the same as the server or it wont work at all."))
|
|---|
| 112 |
|
|---|
| 113 | (_T("payload length"), sh::uint_key(&payload_length, 512),
|
|---|
| 114 | _T("PAYLOAD LENGTH"), _T("Length of payload to/from the NSCA agent. This is a hard specific value so you have to \"configure\" (read recompile) your NSCA agent to use the same value for it to work."))
|
|---|
| 115 |
|
|---|
| 116 | (_T("time offset"), sh::string_key(&delay, "0"),
|
|---|
| 117 | _T("TIME OFFSET"), _T("Time offset."))
|
|---|
| 118 | ;
|
|---|
| 119 |
|
|---|
| 120 | settings.register_all();
|
|---|
| 121 | settings.notify();
|
|---|
| 122 |
|
|---|
| 123 | get_core()->registerSubmissionListener(get_id(), channel_);
|
|---|
| 124 |
|
|---|
| 125 | if (!targets.has_target(_T("default"))) {
|
|---|
| 126 | add_target(_T("default"), _T("default"));
|
|---|
| 127 | targets.rebuild();
|
|---|
| 128 | }
|
|---|
| 129 | nscapi::target_handler::optarget t = targets.find_target(_T("default"));
|
|---|
| 130 | if (t) {
|
|---|
| 131 | if (!t->has_option("encryption"))
|
|---|
| 132 | t->options[_T("encryption")] = encryption;
|
|---|
| 133 | if (!t->has_option("timeout"))
|
|---|
| 134 | t->options[_T("timeout")] = strEx::itos(timeout);
|
|---|
| 135 | if (!t->has_option("payload length"))
|
|---|
| 136 | t->options[_T("payload length")] = strEx::itos(payload_length);
|
|---|
| 137 | if (!t->has_option("time offset"))
|
|---|
| 138 | t->options[_T("time offset")] = utf8::cvt<std::wstring>(delay);
|
|---|
| 139 | if (!t->has_option("password"))
|
|---|
| 140 | t->options[_T("password")] = password;
|
|---|
| 141 | if (!t->address.empty())
|
|---|
| 142 | t->address = _T("nsca://") + nscahost + _T(":") + strEx::itos(nscaport);
|
|---|
| 143 | targets.add(*t);
|
|---|
| 144 | } else {
|
|---|
| 145 | NSC_LOG_ERROR(_T("Default target not found!"));
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | } catch (nscapi::nscapi_exception &e) {
|
|---|
| 149 | NSC_LOG_ERROR_STD(_T("NSClient API exception: ") + utf8::to_unicode(e.what()));
|
|---|
| 150 | return false;
|
|---|
| 151 | } catch (std::exception &e) {
|
|---|
| 152 | NSC_LOG_ERROR_STD(_T("Exception caught: ") + utf8::to_unicode(e.what()));
|
|---|
| 153 | return false;
|
|---|
| 154 | } catch (...) {
|
|---|
| 155 | NSC_LOG_ERROR_STD(_T("Exception caught: <UNKNOWN EXCEPTION>"));
|
|---|
| 156 | return false;
|
|---|
| 157 | }
|
|---|
| 158 | return true;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | std::wstring NSCAAgent::getCryptos() {
|
|---|
| 162 | std::wstring ret = _T("{");
|
|---|
| 163 | for (int i=0;i<LAST_ENCRYPTION_ID;i++) {
|
|---|
| 164 | if (nsca::nsca_encrypt::hasEncryption(i)) {
|
|---|
| 165 | std::wstring name;
|
|---|
| 166 | try {
|
|---|
| 167 | nsca::nsca_encrypt::any_encryption *core = nsca::nsca_encrypt::get_encryption_core(i);
|
|---|
| 168 | if (core == NULL)
|
|---|
| 169 | name = _T("Broken<NULL>");
|
|---|
| 170 | else
|
|---|
| 171 | name = utf8::to_unicode(core->getName());
|
|---|
| 172 | } catch (nsca::nsca_encrypt::encryption_exception &e) {
|
|---|
| 173 | name = utf8::to_unicode(e.what());
|
|---|
| 174 | }
|
|---|
| 175 | if (ret.size() > 1)
|
|---|
| 176 | ret += _T(", ");
|
|---|
| 177 | ret += strEx::itos(i) + _T("=") + name;
|
|---|
| 178 | }
|
|---|
| 179 | }
|
|---|
| 180 | return ret + _T("}");
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | std::string get_command(std::string alias, std::string command = "") {
|
|---|
| 184 | if (!alias.empty())
|
|---|
| 185 | return alias;
|
|---|
| 186 | if (!command.empty())
|
|---|
| 187 | return command;
|
|---|
| 188 | return "host_check";
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 192 | // Settings helpers
|
|---|
| 193 | //
|
|---|
| 194 |
|
|---|
| 195 | void NSCAAgent::add_target(std::wstring key, std::wstring arg) {
|
|---|
| 196 | try {
|
|---|
| 197 | targets.add(get_settings_proxy(), target_path , key, arg);
|
|---|
| 198 | } catch (...) {
|
|---|
| 199 | NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key);
|
|---|
| 200 | }
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | void NSCAAgent::add_command(std::wstring name, std::wstring args) {
|
|---|
| 204 | try {
|
|---|
| 205 | std::wstring key = commands.add_command(name, args);
|
|---|
| 206 | if (!key.empty())
|
|---|
| 207 | register_command(key.c_str(), _T("NSCA relay for: ") + name);
|
|---|
| 208 | } catch (boost::program_options::validation_error &e) {
|
|---|
| 209 | NSC_LOG_ERROR_STD(_T("Could not add command ") + name + _T(": ") + utf8::to_unicode(e.what()));
|
|---|
| 210 | } catch (...) {
|
|---|
| 211 | NSC_LOG_ERROR_STD(_T("Could not add command ") + name);
|
|---|
| 212 | }
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | /**
|
|---|
| 216 | * Unload (terminate) module.
|
|---|
| 217 | * Attempt to stop the background processing thread.
|
|---|
| 218 | * @return true if successfully, false if not (if not things might be bad)
|
|---|
| 219 | */
|
|---|
| 220 | bool NSCAAgent::unloadModule() {
|
|---|
| 221 | return true;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | NSCAPI::nagiosReturn NSCAAgent::handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf) {
|
|---|
| 225 | std::wstring cmd = client::command_line_parser::parse_command(command, _T("nsca"));
|
|---|
| 226 |
|
|---|
| 227 | client::configuration config;
|
|---|
| 228 | setup(config);
|
|---|
| 229 | if (cmd == _T("query"))
|
|---|
| 230 | return client::command_line_parser::query(config, cmd, arguments, message, perf);
|
|---|
| 231 | if (cmd == _T("submit")) {
|
|---|
| 232 | boost::tuple<int,std::wstring> result = client::command_line_parser::simple_submit(config, cmd, arguments);
|
|---|
| 233 | message = result.get<1>();
|
|---|
| 234 | return result.get<0>();
|
|---|
| 235 | }
|
|---|
| 236 | if (cmd == _T("exec")) {
|
|---|
| 237 | return client::command_line_parser::exec(config, cmd, arguments, message);
|
|---|
| 238 | }
|
|---|
| 239 | return commands.exec_simple(config, target, command, arguments, message, perf);
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | int NSCAAgent::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) {
|
|---|
| 243 | std::wstring cmd = client::command_line_parser::parse_command(command, _T("nsca"));
|
|---|
| 244 | if (!client::command_line_parser::is_command(cmd))
|
|---|
| 245 | return NSCAPI::returnIgnored;
|
|---|
| 246 |
|
|---|
| 247 | client::configuration config;
|
|---|
| 248 | setup(config);
|
|---|
| 249 | return client::command_line_parser::commandLineExec(config, cmd, arguments, result);
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | NSCAPI::nagiosReturn NSCAAgent::handleRAWNotification(const wchar_t* channel, std::string request, std::string &response) {
|
|---|
| 253 | try {
|
|---|
| 254 | client::configuration config;
|
|---|
| 255 | setup(config);
|
|---|
| 256 |
|
|---|
| 257 | if (!client::command_line_parser::relay_submit(config, request, response)) {
|
|---|
| 258 | NSC_LOG_ERROR_STD(_T("Failed to submit message..."));
|
|---|
| 259 | return NSCAPI::hasFailed;
|
|---|
| 260 | }
|
|---|
| 261 | return NSCAPI::isSuccess;
|
|---|
| 262 | } catch (nsca::nsca_encrypt::encryption_exception &e) {
|
|---|
| 263 | NSC_LOG_ERROR_STD(_T("Failed to encrypt data: ") + utf8::to_unicode(e.what()));
|
|---|
| 264 | return NSCAPI::hasFailed;
|
|---|
| 265 | } catch (std::exception &e) {
|
|---|
| 266 | NSC_LOG_ERROR_STD(_T("Failed to send data: ") + utf8::to_unicode(e.what()));
|
|---|
| 267 | return NSCAPI::hasFailed;
|
|---|
| 268 | } catch (...) {
|
|---|
| 269 | NSC_LOG_ERROR_STD(_T("Failed to send data: UNKNOWN"));
|
|---|
| 270 | return NSCAPI::hasFailed;
|
|---|
| 271 | }
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 275 | // Parser setup/Helpers
|
|---|
| 276 | //
|
|---|
| 277 |
|
|---|
| 278 | void NSCAAgent::add_local_options(po::options_description &desc, client::configuration::data_type data) {
|
|---|
| 279 | desc.add_options()
|
|---|
| 280 | ("encryption,e", po::value<std::string>()->notifier(boost::bind(&nscapi::functions::destination_container::set_string_data, &data->recipient, "encryption", _1)),
|
|---|
| 281 | "Length of payload (has to be same as on the server)")
|
|---|
| 282 |
|
|---|
| 283 | ("buffer-length,l", po::value<unsigned int>()->notifier(boost::bind(&nscapi::functions::destination_container::set_int_data, &data->recipient, "payload length", _1)),
|
|---|
| 284 | "Length of payload (has to be same as on the server)")
|
|---|
| 285 |
|
|---|
| 286 | ("timeout", po::value<unsigned int>()->notifier(boost::bind(&nscapi::functions::destination_container::set_int_data, &data->recipient, "timeout", _1)),
|
|---|
| 287 | "")
|
|---|
| 288 |
|
|---|
| 289 | ("password", po::value<std::string>()->notifier(boost::bind(&nscapi::functions::destination_container::set_string_data, &data->recipient, "password", _1)),
|
|---|
| 290 | "Password")
|
|---|
| 291 |
|
|---|
| 292 | ("time-offset", po::value<std::string>()->notifier(boost::bind(&nscapi::functions::destination_container::set_string_data, &data->recipient, "time offset", _1)),
|
|---|
| 293 | "")
|
|---|
| 294 | ;
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | void NSCAAgent::setup(client::configuration &config) {
|
|---|
| 298 | boost::shared_ptr<clp_handler_impl> handler = boost::shared_ptr<clp_handler_impl>(new clp_handler_impl(this));
|
|---|
| 299 | add_local_options(config.local, config.data);
|
|---|
| 300 |
|
|---|
| 301 | net::wurl url;
|
|---|
| 302 | url.protocol = _T("nsca");
|
|---|
| 303 | url.port = 5667;
|
|---|
| 304 | nscapi::target_handler::optarget opt = targets.find_target(_T("default"));
|
|---|
| 305 | if (opt) {
|
|---|
| 306 | nscapi::target_handler::target t = *opt;
|
|---|
| 307 | url.host = t.host;
|
|---|
| 308 | if (t.has_option("port")) {
|
|---|
| 309 | try {
|
|---|
| 310 | url.port = strEx::stoi(t.options[_T("port")]);
|
|---|
| 311 | } catch (...) {}
|
|---|
| 312 | }
|
|---|
| 313 | std::string keys[] = {"encryption", "timeout", "payload length", "password", "time offset"};
|
|---|
| 314 | BOOST_FOREACH(std::string s, keys) {
|
|---|
| 315 | config.data->recipient.data[s] = utf8::cvt<std::string>(t.options[utf8::cvt<std::wstring>(s)]);
|
|---|
| 316 | }
|
|---|
| 317 | }
|
|---|
| 318 | config.data->recipient.id = "default";
|
|---|
| 319 | config.data->recipient.address = utf8::cvt<std::string>(url.to_string());
|
|---|
| 320 | config.data->host_self.id = "self";
|
|---|
| 321 | config.data->host_self.host = hostname_;
|
|---|
| 322 |
|
|---|
| 323 | config.target_lookup = handler;
|
|---|
| 324 | config.handler = handler;
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | NSCAAgent::connection_data NSCAAgent::parse_header(const ::Plugin::Common_Header &header) {
|
|---|
| 328 | nscapi::functions::destination_container recipient, sender;
|
|---|
| 329 | nscapi::functions::parse_destination(header, header.recipient_id(), recipient, true);
|
|---|
| 330 | nscapi::functions::parse_destination(header, header.sender_id(), sender, true);
|
|---|
| 331 | return connection_data(recipient, sender);
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 335 | // Parser implementations
|
|---|
| 336 | //
|
|---|
| 337 |
|
|---|
| 338 | int NSCAAgent::clp_handler_impl::query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) {
|
|---|
| 339 | NSCAPI::nagiosReturn ret = NSCAPI::returnOK;
|
|---|
| 340 | try {
|
|---|
| 341 | Plugin::QueryRequestMessage request_message;
|
|---|
| 342 | request_message.ParseFromString(request);
|
|---|
| 343 | connection_data con = parse_header(*header);
|
|---|
| 344 |
|
|---|
| 345 | std::list<nsca::packet> list;
|
|---|
| 346 | for (int i=0;i < request_message.payload_size(); ++i) {
|
|---|
| 347 | nsca::packet packet(con.sender_hostname, con.buffer_length, con.time_delta);
|
|---|
| 348 | nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(request_message.payload(i));
|
|---|
| 349 | packet.code = 0;
|
|---|
| 350 | packet.result = utf8::cvt<std::string>(data.command);
|
|---|
| 351 | list.push_back(packet);
|
|---|
| 352 | }
|
|---|
| 353 |
|
|---|
| 354 | boost::tuple<int,std::wstring> ret = instance->send(con, list);
|
|---|
| 355 | nscapi::functions::create_simple_query_response(_T("UNKNOWN"), ret.get<0>(), ret.get<1>(), _T(""), reply);
|
|---|
| 356 | return NSCAPI::isSuccess;
|
|---|
| 357 | } catch (std::exception &e) {
|
|---|
| 358 | NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::to_unicode(e.what()));
|
|---|
| 359 | nscapi::functions::create_simple_query_response(_T("command"), NSCAPI::returnUNKNOWN, _T("Exception: ") + utf8::to_unicode(e.what()), _T(""), reply);
|
|---|
| 360 | return NSCAPI::returnUNKNOWN;
|
|---|
| 361 | }
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | int NSCAAgent::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) {
|
|---|
| 365 | std::wstring channel;
|
|---|
| 366 | try {
|
|---|
| 367 | Plugin::SubmitRequestMessage message;
|
|---|
| 368 | message.ParseFromString(request);
|
|---|
| 369 |
|
|---|
| 370 | connection_data con = parse_header(*header);
|
|---|
| 371 | channel = utf8::cvt<std::wstring>(message.channel());
|
|---|
| 372 |
|
|---|
| 373 | std::list<nsca::packet> list;
|
|---|
| 374 |
|
|---|
| 375 | for (int i=0;i < message.payload_size(); ++i) {
|
|---|
| 376 | nsca::packet packet(con.sender_hostname, con.buffer_length, con.time_delta);
|
|---|
| 377 | std::wstring alias, msg;
|
|---|
| 378 | packet.code = nscapi::functions::parse_simple_submit_request_payload(message.payload(i), alias, msg);
|
|---|
| 379 | if (alias != _T("host_check"))
|
|---|
| 380 | packet.service = utf8::cvt<std::string>(alias);
|
|---|
| 381 | packet.result = utf8::cvt<std::string>(msg);
|
|---|
| 382 | list.push_back(packet);
|
|---|
| 383 | }
|
|---|
| 384 |
|
|---|
| 385 | boost::tuple<int,std::wstring> ret = instance->send(con, list);
|
|---|
| 386 | nscapi::functions::create_simple_submit_response(channel, _T("UNKNOWN"), ret.get<0>(), ret.get<1>(), reply);
|
|---|
| 387 | return NSCAPI::isSuccess;
|
|---|
| 388 | } catch (std::exception &e) {
|
|---|
| 389 | NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::to_unicode(e.what()));
|
|---|
| 390 | nscapi::functions::create_simple_submit_response(channel, _T("UNKNOWN"), NSCAPI::returnUNKNOWN, utf8::to_unicode(e.what()), reply);
|
|---|
| 391 | return NSCAPI::hasFailed;
|
|---|
| 392 | }
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | int NSCAAgent::clp_handler_impl::exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) {
|
|---|
| 396 | NSCAPI::nagiosReturn ret = NSCAPI::returnOK;
|
|---|
| 397 | try {
|
|---|
| 398 | Plugin::ExecuteRequestMessage request_message;
|
|---|
| 399 | request_message.ParseFromString(request);
|
|---|
| 400 | connection_data con = parse_header(*header);
|
|---|
| 401 |
|
|---|
| 402 | std::list<nsca::packet> list;
|
|---|
| 403 | for (int i=0;i < request_message.payload_size(); ++i) {
|
|---|
| 404 | nsca::packet packet(con.sender_hostname, con.buffer_length, con.time_delta);
|
|---|
| 405 | nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request_payload(request_message.payload(i));
|
|---|
| 406 | packet.code = 0;
|
|---|
| 407 | if (data.command != _T("host_check"))
|
|---|
| 408 | packet.service = utf8::cvt<std::string>(data.command);
|
|---|
| 409 | //packet.result = data.;
|
|---|
| 410 | list.push_back(packet);
|
|---|
| 411 | }
|
|---|
| 412 | boost::tuple<int,std::wstring> ret = instance->send(con, list);
|
|---|
| 413 | nscapi::functions::create_simple_exec_response(_T("UNKNOWN"), ret.get<0>(), ret.get<1>(), reply);
|
|---|
| 414 | return NSCAPI::isSuccess;
|
|---|
| 415 | } catch (std::exception &e) {
|
|---|
| 416 | NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::to_unicode(e.what()));
|
|---|
| 417 | nscapi::functions::create_simple_exec_response(_T("command"), NSCAPI::returnUNKNOWN, _T("Exception: ") + utf8::to_unicode(e.what()), reply);
|
|---|
| 418 | return NSCAPI::hasFailed;
|
|---|
| 419 | }
|
|---|
| 420 | }
|
|---|
| 421 |
|
|---|
| 422 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 423 | // Protocol implementations
|
|---|
| 424 |
|
|---|
| 425 | boost::tuple<int,std::wstring> NSCAAgent::send(connection_data data, const std::list<nsca::packet> packets) {
|
|---|
| 426 | try {
|
|---|
| 427 | NSC_DEBUG_MSG_STD(_T("Connection details: ") + data.to_wstring());
|
|---|
| 428 | boost::asio::io_service io_service;
|
|---|
| 429 | nsca::socket socket(io_service);
|
|---|
| 430 | socket.connect(data.host, data.port);
|
|---|
| 431 | if (!socket.recv_iv(data.password, data.get_encryption(), boost::posix_time::seconds(data.timeout<5?30:data.timeout))) {
|
|---|
| 432 | NSC_LOG_ERROR_STD(_T("Failed to read iv"));
|
|---|
| 433 | return NSCAPI::hasFailed;
|
|---|
| 434 | }
|
|---|
| 435 | NSC_DEBUG_MSG_STD(_T("Got IV sending data: ") + strEx::itos(packets.size()));
|
|---|
| 436 | BOOST_FOREACH(const nsca::packet &packet, packets) {
|
|---|
| 437 | NSC_DEBUG_MSG_STD(_T("Sending: ") + utf8::cvt<std::wstring>(packet.to_string()));
|
|---|
| 438 | socket.send_nsca(packet, boost::posix_time::seconds(data.timeout));
|
|---|
| 439 | }
|
|---|
| 440 | return NSCAPI::isSuccess;
|
|---|
| 441 | } catch (nsca::nsca_encrypt::encryption_exception &e) {
|
|---|
| 442 | NSC_LOG_ERROR_STD(_T("NSCA Error: ") + utf8::to_unicode(e.what()));
|
|---|
| 443 | return boost::make_tuple(NSCAPI::returnUNKNOWN, _T("NSCA error: ") + utf8::to_unicode(e.what()));
|
|---|
| 444 | } catch (std::runtime_error &e) {
|
|---|
| 445 | NSC_LOG_ERROR_STD(_T("Socket error: ") + utf8::to_unicode(e.what()));
|
|---|
| 446 | return boost::make_tuple(NSCAPI::returnUNKNOWN, _T("Socket error: ") + utf8::to_unicode(e.what()));
|
|---|
| 447 | } catch (std::exception &e) {
|
|---|
| 448 | NSC_LOG_ERROR_STD(_T("Error: ") + utf8::to_unicode(e.what()));
|
|---|
| 449 | return boost::make_tuple(NSCAPI::returnUNKNOWN, _T("Error: ") + utf8::to_unicode(e.what()));
|
|---|
| 450 | } catch (...) {
|
|---|
| 451 | return boost::make_tuple(NSCAPI::returnUNKNOWN, _T("Unknown error -- REPORT THIS!"));
|
|---|
| 452 | }
|
|---|
| 453 | }
|
|---|
| 454 |
|
|---|
| 455 | NSC_WRAP_DLL();
|
|---|
| 456 | NSC_WRAPPERS_MAIN_DEF(NSCAAgent);
|
|---|
| 457 | NSC_WRAPPERS_IGNORE_MSG_DEF();
|
|---|
| 458 | NSC_WRAPPERS_HANDLE_CMD_DEF();
|
|---|
| 459 | NSC_WRAPPERS_CLI_DEF();
|
|---|
| 460 | NSC_WRAPPERS_HANDLE_NOTIFICATION_DEF();
|
|---|
| 461 |
|
|---|