Changeset e396b2f in nscp
- Timestamp:
- 02/05/12 22:37:50 (16 months ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- 0f7b655
- Parents:
- a8c6e93
- Files:
-
- 29 edited
-
changelog (modified) (1 diff)
-
include/client/command_line_parser.cpp (modified) (2 diffs)
-
include/net/net.hpp (modified) (1 diff)
-
include/nrpe/server/connection.cpp (modified) (1 diff)
-
include/nrpe/server/parser.hpp (modified) (1 diff)
-
include/nrpe/server/ssl_connection.cpp (modified) (1 diff)
-
include/nscapi/functions.hpp (modified) (9 diffs)
-
include/nscapi/targets.cpp (modified) (1 diff)
-
include/nscapi/targets.hpp (modified) (2 diffs)
-
include/settings/client/settings_client.hpp (modified) (5 diffs)
-
include/settings/settings_http.hpp (modified) (1 diff)
-
include/settings/settings_interface_impl.hpp (modified) (7 diffs)
-
modules/NRPEClient/NRPEClient.cpp (modified) (9 diffs)
-
modules/NRPEClient/NRPEClient.h (modified) (5 diffs)
-
modules/NSCAClient/NSCAClient.cpp (modified) (4 diffs)
-
modules/NSCAClient/NSCAClient.h (modified) (4 diffs)
-
modules/NSCPClient/NSCPClient.cpp (modified) (3 diffs)
-
modules/NSCPClient/NSCPClient.h (modified) (5 diffs)
-
modules/SMTPClient/SMTPClient.cpp (modified) (2 diffs)
-
modules/SMTPClient/SMTPClient.h (modified) (4 diffs)
-
modules/Scheduler/Scheduler.cpp (modified) (2 diffs)
-
modules/Scheduler/simple_scheduler.hpp (modified) (4 diffs)
-
modules/SyslogClient/SyslogClient.cpp (modified) (2 diffs)
-
modules/SyslogClient/SyslogClient.h (modified) (4 diffs)
-
scripts/python/test_nrpe.py (modified) (5 diffs)
-
scripts/python/test_nsca.py (modified) (2 diffs)
-
service/NSCPlugin.cpp (modified) (1 diff)
-
service/NSCPlugin.h (modified) (2 diffs)
-
service/NSClient++.cpp (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
changelog
ra8c6e93 re396b2f 5 5 * Fixa dependonservice LanManWorkStation (old win) 6 6 * Fix RtlStringFromGUID problem on NT4 7 8 9 2012-02-05 MickeM 10 * Refactored the targets concept internally to be simpler to use (less code) 11 * Fixed issue with reloading plugins 12 * Fixed target handling in NRPE Client, will add NSCA client tomorrow... 7 13 8 14 2012-02-02 MickeM -
include/client/command_line_parser.cpp
r7354aa1 re396b2f 1 1 #include <client/command_line_parser.hpp> 2 2 #include <nscapi/functions.hpp> 3 #include <boost/bind.hpp> 3 4 4 5 namespace po = boost::program_options; … … 6 7 void client::command_line_parser::add_common_options(po::options_description &desc, data_type command_data) { 7 8 desc.add_options() 8 ("host,H", po::value<std::string>(&command_data->recipient.address), "The address of the host running the server") 9 ("host,H", po::value<std::string>()->notifier(boost::bind(&nscapi::functions::destination_container::set_host, &command_data->recipient, _1)), 10 "The host of the host running the server") 11 ("port,P", po::value<std::string>()->notifier(boost::bind(&nscapi::functions::destination_container::set_port, &command_data->recipient, _1)), 12 "The port of the host running the server") 13 ("address", po::value<std::string>()->notifier(boost::bind(&nscapi::functions::destination_container::set_address, &command_data->recipient, _1)), 14 "The address (host:port) of the host running the server") 9 15 ("timeout,T", po::value<int>(&command_data->timeout), "Number of seconds before connection times out (default=10)") 10 16 ("target,t", po::wvalue<std::wstring>(&command_data->target_id), "Target to use (lookup connection info from config)") -
include/net/net.hpp
rb24c97f re396b2f 4 4 5 5 namespace net { 6 struct url { 7 std::string protocol; 8 std::string host; 9 std::string path; 10 std::string query; 6 7 struct string_traits { 8 static std::string protocol_suffix() { 9 return "://"; 10 } 11 static std::string port_prefix() { 12 return ":"; 13 } 14 }; 15 struct wstring_traits { 16 static std::wstring protocol_suffix() { 17 return _T("://"); 18 } 19 static std::wstring port_prefix() { 20 return _T(":"); 21 } 22 }; 23 24 25 template<class string_type, class stream = std::wstringstream, class traits = wstring_traits> 26 struct base_url { 27 typedef base_url<string_type, stream, traits> my_type; 28 string_type protocol; 29 string_type host; 30 string_type path; 31 string_type query; 11 32 unsigned int port; 12 std::string get_port() { 13 std::stringstream ss; 14 ss << port; 33 base_url() : port(0) {} 34 35 string_type to_string() const { 36 stream ss; 37 ss << protocol << traits::protocol_suffix() << host << traits::port_prefix() << port << path; 15 38 return ss.str(); 16 39 } 17 std::string to_string() { 18 std::stringstream ss; 19 ss << protocol << "://" << host << ":" << port << path; 20 return ss.str(); 40 41 inline int get_port() const { 42 return port; 43 } 44 inline int get_port(int default_port) const { 45 if (port == 0) 46 return default_port; 47 return port; 48 } 49 inline std::wstring get_whost(std::wstring default_host = _T("127.0.0.1")) const { 50 if (!host.empty()) 51 return utf8::cvt<std::wstring>(host); 52 return default_host; 53 } 54 inline std::string get_host(std::string default_host = "127.0.0.1") const { 55 if (!host.empty()) 56 return utf8::cvt<std::string>(host); 57 return default_host; 58 } 59 inline std::string get_port_string(std::string default_port) const { 60 if (port != 0) 61 return boost::lexical_cast<std::string>(port); 62 return default_port; 63 } 64 inline std::string get_port_string() const { 65 return boost::lexical_cast<std::string>(port); 66 } 67 68 void import(const base_url &n) { 69 70 if (protocol.empty() && !n.protocol.empty()) 71 protocol = n.protocol; 72 if (host.empty() && !n.host.empty()) 73 host = n.host; 74 if (port == 0 && n.port != 0) 75 port = n.port; 76 if (path.empty() && !n.path.empty()) 77 path = n.path; 78 if (query.empty() && !n.query.empty()) 79 query = n.query; 80 } 81 void apply(const base_url &n) { 82 if (!n.protocol.empty()) 83 protocol = n.protocol; 84 if (!n.host.empty()) 85 host = n.host; 86 if (n.port != 0) 87 port = n.port; 88 if (!n.path.empty()) 89 path = n.path; 90 if (!n.query.empty()) 91 query = n.query; 21 92 } 22 93 }; 23 struct wurl {24 std::wstring protocol;25 std::wstring host;26 std::wstring path;27 std::wstring query;28 unsigned int port;29 std::wstring to_string() {30 std::wstringstream ss;31 ss << protocol << _T("://") << host << _T(":") << port << path;32 return ss.str();33 }34 inline url get_url() {35 url r;36 r.protocol = utf8::cvt<std::string>(protocol);37 r.host = utf8::cvt<std::string>(host);38 r.path = utf8::cvt<std::string>(path);39 r.query = utf8::cvt<std::string>(query);40 r.port = port;41 return r;42 }43 94 44 }; 95 typedef public base_url<std::string, std::stringstream, string_traits> url; 96 typedef public base_url<std::wstring, std::wstringstream, wstring_traits> wurl; 97 98 inline wurl url_to_wide(const url &u) { 99 wurl r; 100 r.protocol = utf8::cvt<std::wstring>(u.protocol); 101 r.host = utf8::cvt<std::wstring>(u.host); 102 r.path = utf8::cvt<std::wstring>(u.path); 103 r.query = utf8::cvt<std::wstring>(u.query); 104 r.port = u.port; 105 return r; 106 } 107 inline url wide_to_url(const wurl &u) { 108 url r; 109 r.protocol = utf8::cvt<std::string>(u.protocol); 110 r.host = utf8::cvt<std::string>(u.host); 111 r.path = utf8::cvt<std::string>(u.path); 112 r.query = utf8::cvt<std::string>(u.query); 113 r.port = u.port; 114 return r; 115 } 116 117 45 118 inline wurl parse(const std::wstring& url_s, unsigned int default_port = 80) { 46 119 wurl ret; -
include/nrpe/server/connection.cpp
rf7a074d re396b2f 49 49 50 50 void connection::timeout(const boost::system::error_code& e) { 51 handler_->log_debug(__FILE__, __LINE__, _T("Timeout "));51 handler_->log_debug(__FILE__, __LINE__, _T("Timeout reading: ") + strEx::itos(parser_.get_payload_length())); 52 52 if (e != boost::asio::error::operation_aborted) { 53 53 handler_->log_debug(__FILE__, __LINE__, _T("Timeout <<<-")); -
include/nrpe/server/parser.hpp
r294b37b re396b2f 36 36 packet_length_ = nrpe::length::get_packet_length(length); 37 37 } 38 unsigned int get_payload_length() const { 39 return payload_length_; 40 } 38 41 }; 39 42 -
include/nrpe/server/ssl_connection.cpp
rc391984 re396b2f 25 25 26 26 void ssl_connection::start() { 27 handler_->log_debug(__FILE__, __LINE__, _T("starting secure data connection")); 27 28 socket_.async_handshake(boost::asio::ssl::stream_base::server, 28 29 strand_.wrap( -
include/nscapi/functions.hpp
rc5ec0c8 re396b2f 25 25 #include <string> 26 26 #include <list> 27 #include <set> 27 28 #include <iostream> 28 29 … … 177 178 struct destination_container { 178 179 std::string id; 179 std::string host; 180 std::string address; 180 net::url address; 181 181 std::string comment; 182 std:: list<std::string> tags;182 std::set<std::string> tags; 183 183 typedef std::map<std::string,std::string> data_map; 184 184 data_map data; 185 185 void set_host(std::string value) { 186 address.host = value; 187 } 188 void set_address(std::string value) { 189 address = net::parse(value); 190 } 191 void set_port(std::string value) { 192 address.port = strEx::stoi(value); 193 } 186 194 std::string get_protocol() const { 187 net::url url = get_url(); 188 return url.protocol; 195 return address.protocol; 189 196 } 190 197 bool has_protocol() const { 191 return !address.empty(); 192 } 198 return !address.protocol.empty(); 199 } 200 /* 193 201 net::url get_url(unsigned int port = 80) const { 194 202 return net::parse(address, port); 195 203 } 204 */ 196 205 static bool to_bool(std::string value, bool def = false) { 197 206 if (value.empty()) … … 227 236 } 228 237 238 /* 229 239 inline net::url get_url(int default_port) { 230 240 return net::parse(address, default_port); 231 241 } 242 */ 232 243 void set_string_data(std::string key, std::string value) { 233 244 if (key == "host") 234 host = value;235 else 245 set_host(value); 246 else 236 247 data[key] = value; 237 248 } 238 249 void set_int_data(std::string key, int value) { 239 data[key] = boost::lexical_cast<std::string>(value); 250 if (key == "port") 251 address.port = value; 252 else 253 data[key] = boost::lexical_cast<std::string>(value); 240 254 } 241 255 void set_bool_data(std::string key, bool value) { … … 246 260 std::stringstream ss; 247 261 ss << "id: " << id; 248 ss << ", host: " << host; 249 ss << ", address: " << address; 250 // ss << ", protocol: " << protocol; 262 ss << ", address: " << address.to_string(); 251 263 ss << ", comment: " << comment; 252 264 int i=0; … … 261 273 262 274 void import(const destination_container &other) { 275 if (id.empty() && !other.id.empty()) 276 id = other.id; 277 address.import(other.address); 278 if (comment.empty() && !other.comment.empty()) 279 comment = other.comment; 280 BOOST_FOREACH(const std::string &t, other.tags) { 281 tags.insert(t); 282 } 283 BOOST_FOREACH(const data_map::value_type &kvp, other.data) { 284 if (data.find(kvp.first) == data.end()) 285 data[kvp.first] = kvp.second; 286 } 287 } 288 void apply(const destination_container &other) { 263 289 if (!other.id.empty()) 264 290 id = other.id; 265 if (!other.host.empty()) 266 host = other.host; 267 if (!other.address.empty()) 268 address = other.address; 269 // if (!other.protocol.empty()) 270 // protocol = other.protocol; 291 address.apply(other.address); 271 292 if (!other.comment.empty()) 272 293 comment = other.comment; 273 294 BOOST_FOREACH(const std::string &t, other.tags) { 274 tags. push_back(t);295 tags.insert(t); 275 296 } 276 297 BOOST_FOREACH(const data_map::value_type &kvp, other.data) { … … 284 305 if (!dst.id.empty()) 285 306 host->set_id(dst.id); 286 if (!dst.host.empty()) 287 host->set_host(dst.host); 288 if (!dst.address.empty()) 289 host->set_address(dst.address); 307 if (!dst.address.host.empty()) 308 host->set_address(dst.address.to_string()); 290 309 if (!dst.has_protocol()) 291 310 host->set_protocol(dst.get_protocol()); … … 307 326 if (host.id() == tag) { 308 327 data.id = tag; 309 if (!host.host().empty()) 310 data.host = host.host(); 311 if (!host.address().empty()) 312 data.address = host.address(); 313 if (data.address.empty() && !host.host().empty()) 314 data.address = host.host(); 315 // if (!host.protocol().empty()) 316 // data.protocol = host.protocol(); 328 data.address.import(net::parse(host.address())); 317 329 if (!host.comment().empty()) 318 330 data.comment = host.comment(); 319 331 if (expand_meta) { 320 332 for(int j=0;j<host.tags_size(); ++j) { 321 data.tags. push_back(host.tags(j));333 data.tags.insert(host.tags(j)); 322 334 } 323 335 for(int j=0;j<host.metadata_size(); ++j) { … … 333 345 ////////////////////////////////////////////////////////////////////////// 334 346 335 static void make_submit_from_query(std::string &message, const std::wstring channel, const std::wstring alias = _T("") ) {347 static void make_submit_from_query(std::string &message, const std::wstring channel, const std::wstring alias = _T(""), const std::wstring target = _T("")) { 336 348 Plugin::QueryResponseMessage response; 337 349 response.ParseFromString(message); … … 340 352 request.mutable_header()->set_source_id(request.mutable_header()->recipient_id()); 341 353 request.set_channel(to_string(channel)); 354 if (!target.empty()) { 355 request.mutable_header()->set_recipient_id(to_string(target)); 356 } 342 357 for (int i=0;i<response.payload_size();++i) { 343 358 request.add_payload()->CopyFrom(response.payload(i)); -
include/nscapi/targets.cpp
r96c1461 re396b2f 1 1 #include <nscapi/targets.hpp> 2 2 3 namespace sh = nscapi::settings_helper;4 5 6 nscapi::target_handler::target nscapi::target_handler::read_target(boost::shared_ptr<nscapi::settings_proxy> proxy, std::wstring path, std::wstring alias, std::wstring host) {7 target t;8 t.alias = alias;9 t.host = host;10 std::wstring tpath = path + _T("/") + alias;11 12 sh::settings_registry settings(proxy);13 14 settings.path(path).add_path()15 (alias, sh::wstring_map_path(&t.options),16 _T("TARGET DEFENITION"), _T("Target defnition for: ") + alias)17 18 ;19 20 settings.path(tpath).add_key()21 // (_T("protocol"), sh::wstring_key(&t.protocol, _T("*")),22 // _T("TARGET PROTOCOL"), _T("The default protocol to use for target"))23 24 (_T("host"), sh::wstring_key(&t.host, t.host),25 _T("TARGET HOST"), _T("Target host name"))26 27 (_T("address"), sh::wstring_key(&t.address, t.address),28 _T("TARGET ADDRESS"), _T("Target host address"))29 30 (_T("alias"), sh::wstring_key(&t.alias, t.alias),31 _T("TARGET ALIAS"), _T("The alias for the target"))32 33 (_T("parent"), sh::wstring_key(&t.parent, _T("default")),34 _T("TARGET PARENT"), _T("The parent the target inherits from"))35 36 ;37 38 settings.register_all();39 settings.notify();40 41 if (!t.parent.empty() && t.parent != alias & t.parent != t.alias) {42 target p;43 optarget tmp = find_target(t.parent);44 if (tmp)45 p = *tmp;46 else {47 p = read_target(proxy, path, t.parent, _T(""));48 add_template(p);49 }50 apply_parent(t, p);51 }52 return t;53 }54 55 nscapi::target_handler::optarget nscapi::target_handler::find_target(std::wstring alias) {56 target_list_type::const_iterator cit = target_list.find(alias);57 if (cit != target_list.end())58 return optarget(cit->second);59 cit = template_list.find(alias);60 if (cit != template_list.end())61 return optarget(cit->second);62 return optarget();63 }64 65 bool nscapi::target_handler::has_target(std::wstring alias) {66 target_list_type::const_iterator cit = target_list.find(alias);67 if (cit != target_list.end())68 return true;69 cit = template_list.find(alias);70 if (cit != template_list.end())71 return true;72 return false;73 }74 75 void nscapi::target_handler::apply_parent(target &t, target &p) {76 if (t.host.empty())77 t.host = p.host;78 if (t.host.empty())79 t.host = p.host;80 BOOST_FOREACH(target::options_type::value_type i, p.options) {81 if (t.options.find(i.first) != t.options.end())82 t.options[i.first] = i.second;83 }84 }85 86 void nscapi::target_handler::rebuild() {87 // TODO re-build targets since default has changed!88 }89 90 91 std::wstring nscapi::target_handler::to_wstring() {92 std::wstringstream ss;93 ss << _T("Targets: ");94 BOOST_FOREACH(target_list_type::value_type t, target_list) {95 ss << _T(", ") << t.first << _T(" = {") << t.second.to_wstring() + _T("} ");96 }97 ss << _T("Templates: ");98 BOOST_FOREACH(target_list_type::value_type t, target_list) {99 ss << _T(", ") << t.first << _T(" = {") << t.second.to_wstring() + _T("} ");100 }101 return ss.str();102 } -
include/nscapi/targets.hpp
r96c1461 re396b2f 10 10 #include <settings/client/settings_client.hpp> 11 11 #include <nscapi/settings_proxy.hpp> 12 #include <nscapi/functions.hpp> 12 13 13 14 #include <net/net.hpp> 14 15 15 16 namespace nscapi { 16 struct target_handler { 17 18 struct target{19 std::wstring host;17 18 namespace settings_object_helper { 19 class default_object_type { 20 std::wstring path; 20 21 std::wstring alias; 21 //std::wstring protocol; 22 std::wstring address; 22 std::wstring value; 23 23 std::wstring parent; 24 }; 25 26 template<class object_type> 27 class default_object_reader { 28 static void read_target(boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object) {} 29 static void apply_parent(object_type &object, object_type &parent) {} 30 static void post_process_object(object_type &object) {} 31 static void init_default(object_type &object) {} 32 }; 33 34 35 template<class object_type = default_object_type, class object_reader = default_object_reader<default_object_type> > 36 struct target_handler : boost::noncopyable { 37 typedef boost::optional<object_type> optional_object; 38 typedef optional_object optarget; 39 typedef object_type target; 40 typedef std::map<std::wstring, object_type> object_list_type; 41 typedef target_handler<object_type, object_reader> my_type; 42 43 object_list_type object_list; 44 object_list_type template_list; 45 46 void add_missing(boost::shared_ptr<nscapi::settings_proxy> proxy, std::wstring path, std::wstring alias, std::wstring value, bool is_template = false) { 47 if (has_object(alias)) 48 return; 49 add(proxy, path, alias, value, is_template); 50 } 51 52 object_type add(boost::shared_ptr<nscapi::settings_proxy> proxy, std::wstring path, std::wstring alias, std::wstring value, bool is_template = false) { 53 object_type object; 54 object.alias = alias; 55 object.value = value; 56 object.path = path + _T("/") + alias; 57 object.parent = _T("default"); 58 59 object_reader::read_target(proxy, object); 60 61 if (!object.parent.empty() && object.parent != alias & object.parent != object.alias) { 62 object_type parent; 63 optional_object tmp = find_object(object.parent); 64 if (!tmp) { 65 parent = add(proxy, path, object.parent, _T("")); 66 add_template(parent); 67 } else { 68 parent = *tmp; 69 } 70 object_reader::apply_parent(object, parent); 71 } 72 if (is_template) 73 add_template(object); 74 else 75 add_object(object); 76 return object; 77 } 78 79 void rebuild(boost::shared_ptr<nscapi::settings_proxy> proxy) { 80 std::list<object_type> tmp; 81 BOOST_FOREACH(object_list_type::value_type t, object_list) { 82 tmp.push_back(t.second); 83 } 84 object_list.clear(); 85 BOOST_FOREACH(const &object_item o, tmp) { 86 std::wstring::size_type pos = o.path.find_last_of(_T("/")); 87 if (pos == std::wstring::npos) 88 continue; 89 add(proxy, o.path.substr(0, pos-1), o.path.substr(pos), o.host); 90 } 91 } 92 93 94 optional_object find_object(std::wstring alias) { 95 object_list_type::const_iterator cit = object_list.find(alias); 96 if (cit != object_list.end()) 97 return optional_object(cit->second); 98 cit = template_list.find(alias); 99 if (cit != template_list.end()) 100 return optional_object(cit->second); 101 return optional_object(); 102 } 103 104 bool has_object(std::wstring alias) { 105 object_list_type::const_iterator cit = object_list.find(alias); 106 if (cit != object_list.end()) 107 return true; 108 cit = template_list.find(alias); 109 if (cit != template_list.end()) 110 return true; 111 return false; 112 } 113 114 115 std::wstring to_wstring() { 116 std::wstringstream ss; 117 ss << _T("Objects: "); 118 BOOST_FOREACH(object_list_type::value_type t, object_list) { 119 ss << _T(", ") << t.first << _T(" = {") << t.second.to_wstring() + _T("} "); 120 } 121 ss << _T("Templates: "); 122 BOOST_FOREACH(object_list_type::value_type t, template_list) { 123 ss << _T(", ") << t.first << _T(" = {") << t.second.to_wstring() + _T("} "); 124 } 125 return ss.str(); 126 } 127 128 void add_object(object_type object) { 129 object_reader::post_process_object(object); 130 object_list_type::iterator cit = template_list.find(object.alias); 131 if (cit != template_list.end()) 132 template_list.erase(cit); 133 object_list[object.alias] = object; 134 } 135 void add_template(object_type object) { 136 object_list_type::const_iterator cit = object_list.find(object.alias); 137 if (cit != object_list.end()) 138 return; 139 object_reader::post_process_object(object); 140 template_list[object.alias] = object; 141 } 142 }; 143 } 144 145 146 namespace targets { 147 struct target_object { 148 149 std::wstring path; 150 std::wstring alias; 151 std::wstring value; 152 std::wstring parent; 153 154 net::wurl address; 24 155 typedef std::map<std::wstring,std::wstring> options_type; 25 156 options_type options; 26 157 27 std::wstring to_wstring() {158 std::wstring to_wstring() const { 28 159 std::wstringstream ss; 29 160 ss << _T("Target: ") << alias; 30 ss << _T(", host: ") << host; 31 // ss << _T(", protocol: ") << protocol; 32 ss << _T(", address: ") << address; 161 ss << _T(", address: ") << get_address(); 33 162 ss << _T(", parent: ") << parent; 34 163 BOOST_FOREACH(options_type::value_type o, options) { … … 37 166 return ss.str(); 38 167 } 39 40 bool has_protocol() { 41 return !address.empty(); 42 } 43 std::wstring get_protocol() { 44 net::wurl url = net::parse(address); 45 return url.protocol; 46 } 47 bool has_option(std::wstring key) { 168 std::wstring get_address() const { 169 return address.to_string(); 170 } 171 void set_address(std::wstring value) { 172 net::wurl n = net::parse(value); 173 address.apply(n); 174 } 175 void set_host(std::wstring value) { 176 address.host = value; 177 } 178 void set_port(int value) { 179 address.host = strEx::itos(value); 180 } 181 bool has_option(std::wstring key) const { 48 182 return options.find(key) != options.end(); 49 183 } 50 bool has_option(std::string key) {184 bool has_option(std::string key) const { 51 185 return has_option(utf8::cvt<std::wstring>(key)); 52 186 } 53 54 }; 55 typedef boost::optional<target> optarget; 56 typedef std::map<std::wstring, target> target_list_type; 57 58 target_list_type target_list; 59 target_list_type template_list; 60 target add(boost::shared_ptr<nscapi::settings_proxy> proxy, std::wstring path, std::wstring key, std::wstring value) { 61 target t = read_target(proxy, path, key, value); 62 add(t); 63 return t; 64 } 65 void add(target t) { 66 target_list[t.alias] = t; 67 } 68 void add_template(target t) { 69 template_list[t.alias] = t; 70 } 71 target read_target(boost::shared_ptr<nscapi::settings_proxy> proxy, std::wstring path, std::wstring alias, std::wstring host); 72 void rebuild(); 73 74 optarget find_target(std::wstring alias); 75 bool has_target(std::wstring alias); 76 static void apply_parent(target &t, target &p); 77 std::wstring to_wstring(); 78 79 }; 187 void set_property_int(std::wstring key, int value) { 188 if (key == _T("port")) { 189 set_port(value); 190 } else 191 options[key] = strEx::itos(value); 192 } 193 void set_property_bool(std::wstring key, bool value) { 194 options[key] = value?_T("true"):_T("false"); 195 } 196 void set_property_string(std::wstring key, std::wstring value) { 197 if (key == _T("host")) { 198 set_host(value); 199 } else 200 options[key] = value; 201 } 202 203 nscapi::functions::destination_container to_destination_container() const { 204 nscapi::functions::destination_container ret; 205 if (!alias.empty()) 206 ret.id = utf8::cvt<std::string>(alias); 207 ret.address.apply(net::wide_to_url(address)); 208 BOOST_FOREACH(const options_type::value_type &kvp, options) { 209 ret.data[utf8::cvt<std::string>(kvp.first)] = utf8::cvt<std::string>(kvp.second); 210 } 211 return ret; 212 } 213 214 }; 215 typedef boost::optional<target_object> optional_target_object; 216 typedef std::map<std::wstring,std::wstring> targets_type; 217 218 struct target_object_reader { 219 typedef target_object object_type; 220 static void read_target(boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object); 221 static void apply_parent(object_type &object, object_type &parent); 222 }; 223 224 namespace sh = nscapi::settings_helper; 225 struct dummy_custom_reader { 226 typedef target_object object_type; 227 static void add_custom_keys(sh::settings_registry &settings, boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object) {} 228 }; 229 230 template<class custom_reader> 231 struct split_object_reader { 232 typedef target_object object_type; 233 234 static void post_process_object(object_type &object) { 235 custom_reader::post_process_target(object); 236 } 237 238 static void read_target(boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object) { 239 object.address = net::parse(object.value, 0); 240 if (object.alias == _T("default")) 241 custom_reader::init_default(object); 242 243 nscapi::settings_helper::settings_registry settings(proxy); 244 245 object_type::options_type options; 246 settings.path(object.path).add_path() 247 (object.alias, nscapi::settings_helper::wstring_map_path(&options), 248 _T("TARGET DEFENITION"), _T("Target definition for: ") + object.alias) 249 250 ; 251 252 settings.path(object.path).add_key() 253 254 (_T("address"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_address, &object, _1)), 255 _T("TARGET ADDRESS"), _T("Target host address")) 256 257 (_T("host"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_host, &object, _1)), 258 _T("TARGET HOST"), _T("The target server to report results to.")) 259 260 (_T("port"), sh::int_fun_key<int>(boost::bind(&object_type::set_port, &object, _1)), 261 _T("TARGET PORT"), _T("The target server port")) 262 263 (_T("alias"), nscapi::settings_helper::wstring_key(&object.alias, object.alias), 264 _T("TARGET ALIAS"), _T("The alias for the target")) 265 266 (_T("parent"), nscapi::settings_helper::wstring_key(&object.parent, _T("default")), 267 _T("TARGET PARENT"), _T("The parent the target inherits from")) 268 269 ; 270 custom_reader::add_custom_keys(settings, proxy, object); 271 272 settings.register_all(); 273 settings.notify(); 274 275 BOOST_FOREACH(const object_type::options_type::value_type &kvp, options) { 276 if (!object.has_option(kvp.first)) 277 object.options[kvp.first] = kvp.second; 278 } 279 280 } 281 282 static void apply_parent(object_type &object, object_type &parent) { 283 object.address.import(parent.address); 284 BOOST_FOREACH(object_type::options_type::value_type i, parent.options) { 285 if (object.options.find(i.first) == object.options.end()) 286 object.options[i.first] = i.second; 287 } 288 } 289 290 }; 291 template<class custom_reader> 292 struct handler : public nscapi::settings_object_helper::target_handler<target_object, split_object_reader<custom_reader > > {}; 293 294 /* 295 template<class custom_reader> 296 */ 297 //typedef nscapi::settings_object_helper::target_handler<target_object, split_object_reader<dummy_custom_reader > > handler; 298 299 } 80 300 } 81 301 -
include/settings/client/settings_client.hpp
r8013c0c re396b2f 67 67 }; 68 68 template<class T> 69 class typed_string_value_no_default : public typed_key<T> { 70 public: 71 typed_string_value_no_default(const T& v) : typed_key<T>(v) {} 72 virtual NSCAPI::settings_type get_type() const { 73 return NSCAPI::key_string; 74 } 75 virtual void notify(settings_impl_interface_ptr core_, std::wstring path, std::wstring key) const { 76 std::wstring dummy = _T("$$DUMMY_VALUE_DO_NOT_USE$$"); 77 std::wstring data = core_->get_string(path, key, dummy); 78 if (data != dummy) { 79 T value = boost::lexical_cast<T>(data); 80 update_target(&value); 81 } 82 } 83 virtual void notify(settings_impl_interface_ptr core_, std::wstring parent, std::wstring path, std::wstring key) const { 84 std::wstring dummy = _T("$$DUMMY_VALUE_DO_NOT_USE$$"); 85 std::wstring data = core_->get_string(parent, key, dummy); 86 if (data != dummy) 87 dummy = data; 88 data = core_->get_string(path, key, dummy); 89 if (data != dummy) { 90 T value = boost::lexical_cast<T>(data); 91 update_target(&value); 92 } 93 } 94 }; 95 template<class T> 69 96 class typed_path_value : public typed_key<T> { 70 97 public: … … 85 112 } 86 113 }; 114 87 115 template<class T> 88 116 class typed_int_value : public typed_key<T> { … … 104 132 T default_value = static_cast<T>(core_->get_int(parent, key, default_value_as_int_)); 105 133 T value = static_cast<T>(core_->get_int(path, key, default_value)); 134 update_target(&value); 135 } 136 protected: 137 int default_value_as_int_; 138 }; 139 template<class T> 140 class typed_int_value_no_default : public typed_key<T> { 141 public: 142 typed_int_value_no_default(const T& v) : typed_key<T>(v), default_value_as_int_(0) {} 143 typed_key<T>* default_value(const T& v) { 144 typed_key<T>::default_value(v); 145 default_value_as_int_ = boost::lexical_cast<int>(v); 146 return this; 147 } 148 virtual NSCAPI::settings_type get_type() const { 149 return NSCAPI::key_integer; 150 } 151 virtual void notify(settings_impl_interface_ptr core_, std::wstring path, std::wstring key) const { 152 int dummy = -1; 153 int val = core_->get_int(path, key, dummy); 154 if (val == dummy) { 155 dummy = -2; 156 val = core_->get_int(path, key, dummy); 157 if (val == dummy) 158 return; 159 } 160 T value = static_cast<T>(val); 161 update_target(&value); 162 } 163 virtual void notify(settings_impl_interface_ptr core_, std::wstring parent, std::wstring path, std::wstring key) const { 164 int dummy = -1; 165 int defval = core_->get_int(path, key, dummy); 166 if (defval == dummy) { 167 dummy = -2; 168 defval = core_->get_int(path, key, dummy); 169 } 170 if (defval != dummy) { 171 T value = static_cast<T>(core_->get_int(path, key, defval)); 172 update_target(&value); 173 } 174 dummy = -1; 175 int val = core_->get_int(path, key, dummy); 176 if (val == dummy) { 177 dummy = -2; 178 val = core_->get_int(path, key, dummy); 179 if (val == dummy) 180 return; 181 } 182 T value = static_cast<T>(val); 106 183 update_target(&value); 107 184 } … … 196 273 } 197 274 template<class T> 275 boost::shared_ptr<typed_key_fun<T, typed_int_value_no_default<T> > > int_fun_key(boost::function<void (T)> fun) { 276 boost::shared_ptr<typed_key_fun<T, typed_int_value_no_default<T> > > r(new typed_key_fun<T, typed_int_value_no_default<T> >(fun, 0)); 277 return r; 278 } 279 template<class T> 198 280 boost::shared_ptr<typed_key_fun<T, typed_bool_value<T> > > bool_fun_key(boost::function<void (T)> fun, T def) { 199 281 boost::shared_ptr<typed_key_fun<T, typed_bool_value<T> > > r(new typed_key_fun<T, typed_bool_value<T> >(fun, def)); … … 203 285 boost::shared_ptr<typed_key_fun<T, typed_string_value<T> > > string_fun_key(boost::function<void (T)> fun, T def) { 204 286 boost::shared_ptr<typed_key_fun<T, typed_string_value<T> > > r(new typed_key_fun<T, typed_string_value<T> >(fun, def)); 287 return r; 288 } 289 template<class T> 290 boost::shared_ptr<typed_key_fun<T, typed_string_value_no_default<T> > > string_fun_key(boost::function<void (T)> fun) { 291 boost::shared_ptr<typed_key_fun<T, typed_string_value_no_default<T> > > r(new typed_key_fun<T, typed_string_value_no_default<T> >(fun, T())); 205 292 return r; 206 293 } -
include/settings/settings_http.hpp
rb7d17f8 re396b2f 22 22 public: 23 23 settings_http(settings::settings_core *core, std::wstring context) : settings::SettingsInterfaceImpl(core, context) { 24 net::url url = net::parse( context).get_url();24 net::url url = net::parse(utf8::cvt<std::string>(context)); 25 25 std::wstring path = core->expand_path(DEFAULT_CACHE_PATH); 26 26 if (!file_helpers::checks::is_directory(path)) { -
include/settings/settings_interface_impl.hpp
re11d494 re396b2f 183 183 return get_string(path, key); 184 184 } catch (KeyNotFoundException e) { 185 MUTEX_GUARD();186 settings_cache_[cache_key_type(path,key)] = def;185 //MUTEX_GUARD(); 186 //settings_cache_[cache_key_type(path,key)] = def; 187 187 return def; 188 188 } … … 200 200 MUTEX_GUARD(); 201 201 settings_cache_[cache_key_type(path,key)] = value; 202 path_cache_.insert(path); 202 203 } 203 204 add_key(path, key); … … 269 270 return get_int(path, key); 270 271 } catch (KeyNotFoundException e) { 271 MUTEX_GUARD();272 settings_cache_[cache_key_type(path,key)] = def;272 //MUTEX_GUARD(); 273 //settings_cache_[cache_key_type(path,key)] = def; 273 274 return def; 274 275 } … … 286 287 MUTEX_GUARD(); 287 288 settings_cache_[cache_key_type(path,key)] = value; 289 path_cache_.insert(path); 288 290 } 289 291 add_key(path, key); … … 353 355 return get_bool(path, key); 354 356 } catch (KeyNotFoundException e) { 355 MUTEX_GUARD();356 settings_cache_[cache_key_type(path,key)] = def;357 //MUTEX_GUARD(); 358 //settings_cache_[cache_key_type(path,key)] = def; 357 359 return def; 358 360 } … … 370 372 MUTEX_GUARD(); 371 373 settings_cache_[cache_key_type(path,key)] = value; 374 path_cache_.insert(path); 372 375 } 373 376 add_key(path, key); … … 406 409 } 407 410 } 411 // TODO add support for retrieving all key paths here! 408 412 } else { 409 413 std::wstring::size_type path_len = path.length(); -
modules/NRPEClient/NRPEClient.cpp
r89838be re396b2f 81 81 ; 82 82 83 settings.alias().add_key_to_settings(_T("targets/default"))84 85 (_T("timeout"), sh::uint_key(&timeout, 30),86 _T("TIMEOUT"), _T("Timeout when reading/writing packets to/from sockets."))87 88 (_T("use ssl"), sh::bool_key(&use_ssl, true),89 _T("ENABLE SSL ENCRYPTION"), _T("This option controls if SSL should be enabled."))90 91 (_T("certificate"), sh::wpath_key(&certificate, _T("${certificate-path}/nrpe_dh_512.pem")),92 _T("SSL CERTIFICATE"), _T(""))93 94 (_T("payload length"), sh::uint_key(&buffer_length, 1024),95 _T("PAYLOAD LENGTH"), _T("Length of payload to/from the NRPE agent. This is a hard specific value so you have to \"configure\" (read recompile) your NRPE agent to use the same value for it to work."))96 ;97 98 83 settings.register_all(); 99 84 settings.notify(); 85 86 targets.add_missing(get_settings_proxy(), target_path, _T("default"), _T(""), true); 87 100 88 101 89 get_core()->registerSubmissionListener(get_id(), channel_); … … 105 93 register_command(_T("nrpe_exec"), _T("Execute (via query) remote NRPE host")); 106 94 register_command(_T("nrpe_help"), _T("Help on using NRPE Client")); 107 if (!targets.has_target(_T("default"))) {108 add_target(_T("default"), _T("default"));109 targets.rebuild();110 }111 nscapi::target_handler::optarget t = targets.find_target(_T("default"));112 if (t) {113 if (!t->has_option("certificate"))114 t->options[_T("certificate")] = certificate;115 if (!t->has_option("timeout"))116 t->options[_T("timeout")] = strEx::itos(timeout);117 if (!t->has_option("payload length"))118 t->options[_T("payload length")] = strEx::itos(buffer_length);119 if (!t->has_option("ssl"))120 t->options[_T("ssl")] = use_ssl?_T("true"):_T("false");121 targets.add(*t);122 } else {123 NSC_LOG_ERROR(_T("Default target not found!"));124 }125 126 95 } catch (nscapi::nscapi_exception &e) { 127 96 NSC_LOG_ERROR_STD(_T("NSClient API exception: ") + utf8::to_unicode(e.what())); … … 150 119 void NRPEClient::add_target(std::wstring key, std::wstring arg) { 151 120 try { 152 nscapi::target_handler::target t = targets.add(get_settings_proxy(), target_path , key, arg); 153 if (t.has_option(_T("certificate"))) { 154 std::wstring value = t.options[_T("certificate")]; 155 boost::filesystem::wpath p = value; 156 if (!boost::filesystem::is_regular(p)) { 157 p = get_core()->getBasePath() / p; 158 if (boost::filesystem::is_regular(p)) { 159 value = p.string(); 160 } else { 161 value = get_core()->expand_path(value); 162 } 163 t.options[_T("certificate")] = value; 164 targets.add(t); 165 } 166 if (boost::filesystem::is_regular(p)) { 167 NSC_DEBUG_MSG_STD(_T("Using certificate: ") + p.string()); 168 } else { 169 NSC_LOG_ERROR_STD(_T("Certificate not found: ") + p.string()); 170 } 171 } 121 nscapi::targets::target_object t = targets.add(get_settings_proxy(), target_path , key, arg); 172 122 } catch (const std::exception &e) { 173 123 NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key + _T(", ") + utf8::to_unicode(e.what())); … … 199 149 200 150 NSCAPI::nagiosReturn NRPEClient::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) { 201 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 202 std::wstring cmd = client::command_line_parser::parse_command(data.command, _T("nrpe")); 151 Plugin::QueryRequestMessage message; 152 message.ParseFromString(request); 153 if (message.payload_size() != 1) { 154 return nscapi::functions::create_simple_query_response_unknown(char_command, _T("Multiple payloads currently not supported"), result); 155 } 156 std::string recipient = message.header().recipient_id(); 157 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(message.payload(0)); 158 std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("nrpe")); 203 159 client::configuration config; 160 config.data->recipient.id = recipient; 204 161 setup(config); 205 162 if (client::command_line_parser::is_command(cmd)) 206 163 return client::command_line_parser::do_execute_command_as_query(config, cmd, data.args, request, result); 207 return commands.exec_simple(config, data.target, c har_command, data.args, result);164 return commands.exec_simple(config, data.target, cmd, data.args, result); 208 165 } 209 166 … … 245 202 add_local_options(config.local, config.data); 246 203 247 net::wurl url; 248 url.protocol = _T("nrpe"); 249 url.port = 5666; 250 nscapi::target_handler::optarget opt = targets.find_target(_T("default")); 204 config.data->recipient.address = net::parse("nrpe://localhost:5666"); 205 std::wstring recipient = utf8::cvt<std::wstring>(config.data->recipient.id); 206 if (!targets.has_object(recipient)) { 207 NSC_LOG_ERROR(_T("Target not found (using default): ") + recipient); 208 recipient = _T("default"); 209 } 210 nscapi::targets::optional_target_object opt = targets.find_object(recipient); 211 251 212 if (opt) { 252 nscapi::target_handler::target t = *opt; 253 url.host = t.host; 254 if (t.has_option("port")) { 255 try { 256 url.port = strEx::stoi(t.options[_T("port")]); 257 } catch (...) {} 258 } 259 std::string keys[] = {"certificate", "timeout", "payload length", "ssl"}; 260 BOOST_FOREACH(std::string s, keys) { 261 config.data->recipient.data[s] = utf8::cvt<std::string>(t.options[utf8::cvt<std::wstring>(s)]); 262 } 213 nscapi::targets::target_object t = *opt; 214 nscapi::functions::destination_container def = t.to_destination_container(); 215 config.data->recipient.apply(def); 263 216 } 264 217 config.data->recipient.id = "default"; 265 config.data->recipient.address = utf8::cvt<std::string>(url.to_string());266 218 config.data->host_self.id = "self"; 267 219 //config.data->host_self.host = hostname_; … … 271 223 } 272 224 273 NRPEClient::connection_data NRPEClient::parse_header(const ::Plugin::Common_Header &header ) {225 NRPEClient::connection_data NRPEClient::parse_header(const ::Plugin::Common_Header &header, client::configuration::data_type data) { 274 226 nscapi::functions::destination_container recipient; 275 227 nscapi::functions::parse_destination(header, header.recipient_id(), recipient, true); 276 return connection_data(recipient, targets.find_target(_T("default")));228 return connection_data(recipient, data->recipient); 277 229 } 278 230 … … 284 236 Plugin::QueryRequestMessage request_message; 285 237 request_message.ParseFromString(request); 286 connection_data con = this->instance->parse_header(*header );238 connection_data con = this->instance->parse_header(*header, data); 287 239 288 240 Plugin::QueryResponseMessage response_message; … … 306 258 Plugin::SubmitRequestMessage request_message; 307 259 request_message.ParseFromString(request); 308 connection_data con = this->instance->parse_header(*header );260 connection_data con = this->instance->parse_header(*header, data); 309 261 std::wstring channel = utf8::cvt<std::wstring>(request_message.channel()); 310 262 … … 328 280 Plugin::ExecuteRequestMessage request_message; 329 281 request_message.ParseFromString(request); 330 connection_data con = this->instance->parse_header(*header );282 connection_data con = this->instance->parse_header(*header, data); 331 283 332 284 Plugin::ExecuteResponseMessage response_message; -
modules/NRPEClient/NRPEClient.h
ra87ce04 re396b2f 33 33 34 34 namespace po = boost::program_options; 35 namespace sh = nscapi::settings_helper; 35 36 36 37 class NRPEClient : public nscapi::impl::simple_plugin { … … 40 41 std::wstring target_path; 41 42 42 nscapi::target_handler targets; 43 struct custom_reader { 44 typedef nscapi::targets::target_object object_type; 45 typedef nscapi::targets::target_object target_object; 46 47 static void init_default(target_object &target) { 48 target.set_property_int(_T("timeout"), 30); 49 target.set_property_bool(_T("ssl"), true); 50 target.set_property_string(_T("certificate"), _T("${certificate-path}/nrpe_dh_512.pem")); 51 target.set_property_int(_T("payload length"), 1024); 52 } 53 54 static void add_custom_keys(sh::settings_registry &settings, boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object) { 55 settings.path(object.path).add_key() 56 57 (_T("timeout"), sh::int_fun_key<int>(boost::bind(&object_type::set_property_int, &object, _T("timeout"), _1), 30), 58 _T("TIMEOUT"), _T("Timeout when reading/writing packets to/from sockets.")) 59 60 (_T("use ssl"), sh::bool_fun_key<bool>(boost::bind(&object_type::set_property_bool, &object, _T("ssl"), _1), true), 61 _T("ENABLE SSL ENCRYPTION"), _T("This option controls if SSL should be enabled.")) 62 63 (_T("certificate"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("certificate"), _1), _T("${certificate-path}/nrpe_dh_512.pem")), 64 _T("SSL CERTIFICATE"), _T("")) 65 66 (_T("payload length"), sh::int_fun_key<int>(boost::bind(&object_type::set_property_int, &object, _T("payload length"), _1), 1024), 67 _T("PAYLOAD LENGTH"), _T("Length of payload to/from the NRPE agent. This is a hard specific value so you have to \"configure\" (read recompile) your NRPE agent to use the same value for it to work.")) 68 ; 69 } 70 71 static void post_process_target(target_object &target) { 72 nscapi::core_wrapper* core = GET_CORE(); 73 if (core == NULL) { 74 NSC_LOG_ERROR_STD(_T("Invalid core")); 75 return; 76 } 77 if (target.has_option(_T("certificate"))) { 78 std::wstring value = target.options[_T("certificate")]; 79 boost::filesystem::wpath p = value; 80 if (!boost::filesystem::is_regular(p)) { 81 p = core->getBasePath() / p; 82 if (boost::filesystem::is_regular(p)) { 83 value = p.string(); 84 } else { 85 value = core->expand_path(value); 86 } 87 target.options[_T("certificate")] = value; 88 } 89 if (boost::filesystem::is_regular(p)) { 90 NSC_DEBUG_MSG_STD(_T("Using certificate: ") + p.string()); 91 } else { 92 NSC_LOG_ERROR_STD(_T("Certificate not found: ") + p.string()); 93 } 94 } 95 } 96 97 }; 98 99 nscapi::targets::handler<custom_reader> targets; 43 100 client::command_manager commands; 44 101 … … 51 108 bool use_ssl; 52 109 53 connection_data(nscapi::functions::destination_container recipient, nscapi::target_handler::optarget dt) {54 cert = recipient.get_string_data("certificate", dt?utf8::cvt<std::string>(dt->options[_T("certificate")]):"");55 timeout = recipient.get_int_data("timeout", 30);56 buffer_length = recipient.get_int_data("payload length", 1024);57 use_ssl = recipient.get_bool_data("ssl");58 if (recipient.has_data("no ssl"))59 use_ssl = !recipient.get_bool_data("no ssl");60 if (recipient.has_data("use ssl"))61 use_ssl = recipient.get_bool_data("use ssl");62 63 net::url url = recipient.get_url(5666); 64 host = url.host;65 port = url.get_port();110 connection_data(nscapi::functions::destination_container arguments, nscapi::functions::destination_container target) { 111 arguments.import(target); 112 cert = arguments.get_string_data("certificate", ""); 113 timeout = arguments.get_int_data("timeout", 30); 114 buffer_length = arguments.get_int_data("payload length", 1024); 115 use_ssl = arguments.get_bool_data("ssl"); 116 if (arguments.has_data("no ssl")) 117 use_ssl = !arguments.get_bool_data("no ssl"); 118 if (arguments.has_data("use ssl")) 119 use_ssl = arguments.get_bool_data("use ssl"); 120 121 host = arguments.address.host; 122 port = arguments.address.get_port_string("5666"); 66 123 } 67 124 … … 88 145 89 146 virtual nscapi::functions::destination_container lookup_target(std::wstring &id) { 147 nscapi::targets::optional_target_object t = instance->targets.find_object(id); 148 if (t) 149 return t->to_destination_container(); 90 150 nscapi::functions::destination_container ret; 91 nscapi::target_handler::optarget t = instance->targets.find_target(id);92 if (t) {93 if (!t->alias.empty())94 ret.id = utf8::cvt<std::string>(t->alias);95 if (!t->host.empty())96 ret.host = utf8::cvt<std::string>(t->host);97 if (t->has_option("address"))98 ret.address = utf8::cvt<std::string>(t->options[_T("address")]);99 else100 ret.address = utf8::cvt<std::string>(t->host);101 BOOST_FOREACH(const nscapi::target_handler::target::options_type::value_type &kvp, t->options) {102 ret.data[utf8::cvt<std::string>(kvp.first)] = utf8::cvt<std::string>(kvp.second);103 }104 }105 151 return ret; 106 152 } … … 155 201 static nrpe::packet send_ssl(std::string cert, std::string host, std::string port, int timeout, nrpe::packet packet); 156 202 157 connection_data parse_header(const ::Plugin::Common_Header &header );203 connection_data parse_header(const ::Plugin::Common_Header &header, client::configuration::data_type data); 158 204 159 205 private: -
modules/NSCAClient/NSCAClient.cpp
rc5ec0c8 re396b2f 94 94 ; 95 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 96 settings.register_all(); 121 97 settings.notify(); … … 126 102 hostname_ = boost::asio::ip::host_name(); 127 103 } 128 129 if (!targets.has_target(_T("default"))) {130 add_target(_T("default"), _T("default"));131 targets.rebuild();132 }133 nscapi::target_handler::optarget t = targets.find_target(_T("default"));134 if (t) {135 nscapi::target_handler::target target = *t;136 if (!target.has_option("encryption"))137 target.options[_T("encryption")] = encryption;138 if (!target.has_option("timeout"))139 target.options[_T("timeout")] = strEx::itos(timeout);140 if (!target.has_option("payload length"))141 target.options[_T("payload length")] = strEx::itos(payload_length);142 if (!target.has_option("time offset"))143 target.options[_T("time offset")] = utf8::cvt<std::wstring>(delay);144 if (!target.has_option("password"))145 target.options[_T("password")] = password;146 if (target.address.empty())147 target.address = _T("nsca://") + nscahost + _T(":") + strEx::itos(nscaport);148 targets.add(target);149 } else {150 NSC_LOG_ERROR(_T("Default target not found!"));151 }152 153 104 } catch (nscapi::nscapi_exception &e) { 154 105 NSC_LOG_ERROR_STD(_T("NSClient API exception: ") + utf8::to_unicode(e.what())); … … 200 151 void NSCAAgent::add_target(std::wstring key, std::wstring arg) { 201 152 try { 202 targets.add(get_settings_proxy(), target_path , key, arg);153 nscapi::targets::target_object t = targets.add(get_settings_proxy(), target_path , key, arg); 203 154 } catch (...) { 204 155 NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key); … … 286 237 add_local_options(config.local, config.data); 287 238 288 net::wurl url;289 url.protocol = _T("nsca");290 url.port = 5667; 291 nscapi::target _handler::optarget opt = targets.find_target(_T("default"));239 config.data->recipient.id = "default"; 240 config.data->recipient.address = net::parse("nsca://localhost:5667"); 241 242 nscapi::targets::optional_target_object opt = targets.find_object(_T("default")); 292 243 if (opt) { 293 nscapi::target_handler::target t = *opt; 294 if (t.address.empty()) { 295 if (t.host.empty()) 296 url.host = t.host; 297 if (t.has_option("port")) { 298 try { 299 url.port = strEx::stoi(t.options[_T("port")]); 300 } catch (...) {} 301 } 302 } else { 303 url = net::parse(t.address); 304 } 305 std::string keys[] = {"encryption", "timeout", "payload length", "password", "time offset"}; 306 BOOST_FOREACH(std::string s, keys) { 307 config.data->recipient.data[s] = utf8::cvt<std::string>(t.options[utf8::cvt<std::wstring>(s)]); 308 } 309 } 310 config.data->recipient.id = "default"; 311 config.data->recipient.address = utf8::cvt<std::string>(url.to_string()); 244 nscapi::targets::target_object t = *opt; 245 nscapi::functions::destination_container def = t.to_destination_container(); 246 config.data->recipient.import(def); 247 } 312 248 config.data->host_self.id = "self"; 313 config.data->host_self. host = hostname_;249 config.data->host_self.address.host = hostname_; 314 250 315 251 config.target_lookup = handler; -
modules/NSCAClient/NSCAClient.h
r7354aa1 re396b2f 33 33 34 34 namespace po = boost::program_options; 35 namespace sh = nscapi::settings_helper; 35 36 36 37 class NSCAAgent : public nscapi::impl::simple_plugin { … … 43 44 long time_delta_; 44 45 45 nscapi::target_handler targets; 46 struct custom_reader { 47 typedef nscapi::targets::target_object object_type; 48 typedef nscapi::targets::target_object target_object; 49 50 static void init_default(target_object &target) { 51 target.set_property_int(_T("timeout"), 30); 52 target.set_property_string(_T("encryption"), _T("ase")); 53 target.set_property_int(_T("payload length"), 512); 54 } 55 static void post_process_target(target_object &target) {} 56 57 static void add_custom_keys(sh::settings_registry &settings, boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object) { 58 settings.path(object.path).add_key() 59 60 (_T("timeout"), sh::int_fun_key<int>(boost::bind(&object_type::set_property_int, &object, _T("timeout"), _1), 30), 61 _T("TIMEOUT"), _T("Timeout when reading/writing packets to/from sockets.")) 62 63 (_T("payload length"), sh::int_fun_key<int>(boost::bind(&object_type::set_property_int, &object, _T("payload length"), _1), 512), 64 _T("PAYLOAD LENGTH"), _T("Length of payload to/from the NRPE agent. This is a hard specific value so you have to \"configure\" (read recompile) your NRPE agent to use the same value for it to work.")) 65 66 (_T("encryption"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("encryption"), _1), _T("aes")), 67 _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.")) 68 69 (_T("password"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("password"), _1), _T("")), 70 _T("PASSWORD"), _T("The password to use. Again has to be the same as the server or it wont work at all.")) 71 72 (_T("time offset"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("delay"), _1), _T("0")), 73 _T("TIME OFFSET"), _T("Time offset.")) 74 ; 75 } 76 }; 77 78 nscapi::targets::handler<custom_reader> targets; 46 79 client::command_manager commands; 47 80 … … 65 98 else 66 99 time_delta = 0; 67 net::url url = recipient.get_url(5667); 68 host = url.host; 69 port = url.get_port(); 70 sender_hostname = sender.host; 100 host = recipient.address.get_host(); 101 port = strEx::s::itos(recipient.address.get_port(5667)); 102 sender_hostname = sender.address.host; 71 103 if (sender.has_data("host")) 72 104 sender_hostname = sender.get_string_data("host"); … … 100 132 virtual nscapi::functions::destination_container lookup_target(std::wstring &id) { 101 133 nscapi::functions::destination_container ret; 102 nscapi::target_handler::optarget t = instance->targets.find_target(id); 103 if (t) { 104 if (!t->alias.empty()) 105 ret.id = utf8::cvt<std::string>(t->alias); 106 if (!t->host.empty()) 107 ret.host = utf8::cvt<std::string>(t->host); 108 if (t->has_option("address")) 109 ret.address = utf8::cvt<std::string>(t->options[_T("address")]); 110 else 111 ret.address = utf8::cvt<std::string>(t->host); 112 BOOST_FOREACH(const nscapi::target_handler::target::options_type::value_type &kvp, t->options) { 113 ret.data[utf8::cvt<std::string>(kvp.first)] = utf8::cvt<std::string>(kvp.second); 114 } 115 } 134 nscapi::targets::optional_target_object opt = instance->targets.find_object(id); 135 if (opt) 136 return opt->to_destination_container(); 116 137 return ret; 117 138 } -
modules/NSCPClient/NSCPClient.cpp
ra87ce04 re396b2f 88 88 ; 89 89 90 settings.alias().add_key_to_settings(_T("targets/default"))91 92 (_T("timeout"), sh::uint_key(&timeout, 30),93 _T("TIMEOUT"), _T("Timeout when reading/writing packets to/from sockets."))94 95 (_T("use ssl"), sh::bool_key(&use_ssl, true),96 _T("ENABLE SSL ENCRYPTION"), _T("This option controls if SSL should be enabled."))97 98 (_T("certificate"), sh::wpath_key(&certificate, _T("${certificate-path}/nrpe_dh_512.pem")),99 _T("SSL CERTIFICATE"), _T(""))100 101 (_T("payload length"), sh::uint_key(&buffer_length, 1024),102 _T("PAYLOAD LENGTH"), _T("Length of payload to/from the NRPE agent. This is a hard specific value so you have to \"configure\" (read recompile) your NRPE agent to use the same value for it to work."))103 ;104 105 90 settings.register_all(); 106 91 settings.notify(); 107 92 108 93 get_core()->registerSubmissionListener(get_id(), channel_); 109 110 if (!targets.has_target(_T("default"))) {111 add_target(_T("default"), _T("default"));112 targets.rebuild();113 }114 nscapi::target_handler::optarget t = targets.find_target(_T("default"));115 if (t) {116 if (!t->has_option("certificate"))117 t->options[_T("certificate")] = certificate;118 if (!t->has_option("timeout"))119 t->options[_T("timeout")] = strEx::itos(timeout);120 if (!t->has_option("payload length"))121 t->options[_T("payload length")] = strEx::itos(buffer_length);122 if (!t->has_option("ssl"))123 t->options[_T("ssl")] = use_ssl?_T("true"):_T("false");124 targets.add(*t);125 } else {126 NSC_LOG_ERROR(_T("Default target not found!"));127 }128 94 129 95 } catch (nscapi::nscapi_exception &e) { … … 153 119 void NSCPClient::add_target(std::wstring key, std::wstring arg) { 154 120 try { 155 nscapi::target_handler::target t = targets.add(get_settings_proxy(), target_path , key, arg); 156 if (t.has_option(_T("certificate"))) { 157 boost::filesystem::wpath p = t.options[_T("certificate")]; 158 if (!boost::filesystem::is_regular(p)) { 159 p = get_core()->getBasePath() / p; 160 t.options[_T("certificate")] = utf8::cvt<std::wstring>(p.string()); 161 targets.add(t); 162 } 163 if (boost::filesystem::is_regular(p)) { 164 NSC_DEBUG_MSG_STD(_T("Using certificate: ") + p.string()); 165 } else { 166 NSC_LOG_ERROR_STD(_T("Certificate not found: ") + p.string()); 167 } 168 } 121 targets.add(get_settings_proxy(), target_path , key, arg); 169 122 } catch (...) { 170 123 NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key); … … 239 192 add_local_options(config.local, config.data); 240 193 241 net::wurl url; 242 url.protocol = _T("nscp"); 243 url.port = 5668; 244 nscapi::target_handler::optarget opt = targets.find_target(_T("default")); 194 config.data->recipient.id = "default"; 195 config.data->recipient.address = net::parse("nscp://localhost:5668"); 196 nscapi::targets::optional_target_object opt = targets.find_object(_T("default")); 245 197 if (opt) { 246 nscapi::target_handler::target t = *opt; 247 url.host = t.host; 248 if (t.has_option("port")) { 249 try { 250 url.port = strEx::stoi(t.options[_T("port")]); 251 } catch (...) {} 252 } 253 std::string keys[] = {"certificate", "timeout", "payload length", "ssl"}; 254 BOOST_FOREACH(std::string s, keys) { 255 config.data->recipient.data[s] = utf8::cvt<std::string>(t.options[utf8::cvt<std::wstring>(s)]); 256 } 257 } 258 config.data->recipient.id = "default"; 259 config.data->recipient.address = utf8::cvt<std::string>(url.to_string()); 198 nscapi::functions::destination_container def = opt->to_destination_container(); 199 config.data->recipient.import(def); 200 } 260 201 config.data->host_self.id = "self"; 261 202 //config.data->host_self.host = hostname_; -
modules/NSCPClient/NSCPClient.h
rf33c12f re396b2f 22 22 23 23 #include <boost/tuple/tuple.hpp> 24 #include <boost/filesystem.hpp> 24 25 25 26 #include <client/command_line_parser.hpp> … … 33 34 34 35 namespace po = boost::program_options; 36 namespace sh = nscapi::settings_helper; 35 37 36 38 … … 41 43 std::wstring target_path; 42 44 43 nscapi::target_handler targets; 45 struct custom_reader { 46 typedef nscapi::targets::target_object object_type; 47 typedef nscapi::targets::target_object target_object; 48 49 static void init_default(target_object &target) { 50 target.set_property_int(_T("timeout"), 30); 51 target.set_property_bool(_T("ssl"), true); 52 target.set_property_string(_T("certificate"), _T("${certificate-path}/nrpe_dh_512.pem")); 53 target.set_property_int(_T("payload length"), 1024); 54 } 55 56 //static void post_process_target(target_object &target) {} 57 58 static void add_custom_keys(sh::settings_registry &settings, boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object) { 59 settings.path(object.path).add_key() 60 61 (_T("timeout"), sh::int_fun_key<int>(boost::bind(&object_type::set_property_int, &object, _T("timeout"), _1), 30), 62 _T("TIMEOUT"), _T("Timeout when reading/writing packets to/from sockets.")) 63 64 (_T("use ssl"), sh::bool_fun_key<bool>(boost::bind(&object_type::set_property_bool, &object, _T("ssl"), _1), true), 65 _T("ENABLE SSL ENCRYPTION"), _T("This option controls if SSL should be enabled.")) 66 67 (_T("certificate"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("certificate"), _1), _T("${certificate-path}/nrpe_dh_512.pem")), 68 _T("SSL CERTIFICATE"), _T("")) 69 70 (_T("payload length"), sh::int_fun_key<int>(boost::bind(&object_type::set_property_int, &object, _T("payload length"), _1), 1024), 71 _T("PAYLOAD LENGTH"), _T("Length of payload to/from the NRPE agent. This is a hard specific value so you have to \"configure\" (read recompile) your NRPE agent to use the same value for it to work.")) 72 ; 73 } 74 75 static void post_process_target(target_object &target) { 76 nscapi::core_wrapper* core = GET_CORE(); 77 if (core == NULL) { 78 NSC_LOG_ERROR_STD(_T("Invalid core")); 79 return; 80 } 81 if (target.has_option(_T("certificate"))) { 82 std::wstring value = target.options[_T("certificate")]; 83 boost::filesystem::wpath p = value; 84 if (!boost::filesystem::is_regular(p)) { 85 p = core->getBasePath() / p; 86 if (boost::filesystem::is_regular(p)) { 87 value = p.string(); 88 } else { 89 value = core->expand_path(value); 90 } 91 target.options[_T("certificate")] = value; 92 } 93 if (boost::filesystem::is_regular(p)) { 94 NSC_DEBUG_MSG_STD(_T("Using certificate: ") + p.string()); 95 } else { 96 NSC_LOG_ERROR_STD(_T("Certificate not found: ") + p.string()); 97 } 98 } 99 } 100 101 }; 102 103 nscapi::targets::handler<custom_reader> targets; 44 104 client::command_manager commands; 45 105 … … 57 117 if (recipient.has_data("no ssl")) 58 118 use_ssl = !recipient.get_bool_data("no ssl"); 59 net::url url = recipient.get_url(5668); 60 host = url.host; 61 port = url.get_port(); 119 host = recipient.address.host; 120 port = recipient.address.get_port(5668); 62 121 } 63 122 … … 84 143 virtual nscapi::functions::destination_container lookup_target(std::wstring &id) { 85 144 nscapi::functions::destination_container ret; 86 nscapi::target_handler::optarget t = instance->targets.find_target(id); 87 if (t) { 88 if (!t->alias.empty()) 89 ret.id = utf8::cvt<std::string>(t->alias); 90 if (!t->host.empty()) 91 ret.host = utf8::cvt<std::string>(t->host); 92 if (t->has_option("address")) 93 ret.address = utf8::cvt<std::string>(t->options[_T("address")]); 94 else 95 ret.address = utf8::cvt<std::string>(t->host); 96 BOOST_FOREACH(const nscapi::target_handler::target::options_type::value_type &kvp, t->options) { 97 ret.data[utf8::cvt<std::string>(kvp.first)] = utf8::cvt<std::string>(kvp.second); 98 } 99 } 145 nscapi::targets::optional_target_object opt = instance->targets.find_object(id); 146 if (opt) 147 return opt->to_destination_container(); 100 148 return ret; 101 149 } -
modules/SMTPClient/SMTPClient.cpp
ra87ce04 re396b2f 79 79 80 80 ; 81 82 settings.alias().add_key_to_settings(_T("targets/default"))83 84 (_T("timeout"), sh::uint_key(&timeout, 30),85 _T("TIMEOUT"), _T("Timeout when reading/writing packets to/from sockets."))86 87 (_T("sender"), sh::wpath_key(&sender, _T("nscp@localhost")),88 _T("SENDER"), _T("Sender of email message"))89 90 (_T("recipient"), sh::wpath_key(&recipient, _T("nscp@localhost")),91 _T("RECIPIENT"), _T("Recipient of email message"))92 93 (_T("template"), sh::wpath_key(&template_string, _T("Hello, this is %source% reporting %message%!")),94 _T("TEMPLATE"), _T("Template for message data"))95 96 ;97 98 81 settings.register_all(); 99 82 settings.notify(); 100 83 101 84 get_core()->registerSubmissionListener(get_id(), channel_); 102 103 if (!targets.has_target(_T("default"))) {104 add_target(_T("default"), _T("default"));105 targets.rebuild();106 }107 nscapi::target_handler::optarget t = targets.find_target(_T("default"));108 if (t) {109 if (!t->has_option("template"))110 t->options[_T("template")] = template_string;111 if (!t->has_option("timeout"))112 t->options[_T("timeout")] = strEx::itos(timeout);113 if (!t->has_option("recipient"))114 t->options[_T("recipient")] = recipient;115 if (!t->has_option("sender"))116 t->options[_T("sender")] = sender;117 targets.add(*t);118 } else {119 NSC_LOG_ERROR(_T("Default target not found!"));120 }121 122 85 } catch (nscapi::nscapi_exception &e) { 123 86 NSC_LOG_ERROR_STD(_T("NSClient API exception: ") + utf8::to_unicode(e.what())); … … 223 186 add_local_options(config.local, config.data); 224 187 225 net::wurl url; 226 url.protocol = _T("smtp"); 227 url.port = 25; 228 nscapi::target_handler::optarget opt = targets.find_target(_T("default")); 188 config.data->recipient.id = "default"; 189 config.data->recipient.address = net::parse("smtp://localhost:25"); 190 nscapi::targets::optional_target_object opt = targets.find_object(_T("default")); 229 191 if (opt) { 230 nscapi::target_handler::target t = *opt; 231 url.host = t.host; 232 if (t.has_option("port")) { 233 try { 234 url.port = strEx::stoi(t.options[_T("port")]); 235 } catch (...) {} 236 } 237 std::string keys[] = {"sender", "timeout", "recipient", "template"}; 238 BOOST_FOREACH(std::string s, keys) { 239 config.data->recipient.data[s] = utf8::cvt<std::string>(t.options[utf8::cvt<std::wstring>(s)]); 240 } 241 } 242 config.data->recipient.id = "default"; 243 config.data->recipient.address = utf8::cvt<std::string>(url.to_string()); 192 nscapi::functions::destination_container def = opt->to_destination_container(); 193 config.data->recipient.import(def); 194 } 244 195 config.data->host_self.id = "self"; 245 196 //config.data->host_self.host = hostname_; -
modules/SMTPClient/SMTPClient.h
rf33c12f re396b2f 30 30 31 31 namespace po = boost::program_options; 32 namespace sh = nscapi::settings_helper; 32 33 33 34 class SMTPClient : public nscapi::impl::simple_plugin { … … 37 38 std::wstring target_path; 38 39 39 nscapi::target_handler targets; 40 struct custom_reader { 41 typedef nscapi::targets::target_object object_type; 42 typedef nscapi::targets::target_object target_object; 43 44 45 static void init_default(target_object &target) { 46 target.set_property_int(_T("timeout"), 30); 47 target.set_property_string(_T("sender"), _T("nscp@localhost")); 48 target.set_property_string(_T("recipient"), _T("nscp@localhost")); 49 target.set_property_string(_T("template"), _T("Hello, this is %source% reporting %message%!")); 50 } 51 static void post_process_target(target_object &target) {} 52 53 static void add_custom_keys(sh::settings_registry &settings, boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object) { 54 settings.path(object.path).add_key() 55 56 (_T("timeout"), sh::int_fun_key<int>(boost::bind(&object_type::set_property_int, &object, _T("timeout"), _1), 30), 57 _T("TIMEOUT"), _T("Timeout when reading/writing packets to/from sockets.")) 58 59 (_T("sender"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("sender"), _1), _T("nscp@localhost")), 60 _T("SENDER"), _T("Sender of email message")) 61 62 (_T("recipient"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("recipient"), _1), _T("nscp@localhost")), 63 _T("RECIPIENT"), _T("Recipient of email message")) 64 65 (_T("template"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("template"), _1), _T("Hello, this is %source% reporting %message%!")), 66 _T("TEMPLATE"), _T("Template for message data")) 67 ; 68 } 69 }; 70 71 nscapi::targets::handler<custom_reader> targets; 40 72 client::command_manager commands; 41 73 … … 54 86 template_string = recipient.get_string_data("template"); 55 87 56 net::url url = recipient.get_url(25); 57 host = url.host; 58 port = url.get_port(); 88 host = recipient.address.host; 89 port = recipient.address.get_port(25); 59 90 } 60 91 … … 82 113 virtual nscapi::functions::destination_container lookup_target(std::wstring &id) { 83 114 nscapi::functions::destination_container ret; 84 nscapi::target_handler::optarget t = instance->targets.find_target(id); 85 if (t) { 86 if (!t->alias.empty()) 87 ret.id = utf8::cvt<std::string>(t->alias); 88 if (!t->host.empty()) 89 ret.host = utf8::cvt<std::string>(t->host); 90 if (t->has_option("address")) 91 ret.address = utf8::cvt<std::string>(t->options[_T("address")]); 92 else 93 ret.address = utf8::cvt<std::string>(t->host); 94 BOOST_FOREACH(const nscapi::target_handler::target::options_type::value_type &kvp, t->options) { 95 ret.data[utf8::cvt<std::string>(kvp.first)] = utf8::cvt<std::string>(kvp.second); 96 } 97 } 115 nscapi::targets::optional_target_object opt = instance->targets.find_object(id); 116 if (opt) 117 return opt->to_destination_container(); 98 118 return ret; 99 119 } -
modules/Scheduler/Scheduler.cpp
rf085f9b re396b2f 118 118 _T("REPORT MODE"), _T("What to report to the server (any of the following: all, critical, warning, unknown, ok)")) 119 119 120 (_T("target"), sh::wstring_key(&item.target_id, def==NULL?_T(""):def->target_id), 121 _T("TARGET"), _T("")) 122 120 123 // TODO: get the proper default value here! 121 124 (_T("interval"), sh::wstring_key(&duration, _T("5s")), … … 167 170 // @todo: allow renaming of commands here item.alias, 168 171 // @todo this is broken, fix this (uses the wrong message) 169 nscapi::functions::make_submit_from_query(response, item.channel, item.alias );172 nscapi::functions::make_submit_from_query(response, item.channel, item.alias, item.target_id); 170 173 std::string result; 171 174 GET_CORE()->submit_message(item.channel, response, result); -
modules/Scheduler/simple_scheduler.hpp
rc015acc re396b2f 27 27 int id; 28 28 std::wstring alias; 29 std::wstring target_id; 29 30 30 31 std::wstring command; … … 46 47 target(const target &other) : id(other.id), alias(other.alias) 47 48 , command(other.command), arguments(other.arguments), tag(other.tag) 48 , duration(other.duration), report(other.report), channel(other.channel) {}49 , duration(other.duration), report(other.report), channel(other.channel), target_id(other.target_id) {} 49 50 target& operator=(target const& other) { 50 51 id = other.id; … … 58 59 report = other.report; 59 60 channel = other.channel; 61 target_id = other.target_id; 60 62 return *this; 61 63 } … … 63 65 std::wstring to_string() { 64 66 std::wstringstream ss; 65 ss << alias << _T("[") << id << _T("] = {command: ") << command << _T(", channel ") << channel<< _T("}");67 ss << alias << _T("[") << id << _T("] = {command: ") << command << _T(", channel=") << channel << _T(", target_id=") << target_id << _T("}"); 66 68 return ss.str(); 67 69 } -
modules/SyslogClient/SyslogClient.cpp
ra87ce04 re396b2f 114 114 115 115 ; 116 117 settings.alias().add_key_to_settings(_T("targets/default"))118 119 (_T("severity"), sh::wpath_key(&severity, _T("error")),120 _T("SSL CERTIFICATE"), _T(""))121 122 (_T("facility"), sh::wpath_key(&facility, _T("kernel")),123 _T("SSL CERTIFICATE"), _T(""))124 125 (_T("tag_syntax"), sh::wpath_key(&tag_syntax, _T("NSCP")),126 _T("SSL CERTIFICATE"), _T(""))127 128 (_T("message_syntax"), sh::wpath_key(&message_syntax, _T("%message%")),129 _T("SSL CERTIFICATE"), _T(""))130 131 (_T("ok severity"), sh::wpath_key(&ok_severity, _T("informational")),132 _T("SSL CERTIFICATE"), _T(""))133 134 (_T("warning severity"), sh::wpath_key(&warn_severity, _T("warning")),135 _T("SSL CERTIFICATE"), _T(""))136 137 (_T("critical severity"), sh::wpath_key(&crit_severity, _T("critical")),138 _T("SSL CERTIFICATE"), _T(""))139 140 (_T("unknown severity"), sh::wpath_key(&unknown_severity, _T("emergency")),141 _T("SSL CERTIFICATE"), _T(""))142 143 ;144 145 116 settings.register_all(); 146 117 settings.notify(); 147 118 148 119 get_core()->registerSubmissionListener(get_id(), channel_); 149 150 if (!targets.has_target(_T("default"))) {151 add_target(_T("default"), _T("default"));152 targets.rebuild();153 }154 nscapi::target_handler::optarget t = targets.find_target(_T("default"));155 if (t) {156 if (!t->has_option("severity"))157 t->options[_T("severity")] = severity;158 if (!t->has_option("facility"))159 t->options[_T("facility")] = facility;160 if (!t->has_option("tag syntax"))161 t->options[_T("tag syntax")] = tag_syntax;162 if (!t->has_option("message syntax"))163 t->options[_T("message syntax")] = message_syntax;164 if (!t->has_option("ok severity"))165 t->options[_T("ok severity")] = ok_severity;166 if (!t->has_option("warning severity"))167 t->options[_T("warning severity")] = warn_severity;168 if (!t->has_option("critical severity"))169 t->options[_T("critical severity")] = crit_severity;170 if (!t->has_option("unknown severity"))171 t->options[_T("unknown severity")] = unknown_severity;172 targets.add(*t);173 } else {174 NSC_LOG_ERROR(_T("Default target not found!"));175 }176 120 177 121 } catch (nscapi::nscapi_exception &e) { … … 284 228 add_local_options(config.local, config.data); 285 229 286 net::wurl url; 287 url.protocol = _T("syslog"); 288 url.port = 514; 289 nscapi::target_handler::optarget opt = targets.find_target(_T("default")); 230 config.data->recipient.id = "default"; 231 config.data->recipient.address = net::parse("syslog://localhost:514"); 232 nscapi::targets::optional_target_object opt = targets.find_object(_T("default")); 290 233 if (opt) { 291 nscapi::target_handler::target t = *opt; 292 url.host = t.host; 293 if (t.has_option("port")) { 294 try { 295 url.port = strEx::stoi(t.options[_T("port")]); 296 } catch (...) {} 297 } 298 std::string keys[] = {"message template", "tag template", "severity", "ok_severity", "warning_severity", "critical_severity", "unknown_severity", "facility"}; 299 BOOST_FOREACH(std::string s, keys) { 300 config.data->recipient.data[s] = utf8::cvt<std::string>(t.options[utf8::cvt<std::wstring>(s)]); 301 } 302 } 303 config.data->recipient.id = "default"; 304 config.data->recipient.address = utf8::cvt<std::string>(url.to_string()); 234 nscapi::functions::destination_container def = opt->to_destination_container(); 235 config.data->recipient.import(def); 236 } 305 237 config.data->host_self.id = "self"; 306 config.data->host_self. host = hostname_;238 config.data->host_self.address.host = hostname_; 307 239 308 240 config.target_lookup = handler; -
modules/SyslogClient/SyslogClient.h
rf33c12f re396b2f 31 31 32 32 namespace po = boost::program_options; 33 namespace sh = nscapi::settings_helper; 33 34 34 35 class SyslogClient : public nscapi::impl::simple_plugin { … … 38 39 std::wstring target_path; 39 40 40 nscapi::target_handler targets; 41 struct custom_reader { 42 typedef nscapi::targets::target_object object_type; 43 typedef nscapi::targets::target_object target_object; 44 45 static void init_default(target_object &target) { 46 target.set_property_string(_T("severity"), _T("error")); 47 target.set_property_string(_T("facility"), _T("kernel")); 48 target.set_property_string(_T("tag syntax"), _T("NSCA")); 49 target.set_property_string(_T("message syntax"), _T("%message%")); 50 target.set_property_string(_T("ok severity"), _T("informational")); 51 target.set_property_string(_T("warning severity"), _T("warning")); 52 target.set_property_string(_T("critical severity"), _T("critical")); 53 target.set_property_string(_T("unknown severity"), _T("emergency")); 54 } 55 56 static void post_process_target(target_object &target) {} 57 58 static void add_custom_keys(sh::settings_registry &settings, boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object) { 59 settings.path(object.path).add_key() 60 61 (_T("severity"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("severity"), _1), _T("error")), 62 _T("TODO"), _T("")) 63 64 (_T("facility"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("facility"), _1), _T("kernel")), 65 _T("TODO"), _T("")) 66 67 (_T("tag_syntax"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("tag syntax"), _1), _T("NSCA")), 68 _T("TODO"), _T("")) 69 70 (_T("message_syntax"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("message syntax"), _1), _T("%message%")), 71 _T("TODO"), _T("")) 72 73 (_T("ok severity"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("ok severity"), _1), _T("informational")), 74 _T("TODO"), _T("")) 75 76 (_T("warning severity"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("warning severity"), _1), _T("warning")), 77 _T("TODO"), _T("")) 78 79 (_T("critical severity"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("critical severity"), _1), _T("critical")), 80 _T("TODO"), _T("")) 81 82 (_T("unknown severity"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("unknown severity"), _1), _T("emergency")), 83 _T("TODO"), _T("")) 84 ; 85 } 86 }; 87 88 nscapi::targets::handler<custom_reader> targets; 41 89 client::command_manager commands; 42 90 … … 66 114 unknown_severity = recipient.data["unknown severity"]; 67 115 68 net::url url = recipient.get_url(514); 69 host = url.host; 70 port = url.get_port(); 116 host = recipient.address.host; 117 port = recipient.address.get_port(514); 71 118 } 72 119 … … 94 141 virtual nscapi::functions::destination_container lookup_target(std::wstring &id) { 95 142 nscapi::functions::destination_container ret; 96 nscapi::target_handler::optarget t = instance->targets.find_target(id); 97 if (t) { 98 if (!t->alias.empty()) 99 ret.id = utf8::cvt<std::string>(t->alias); 100 if (!t->host.empty()) 101 ret.host = utf8::cvt<std::string>(t->host); 102 if (t->has_option("address")) 103 ret.address = utf8::cvt<std::string>(t->options[_T("address")]); 104 else 105 ret.address = utf8::cvt<std::string>(t->host); 106 BOOST_FOREACH(const nscapi::target_handler::target::options_type::value_type &kvp, t->options) { 107 ret.data[utf8::cvt<std::string>(kvp.first)] = utf8::cvt<std::string>(kvp.second); 108 } 109 } 143 nscapi::targets::optional_target_object opt = instance->targets.find_object(id); 144 if (opt) 145 return opt->to_destination_container(); 110 146 return ret; 111 147 } -
scripts/python/test_nrpe.py
r441a022 re396b2f 137 137 None 138 138 139 def submit_payload(self, alias, ssl, length, source, status, msg, perf ):139 def submit_payload(self, alias, ssl, length, source, status, msg, perf, target): 140 140 message = plugin_pb2.QueryRequestMessage() 141 141 142 142 message.header.version = plugin_pb2.Common.VERSION_1 143 message.header.recipient_id = "test_rp"143 message.header.recipient_id = target 144 144 host = message.header.hosts.add() 145 145 host.address = "127.0.0.1:15666" 146 host.id = "test_rp" 146 host.id = target 147 if (target == 'valid'): 148 pass 149 else: 150 enc = host.metadata.add() 151 enc.key = "use ssl" 152 enc.value = '%s'%ssl 153 enc = host.metadata.add() 154 enc.key = "payload length" 155 enc.value = '%d'%length 147 156 enc = host.metadata.add() 148 enc.key = "use ssl" 149 enc.value = '%s'%ssl 150 enc = host.metadata.add() 151 enc.key = "payload length" 152 enc.value = '%d'%length 157 enc.key = "timeout" 158 enc.value = '5' 153 159 154 160 uid = str(uuid.uuid4()) … … 164 170 response_message = plugin_pb2.QueryResponseMessage() 165 171 response_message.ParseFromString(response) 166 result = TestResult( )172 result = TestResult('Testing NRPE: %s for %s'%(alias, target)) 167 173 168 174 found = False … … 180 186 break 181 187 else: 182 log('Waiting for %s '%uid)188 log('Waiting for %s (%s/%s)'%(uid,alias,target)) 183 189 sleep(1) 184 190 if found: … … 187 193 188 194 def test_one(self, ssl=True, length=1024, state = status.UNKNOWN, tag = 'TODO'): 189 r1 = self.submit_payload('%s/%s/%s'%(ssl, length, tag), ssl, length, '%ssrc%s'%(tag, tag), state, '%smsg%s'%(tag, tag), '') 190 if r1: 191 return r1 192 result = TestResult() 193 result.add_message(True, '*FAILED* to send message with %s (retrying)'%tag) 194 r2 = self.submit_payload('%s/%s'%(ssl, length), ssl, length, '%ssrc%s'%(tag, tag), state, '%smsg%s'%(tag, tag), '') 195 if r2: 196 result.add(r2) 197 return result 198 result.add_message(False, 'Failed to send message with: %s/%s'%(ssl, length)) 195 result = TestResult('Testing NRPE: %s/%s/%s with various targets'%(ssl, length, tag)) 196 result.add(self.submit_payload('%s/%s/%s'%(ssl, length, tag), ssl, length, '%ssrc%s'%(tag, tag), state, '%smsg%s'%(tag, tag), '', "valid")) 197 result.add(self.submit_payload('%s/%s/%s'%(ssl, length, tag), ssl, length, '%ssrc%s'%(tag, tag), state, '%smsg%s'%(tag, tag), '', "test_rp")) 198 result.add(self.submit_payload('%s/%s/%s'%(ssl, length, tag), ssl, length, '%ssrc%s'%(tag, tag), state, '%smsg%s'%(tag, tag), '', "invalid")) 199 199 return result 200 200 … … 206 206 # TODO: conf.set_string('/settings/NRPE/test_nrpe_server', 'certificate', ssl) 207 207 core.reload('test_nrpe_server') 208 209 # /settings/NRPE/test_nrpe_client/targets/default 210 conf.set_string('/settings/NRPE/test_nrpe_client/targets/default', 'address', 'nrpe://127.0.0.1:35666') 211 conf.set_bool('/settings/NRPE/test_nrpe_client/targets/default', 'use ssl', not ssl) 212 conf.set_int('/settings/NRPE/test_nrpe_client/targets/default', 'payload length', length*3) 213 214 conf.set_string('/settings/NRPE/test_nrpe_client/targets/invalid', 'address', 'nrpe://127.0.0.1:25666') 215 conf.set_bool('/settings/NRPE/test_nrpe_client/targets/invalid', 'use ssl', not ssl) 216 conf.set_int('/settings/NRPE/test_nrpe_client/targets/invalid', 'payload length', length*2) 217 218 conf.set_string('/settings/NRPE/test_nrpe_client/targets/valid', 'address', 'nrpe://127.0.0.1:15666') 219 conf.set_bool('/settings/NRPE/test_nrpe_client/targets/valid', 'use ssl', ssl) 220 conf.set_int('/settings/NRPE/test_nrpe_client/targets/valid', 'payload length', length) 221 core.reload('test_nrpe_client') 222 208 223 result = TestResult() 209 224 result.add_message(isOpen('127.0.0.1', 15666), 'Checking that port is open (server is up)') -
scripts/python/test_nsca.py
r28f6a64 re396b2f 218 218 '--encryption', encryption, 219 219 '--password', 'pwd-%s'%encryption, 220 '-- host', '127.0.0.1:15667'220 '--address', '127.0.0.1:15667' 221 221 ] 222 222 (result_code, result_message) = core.simple_exec('any', 'nsca_submit', args) … … 225 225 result.add_message(result_code == 0, 'Testing to send message using %s/exec:1'%tag) 226 226 result.add_message(len(result_message) == 1, 'Testing to send message using %s/exec:2'%tag, len(result_message)) 227 result.add_message(len(result_message[0]) == 0, 'Testing to send message using %s/exec:3'%tag, result_message[0]) 228 self.wait_and_validate(uid, result, msg, perf, '%s/exec'%tag) 227 if len(result_message) > 0: 228 result.add_message(len(result_message[0]) == 0, 'Testing to send message using %s/exec:3'%tag, result_message[0]) 229 self.wait_and_validate(uid, result, msg, perf, '%s/exec'%tag) 230 else: 231 result.add_message(False, 'Sending faliled: giving up on %s'%tag) 229 232 return result 230 233 -
service/NSCPlugin.cpp
rf085f9b re396b2f 323 323 throw NSPluginException(module_, _T("Unhandled exception in fUnLoadModule.")); 324 324 } 325 fHandleCommand = NULL;326 325 } 327 326 void NSCPlugin::unload_dll() { 328 327 if (isLoaded()) 329 328 return; 329 fModuleHelperInit = NULL; 330 fLoadModule = NULL; 331 fGetName = NULL; 332 fGetVersion = NULL; 333 fGetDescription = NULL; 334 fHasCommandHandler = NULL; 335 fHasMessageHandler = NULL; 336 fHandleCommand = NULL; 337 fDeleteBuffer = NULL; 338 fHandleMessage = NULL; 339 fUnLoadModule = NULL; 340 fCommandLineExec = NULL; 341 fHasNotificationHandler = NULL; 342 fHandleNotification = NULL; 343 fHasRoutingHandler = NULL; 344 fRouteMessage = NULL; 330 345 module_.unload_library(); 331 346 } -
service/NSCPlugin.h
rf7a074d re396b2f 52 52 * 53 53 */ 54 class NSPluginException {54 class NSPluginException : public std::exception { 55 55 public: 56 56 std::wstring file_; // DLL filename (for which the exception was thrown) 57 57 std::wstring error_; // An error message (human readable format) 58 std::string msg_; 58 59 /** 59 60 * @param file DLL filename (for which the exception is thrown) … … 62 63 NSPluginException(dll::dll &module, std::wstring error) : error_(error) { 63 64 file_ = module.get_module_name(); 64 } 65 std::wstring what() { 66 return error_ + _T(" in file: ") + file_; 67 } 68 69 65 msg_ = utf8::cvt<std::string>(error_ + _T(" in file: ") + file_); 66 } 67 68 ~NSPluginException() throw() {} 69 const char* what() const throw() { 70 return msg_.c_str(); 71 } 72 const std::wstring wwhat() const throw() { 73 return utf8::cvt<std::wstring>(msg_); 74 } 70 75 }; 71 76 -
service/NSClient++.cpp
r8013c0c re396b2f 463 463 try { 464 464 addPlugin(pluginPath / v.second, v.first); 465 } catch ( NSPluginException &e) {466 LOG_CRITICAL_CORE_STD(_T("Failed to register plugin: ") + e.w hat());465 } catch (const NSPluginException &e) { 466 LOG_CRITICAL_CORE_STD(_T("Failed to register plugin: ") + e.wwhat()); 467 467 } catch (...) { 468 468 LOG_CRITICAL_CORE_STD(_T("Failed to register plugin key: ") + v.second); … … 921 921 LOG_DEBUG_CORE_STD(_T("Skipping log plugin: ") + p->getModule() + _T("...")); 922 922 } 923 } catch( NSPluginExceptione) {923 } catch(const NSPluginException &e) { 924 924 LOG_ERROR_CORE_STD(_T("Exception raised when unloading plugin: ") + e.error_ + _T(" in module: ") + e.file_); 925 925 } catch(...) { … … 1139 1139 LOG_DEBUG_CORE_STD(_T("Result ") + cmd + _T(": ") + nscapi::plugin_helper::translateReturn(c)); 1140 1140 return c; 1141 } catch (nsclient::commands::command_exception &e) { 1142 LOG_ERROR_CORE(_T("No handler for command: ") + cmd + _T(": ") + to_wstring(e.what())); 1141 } catch (const nsclient::commands::command_exception &e) { 1142 LOG_ERROR_CORE(_T("Failed to process command: ") + cmd + _T(": ") + utf8::to_unicode(e.what())); 1143 return NSCAPI::returnIgnored; 1144 } catch (const std::exception &e) { 1145 LOG_ERROR_CORE(_T("Failed to process command: ") + cmd + _T(": ") + utf8::to_unicode(e.what())); 1143 1146 return NSCAPI::returnIgnored; 1144 1147 } catch (...) {
Note: See TracChangeset
for help on using the changeset viewer.








