Changeset 96c1461 in nscp for include/nscapi/functions.hpp
- Timestamp:
- 11/27/11 22:04:35 (18 months ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- 9853bc3
- Parents:
- 16198e3
- File:
-
- 1 edited
-
include/nscapi/functions.hpp (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
include/nscapi/functions.hpp
rf7a074d r96c1461 127 127 std::string host; 128 128 std::string address; 129 std::string protocol;130 129 std::string comment; 131 130 std::list<std::string> tags; … … 133 132 data_map data; 134 133 135 net::url get_url(unsigned int port = 80) { 134 std::string get_protocol() const { 135 net::url url = get_url(); 136 return url.protocol; 137 } 138 bool has_protocol() const { 139 return !address.empty(); 140 } 141 net::url get_url(unsigned int port = 80) const { 136 142 return net::parse(address, port); 143 } 144 static bool to_bool(std::string value) { 145 if (value.empty()) 146 return false; 147 if (value == "true" || value == "1") 148 return true; 149 return false; 150 } 151 static int to_int(std::string value, int def = 0) { 152 if (value.empty()) 153 return def; 154 try { 155 return boost::lexical_cast<int>(value); 156 } catch (...) { 157 return def; 158 } 159 } 160 161 inline int get_int_data(std::string key, int def = 0) { 162 return to_int(data[key]); 163 } 164 inline bool get_bool_data(std::string key, int def = 0) { 165 return to_bool(data[key]); 166 } 167 inline std::string get_string_data(std::string key) { 168 return data[key]; 169 } 170 inline bool has_data(std::string key) { 171 return data.find(key) != data.end(); 172 } 173 174 inline net::url get_url(int default_port) { 175 return net::parse(address, default_port); 176 } 177 void set_string_data(std::string key, std::string value) { 178 data[key] = value; 179 } 180 void set_int_data(std::string key, int value) { 181 data[key] = boost::lexical_cast<std::string>(value); 182 } 183 void set_bool_data(std::string key, bool value) { 184 data[key] = value?"true":"false"; 137 185 } 138 186 … … 142 190 ss << ", host: " << host; 143 191 ss << ", address: " << address; 144 ss << ", protocol: " << protocol;192 // ss << ", protocol: " << protocol; 145 193 ss << ", comment: " << comment; 146 194 int i=0; … … 161 209 if (!other.address.empty()) 162 210 address = other.address; 163 if (!other.protocol.empty())164 protocol = other.protocol;211 // if (!other.protocol.empty()) 212 // protocol = other.protocol; 165 213 if (!other.comment.empty()) 166 214 comment = other.comment; … … 182 230 if (!dst.address.empty()) 183 231 host->set_address(dst.address); 184 if (!dst.protocol.empty())185 host->set_protocol(dst.protocol);232 if (!dst.has_protocol()) 233 host->set_protocol(dst.get_protocol()); 186 234 if (!dst.comment.empty()) 187 235 host->set_comment(dst.comment); … … 205 253 if (!host.address().empty()) 206 254 data.address = host.address(); 207 if (!host.protocol().empty()) 208 data.protocol = host.protocol(); 255 if (data.address.empty() && !host.host().empty()) 256 data.address = host.host(); 257 // if (!host.protocol().empty()) 258 // data.protocol = host.protocol(); 209 259 if (!host.comment().empty()) 210 260 data.comment = host.comment(); … … 291 341 return gbp_to_nagios_status(payload.result()); 292 342 } 343 /* 293 344 static NSCAPI::errorReturn parse_simple_submit_request_payload(const Plugin::QueryResponseMessage::Response &payload, std::wstring &alias, std::wstring &command, std::wstring &msg, std::wstring &perf) { 294 345 alias = utf8::cvt<std::wstring>(payload.alias()); … … 298 349 return gbp_to_nagios_status(payload.result()); 299 350 } 300 static NSCAPI::errorReturn parse_simple_submit_response(const std::string &request, std::wstring response) { 351 */ 352 static int parse_simple_submit_request_payload(const Plugin::QueryResponseMessage::Response &payload, std::wstring &alias, std::wstring &message) { 353 alias = utf8::cvt<std::wstring>(payload.alias()); 354 message = utf8::cvt<std::wstring>(payload.message()); 355 return gbp_to_nagios_status(payload.result()); 356 } 357 static void parse_simple_query_request_payload(const Plugin::QueryRequestMessage::Request &payload, std::wstring &alias, std::wstring &command) { 358 alias = utf8::cvt<std::wstring>(payload.alias()); 359 command = utf8::cvt<std::wstring>(payload.command()); 360 } 361 static NSCAPI::errorReturn parse_simple_submit_response(const std::string &request, std::wstring &response) { 301 362 Plugin::SubmitResponseMessage message; 302 363 message.ParseFromString(request); … … 357 418 message.SerializeToString(&buffer); 358 419 } 359 static void append_simple_query_response_payload(Plugin::QueryResponseMessage::Response *payload, std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf) { 420 421 422 static void append_simple_submit_request_payload(Plugin::QueryResponseMessage::Response *payload, std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf = _T("")) { 360 423 payload->set_command(to_string(command)); 361 424 payload->set_message(to_string(msg)); … … 365 428 } 366 429 430 static void append_simple_query_response_payload(Plugin::QueryResponseMessage::Response *payload, std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf) { 431 payload->set_command(to_string(command)); 432 payload->set_message(to_string(msg)); 433 payload->set_result(nagios_status_to_gpb(ret)); 434 if (!perf.empty()) 435 parse_performance_data(payload, perf); 436 } 437 438 static void append_simple_exec_response_payload(Plugin::ExecuteResponseMessage::Response *payload, std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg) { 439 payload->set_command(to_string(command)); 440 payload->set_message(to_string(msg)); 441 payload->set_result(nagios_status_to_gpb(ret)); 442 } 443 444 445 static void append_simple_query_request_payload(Plugin::QueryRequestMessage::Request *payload, std::wstring command, std::vector<std::wstring> arguments) { 446 payload->set_command(to_string(command)); 447 BOOST_FOREACH(const std::wstring &s, arguments) { 448 payload->add_arguments(to_string(s)); 449 } 450 } 451 452 static void append_simple_exec_request_payload(Plugin::ExecuteRequestMessage::Request *payload, std::wstring command, std::vector<std::wstring> arguments) { 453 payload->set_command(to_string(command)); 454 BOOST_FOREACH(const std::wstring &s, arguments) { 455 payload->add_arguments(to_string(s)); 456 } 457 } 458 459 367 460 static decoded_simple_command_data parse_simple_query_request(const wchar_t* char_command, const std::string &request) { 368 461 decoded_simple_command_data data; … … 378 471 for (int i=0;i<payload.arguments_size();i++) { 379 472 data.args.push_back(to_wstring(payload.arguments(i))); 473 } 474 return data; 475 } 476 static decoded_simple_command_data parse_simple_query_request(const ::Plugin::QueryRequestMessage::Request &payload) { 477 decoded_simple_command_data data; 478 data.command = utf8::cvt<std::wstring>(payload.command()); 479 for (int i=0;i<payload.arguments_size();i++) { 480 data.args.push_back(utf8::cvt<std::wstring>(payload.arguments(i))); 380 481 } 381 482 return data; … … 434 535 for (int i=0;i<message.payload_size(); i++) { 435 536 result.push_back(utf8::cvt<std::wstring>(message.payload(i).message())); 537 } 538 } 539 static void parse_simple_exec_result(const std::string &response, std::wstring &result) { 540 Plugin::ExecuteResponseMessage message; 541 message.ParseFromString(response); 542 543 for (int i=0;i<message.payload_size(); i++) { 544 result += utf8::cvt<std::wstring>(message.payload(i).message()); 436 545 } 437 546 } … … 463 572 for (int i=0;i<payload.arguments_size();i++) { 464 573 data.args.push_back(to_wstring(payload.arguments(i))); 574 } 575 return data; 576 } 577 static decoded_simple_command_data parse_simple_exec_request_payload(const Plugin::ExecuteRequestMessage::Request &payload) { 578 decoded_simple_command_data data; 579 data.command = utf8::cvt<std::wstring>(payload.command()); 580 for (int i=0;i<payload.arguments_size();i++) { 581 data.args.push_back(utf8::cvt<std::wstring>(payload.arguments(i))); 465 582 } 466 583 return data;
Note: See TracChangeset
for help on using the changeset viewer.








