| 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::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) {
|
|---|
| 225 | nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request);
|
|---|
| 226 | std::wstring cmd = client::command_line_parser::parse_command(data.command, _T("syslog"));
|
|---|
| 227 | client::configuration config;
|
|---|
| 228 | setup(config);
|
|---|
| 229 | if (!client::command_line_parser::is_command(cmd))
|
|---|
| 230 | return client::command_line_parser::do_execute_command_as_query(config, cmd, data.args, result);
|
|---|
| 231 | return commands.exec_simple(config, data.target, char_command, data.args, result);
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | NSCAPI::nagiosReturn NSCAAgent::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &result) {
|
|---|
| 235 | nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request);
|
|---|
| 236 | std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("syslog"));
|
|---|
| 237 | if (!client::command_line_parser::is_command(cmd))
|
|---|
| 238 | return NSCAPI::returnIgnored;
|
|---|
| 239 | client::configuration config;
|
|---|
| 240 | setup(config);
|
|---|
| 241 | return client::command_line_parser::do_execute_command_as_exec(config, cmd, data.args, result);
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | NSCAPI::nagiosReturn NSCAAgent::handleRAWNotification(const wchar_t* channel, std::string request, std::string &result) {
|
|---|
| 245 | client::configuration config;
|
|---|
| 246 | setup(config);
|
|---|
| 247 | return client::command_line_parser::do_relay_submit(config, request, result);
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 251 | // Parser setup/Helpers
|
|---|
| 252 | //
|
|---|
| 253 |
|
|---|
| 254 | void NSCAAgent::add_local_options(po::options_description &desc, client::configuration::data_type data) {
|
|---|
| 255 | desc.add_options()
|
|---|
| 256 | ("encryption,e", po::value<std::string>()->notifier(boost::bind(&nscapi::functions::destination_container::set_string_data, &data->recipient, "encryption", _1)),
|
|---|
| 257 | "Length of payload (has to be same as on the server)")
|
|---|
| 258 |
|
|---|
| 259 | ("buffer-length,l", po::value<unsigned int>()->notifier(boost::bind(&nscapi::functions::destination_container::set_int_data, &data->recipient, "payload length", _1)),
|
|---|
| 260 | "Length of payload (has to be same as on the server)")
|
|---|
| 261 |
|
|---|
| 262 | ("timeout", po::value<unsigned int>()->notifier(boost::bind(&nscapi::functions::destination_container::set_int_data, &data->recipient, "timeout", _1)),
|
|---|
| 263 | "")
|
|---|
| 264 |
|
|---|
| 265 | ("password", po::value<std::string>()->notifier(boost::bind(&nscapi::functions::destination_container::set_string_data, &data->recipient, "password", _1)),
|
|---|
| 266 | "Password")
|
|---|
| 267 |
|
|---|
| 268 | ("time-offset", po::value<std::string>()->notifier(boost::bind(&nscapi::functions::destination_container::set_string_data, &data->recipient, "time offset", _1)),
|
|---|
| 269 | "")
|
|---|
| 270 | ;
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | void NSCAAgent::setup(client::configuration &config) {
|
|---|
| 274 | boost::shared_ptr<clp_handler_impl> handler = boost::shared_ptr<clp_handler_impl>(new clp_handler_impl(this));
|
|---|
| 275 | add_local_options(config.local, config.data);
|
|---|
| 276 |
|
|---|
| 277 | net::wurl url;
|
|---|
| 278 | url.protocol = _T("nsca");
|
|---|
| 279 | url.port = 5667;
|
|---|
| 280 | nscapi::target_handler::optarget opt = targets.find_target(_T("default"));
|
|---|
| 281 | if (opt) {
|
|---|
| 282 | nscapi::target_handler::target t = *opt;
|
|---|
| 283 | url.host = t.host;
|
|---|
| 284 | if (t.has_option("port")) {
|
|---|
| 285 | try {
|
|---|
| 286 | url.port = strEx::stoi(t.options[_T("port")]);
|
|---|
| 287 | } catch (...) {}
|
|---|
| 288 | }
|
|---|
| 289 | std::string keys[] = {"encryption", "timeout", "payload length", "password", "time offset"};
|
|---|
| 290 | BOOST_FOREACH(std::string s, keys) {
|
|---|
| 291 | config.data->recipient.data[s] = utf8::cvt<std::string>(t.options[utf8::cvt<std::wstring>(s)]);
|
|---|
| 292 | }
|
|---|
| 293 | }
|
|---|
| 294 | config.data->recipient.id = "default";
|
|---|
| 295 | config.data->recipient.address = utf8::cvt<std::string>(url.to_string());
|
|---|
| 296 | config.data->host_self.id = "self";
|
|---|
| 297 | config.data->host_self.host = hostname_;
|
|---|
| 298 |
|
|---|
| 299 | config.target_lookup = handler;
|
|---|
| 300 | config.handler = handler;
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | NSCAAgent::connection_data NSCAAgent::parse_header(const ::Plugin::Common_Header &header) {
|
|---|
| 304 | nscapi::functions::destination_container recipient, sender;
|
|---|
| 305 | nscapi::functions::parse_destination(header, header.recipient_id(), recipient, true);
|
|---|
| 306 | nscapi::functions::parse_destination(header, header.sender_id(), sender, true);
|
|---|
| 307 | return connection_data(recipient, sender);
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 311 | // Parser implementations
|
|---|
| 312 | //
|
|---|
| 313 |
|
|---|
| 314 | int NSCAAgent::clp_handler_impl::query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) {
|
|---|
| 315 | Plugin::QueryRequestMessage request_message;
|
|---|
| 316 | request_message.ParseFromString(request);
|
|---|
| 317 | connection_data con = parse_header(*header);
|
|---|
| 318 |
|
|---|
| 319 | Plugin::QueryResponseMessage response_message;
|
|---|
| 320 | nscapi::functions::make_return_header(response_message.mutable_header(), *header);
|
|---|
| 321 |
|
|---|
| 322 | std::list<nsca::packet> list;
|
|---|
| 323 | for (int i=0;i < request_message.payload_size(); ++i) {
|
|---|
| 324 | nsca::packet packet(con.sender_hostname, con.buffer_length, con.time_delta);
|
|---|
| 325 | nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(request_message.payload(i));
|
|---|
| 326 | packet.code = 0;
|
|---|
| 327 | packet.result = utf8::cvt<std::string>(data.command);
|
|---|
| 328 | list.push_back(packet);
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | boost::tuple<int,std::wstring> ret = instance->send(con, list);
|
|---|
| 332 |
|
|---|
| 333 | nscapi::functions::append_simple_query_response_payload(response_message.add_payload(), "TODO", ret.get<0>(), utf8::cvt<std::string>(ret.get<1>()), "");
|
|---|
| 334 | response_message.SerializeToString(&reply);
|
|---|
| 335 | return NSCAPI::isSuccess;
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | int NSCAAgent::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) {
|
|---|
| 339 | Plugin::SubmitRequestMessage message;
|
|---|
| 340 | message.ParseFromString(request);
|
|---|
| 341 | connection_data con = parse_header(*header);
|
|---|
| 342 | std::wstring channel = utf8::cvt<std::wstring>(message.channel());
|
|---|
| 343 |
|
|---|
| 344 | Plugin::SubmitResponseMessage response_message;
|
|---|
| 345 | nscapi::functions::make_return_header(response_message.mutable_header(), *header);
|
|---|
| 346 |
|
|---|
| 347 | std::list<nsca::packet> list;
|
|---|
| 348 |
|
|---|
| 349 | for (int i=0;i < message.payload_size(); ++i) {
|
|---|
| 350 | nsca::packet packet(con.sender_hostname, con.buffer_length, con.time_delta);
|
|---|
| 351 | std::wstring alias, msg;
|
|---|
| 352 | packet.code = nscapi::functions::parse_simple_submit_request_payload(message.payload(i), alias, msg);
|
|---|
| 353 | if (alias != _T("host_check"))
|
|---|
| 354 | packet.service = utf8::cvt<std::string>(alias);
|
|---|
| 355 | packet.result = utf8::cvt<std::string>(msg);
|
|---|
| 356 | list.push_back(packet);
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | boost::tuple<int,std::wstring> ret = instance->send(con, list);
|
|---|
| 360 | nscapi::functions::append_simple_submit_response_payload(response_message.add_payload(), "TODO", ret.get<0>(), utf8::cvt<std::string>(ret.get<1>()));
|
|---|
| 361 | response_message.SerializeToString(&reply);
|
|---|
| 362 | return NSCAPI::isSuccess;
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | int NSCAAgent::clp_handler_impl::exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) {
|
|---|
| 366 | Plugin::ExecuteRequestMessage request_message;
|
|---|
| 367 | request_message.ParseFromString(request);
|
|---|
| 368 | connection_data con = parse_header(*header);
|
|---|
| 369 |
|
|---|
| 370 | Plugin::ExecuteResponseMessage response_message;
|
|---|
| 371 | nscapi::functions::make_return_header(response_message.mutable_header(), *header);
|
|---|
| 372 |
|
|---|
| 373 | std::list<nsca::packet> list;
|
|---|
| 374 | for (int i=0;i < request_message.payload_size(); ++i) {
|
|---|
| 375 | nsca::packet packet(con.sender_hostname, con.buffer_length, con.time_delta);
|
|---|
| 376 | nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request_payload(request_message.payload(i));
|
|---|
| 377 | packet.code = 0;
|
|---|
| 378 | if (data.command != _T("host_check"))
|
|---|
| 379 | packet.service = utf8::cvt<std::string>(data.command);
|
|---|
| 380 | //packet.result = data.;
|
|---|
| 381 | list.push_back(packet);
|
|---|
| 382 | }
|
|---|
| 383 | boost::tuple<int,std::wstring> ret = instance->send(con, list);
|
|---|
| 384 | nscapi::functions::append_simple_exec_response_payload(response_message.add_payload(), "TODO", ret.get<0>(), utf8::cvt<std::string>(ret.get<1>()));
|
|---|
| 385 | response_message.SerializeToString(&reply);
|
|---|
| 386 | return NSCAPI::isSuccess;
|
|---|
| 387 | }
|
|---|
| 388 |
|
|---|
| 389 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 390 | // Protocol implementations
|
|---|
| 391 | //
|
|---|
| 392 |
|
|---|
| 393 | boost::tuple<int,std::wstring> NSCAAgent::send(connection_data data, const std::list<nsca::packet> packets) {
|
|---|
| 394 | try {
|
|---|
| 395 | NSC_DEBUG_MSG_STD(_T("Connection details: ") + data.to_wstring());
|
|---|
| 396 | boost::asio::io_service io_service;
|
|---|
| 397 | nsca::socket socket(io_service);
|
|---|
| 398 | socket.connect(data.host, data.port);
|
|---|
| 399 | if (!socket.recv_iv(data.password, data.get_encryption(), boost::posix_time::seconds(data.timeout<5?30:data.timeout))) {
|
|---|
| 400 | NSC_LOG_ERROR_STD(_T("Failed to read iv"));
|
|---|
| 401 | return NSCAPI::hasFailed;
|
|---|
| 402 | }
|
|---|
| 403 | NSC_DEBUG_MSG_STD(_T("Got IV sending data: ") + strEx::itos(packets.size()));
|
|---|
| 404 | BOOST_FOREACH(const nsca::packet &packet, packets) {
|
|---|
| 405 | NSC_DEBUG_MSG_STD(_T("Sending: ") + utf8::cvt<std::wstring>(packet.to_string()));
|
|---|
| 406 | socket.send_nsca(packet, boost::posix_time::seconds(data.timeout));
|
|---|
| 407 | }
|
|---|
| 408 | return boost::make_tuple(NSCAPI::returnUNKNOWN, _T(""));
|
|---|
| 409 | } catch (nsca::nsca_encrypt::encryption_exception &e) {
|
|---|
| 410 | NSC_LOG_ERROR_STD(_T("NSCA Error: ") + utf8::to_unicode(e.what()));
|
|---|
| 411 | return boost::make_tuple(NSCAPI::returnUNKNOWN, _T("NSCA error: ") + utf8::to_unicode(e.what()));
|
|---|
| 412 | } catch (std::runtime_error &e) {
|
|---|
| 413 | NSC_LOG_ERROR_STD(_T("Socket error: ") + utf8::to_unicode(e.what()));
|
|---|
| 414 | return boost::make_tuple(NSCAPI::returnUNKNOWN, _T("Socket error: ") + utf8::to_unicode(e.what()));
|
|---|
| 415 | } catch (std::exception &e) {
|
|---|
| 416 | NSC_LOG_ERROR_STD(_T("Error: ") + utf8::to_unicode(e.what()));
|
|---|
| 417 | return boost::make_tuple(NSCAPI::returnUNKNOWN, _T("Error: ") + utf8::to_unicode(e.what()));
|
|---|
| 418 | } catch (...) {
|
|---|
| 419 | return boost::make_tuple(NSCAPI::returnUNKNOWN, _T("Unknown error -- REPORT THIS!"));
|
|---|
| 420 | }
|
|---|
| 421 | }
|
|---|
| 422 |
|
|---|
| 423 | NSC_WRAP_DLL();
|
|---|
| 424 | NSC_WRAPPERS_MAIN_DEF(NSCAAgent);
|
|---|
| 425 | NSC_WRAPPERS_IGNORE_MSG_DEF();
|
|---|
| 426 | NSC_WRAPPERS_HANDLE_CMD_DEF();
|
|---|
| 427 | NSC_WRAPPERS_CLI_DEF();
|
|---|
| 428 | NSC_WRAPPERS_HANDLE_NOTIFICATION_DEF();
|
|---|
| 429 |
|
|---|