| 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 "NSCAServer.h"
|
|---|
| 23 |
|
|---|
| 24 | #include "handler_impl.hpp"
|
|---|
| 25 |
|
|---|
| 26 | #include <settings/client/settings_client.hpp>
|
|---|
| 27 |
|
|---|
| 28 | namespace sh = nscapi::settings_helper;
|
|---|
| 29 | namespace str = nscp::helpers;
|
|---|
| 30 |
|
|---|
| 31 | NSCAServer::NSCAServer() : handler_(new nsca_handler_impl(1024)) {}
|
|---|
| 32 |
|
|---|
| 33 | bool NSCAServer::loadModule() {
|
|---|
| 34 | return false;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | bool NSCAServer::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) {
|
|---|
| 38 |
|
|---|
| 39 | try {
|
|---|
| 40 |
|
|---|
| 41 | sh::settings_registry settings(get_settings_proxy());
|
|---|
| 42 | settings.set_alias(_T("NSCA"), alias, _T("server"));
|
|---|
| 43 |
|
|---|
| 44 | settings.alias().add_path_to_settings()
|
|---|
| 45 | (_T("NSCA SERVER SECTION"), _T("Section for NSCA (NSCAServer) (check_nsca) protocol options."))
|
|---|
| 46 | ;
|
|---|
| 47 |
|
|---|
| 48 | settings.alias().add_key_to_settings()
|
|---|
| 49 | (_T("port"), sh::uint_key(&info_.port, 5667),
|
|---|
| 50 | _T("PORT NUMBER"), _T("Port to use for NSCA."))
|
|---|
| 51 |
|
|---|
| 52 | (_T("payload length"), sh::int_fun_key<unsigned int>(boost::bind(&nsca::server::handler::set_payload_length, handler_, _1), 512),
|
|---|
| 53 | _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."))
|
|---|
| 54 |
|
|---|
| 55 | (_T("performance data"), sh::bool_fun_key<bool>(boost::bind(&nsca::server::handler::set_perf_data, handler_, _1), true),
|
|---|
| 56 | _T("PERFORMANCE DATA"), _T("Send performance data back to nagios (set this to 0 to remove all performance data)."))
|
|---|
| 57 |
|
|---|
| 58 | (_T("encryption"), sh::string_fun_key<std::string>(boost::bind(&nsca::server::handler::set_encryption, handler_, _1), "aes"),
|
|---|
| 59 | _T("ENCRYPTION"), _T("Encryption to use"))
|
|---|
| 60 |
|
|---|
| 61 | (_T("password"), sh::string_fun_key<std::string>(boost::bind(&nsca::server::handler::set_password, handler_, _1), ""),
|
|---|
| 62 | _T("PASSWORD"), _T("Password to use"))
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 | ;
|
|---|
| 66 |
|
|---|
| 67 | settings.alias().add_parent(_T("/settings/default")).add_key_to_settings()
|
|---|
| 68 |
|
|---|
| 69 | (_T("thread pool"), sh::uint_key(&info_.thread_pool_size, 10),
|
|---|
| 70 | _T("THREAD POOL"), _T(""))
|
|---|
| 71 |
|
|---|
| 72 | (_T("bind to"), sh::string_key(&info_.address),
|
|---|
| 73 | _T("BIND TO ADDRESS"), _T("Allows you to bind server to a specific local address. This has to be a dotted ip address not a host name. Leaving this blank will bind to all available IP addresses."))
|
|---|
| 74 |
|
|---|
| 75 | (_T("socket queue size"), sh::int_key(&info_.back_log, 0),
|
|---|
| 76 | _T("LISTEN QUEUE"), _T("Number of sockets to queue before starting to refuse new incoming connections. This can be used to tweak the amount of simultaneous sockets that the server accepts."))
|
|---|
| 77 |
|
|---|
| 78 | (_T("allowed hosts"), sh::string_fun_key<std::wstring>(boost::bind(&socket_helpers::allowed_hosts_manager::set_source, &info_.allowed_hosts, _1), _T("127.0.0.1")),
|
|---|
| 79 | _T("ALLOWED HOSTS"), _T("A comaseparated list of allowed hosts. You can use netmasks (/ syntax) or * to create ranges."))
|
|---|
| 80 |
|
|---|
| 81 | (_T("cache allowed hosts"), sh::bool_key(&info_.allowed_hosts.cached, true),
|
|---|
| 82 | _T("CACHE ALLOWED HOSTS"), _T("If hostnames should be cached, improves speed and security somewhat but wont allow you to have dynamic IPs for your nagios server."))
|
|---|
| 83 |
|
|---|
| 84 | (_T("timeout"), sh::uint_key(&info_.timeout, 30),
|
|---|
| 85 | _T("TIMEOUT"), _T("Timeout when reading packets on incoming sockets. If the data has not arrived within this time we will bail out."))
|
|---|
| 86 |
|
|---|
| 87 | (_T("inbox"), sh::string_fun_key<std::wstring>(boost::bind(&nsca::server::handler::set_channel, handler_, _1), _T("inbox")),
|
|---|
| 88 | _T("INBOX"), _T("The default channel to post incoming messages on"))
|
|---|
| 89 |
|
|---|
| 90 | ;
|
|---|
| 91 |
|
|---|
| 92 | settings.register_all();
|
|---|
| 93 | settings.notify();
|
|---|
| 94 |
|
|---|
| 95 | if (handler_->get_payload_length() != 512)
|
|---|
| 96 | NSC_DEBUG_MSG_STD(_T("Non-standard buffer length (hope you have recompiled check_nsca changing #define MAX_PACKETBUFFER_LENGTH = ") + strEx::itos(handler_->get_payload_length()));
|
|---|
| 97 |
|
|---|
| 98 | std::list<std::string> errors;
|
|---|
| 99 | info_.allowed_hosts.refresh(errors);
|
|---|
| 100 | BOOST_FOREACH(const std::string &e, errors) {
|
|---|
| 101 | NSC_LOG_ERROR_STD(utf8::cvt<std::wstring>(e));
|
|---|
| 102 | }
|
|---|
| 103 | NSC_DEBUG_MSG_STD(_T("Allowed hosts definition: ") + info_.allowed_hosts.to_wstring());
|
|---|
| 104 | NSC_LOG_ERROR_LISTW(info_.validate());
|
|---|
| 105 |
|
|---|
| 106 | if (mode == NSCAPI::normalStart) {
|
|---|
| 107 |
|
|---|
| 108 | server_.reset(new nsca::server::server(boost::shared_ptr<nsca::read_protocol>(new nsca::read_protocol(info_, handler_))));
|
|---|
| 109 | if (!server_) {
|
|---|
| 110 | NSC_LOG_ERROR_STD(_T("Failed to create server instance!"));
|
|---|
| 111 | return false;
|
|---|
| 112 | }
|
|---|
| 113 | //server_->setup();
|
|---|
| 114 | server_->start();
|
|---|
| 115 | }
|
|---|
| 116 | } catch (std::exception &e) {
|
|---|
| 117 | NSC_LOG_ERROR_STD(_T("Exception caught: ") + str::to_wstring(e.what()));
|
|---|
| 118 | return false;
|
|---|
| 119 | } catch (...) {
|
|---|
| 120 | NSC_LOG_ERROR_STD(_T("Exception caught: <UNKNOWN EXCEPTION>"));
|
|---|
| 121 | return false;
|
|---|
| 122 | }
|
|---|
| 123 | return true;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | bool NSCAServer::unloadModule() {
|
|---|
| 127 | try {
|
|---|
| 128 | if (server_) {
|
|---|
| 129 | server_->stop();
|
|---|
| 130 | server_.reset();
|
|---|
| 131 | }
|
|---|
| 132 | } catch (...) {
|
|---|
| 133 | NSC_LOG_ERROR_STD(_T("Exception caught: <UNKNOWN>"));
|
|---|
| 134 | return false;
|
|---|
| 135 | }
|
|---|
| 136 | return true;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 | std::wstring NSCAServer::getCryptos() {
|
|---|
| 141 | std::wstring ret = _T("{");
|
|---|
| 142 | for (int i=0;i<LAST_ENCRYPTION_ID;i++) {
|
|---|
| 143 | if (nsca::nsca_encrypt::hasEncryption(i)) {
|
|---|
| 144 | std::wstring name;
|
|---|
| 145 | try {
|
|---|
| 146 | nsca::nsca_encrypt::any_encryption *core = nsca::nsca_encrypt::get_encryption_core(i);
|
|---|
| 147 | if (core == NULL)
|
|---|
| 148 | name = _T("Broken<NULL>");
|
|---|
| 149 | else
|
|---|
| 150 | name = str::to_wstring(core->getName());
|
|---|
| 151 | } catch (nsca::nsca_encrypt::encryption_exception &e) {
|
|---|
| 152 | name = str::to_wstring(e.what());
|
|---|
| 153 | }
|
|---|
| 154 | if (ret.size() > 1)
|
|---|
| 155 | ret += _T(", ");
|
|---|
| 156 | ret += strEx::itos(i) + _T("=") + name;
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|
| 159 | return ret + _T("}");
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 | NSC_WRAP_DLL();
|
|---|
| 164 | NSC_WRAPPERS_MAIN_DEF(NSCAServer);
|
|---|
| 165 | NSC_WRAPPERS_IGNORE_MSG_DEF();
|
|---|
| 166 | NSC_WRAPPERS_IGNORE_CMD_DEF();
|
|---|