Changeset b38e845 in nscp
- Timestamp:
- 08/24/11 18:37:39 (22 months ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- d7e265d
- Parents:
- 0b8df3e
- Files:
-
- 4 deleted
- 25 edited
-
changelog (modified) (1 diff)
-
include/nscapi/functions.hpp (modified) (7 diffs)
-
include/nscapi/nscapi_core_wrapper.cpp (modified) (5 diffs)
-
include/nscapi/nscapi_plugin_wrapper.cpp (modified) (5 diffs)
-
include/nscapi/nscapi_plugin_wrapper.hpp (modified) (2 diffs)
-
include/nscp/client/socket.hpp (modified) (4 diffs)
-
include/nscp/packet.cpp (modified) (1 diff)
-
include/nscp/packet.hpp (modified) (4 diffs)
-
include/nscp/server/connection.cpp (modified) (4 diffs)
-
include/nscp/server/connection.hpp (modified) (1 diff)
-
include/nscp/server/handler.hpp (modified) (1 diff)
-
include/nscp/server/parser.hpp (modified) (2 diffs)
-
libs/protobuf/CMakeLists.txt (modified) (2 diffs)
-
libs/protobuf/envelope.proto (deleted)
-
libs/protobuf/exec.proto (deleted)
-
libs/protobuf/ipc.proto (modified) (2 diffs)
-
libs/protobuf/log.proto (deleted)
-
libs/protobuf/plugin.proto (modified) (2 diffs)
-
libs/protobuf/settings.proto (deleted)
-
modules/CheckExternalScripts/CheckExternalScripts.cpp (modified) (3 diffs)
-
modules/NSCPClient/NSCPClient.cpp (modified) (4 diffs)
-
modules/NSCPClient/NSCPClient.h (modified) (1 diff)
-
modules/NSCPServer/handler_impl.cpp (modified) (2 diffs)
-
modules/NSCPServer/handler_impl.hpp (modified) (1 diff)
-
modules/PythonScript/PythonScript.cpp (modified) (3 diffs)
-
service/NSClient++.cpp (modified) (6 diffs)
-
service/NSClient++.h (modified) (1 diff)
-
service/cli_parser.hpp (modified) (1 diff)
-
service/logger.hpp (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
changelog
r0b8df3e rb38e845 6 6 * Fix RtlStringFromGUID problem on NT4 7 7 8 2011-08-22 MickeM 8 2011-08-24 MickeM 9 * Sever refactoring of the new API (there is now two pb files ipc for NSCP protocol and plugin for plugin communication) 10 * Cleaned up API helper functions 11 12 2011-08-23 MickeM 9 13 * Fixed issue in the NSCP server 10 14 -
include/nscapi/functions.hpp
r438998b rb38e845 37 37 38 38 #include <protobuf/plugin.pb.h> 39 #include <protobuf/log.pb.h>40 #include <protobuf/exec.pb.h>41 39 42 40 using namespace nscp::helpers; 43 41 44 42 namespace nscapi { 45 46 /*47 class nscapi_exception : public exception {48 std::string what_;49 public:50 nscapi_exception() {}51 nscapi_exception(std::string what) : what_(what) {}52 virtual const char* what() const throw() {53 return what_;54 }55 };56 */57 43 class functions { 58 44 public: 59 static Plugin Command::Response_Code nagios_to_gpb(int ret) {45 static Plugin::Common::ResultCode nagios_status_to_gpb(int ret) { 60 46 if (ret == NSCAPI::returnOK) 61 return Plugin Command::Response_Code_OK;47 return Plugin::Common_ResultCode_OK; 62 48 if (ret == NSCAPI::returnWARN) 63 return Plugin Command::Response_Code_WARNING;49 return Plugin::Common_ResultCode_WARNING; 64 50 if (ret == NSCAPI::returnCRIT) 65 return Plugin Command::Response_Code_CRITCAL;66 return Plugin Command::Response_Code_UNKNOWN;67 } 68 static ExecuteCommand::Response_Code exec_nagios_to_gpb(intret) {69 if (ret == NSCAPI::returnOK)70 return ExecuteCommand::Response_Code_OK;71 if (ret == NSCAPI::returnWARN)72 return ExecuteCommand::Response_Code_WARNING;73 if (ret == NSCAPI::returnCRIT)74 return ExecuteCommand::Response_Code_CRITCAL;75 return ExecuteCommand::Response_Code_UNKNOWN;76 } 77 static LogMessage::Message_Level log_to_gpb(NSCAPI::messageTypes ret) {51 return Plugin::Common_ResultCode_CRITCAL; 52 return Plugin::Common_ResultCode_UNKNOWN; 53 } 54 static int gbp_to_nagios_status(Plugin::Common::ResultCode ret) { 55 if (ret == Plugin::Common_ResultCode_OK) 56 return NSCAPI::returnOK; 57 if (ret == Plugin::Common_ResultCode_WARNING) 58 return NSCAPI::returnWARN; 59 if (ret == Plugin::Common_ResultCode_CRITCAL) 60 return NSCAPI::returnCRIT; 61 return NSCAPI::returnUNKNOWN; 62 } 63 static Plugin::LogEntry::Entry::Level log_to_gpb(NSCAPI::messageTypes ret) { 78 64 if (ret == NSCAPI::critical) 79 return ::LogMessage::Message_Level_LOG_CRITICAL;65 return Plugin::LogEntry_Entry_Level_LOG_CRITICAL; 80 66 if (ret == NSCAPI::debug) 81 return ::LogMessage::Message_Level_LOG_DEBUG;67 return Plugin::LogEntry_Entry_Level_LOG_DEBUG; 82 68 if (ret == NSCAPI::error) 83 return ::LogMessage::Message_Level_LOG_ERROR;69 return Plugin::LogEntry_Entry_Level_LOG_ERROR; 84 70 if (ret == NSCAPI::log) 85 return ::LogMessage::Message_Level_LOG_INFO;71 return Plugin::LogEntry_Entry_Level_LOG_INFO; 86 72 if (ret == NSCAPI::warning) 87 return ::LogMessage::Message_Level_LOG_WARNING;88 return ::LogMessage::Message_Level_LOG_ERROR;89 } 90 static NSCAPI::messageTypes gpb_to_log( LogMessage::Message_Level ret) {91 if (ret == ::LogMessage::Message_Level_LOG_CRITICAL)73 return Plugin::LogEntry_Entry_Level_LOG_WARNING; 74 return Plugin::LogEntry_Entry_Level_LOG_ERROR; 75 } 76 static NSCAPI::messageTypes gpb_to_log(Plugin::LogEntry::Entry::Level ret) { 77 if (ret == Plugin::LogEntry_Entry_Level_LOG_CRITICAL) 92 78 return NSCAPI::critical; 93 if (ret == ::LogMessage::Message_Level_LOG_DEBUG)79 if (ret == Plugin::LogEntry_Entry_Level_LOG_DEBUG) 94 80 return NSCAPI::debug; 95 if (ret == ::LogMessage::Message_Level_LOG_ERROR)81 if (ret == Plugin::LogEntry_Entry_Level_LOG_ERROR) 96 82 return NSCAPI::error; 97 if (ret == ::LogMessage::Message_Level_LOG_INFO)83 if (ret == Plugin::LogEntry_Entry_Level_LOG_INFO) 98 84 return NSCAPI::log; 99 if (ret == ::LogMessage::Message_Level_LOG_WARNING)85 if (ret == Plugin::LogEntry_Entry_Level_LOG_WARNING) 100 86 return NSCAPI::warning; 101 87 return NSCAPI::error; … … 117 103 }; 118 104 119 105 106 107 static void create_simple_header(Plugin::Common::Header* hdr, Plugin::Common_Header_Type type) { 108 hdr->set_type(type); 109 hdr->set_version(Plugin::Common_Version_VERSION_1); 110 hdr->set_max_supported_version(Plugin::Common_Version_VERSION_1); 111 // @todo add additional fields here! 112 } 113 114 115 ////////////////////////////////////////////////////////////////////////// 116 117 static void create_simple_query_request(std::wstring command, std::vector<std::wstring> arguments, std::string &buffer) { 118 Plugin::QueryRequestMessage message; 119 create_simple_header(message.mutable_header(), Plugin::Common_Header_Type_QUERY_REQUEST); 120 121 Plugin::QueryRequestMessage::Request *payload = message.add_payload(); 122 payload->set_command(to_string(command)); 123 124 BOOST_FOREACH(std::wstring s, arguments) 125 payload->add_arguments(to_string(s)); 126 127 message.SerializeToString(&buffer); 128 } 129 static void create_simple_query_request(std::wstring command, std::list<std::wstring> arguments, std::string &buffer) { 130 Plugin::QueryRequestMessage message; 131 create_simple_header(message.mutable_header(), Plugin::Common_Header_Type_QUERY_REQUEST); 132 133 Plugin::QueryRequestMessage::Request *payload = message.add_payload(); 134 payload->set_command(to_string(command)); 135 136 BOOST_FOREACH(std::wstring s, arguments) 137 payload->add_arguments(to_string(s)); 138 139 message.SerializeToString(&buffer); 140 } 141 static NSCAPI::nagiosReturn create_simple_query_response_unknown(std::wstring msg, std::wstring perf, std::string &buffer, std::wstring command = _T("")) { 142 create_simple_query_response(NSCAPI::returnUNKNOWN, msg, perf, buffer, command); 143 return NSCAPI::returnUNKNOWN; 144 } 145 146 static void create_simple_query_response(NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf, std::string &buffer, std::wstring command = _T("")) { 147 Plugin::QueryResponseMessage message; 148 create_simple_header(message.mutable_header(), Plugin::Common_Header_Type_QUERY_RESPONSE); 149 150 Plugin::QueryResponseMessage::Response *payload = message.add_payload(); 151 if (!command.empty()) 152 payload->set_command(to_string(command)); 153 payload->set_message(to_string(msg)); 154 payload->set_result(nagios_status_to_gpb(ret)); 155 if (!perf.empty()) 156 parse_performance_data(payload, perf); 157 158 message.SerializeToString(&buffer); 159 } 160 161 static decoded_simple_command_data parse_simple_query_request(const wchar_t* char_command, const std::string &request) { 162 decoded_simple_command_data data; 163 164 data.command = char_command; 165 Plugin::QueryRequestMessage message; 166 message.ParseFromString(request); 167 168 if (message.payload_size() != 1) { 169 throw nscapi_exception(_T("Whoops, invalid payload size (for now)")); 170 } 171 ::Plugin::QueryRequestMessage::Request payload = message.payload().Get(0); 172 for (int i=0;i<payload.arguments_size();i++) { 173 data.args.push_back(to_wstring(payload.arguments(i))); 174 } 175 return data; 176 } 177 178 static void parse_simple_query_response(std::string &response, std::wstring &msg, std::wstring &perf) { 179 Plugin::QueryResponseMessage message; 180 message.ParseFromString(response); 181 182 183 if (message.payload_size() != 1) { 184 throw nscapi_exception(_T("Whoops, invalid payload size (for now)")); 185 } 186 187 Plugin::QueryResponseMessage::Response payload = message.payload().Get(0); 188 msg = utf8::cvt<std::wstring>(payload.message()); 189 perf = utf8::cvt<std::wstring>(build_performance_data(payload)); 190 } 191 192 193 ////////////////////////////////////////////////////////////////////////// 194 195 static void create_simple_exec_request(const std::wstring &command, const std::list<std::wstring> & args, std::string &request) { 196 197 Plugin::ExecuteRequestMessage message; 198 create_simple_header(message.mutable_header(), Plugin::Common_Header_Type_EXEC_REQUEST); 199 200 Plugin::ExecuteRequestMessage::Request *payload = message.add_payload(); 201 payload->set_command(to_string(command)); 202 203 BOOST_FOREACH(std::wstring s, args) 204 payload->add_arguments(to_string(s)); 205 206 message.SerializeToString(&request); 207 } 208 static void create_simple_exec_request(const std::wstring &command, const std::vector<std::wstring> & args, std::string &request) { 209 210 Plugin::ExecuteRequestMessage message; 211 create_simple_header(message.mutable_header(), Plugin::Common_Header_Type_EXEC_REQUEST); 212 213 Plugin::ExecuteRequestMessage::Request *payload = message.add_payload(); 214 payload->set_command(to_string(command)); 215 216 BOOST_FOREACH(std::wstring s, args) 217 payload->add_arguments(to_string(s)); 218 219 message.SerializeToString(&request); 220 } 221 static void parse_simple_exec_result(const std::string &response, std::list<std::wstring> &result) { 222 Plugin::ExecuteResponseMessage message; 223 message.ParseFromString(response); 224 225 for (int i=0;i<message.payload_size(); i++) { 226 result.push_back(utf8::cvt<std::wstring>(message.payload(i).message())); 227 } 228 } 229 230 static void create_simple_exec_response(std::wstring command, NSCAPI::nagiosReturn ret, std::wstring result, std::string &response) { 231 Plugin::ExecuteResponseMessage message; 232 create_simple_header(message.mutable_header(), Plugin::Common_Header_Type_EXEC_RESPONSE); 233 234 Plugin::ExecuteResponseMessage::Response *payload = message.add_payload(); 235 payload->set_command(to_string(command)); 236 payload->set_message(to_string(result)); 237 238 payload->set_result(nagios_status_to_gpb(ret)); 239 message.SerializeToString(&response); 240 } 120 241 static decoded_simple_command_data parse_simple_exec_request(const wchar_t* char_command, const std::string &request) { 121 242 decoded_simple_command_data data; 122 243 123 244 data.command = char_command; 124 ExecuteCommand::RequestMessage request_message;125 request_message.ParseFromString(request);126 if ( request_message.has_header())127 data.target = utf8::cvt<std::wstring>( request_message.header().recipient());128 129 if ( request_message.payload_size() != 1) {245 Plugin::ExecuteRequestMessage message; 246 message.ParseFromString(request); 247 if (message.has_header()) 248 data.target = utf8::cvt<std::wstring>(message.header().recipient()); 249 250 if (message.payload_size() != 1) { 130 251 throw nscapi_exception(_T("Whoops, invalid payload size (for now)")); 131 252 } 132 ::ExecuteCommand::Request payload = request_message.payload().Get(0);253 Plugin::ExecuteRequestMessage::Request payload = message.payload().Get(0); 133 254 for (int i=0;i<payload.arguments_size();i++) { 134 255 data.args.push_back(to_wstring(payload.arguments(i))); … … 136 257 return data; 137 258 } 138 static NSCAPI::nagiosReturn create_simple_exec_result(std::wstring command, NSCAPI::nagiosReturn ret, std::wstring result, std::string &response) { 139 ExecuteCommand::ResponseMessage response_message; 140 ::ExecuteCommand::Header* hdr = response_message.mutable_header(); 141 142 hdr->set_type(ExecuteCommand::Header_Type_RESPONSE); 143 hdr->set_version(ExecuteCommand::Header_Version_VERSION_1); 144 145 ExecuteCommand::Response *resp = response_message.add_payload(); 146 resp->set_command(to_string(command)); 147 resp->set_message(to_string(result)); 148 149 resp->set_version(ExecuteCommand::Response_Version_VERSION_1); 150 resp->set_result(exec_nagios_to_gpb(ret)); 151 response_message.SerializeToString(&response); 152 return ret; 153 } 154 155 static void create_simple_query_request(std::wstring command, std::vector<std::wstring> arguments, std::string &buffer) { 156 PluginCommand::RequestMessage message; 157 ::PluginCommand::Header* header = message.mutable_header(); 158 159 header->set_type(PluginCommand::Header_Type_REQUEST); 160 header->set_version(PluginCommand::Header_Version_VERSION_1); 161 162 PluginCommand::Request *payload = message.add_payload(); 163 payload->set_command(to_string(command)); 164 165 BOOST_FOREACH(std::wstring s, arguments) 166 payload->add_arguments(to_string(s)); 167 168 payload->set_version(PluginCommand::Request_Version_VERSION_1); 169 message.SerializeToString(&buffer); 170 } 171 172 static void create_simple_query_result(NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf, std::string &buffer) { 173 PluginCommand::ResponseMessage message; 174 ::PluginCommand::Header* header = message.mutable_header(); 175 176 header->set_type(PluginCommand::Header_Type_RESPONSE); 177 header->set_version(PluginCommand::Header_Version_VERSION_1); 178 179 PluginCommand::Response *payload = message.add_payload(); 180 payload->set_message(to_string(msg)); 181 if (!perf.empty()) 182 parse_performance_data(payload, perf); 183 184 payload->set_version(PluginCommand::Response_Version_VERSION_1); 185 message.SerializeToString(&buffer); 186 } 187 188 189 static decoded_simple_command_data process_simple_command_request(const wchar_t* char_command, const std::string &request) { 190 decoded_simple_command_data data; 191 192 data.command = char_command; 193 PluginCommand::RequestMessage request_message; 194 request_message.ParseFromString(request); 195 196 if (request_message.payload_size() != 1) { 197 throw nscapi_exception(_T("Whoops, invalid payload size (for now)")); 198 } 199 ::PluginCommand::Request payload = request_message.payload().Get(0); 200 for (int i=0;i<payload.arguments_size();i++) { 201 data.args.push_back(to_wstring(payload.arguments(i))); 202 } 203 return data; 204 } 205 206 static NSCAPI::nagiosReturn process_simple_command_result(std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf, std::string &response) { 207 PluginCommand::ResponseMessage response_message; 208 ::PluginCommand::Header* hdr = response_message.mutable_header(); 209 210 hdr->set_type(PluginCommand::Header_Type_RESPONSE); 211 hdr->set_version(PluginCommand::Header_Version_VERSION_1); 212 213 PluginCommand::Response *resp = response_message.add_payload(); 214 resp->set_command(to_string(command)); 215 resp->set_message(to_string(msg)); 216 parse_performance_data(resp, perf); 217 218 resp->set_version(PluginCommand::Response_Version_VERSION_1); 219 resp->set_result(nagios_to_gpb(ret)); 220 response_message.SerializeToString(&response); 221 return ret; 222 } 223 224 static void create_simple_exec_request(const std::wstring &command, const std::list<std::wstring> & args, std::string &request) { 225 226 ExecuteCommand::RequestMessage message; 227 ExecuteCommand::Header *hdr = message.mutable_header(); 228 hdr->set_type(ExecuteCommand::Header_Type_REQUEST); 229 hdr->set_version(ExecuteCommand::Header_Version_VERSION_1); 230 231 ExecuteCommand::Request *req = message.add_payload(); 232 req->set_command(to_string(command)); 233 req->set_version(ExecuteCommand::Request_Version_VERSION_1); 234 235 BOOST_FOREACH(std::wstring s, args) 236 req->add_arguments(to_string(s)); 237 238 message.SerializeToString(&request); 239 } 240 static void parse_simple_exec_result(const std::string &response, std::list<std::wstring> &result) { 241 ExecuteCommand::ResponseMessage response_message; 242 response_message.ParseFromString(response); 243 244 for (int i=0;i<response_message.payload_size(); i++) { 245 result.push_back(utf8::cvt<std::wstring>(response_message.payload(i).message())); 246 } 247 } 248 249 250 static void create_simple_message_request(std::wstring command, NSCAPI::nagiosReturn code, std::wstring &msg, std::wstring &perf, std::string &request) { 251 PluginCommand::ResponseMessage message; 252 PluginCommand::Header *hdr = message.mutable_header(); 253 hdr->set_type(PluginCommand::Header_Type_RESPONSE); 254 hdr->set_version(PluginCommand::Header_Version_VERSION_1); 255 256 PluginCommand::Response *resp = message.add_payload(); 257 resp->set_command(to_string(command)); 258 resp->set_result(nagios_to_gpb(code)); 259 resp->set_version(PluginCommand::Response_Version_VERSION_1); 260 resp->set_message(to_string(msg)); 261 parse_performance_data(resp, perf); 262 263 message.SerializeToString(&request); 264 } 265 266 267 static void parse_simple_message(std::string &response, std::wstring &msg, std::wstring &perf) { 268 PluginCommand::ResponseMessage response_message; 269 response_message.ParseFromString(response); 270 271 272 if (response_message.payload_size() != 1) { 273 throw nscapi_exception(_T("Whoops, invalid payload size (for now)")); 274 } 275 PluginCommand::Response payload = response_message.payload().Get(0); 276 msg = utf8::cvt<std::wstring>(payload.message()); 277 perf = utf8::cvt<std::wstring>(build_performance_data(payload)); 278 } 279 280 static void parse_performance_data(PluginCommand::Response *resp, std::wstring &perf) { 259 260 261 ////////////////////////////////////////////////////////////////////////// 262 263 static void parse_performance_data(Plugin::QueryResponseMessage::Response *payload, std::wstring &perf) { 281 264 boost::tokenizer<boost::escaped_list_separator<wchar_t>, std::wstring::const_iterator, std::wstring> tok(perf, boost::escaped_list_separator<wchar_t>(L'\\', L' ', L'\'')); 282 265 BOOST_FOREACH(std::wstring s, tok) { … … 285 268 strEx::splitVector items = strEx::splitV(s, _T(";")); 286 269 if (items.size() < 1) { 287 ::PluginCommand::PerformanceData* perfData = resp->add_perf();288 perfData->set_type(Plugin Command::PerformanceData_Type_STRING);270 Plugin::Common::PerformanceData* perfData = payload->add_perf(); 271 perfData->set_type(Plugin::Common_DataType_STRING); 289 272 std::pair<std::wstring,std::wstring> fitem = strEx::split(_T(""), _T("=")); 290 273 perfData->set_alias("invalid"); 291 ::PluginCommand::PerformanceData_StringValue* stringPerfData = perfData->mutable_string_value();274 Plugin::Common_PerformanceData_StringValue* stringPerfData = perfData->mutable_string_value(); 292 275 stringPerfData->set_value("invalid performance data"); 293 276 break; 294 277 } 295 278 296 ::PluginCommand::PerformanceData* perfData = resp->add_perf();297 perfData->set_type(Plugin Command::PerformanceData_Type_FLOAT);279 Plugin::Common::PerformanceData* perfData = payload->add_perf(); 280 perfData->set_type(Plugin::Common_DataType_FLOAT); 298 281 std::pair<std::wstring,std::wstring> fitem = strEx::split(items[0], _T("=")); 299 282 perfData->set_alias(to_string(fitem.first)); 300 ::PluginCommand::PerformanceData_FloatValue* floatPerfData = perfData->mutable_float_value();283 Plugin::Common_PerformanceData_FloatValue* floatPerfData = perfData->mutable_float_value(); 301 284 302 285 std::wstring::size_type pend = fitem.second.find_first_not_of(_T("0123456789,.")); … … 318 301 // std::wcout << _T("Converting performance data") << perf << _T(" -- ") << utf8::cvt<std::wstring>(build_performance_data(*resp)) << std::endl; 319 302 } 320 static std::string build_performance_data( ::PluginCommand::Response const &payload) {303 static std::string build_performance_data(Plugin::QueryResponseMessage::Response const &payload) { 321 304 std::stringstream ss; 322 305 ss.precision(5); … … 324 307 bool first = true; 325 308 for (int i=0;i<payload.perf_size();i++) { 326 ::PluginCommand::PerformanceData perfData = payload.perf(i);309 Plugin::Common::PerformanceData perfData = payload.perf(i); 327 310 if (!first) 328 311 ss << " "; … … 330 313 ss << '\'' << perfData.alias() << "'="; 331 314 if (perfData.has_float_value()) { 332 ::PluginCommand::PerformanceData_FloatValue fval = perfData.float_value();315 Plugin::Common_PerformanceData_FloatValue fval = perfData.float_value(); 333 316 334 317 ss << fval.value(); -
include/nscapi/nscapi_core_wrapper.cpp
r2c95d22 rb38e845 32 32 33 33 #include <protobuf/plugin.pb.h> 34 #include <protobuf/log.pb.h>35 34 36 35 using namespace nscp::helpers; … … 70 69 std::string str; 71 70 try { 72 LogMessage::LogMessagemessage;73 LogMessage::Message *msg = message.add_message();71 Plugin::LogEntry message; 72 Plugin::LogEntry::Entry *msg = message.add_entry(); 74 73 msg->set_level(nscapi::functions::log_to_gpb(msgType)); 75 74 msg->set_file(file); … … 129 128 void nscapi::core_wrapper::submit_simple_message(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn code, std::wstring & message, std::wstring & perf) { 130 129 std::string request; 131 nscapi::functions::create_simple_ message_request(command, code, message, perf, request);130 nscapi::functions::create_simple_query_response(code, message, perf, request, command); 132 131 NSCAPI::nagiosReturn ret = NotifyChannel(channel, command, code, request); 133 132 } … … 156 155 NSCAPI::nagiosReturn ret = simple_query(command, argument, response); 157 156 if (!response.empty()) { 158 PluginCommand::ResponseMessage rsp_msg;159 rsp_msg.ParseFromString(response);160 if (rsp_msg.payload_size() != 1) {161 CORE_LOG_ERROR_STD(_T("Failed to extract return message not 1 payload: ") + strEx::itos(rsp_msg.payload_size()));157 try { 158 nscapi::functions::parse_simple_query_response(response, msg, perf); 159 } catch (std::exception &e) { 160 CORE_LOG_ERROR_STD(_T("Failed to extract return message: ") + utf8::cvt<std::wstring>(e.what())); 162 161 return NSCAPI::returnUNKNOWN; 163 162 } 164 ::PluginCommand::Response payload = rsp_msg.payload(0);165 msg = utf8::cvt<std::wstring>(payload.message());166 perf = utf8::cvt<std::wstring>(::nscapi::functions::build_performance_data(payload));167 163 } 168 164 return ret; … … 177 173 * @return The return of the command 178 174 */ 179 NSCAPI::nagiosReturn nscapi::core_wrapper::simple_query(const std::wstring command, const std::list<std::wstring> & argument , std::string & result)175 NSCAPI::nagiosReturn nscapi::core_wrapper::simple_query(const std::wstring command, const std::list<std::wstring> & arguments, std::string & result) 180 176 { 181 177 if (!fNSAPIInject) 182 178 throw nscapi::nscapi_exception(_T("NSCore has not been initiated...")); 183 179 184 PluginCommand::RequestMessage message;185 PluginCommand::Header *hdr = message.mutable_header();186 hdr->set_type(PluginCommand::Header_Type_REQUEST);187 hdr->set_version(PluginCommand::Header_Version_VERSION_1);188 189 PluginCommand::Request *req = message.add_payload();190 req->set_command(to_string(command));191 req->set_version(PluginCommand::Request_Version_VERSION_1);192 193 BOOST_FOREACH(std::wstring s, argument)194 req->add_arguments(to_string(s));195 196 180 std::string request; 197 message.SerializeToString(&request); 198 181 try { 182 nscapi::functions::create_simple_query_request(command, arguments, request); 183 } catch (std::exception &e) { 184 CORE_LOG_ERROR_STD(_T("Failed to extract return message: ") + utf8::cvt<std::wstring>(e.what())); 185 return NSCAPI::returnUNKNOWN; 186 } 199 187 return query(command.c_str(), request, result); 200 188 } -
include/nscapi/nscapi_plugin_wrapper.cpp
rfe75eff rb38e845 35 35 36 36 #include <protobuf/plugin.pb.h> 37 #include <protobuf/log.pb.h>38 37 39 38 using namespace nscp::helpers; … … 204 203 205 204 206 205 /* 207 206 NSCAPI::nagiosReturn nscapi::impl::CommandImpl::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response) { 208 207 208 nscapi::functions::parse_simple_query_request(char_command, request); 209 209 std::wstring command = char_command; 210 210 PluginCommand::RequestMessage request_message; … … 232 232 return NSCAPI::returnOK; 233 233 } 234 234 */ 235 235 void nscapi::impl::simple_log_handler::handleMessageRAW(std::string data) { 236 236 try { 237 LogMessage::LogMessagemessage;237 Plugin::LogEntry message; 238 238 message.ParseFromString(data); 239 239 240 for (int i=0;i<message. message_size();i++) {241 LogMessage::Message msg = message.message(i);240 for (int i=0;i<message.entry_size();i++) { 241 Plugin::LogEntry::Entry msg = message.entry(i); 242 242 handleMessage(msg.level(), msg.file(), msg.line(), msg.message()); 243 243 } … … 250 250 251 251 NSCAPI::nagiosReturn nscapi::impl::simple_command::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response) { 252 nscapi::functions::decoded_simple_command_data data = nscapi::functions::p rocess_simple_command_request(char_command, request);252 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 253 253 std::wstring msg, perf; 254 254 NSCAPI::nagiosReturn ret = handleCommand(data.target, data.command, data.args, msg, perf); 255 return nscapi::functions::process_simple_command_result(data.command, ret, msg, perf, response); 255 nscapi::functions::create_simple_query_response(ret, msg, perf, response, data.command); 256 return ret; 256 257 } 257 258 … … 262 263 if (ret == NSCAPI::returnIgnored) 263 264 return NSCAPI::returnIgnored; 264 return nscapi::functions::create_simple_exec_result(data.command, ret, result, response); 265 nscapi::functions::create_simple_exec_response(data.command, ret, result, response); 266 return ret; 265 267 } 266 268 267 269 NSCAPI::nagiosReturn nscapi::impl::SimpleNotificationHandler::handleRAWNotification(const wchar_t* channel, const wchar_t* command, NSCAPI::nagiosReturn code, std::string result) { 268 270 try { 269 PluginCommand::ResponseMessage message; 270 if (result.size() == 0) { 271 nscapi::plugin_singleton->get_core()->Message(NSCAPI::error, __FILE__, __LINE__, _T("Return data is empty cant parse response!")); 272 return NSCAPI::returnUNKNOWN; 273 } 274 message.ParseFromString(result); 275 if (message.payload_size() != 1) { 276 nscapi::plugin_singleton->get_core()->Message(NSCAPI::error, __FILE__, __LINE__, _T("Unsupported payload size: ") + to_wstring(message.payload_size())); 277 //NSC_LOG_ERROR_STD(); 278 return NSCAPI::returnIgnored; 279 } 280 281 ::PluginCommand::Response payload = message.payload().Get(0); 282 283 std::list<std::wstring> args; 284 for (int i=0;i<payload.arguments_size();i++) { 285 args.push_back(to_wstring(payload.arguments(i))); 286 } 287 std::wstring msg = utf8::cvt<std::wstring>(payload.message()); 288 std::wstring perf = utf8::cvt<std::wstring>(::nscapi::functions::build_performance_data(payload)); 271 std::wstring msg, perf; 272 nscapi::functions::parse_simple_query_response(result, msg, perf); 289 273 return handleSimpleNotification(channel, command, code, msg, perf); 290 274 } catch (std::exception &e) { -
include/nscapi/nscapi_plugin_wrapper.hpp
rfe75eff rb38e845 130 130 }; 131 131 132 132 /* 133 133 class CommandImpl { 134 134 public: … … 136 136 virtual void handleCommand(std::wstring command, PluginCommand::Request *request, PluginCommand::Response *response) = 0; 137 137 }; 138 */ 138 139 } 139 140 }; -
include/nscp/client/socket.hpp
r4632ff7 rb38e845 58 58 } 59 59 60 virtual void send(std::list<nscp::packet ::nscp_chunk> &chunks, boost::posix_time::seconds timeout) {60 virtual void send(std::list<nscp::packet> &chunks, boost::posix_time::seconds timeout) { 61 61 boost::shared_ptr<socket_helpers::io::timed_writer> writer(new socket_helpers::io::timed_writer(get_io_service())); 62 62 writer->start_timer(timeout); 63 BOOST_FOREACH(nscp::packet ::nscp_chunk&chunk, chunks) {63 BOOST_FOREACH(nscp::packet &chunk, chunks) { 64 64 if (!writer->write_and_wait(*socket_, get_socket(), boost::asio::buffer(chunk.to_buffer()))) { 65 65 std::cout << "FaILED TO SEND DATA..." << std::endl; … … 70 70 writer.reset(); 71 71 } 72 virtual std::list<nscp::packet ::nscp_chunk> recv(boost::posix_time::seconds timeout) {72 virtual std::list<nscp::packet> recv(boost::posix_time::seconds timeout) { 73 73 int left = 1; 74 std::list<nscp::packet ::nscp_chunk> chunks;74 std::list<nscp::packet> chunks; 75 75 boost::shared_ptr<socket_helpers::io::timed_reader> reader(new socket_helpers::io::timed_reader(get_io_service())); 76 76 reader->start_timer(timeout); 77 77 while (left > 0) { 78 nscp::packet ::nscp_chunkchunk;78 nscp::packet chunk; 79 79 std::vector<char> buf(sizeof(nscp::data::signature_packet)); 80 80 if (!reader->read_and_wait(*socket_, get_socket(), boost::asio::buffer(buf))) { … … 134 134 } 135 135 136 virtual void send(std::list<nscp::packet ::nscp_chunk> &chunks, boost::posix_time::seconds timeout) {136 virtual void send(std::list<nscp::packet> &chunks, boost::posix_time::seconds timeout) { 137 137 boost::shared_ptr<socket_helpers::io::timed_writer> writer(new socket_helpers::io::timed_writer(get_io_service())); 138 138 writer->start_timer(timeout); 139 BOOST_FOREACH(nscp::packet ::nscp_chunk&chunk, chunks) {139 BOOST_FOREACH(nscp::packet &chunk, chunks) { 140 140 if (!writer->write_and_wait(*ssl_socket_, get_socket(), boost::asio::buffer(chunk.to_buffer()))) { 141 141 std::cout << "FaILED TO SEND DATA..." << std::endl; … … 147 147 } 148 148 149 virtual std::list<nscp::packet ::nscp_chunk> recv(boost::posix_time::seconds timeout) {149 virtual std::list<nscp::packet> recv(boost::posix_time::seconds timeout) { 150 150 int left = 1; 151 std::list<nscp::packet ::nscp_chunk> chunks;151 std::list<nscp::packet> chunks; 152 152 boost::shared_ptr<socket_helpers::io::timed_reader> reader(new socket_helpers::io::timed_reader(get_io_service())); 153 153 reader->start_timer(timeout); 154 154 while (left > 0) { 155 nscp::packet ::nscp_chunkchunk;155 nscp::packet chunk; 156 156 std::vector<char> buf(sizeof(nscp::data::signature_packet)); 157 157 if (!reader->read_and_wait(*ssl_socket_, get_socket(), boost::asio::buffer(buf))) { -
include/nscp/packet.cpp
r438998b rb38e845 23 23 #include <nscp/packet.hpp> 24 24 25 26 -
include/nscp/packet.hpp
r438998b rb38e845 30 30 #include <utils.h> 31 31 32 #include <protobuf/envelope.pb.h> 32 #include <protobuf/ipc.pb.h> 33 #include <protobuf/plugin.pb.h> 33 34 34 35 using namespace nscp::helpers; 35 36 36 37 namespace nscp { 37 38 39 40 38 class data { 41 39 public: 42 40 static const short unknown_packet = 0; 41 43 42 static const short envelope_request = 1; 44 43 static const short envelope_response = 2; … … 46 45 static const short command_request = 10; 47 46 static const short command_response = 11; 47 48 static const short error = 100; 48 49 49 50 static const short version_1 = 1; … … 91 92 } 92 93 }; 93 struct raw_header_packet {94 char buffer[];95 };96 97 94 }; 98 95 struct length { … … 109 106 }; 110 107 111 class nscp_exception {112 std:: wstring error_;108 class nscp_exception : public std::exception { 109 std::string error_; 113 110 public: 114 nscp_exception(std::wstring error) : error_(error) {} 115 std::wstring getMessage() { 116 return error_; 117 } 118 }; 119 120 class packet /*: public boost::noncopyable*/ { 121 private: 122 std::wstring payload_; 123 short type_; 124 short version_; 125 int result_; 126 unsigned int crc32_; 127 unsigned int calculatedCRC32_; 128 char *tmpBuffer; 129 unsigned int payload_length_; 130 public: 131 132 struct nscp_chunk { 133 nscp::data::signature_packet signature; 134 std::string header; 135 std::string payload; 136 std::string to_buffer() const { 137 std::string ret = write_signature(); 138 if (!header.empty()) 139 ret.insert(ret.end(), header.begin(), header.end()); 140 if (!payload.empty()) 141 ret.insert(ret.end(), payload.begin(), payload.end()); 142 return ret; 143 } 144 void read_signature(std::vector<char> &buf) { 145 assert(buf.size() >= sizeof(nscp::data::signature_packet)); 146 nscp::data::signature_packet *tmp = reinterpret_cast<nscp::data::signature_packet*>(&(*buf.begin())); 147 signature = *tmp; 148 signature.payload_type = tmp->payload_type; 149 signature.payload_length = tmp->payload_length; 150 } 151 void read_payload(std::vector<char> &buf) { 152 payload = std::string(buf.begin(), buf.end()); 153 } 154 std::string write_signature() const { 155 char * buffer = new char[sizeof(nscp::data::signature_packet)+1]; 156 nscp::data::signature_packet *tmp = reinterpret_cast<nscp::data::signature_packet*>(buffer); 157 *tmp = signature; 158 std::string str_buf(buffer, sizeof(nscp::data::signature_packet)); 159 delete [] buffer; 160 return str_buf; 161 } 162 163 }; 164 165 static nscp_chunk build_envelope_request(unsigned long additionl_packets) { 166 nscp_chunk chunk; 167 chunk.signature.header_length = 0; 168 chunk.signature.header_type = 0; 169 170 chunk.signature.additional_packet_count = additionl_packets; 171 chunk.signature.version = nscp::data::version_1; 172 173 NSCPEnvelope::Request request_envelope; 174 request_envelope.set_version(NSCPEnvelope::VERSION_1); 175 request_envelope.set_max_supported_version(NSCPEnvelope::VERSION_1); 176 request_envelope.SerializeToString(&chunk.payload); 177 178 chunk.signature.payload_length = chunk.payload.size(); 179 chunk.signature.payload_type = nscp::data::envelope_request; 180 181 return chunk; 182 } 183 184 static nscp_chunk build_payload(unsigned long payload_type, std::string buffer, unsigned long additionl_packets) { 185 nscp_chunk chunk; 186 chunk.signature.header_length = 0; 187 chunk.signature.header_type = 0; 188 189 chunk.signature.additional_packet_count = additionl_packets; 190 chunk.signature.version = nscp::data::version_1; 191 192 chunk.payload = std::string(buffer.begin(), buffer.end()); 193 chunk.signature.payload_length = chunk.payload.size(); 194 chunk.signature.payload_type = payload_type; 195 196 return chunk; 197 } 198 199 packet(unsigned int payload_length) : tmpBuffer(NULL), payload_length_(payload_length) {}; 200 packet(std::vector<char> buffer, unsigned int payload_length) : tmpBuffer(NULL), payload_length_(payload_length) { 201 char *tmp = new char[buffer.size()+1]; 202 copy( buffer.begin(), buffer.end(), tmp); 203 readFrom(tmp, buffer.size()); 204 delete [] tmp; 205 }; 206 packet(const char *buffer, unsigned int buffer_length, unsigned int payload_length) : tmpBuffer(NULL), payload_length_(payload_length) { 207 readFrom(buffer, buffer_length); 208 }; 209 packet(short type, short version, int result, std::wstring payLoad, unsigned int payload_length) 210 : tmpBuffer(NULL) 211 ,type_(type) 212 ,version_(version) 213 ,result_(result) 214 ,payload_(payLoad) 215 ,payload_length_(payload_length) 216 { 217 } 218 packet() 219 : tmpBuffer(NULL) 220 ,type_(nscp::data::unknown_packet) 221 ,version_(nscp::data::version_1) 222 ,result_(0) 223 ,payload_length_(0) 224 { 225 } 226 packet(const packet &other) : tmpBuffer(NULL) { 227 payload_ = other.payload_; 228 type_ = other.type_; 229 version_ = other.version_; 230 result_ = other.result_; 231 crc32_ = other.crc32_; 232 calculatedCRC32_ = other.calculatedCRC32_; 233 payload_length_ = other.payload_length_; 234 } 235 packet& operator=(packet const& other) { 236 tmpBuffer=NULL; 237 payload_ = other.payload_; 238 type_ = other.type_; 239 version_ = other.version_; 240 result_ = other.result_; 241 crc32_ = other.crc32_; 242 calculatedCRC32_ = other.calculatedCRC32_; 243 payload_length_ = other.payload_length_; 111 nscp_exception(std::wstring error) : error_(utf8::cvt<std::string>(error)) {} 112 nscp_exception(std::string error) : error_(error) {} 113 const char* what() const { 114 return error_.c_str(); 115 } 116 }; 117 118 struct packet { 119 nscp::data::signature_packet signature; 120 std::string header; 121 std::string payload; 122 123 packet() {} 124 packet(const nscp::data::signature_packet &sig) : signature(sig) {} 125 packet(const nscp::data::signature_packet &sig, std::string header, std::string payload) : signature(sig), header(header), payload(payload) {} 126 packet(const packet & other) : signature(other.signature), header(other.header), payload(other.payload) {} 127 const packet& operator=(const packet & other) { 128 signature = other.signature; 129 header = other.header; 130 payload = other.payload; 244 131 return *this; 245 132 } 246 133 247 ~packet() { 248 delete [] tmpBuffer; 249 } 250 static packet make_request(std::wstring payload, unsigned int buffer_length) { 251 return packet(nscp::data::envelope_request, nscp::data::version_1, -1, payload, buffer_length); 252 } 253 254 const char* create_buffer() { 255 return NULL; 256 } 257 258 std::vector<char> get_buffer() { 259 const char *c = create_buffer(); 260 std::vector<char> buf(c, c+get_packet_length()); 261 return buf; 262 } 263 264 void readFrom(const char *buffer, unsigned int length) { 265 } 266 267 unsigned short getVersion() const { return version_; } 268 unsigned short getType() const { return type_; } 269 unsigned short getResult() const { return result_; } 270 std::wstring getPayload() const { return payload_; } 271 bool verifyCRC() { return calculatedCRC32_ == crc32_; } 272 unsigned int get_packet_length() const { return 0; } 273 unsigned int get_payload_length() const { return payload_length_; } 274 275 boost::asio::const_buffer to_buffers() { 276 return boost::asio::buffer(create_buffer(), get_packet_length()); 277 } 278 std::wstring to_string() { 279 std::wstringstream ss; 280 ss << _T("type: ") << type_; 281 ss << _T(", version: ") << version_; 282 ss << _T(", result: ") << result_; 283 ss << _T(", crc32: ") << crc32_; 284 ss << _T(", payload: ") << payload_; 285 return ss.str(); 286 } 287 static nscp::packet create_response(int ret, std::wstring string, int buffer_length) { 288 return packet(nscp::data::unknown_packet, nscp::data::version_1, ret, string, buffer_length); 134 std::string to_buffer() const { 135 std::string ret = write_signature(); 136 if (!header.empty()) 137 ret.insert(ret.end(), header.begin(), header.end()); 138 if (!payload.empty()) 139 ret.insert(ret.end(), payload.begin(), payload.end()); 140 return ret; 141 } 142 void read_signature(std::vector<char> &buf) { 143 assert(buf.size() >= sizeof(nscp::data::signature_packet)); 144 nscp::data::signature_packet *tmp = reinterpret_cast<nscp::data::signature_packet*>(&(*buf.begin())); 145 signature = *tmp; 146 signature.payload_type = tmp->payload_type; 147 signature.payload_length = tmp->payload_length; 148 } 149 void read_payload(std::vector<char> &buf) { 150 payload = std::string(buf.begin(), buf.end()); 151 } 152 std::string write_signature() const { 153 char * buffer = new char[sizeof(nscp::data::signature_packet)+1]; 154 nscp::data::signature_packet *tmp = reinterpret_cast<nscp::data::signature_packet*>(buffer); 155 *tmp = signature; 156 std::string str_buf(buffer, sizeof(nscp::data::signature_packet)); 157 delete [] buffer; 158 return str_buf; 159 } 160 161 static packet build_envelope_request(unsigned long additionl_packets) { 162 nscp::data::signature_packet signature; 163 signature.header_length = 0; 164 signature.header_type = 0; 165 166 signature.additional_packet_count = additionl_packets; 167 signature.version = nscp::data::version_1; 168 169 std::string buffer; 170 NSCPIPC::RequestEnvelope request_envelope; 171 request_envelope.set_version(NSCPIPC::Common_Version_VERSION_1); 172 request_envelope.set_max_supported_version(NSCPIPC::Common_Version_VERSION_1); 173 request_envelope.SerializeToString(&buffer); 174 175 signature.payload_length = buffer.size(); 176 signature.payload_type = nscp::data::envelope_request; 177 178 return packet(signature, "", buffer); 179 } 180 181 static nscp::data::signature_packet create_simple_sig(int payload_type, std::string::size_type size) { 182 nscp::data::signature_packet signature; 183 signature.header_length = 0; 184 signature.header_type = 0; 185 186 signature.additional_packet_count = 0; 187 signature.version = nscp::data::version_1; 188 189 signature.payload_length = size; 190 signature.payload_type = payload_type; 191 return signature; 192 193 } 194 static packet create_payload(unsigned long payload_type, std::string buffer, unsigned long additional_packets = 0) { 195 nscp::data::signature_packet signature; 196 signature.header_length = 0; 197 signature.header_type = 0; 198 199 signature.additional_packet_count = additional_packets; 200 signature.version = nscp::data::version_1; 201 202 signature.payload_length = buffer.size(); 203 signature.payload_type = payload_type; 204 205 return packet(signature, "", buffer); 206 } 207 std::wstring to_wstring() { 208 return signature.to_wstring(); 209 } 210 static nscp::packet create_error(std::wstring msg) { 211 nscp::data::signature_packet signature; 212 signature.header_length = 0; 213 signature.header_type = 0; 214 215 signature.additional_packet_count = 0; 216 signature.version = nscp::data::version_1; 217 218 std::string buffer; 219 NSCPIPC::ErrorMessage message; 220 NSCPIPC::ErrorMessage::Message *error = message.add_error(); 221 error->set_severity(NSCPIPC::ErrorMessage_Message_Severity_IS_ERRROR); 222 error->set_message(utf8::cvt<std::string>(msg)); 223 message.SerializeToString(&buffer); 224 225 signature.payload_length = buffer.size(); 226 signature.payload_type = nscp::data::error; 227 228 return packet(signature, "", buffer); 229 } 230 bool is_command_request() { 231 return signature.payload_type == nscp::data::command_request; 232 } 233 static nscp::packet create_query_response(std::string buffer) { 234 return create_payload(nscp::data::command_response, buffer); 289 235 } 290 236 }; -
include/nscp/server/connection.cpp
r0b8df3e rb38e845 9 9 #include <boost/asio/ssl.hpp> 10 10 11 #include <protobuf/ envelope.pb.h>11 #include <protobuf/ipc.pb.h> 12 12 #include <protobuf/plugin.pb.h> 13 13 … … 92 92 unsigned int count = outbound_queue_.size(); 93 93 handler_->log_debug(__FILE__, __LINE__, _T("Sending responses: ") + strEx::itos(count)); 94 BOOST_FOREACH(nscp::packet ::nscp_chunk&chunk, outbound_queue_) {94 BOOST_FOREACH(nscp::packet &chunk, outbound_queue_) { 95 95 chunk.signature.additional_packet_count = --count; 96 96 std::string s = chunk.to_buffer(); … … 125 125 126 126 boost::tuple<bool, connection::process_helper> connection::process_payload() { 127 std::string result; 128 handler_->log_debug(__FILE__, __LINE__, _T("Got payload for: ") + strEx::itos(sig.payload_type)); 129 parser_.parse_payload(result, sig); 127 nscp::packet chunk(sig); 128 parser_.parse_payload(chunk); 129 handler_->log_debug(__FILE__, __LINE__, _T("Processing: ") + chunk.to_wstring()); 130 std::list<nscp::packet> result = handler_->process(chunk); 131 outbound_queue_.insert(outbound_queue_.end(), result.begin(), result.end()); 132 /* 130 133 if (sig.payload_type == nscp::data::envelope_request) { 131 134 NSCPEnvelope::Request envelope; … … 140 143 handler_->log_error(__FILE__, __LINE__, _T("Unhandled packet: ") + strEx::itos(sig.payload_type)); 141 144 } 145 */ 142 146 return boost::make_tuple(sig.additional_packet_count > 0, process_helper(&nscp::server::parser::digest_signature, &connection::process_signature)); 143 147 } -
include/nscp/server/connection.hpp
r438998b rb38e845 86 86 std::list<std::string> buffers_; 87 87 std::vector<boost::asio::const_buffer> response_buffers_; 88 std::list<nscp::packet ::nscp_chunk> outbound_queue_;88 std::list<nscp::packet> outbound_queue_; 89 89 90 90 }; -
include/nscp/server/handler.hpp
r438998b rb38e845 13 13 return *this; 14 14 } 15 //virtual nscp::packet handle_envelope(nscp::packet packet) = 0; 16 virtual std::string process(std::string &buffer) = 0; 15 virtual std::list<nscp::packet> process(nscp::packet &packet) = 0; 17 16 18 17 virtual void log_debug(std::string file, int line, std::wstring msg) = 0; -
include/nscp/server/parser.hpp
r438998b rb38e845 5 5 #include <boost/noncopyable.hpp> 6 6 7 #include <protobuf/ envelope.pb.h>7 #include <protobuf/ipc.pb.h> 8 8 #include "handler.hpp" 9 9 … … 53 53 buffer_.clear(); 54 54 } 55 void parse_payload( std::string &result, const nscp::data::signature_packet &signature) {56 unsigned long wanted = nscp::length::get_payload_size( signature);55 void parse_payload(nscp::packet &packet) { 56 unsigned long wanted = nscp::length::get_payload_size(packet.signature); 57 57 assert(buffer_.size() >= wanted); 58 59 result.insert(result.begin(), buffer_.begin(), buffer_.begin()+wanted); 58 packet.payload.insert(packet.payload.begin(), buffer_.begin(), buffer_.begin()+wanted); 60 59 buffer_.clear(); 61 60 } -
libs/protobuf/CMakeLists.txt
r438998b rb38e845 2 2 3 3 WRAP_PROTO(PROTO_PLUGIN_SRC plugin.proto) 4 WRAP_PROTO(PROTO_LOG_SRC log.proto) 5 WRAP_PROTO(PROTO_EXEC_SRC exec.proto) 6 WRAP_PROTO(PROTO_SETTINGS_SRC settings.proto) 7 WRAP_PROTO(PROTO_ENVELOPE_SRC envelope.proto) 4 WRAP_PROTO(PROTO_IPC_SRC ipc.proto) 8 5 9 6 SET(TARGET protobuf) … … 11 8 SET(SRCS 12 9 plugin.proto 13 log.proto 14 exec.proto 15 settings.proto 16 envelope.proto 10 ipc.proto 17 11 18 12 ${PROTO_PLUGIN_SRC} 19 ${PROTO_LOG_SRC} 20 ${PROTO_EXEC_SRC} 21 ${PROTO_SETTINGS_SRC} 22 ${PROTO_ENVELOPE_SRC} 13 ${PROTO_IPC_SRC} 23 14 ) 24 15 -
libs/protobuf/ipc.proto
r438998b rb38e845 1 1 package NSCPIPC; 2 3 message Common { 4 5 enum Version { 6 VERSION_1 = 1; 7 }; 8 }; 2 9 3 10 message RegisterSlave { … … 7 14 8 15 }; 16 17 message RequestEnvelope { 18 required Common.Version version = 1; 19 optional Common.Version max_supported_version = 2; 20 21 optional string sender = 17; 22 optional string recipient = 18; 23 optional int32 id = 19; 24 }; 25 26 message ResponseEnvelope { 27 enum Status { 28 IS_OK = 1; 29 30 HAS_WARNINGS = 5; 31 IS_WARNING = 6; 32 HAS_ERRRORS = 7; 33 IS_ERRRORS = 8; 34 35 IS_FATAL = 10; 36 37 UNSUPPORTED_VERSION = 20; 38 }; 39 40 required Common.Version version = 1; 41 required Common.Version max_supported_version = 2; 42 43 required Status request_status = 3; 44 45 optional string message = 7; 46 47 optional string sender = 17; 48 optional string recipient = 18; 49 optional int32 id = 19; 50 }; 51 52 message ErrorMessage { 53 54 message Message { 55 enum Severity { 56 IS_OK = 1; 57 HAS_WARNINGS = 5; 58 IS_WARNING = 6; 59 HAS_ERRRORS = 7; 60 IS_ERRROR = 8; 61 IS_FATAL = 10; 62 }; 63 64 65 optional int32 line = 1; 66 optional int64 date = 2; 67 required Severity severity = 3; 68 required string message = 4; 69 repeated string details = 5; 70 } 71 72 repeated Message error = 1; 73 74 optional string sender = 17; 75 optional string recipient = 18; 76 optional int32 request_id = 19; 77 } -
libs/protobuf/plugin.proto
rc391984 rb38e845 1 package PluginCommand; 2 3 message Header { 4 enum Type { 5 REQUEST = 1; 6 RESPONSE = 2; 7 }; 8 enum Version { 9 VERSION_1 = 1; 10 }; 11 required Type type = 1; 12 required Version version = 2; 13 optional Version max_supported_version = 3; 14 15 optional string sender = 17; 16 optional string recipient = 18; 17 optional int32 id = 19; 18 }; 19 20 message Request { 21 enum Version { 22 VERSION_1 = 1; 23 }; 24 25 required Version version = 1; 26 optional int32 id = 2; 27 required string command = 3; 28 repeated string arguments = 4; 29 }; 30 31 message PerformanceData { 32 enum Type { 33 INT = 1; 34 STRING = 1; 35 FLOAT = 1; 36 }; 37 message IntValue { 38 required int64 value = 1; 39 optional string unit = 2; 40 optional int64 warning = 3; 41 optional int64 critical = 4; 42 optional int64 minimum = 6; 43 optional int64 maximum = 7; 44 } 45 message StringValue { 46 required string value = 1; 47 } 48 message FloatValue { 49 required double value = 1; 50 optional string unit = 2; 51 optional double warning = 3; 52 optional double critical = 4; 53 optional double minimum = 6; 54 optional double maximum = 7; 55 } 56 required string alias = 1; 57 required Type type = 2; 58 optional IntValue int_value = 3; 59 optional StringValue string_value = 4; 60 optional FloatValue float_value = 5; 61 62 // TODO add thresholds here! 63 64 } 65 66 message Response { 67 enum Code { 1 package Plugin; 2 3 message Common { 4 5 enum ResultCode { 68 6 OK = 0; 69 7 WARNING = 1; … … 71 9 UNKNOWN = 3; 72 10 }; 11 enum DataType { 12 INT = 1; 13 STRING = 1; 14 FLOAT = 1; 15 BOOL = 1; 16 }; 17 18 message AnyDataType { 19 required Common.DataType type = 1; 20 optional string string_data = 2; 21 optional int64 int_data = 3; 22 optional double float_data = 4; 23 optional bool bool_data = 5; 24 } 25 26 73 27 enum Version { 74 28 VERSION_1 = 1; 75 29 }; 76 30 77 required Version version = 1; 78 optional int32 id = 2; 79 required string command = 5; 80 repeated string arguments = 16; 31 message Header { 32 enum Type { 33 QUERY_REQUEST = 1; 34 QUERY_RESPONSE = 2; 35 36 EXEC_REQUEST = 3; 37 EXEC_RESPONSE = 4; 38 39 LOG_MESSAGE = 5; 40 }; 41 required Type type = 1; 42 required Common.Version version = 2; 43 optional Common.Version max_supported_version = 3; 44 45 optional string sender = 17; 46 optional string recipient = 18; 47 optional int64 id = 19; 48 }; 49 50 message PerformanceData { 51 message IntValue { 52 required int64 value = 1; 53 optional string unit = 2; 54 optional int64 warning = 3; 55 optional int64 critical = 4; 56 optional int64 minimum = 6; 57 optional int64 maximum = 7; 58 } 59 message StringValue { 60 required string value = 1; 61 } 62 message FloatValue { 63 required double value = 1; 64 optional string unit = 2; 65 optional double warning = 3; 66 optional double critical = 4; 67 optional double minimum = 6; 68 optional double maximum = 7; 69 } 70 message BoolValue { 71 required bool value = 1; 72 optional string unit = 2; 73 optional bool warning = 3; 74 optional bool critical = 4; 75 } 76 required string alias = 1; 77 required Common.DataType type = 2; 78 optional IntValue int_value = 3; 79 optional StringValue string_value = 4; 80 optional FloatValue float_value = 5; 81 optional BoolValue bool_value = 6; 82 83 // TODO add thresholds here! 84 } 85 }; 86 87 // // // // // // // // // // // // // // // // // // // // // // // // 88 // 89 // Query request/response 90 // Used for querying the client this is the "normal" check_nrpe thingy 91 // 92 // // // // // // // // // // // // // // // // // // // // // // // // 93 94 message QueryRequestMessage { 95 message Request { 96 optional int32 id = 1; 97 required string command = 2; 98 repeated string arguments = 3; 99 }; 100 101 required Common.Header header = 1; 102 repeated Request payload = 2; 103 } 104 message QueryResponseMessage { 105 106 message Response { 107 108 optional int32 id = 1; 109 required string command = 2; 110 repeated string arguments = 16; 111 112 required Common.ResultCode result = 3; 113 required string message = 4; 114 optional string legacyPerf = 17; 115 repeated Common.PerformanceData perf = 5; 116 117 } 118 119 required Common.Header header = 1; 120 repeated Response payload = 2; 121 } 122 123 // // // // // // // // // // // // // // // // // // // // // // // // 124 // 125 // Log Entry 126 // Used for sending errors and log entries to a logging module 127 // (this is internal errors, not syslog or eventlog) 128 // 129 // // // // // // // // // // // // // // // // // // // // // // // // 130 131 message LogEntry { 132 message Entry { 133 enum Level { 134 LOG_DEBUG = 0; 135 LOG_INFO = 10; 136 LOG_WARNING = 20; 137 LOG_ERROR = 30; 138 LOG_CRITICAL = 40; 139 }; 140 required Level level = 1; 141 optional string sender = 2; 142 optional string file = 3; 143 optional int32 line = 4; 144 optional string message = 5; 145 optional int32 date = 6; 146 }; 147 148 repeated Entry entry = 1; 149 } 150 // // // // // // // // // // // // // // // // // // // // // // // // 151 // 152 // Execute command request and response 153 // Used for executing commands on clients 154 // 155 // // // // // // // // // // // // // // // // // // // // // // // // 156 157 message ExecuteRequestMessage { 158 message Request { 159 optional int32 id = 1; 160 required string command = 2; 161 repeated string arguments = 3; 162 }; 163 164 required Common.Header header = 1; 165 repeated Request payload = 2; 166 } 167 message ExecuteResponseMessage { 168 message Response { 81 169 82 required Code result = 9; 83 required string message = 10; 84 optional string legacyPerf = 11; 85 repeated PerformanceData perf = 12; 86 87 } 88 89 message RequestMessage { 90 required Header header = 1; 170 message ResponseData { 171 message ResponseDataCell { 172 required Common.AnyDataType data = 1; 173 optional string tag = 2; 174 } 175 message ResponseDataRow { 176 repeated ResponseDataCell cells = 1; 177 } 178 required string alias = 1; 179 optional ResponseDataRow headers = 2; 180 repeated ResponseDataRow rows = 3; 181 182 } 183 184 optional int32 id = 2; 185 required string command = 5; 186 repeated string arguments = 16; 187 188 required Common.ResultCode result = 9; 189 required string message = 10; 190 191 optional ResponseData data = 11; 192 193 } 194 required Common.Header header = 1; 195 repeated Response payload = 2; 196 } 197 198 // // // // // // // // // // // // // // // // // // // // // // // // 199 // 200 // Submit result 201 // Used for submitting a passive result 202 // 203 // // // // // // // // // // // // // // // // // // // // // // // // 204 205 message SubmitResponseMessage { 206 required Common.Header header = 1; 207 repeated QueryResponseMessage.Response payload = 2; 208 } 209 210 // // // // // // // // // // // // // // // // // // // // // // // // 211 // 212 // Settings commands 213 // Used for accessing the settings store 214 // 215 // // // // // // // // // // // // // // // // // // // // // // // // 216 message Settings { 217 message Node { 218 message Path { 219 required string path = 1; 220 optional string key = 2; 221 } 222 optional Path path = 1; 223 optional string expression = 2; 224 }; 225 }; 226 227 message QuerySettingsRequestMessage { 228 message Request { 229 optional int64 id = 1; 230 required Settings node = 2; 231 232 optional string default = 3; 233 optional bool fetch_descriptions = 4; 234 235 optional Common.DataType return_type = 17; 236 237 }; 238 required Common.Header header = 1; 91 239 repeated Request payload = 2; 92 240 } 93 message ResponseMessage { 94 required Header header = 1; 241 message QuerySettingsResponseMessage { 242 message Response { 243 message SectionKeys { 244 repeated string key = 1; 245 } 246 optional int64 id = 1; 247 required Settings node = 2; 248 249 optional Common.AnyDataType value = 4; 250 optional SectionKeys keys = 5; 251 252 optional string title = 6; 253 optional string description = 7; 254 255 optional Common.DataType required_type = 17; 256 257 }; 258 required Common.Header header = 1; 95 259 repeated Response payload = 2; 96 260 } 97 261 98 -
modules/CheckExternalScripts/CheckExternalScripts.cpp
r2c95d22 rb38e845 138 138 139 139 NSCAPI::nagiosReturn CheckExternalScripts::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response) { 140 nscapi::functions::decoded_simple_command_data data = nscapi::functions::p rocess_simple_command_request(char_command, request);140 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 141 141 142 142 command_list::const_iterator cit = commands.find(data.command); … … 158 158 if (first && !isAlias && !allowNasty_) { 159 159 if (str.find_first_of(NASTY_METACHARS) != std::wstring::npos) { 160 return nscapi::functions::process_simple_command_result(data.command, NSCAPI::returnUNKNOWN, _T("Request contained illegal characters!"), _T(""), response); 160 return nscapi::functions::create_simple_query_response_unknown(_T("Request contained illegal characters!"), _T(""), response, data.command); 161 161 162 } 162 163 } … … 190 191 int result = process::executeProcess(process::exec_arguments(root_, cd.command + _T(" ") + xargs, timeout), message, perf); 191 192 if (!nscapi::plugin_helper::isNagiosReturnCode(result)) { 192 return nscapi::functions::process_simple_command_result(data.command, NSCAPI::returnUNKNOWN, _T("The command (") + cd.command + _T(") returned an invalid return code: ") + strEx::itos(result), _T(""), response); 193 } 194 return nscapi::functions::process_simple_command_result(data.command, nscapi::plugin_helper::int2nagios(result), message, perf, response); 193 nscapi::functions::create_simple_query_response(NSCAPI::returnUNKNOWN, _T("The command (") + cd.command + _T(") returned an invalid return code: ") + strEx::itos(result), _T(""), response, data.command); 194 return NSCAPI::returnUNKNOWN; 195 } 196 nscapi::functions::create_simple_query_response(nscapi::plugin_helper::int2nagios(result), message, perf, response, data.command); 197 return result; 195 198 } 196 199 } -
modules/NSCPClient/NSCPClient.cpp
r4632ff7 rb38e845 240 240 NSCPClient::nscp_result_data NSCPClient::execute_nscp_command(nscp_connection_data con, std::string buffer) { 241 241 try { 242 std::list<nscp::packet ::nscp_chunk> chunks;242 std::list<nscp::packet> chunks; 243 243 chunks.push_back(nscp::packet::build_envelope_request(1)); 244 chunks.push_back(nscp::packet:: build_payload(nscp::data::command_request, buffer, 0));244 chunks.push_back(nscp::packet::create_payload(nscp::data::command_request, buffer, 0)); 245 245 if (!con.no_ssl) { 246 246 #ifdef USE_SSL … … 253 253 chunks = send_nossl(con.host, con.port, con.timeout, chunks); 254 254 } 255 BOOST_FOREACH(nscp::packet ::nscp_chunk&chunk, chunks) {255 BOOST_FOREACH(nscp::packet &chunk, chunks) { 256 256 NSC_DEBUG_MSG_STD(_T("Found chunk: ") + utf8::cvt<std::wstring>(strEx::format_buffer(chunk.payload.c_str(), chunk.payload.size()))); 257 257 } 258 258 return nscp_result_data(NSCAPI::returnUNKNOWN, _T("Hello")); 259 259 } catch (nscp::nscp_exception &e) { 260 NSC_LOG_ERROR_STD(_T("Socket error: ") + e.getMessage());261 return nscp_result_data(NSCAPI::returnUNKNOWN, _T(" NSCP Packet error: ") + e.getMessage());260 NSC_LOG_ERROR_STD(_T("Socket error: ") + utf8::cvt<std::wstring>(e.what())); 261 return nscp_result_data(NSCAPI::returnUNKNOWN, _T("Socket error: ") + utf8::cvt<std::wstring>(e.what())); 262 262 } catch (std::runtime_error &e) { 263 263 NSC_LOG_ERROR_STD(_T("Socket error: ") + utf8::cvt<std::wstring>(e.what())); … … 274 274 275 275 #ifdef USE_SSL 276 std::list<nscp::packet ::nscp_chunk> NSCPClient::send_ssl(std::wstring host, int port, int timeout, std::list<nscp::packet::nscp_chunk> &chunks) {276 std::list<nscp::packet> NSCPClient::send_ssl(std::wstring host, int port, int timeout, std::list<nscp::packet> &chunks) { 277 277 boost::asio::io_service io_service; 278 278 boost::asio::ssl::context ctx(io_service, boost::asio::ssl::context::sslv23); … … 286 286 #endif 287 287 288 std::list<nscp::packet ::nscp_chunk> NSCPClient::send_nossl(std::wstring host, int port, int timeout, std::list<nscp::packet::nscp_chunk> &chunks) {288 std::list<nscp::packet> NSCPClient::send_nossl(std::wstring host, int port, int timeout, std::list<nscp::packet> &chunks) { 289 289 boost::asio::io_service io_service; 290 290 nscp::client::socket socket(io_service, host, port); -
modules/NSCPClient/NSCPClient.h
r438998b rb38e845 105 105 private: 106 106 nscp_result_data execute_nscp_command(nscp_connection_data con, std::string buffer); 107 std::list<nscp::packet ::nscp_chunk> send_nossl(std::wstring host, int port, int timeout, std::list<nscp::packet::nscp_chunk> &chunks);108 std::list<nscp::packet ::nscp_chunk> send_ssl(std::wstring host, int port, int timeout, std::list<nscp::packet::nscp_chunk> &chunks);107 std::list<nscp::packet> send_nossl(std::wstring host, int port, int timeout, std::list<nscp::packet> &chunks); 108 std::list<nscp::packet> send_ssl(std::wstring host, int port, int timeout, std::list<nscp::packet> &chunks); 109 109 void add_options(po::options_description &desc, nscp_connection_data &command_data); 110 110 -
modules/NSCPServer/handler_impl.cpp
r438998b rb38e845 7 7 #include "handler_impl.hpp" 8 8 9 std::string handler_impl::process(std::string &buffer) { 10 std::string ret; 11 PluginCommand::RequestMessage msg; 12 msg.ParseFromString(buffer); 13 if (msg.payload_size() != 1) { 14 NSC_LOG_ERROR(_T("Multiple payloads not currently supported...")); 15 nscapi::functions::create_simple_query_result(NSCAPI::returnUNKNOWN, _T("Multiple payloads not currently supported..."), _T(""), ret); 16 return ret; 17 } 18 std::wstring command = utf8::cvt<std::wstring>(msg.payload(0).command()); 19 if (command.empty() || command == _T("_NSCP_CHECK")) { 20 nscapi::functions::create_simple_query_result(NSCAPI::returnOK, _T("I (") + nscapi::plugin_singleton->get_core()->getApplicationVersionString() + _T(") seem to be doing fine..."), _T(""), ret); 21 return ret; 22 } 9 std::list<nscp::packet> handler_impl::process(nscp::packet &packet) { 10 std::list<nscp::packet> result; 23 11 24 // @todo re-add support for nasty arguments / arguments passing 12 Plugin::Common::Header hdr; 13 14 if (packet.is_command_request()) { 15 Plugin::QueryRequestMessage msg; 16 msg.ParseFromString(packet.payload); 17 hdr.CopyFrom(msg.header()); 18 19 // @todo: Make split optional 20 21 for (int i=0;i<msg.payload_size();i++) { 22 23 // @todo re-add support for nasty arguments / arguments passing 25 24 /* 26 25 if (!allowNasty_) { … … 35 34 } 36 35 */ 37 try { 38 NSC_DEBUG_MSG_STD(_T("Running command: ") + command); 39 NSCAPI::nagiosReturn returncode = nscapi::plugin_singleton->get_core()->query(command, buffer, ret); 40 } catch (...) { 41 NSC_LOG_ERROR(_T("Internal exception processing request: ") + command); 42 nscapi::functions::create_simple_query_result(NSCAPI::returnUNKNOWN, _T("Internal exception processing request: ") + command, _T(""), ret); 43 return ret; 36 37 std::string outBuffer; 38 std::wstring command = utf8::cvt<std::wstring>(msg.payload(i).command()); 39 if (command.empty() || command == _T("_NSCP_CHECK")) { 40 nscapi::functions::create_simple_query_response(NSCAPI::returnOK, _T("I (") + nscapi::plugin_singleton->get_core()->getApplicationVersionString() + _T(") seem to be doing fine..."), _T(""), outBuffer); 41 } else { 42 std::string tmpBuffer; 43 Plugin::QueryRequestMessage tmp; 44 tmp.mutable_header()->CopyFrom(hdr); 45 tmp.add_payload()->CopyFrom(msg.payload(i)); 46 tmp.SerializeToString(&tmpBuffer); 47 NSCAPI::nagiosReturn returncode = nscapi::plugin_singleton->get_core()->query(command, tmpBuffer, outBuffer); 48 } 49 result.push_back(nscp::packet::create_query_response(outBuffer)); 50 } 51 } else { 52 NSC_LOG_ERROR(_T("Unknown packet: ") + packet.to_wstring()); 53 result.push_back(create_error(_T("Unknown packet: ") + packet.to_wstring())); 44 54 } 45 return re t;55 return result; 46 56 } 47 57 -
modules/NSCPServer/handler_impl.hpp
r438998b rb38e845 20 20 } 21 21 22 std:: string process(std::string&buffer);22 std::list<nscp::packet> process(nscp::packet &buffer); 23 23 24 24 nscp::packet create_error(std::wstring msg) { 25 return nscp::packet::create_ response(4, msg, payload_length_);25 return nscp::packet::create_error(msg); 26 26 } 27 27 -
modules/PythonScript/PythonScript.cpp
r2c95d22 rb38e845 274 274 std::wstring result; 275 275 NSCAPI::nagiosReturn ret = inst->handle_simple_exec(cmd, data.args, result); 276 return nscapi::functions::create_simple_exec_result(data.command, ret, result, response); 276 nscapi::functions::create_simple_exec_response(data.command, ret, result, response); 277 return ret; 277 278 } 278 279 return NSCAPI::returnIgnored; … … 287 288 } 288 289 if (inst->has_simple(cmd)) { 289 nscapi::functions::decoded_simple_command_data data = nscapi::functions::p rocess_simple_command_request(command, request);290 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(command, request); 290 291 std::wstring msg, perf; 291 292 NSCAPI::nagiosReturn ret = inst->exec_simple(cmd, data.args, msg, perf); 292 return nscapi::functions::process_simple_command_result(data.command, ret, msg, perf, response); 293 nscapi::functions::create_simple_query_response(ret, msg, perf, response, data.command); 294 return ret; 293 295 } 294 296 NSC_LOG_ERROR_STD(_T("Could not find python commands for: ") + command + _T(" (avalible python commands are: ") + inst->get_commands() + _T(")")); … … 311 313 if (inst->has_simple_message_handler(chnl)) { 312 314 std::wstring msg, perf; 313 nscapi::functions::parse_simple_ message(request, msg, perf);315 nscapi::functions::parse_simple_query_response(request, msg, perf); 314 316 return inst->handle_simple_message(chnl, cmd, code, msg, perf); 315 317 } -
service/NSClient++.cpp
r2c95d22 rb38e845 46 46 47 47 #include <protobuf/plugin.pb.h> 48 #include <protobuf/exec.pb.h>49 48 50 49 #ifdef USE_BREAKPAD … … 985 984 } 986 985 } else */{ 987 PluginCommand::RequestMessage message; 988 PluginCommand::Header *hdr = message.mutable_header(); 989 hdr->set_type(PluginCommand::Header_Type_REQUEST); 990 hdr->set_version(PluginCommand::Header_Version_VERSION_1); 991 992 PluginCommand::Request *req = message.add_payload(); 993 req->set_command(to_string(command)); 994 req->set_version(PluginCommand::Request_Version_VERSION_1); 995 996 std::string args = to_string(arguments); 997 998 boost::tokenizer<boost::escaped_list_separator<char> > tok(args, boost::escaped_list_separator<char>('\\', ' ', '\"')); 999 BOOST_FOREACH(std::string s, tok) 1000 req->add_arguments(s); 1001 986 987 std::list<std::wstring> args; 988 strEx::parse_command(arguments, args); 1002 989 std::string request, response; 1003 message.SerializeToString(&request); 1004 1005 1006 990 nscapi::functions::create_simple_query_request(command, args, request); 1007 991 NSCAPI::nagiosReturn ret = injectRAW(command.c_str(), request, response); 1008 992 if (response.empty()) { … … 1010 994 return NSCAPI::returnUNKNOWN; 1011 995 } 1012 1013 PluginCommand::ResponseMessage rsp_msg; 1014 1015 rsp_msg.ParseFromString(response); 1016 if (rsp_msg.payload_size() != 1) { 1017 LOG_ERROR_CORE_STD(_T("Failed to extract return message not 1 payload: ") + strEx::itos(rsp_msg.payload_size())); 1018 return NSCAPI::returnUNKNOWN; 1019 } 1020 msg = utf8::cvt<std::wstring>(rsp_msg.payload(0).message()); 1021 perf = utf8::cvt<std::wstring>(nscapi::functions::build_performance_data(rsp_msg.payload(0))); 1022 if ( (ret == NSCAPI::returnInvalidBufferLen) || (ret == NSCAPI::returnIgnored) ) { 1023 return ret; 1024 } 996 nscapi::functions::parse_simple_query_response(response, msg, perf); 1025 997 return ret; 1026 998 } … … 1085 1057 1086 1058 1087 int NSClientT::simple_exec(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std:: vector<std::wstring> &resp) {1059 int NSClientT::simple_exec(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std::list<std::wstring> &resp) { 1088 1060 bool found = false; 1089 std::vector<std::string> responses;1090 ExecuteCommand::RequestMessage message;1091 ExecuteCommand::Header *hdr = message.mutable_header();1092 hdr->set_type(ExecuteCommand::Header_Type_REQUEST);1093 hdr->set_version(ExecuteCommand::Header_Version_VERSION_1);1094 1095 ExecuteCommand::Request *req = message.add_payload();1096 req->set_command(to_string(command));1097 req->set_version(ExecuteCommand::Request_Version_VERSION_1);1098 1099 BOOST_FOREACH(std::wstring s, arguments)1100 req->add_arguments(utf8::cvt<std::string>(s));1101 1102 1061 std::string request; 1103 message.SerializeToString(&request); 1062 std::list<std::string> responses; 1063 nscapi::functions::create_simple_exec_request(command, arguments, request); 1104 1064 int ret = 0; 1105 1065 { … … 1161 1121 } 1162 1122 BOOST_FOREACH(std::string &r, responses) { 1163 ExecuteCommand::ResponseMessage rsp_msg; 1164 1165 rsp_msg.ParseFromString(r); 1166 if (rsp_msg.payload_size() != 1) { 1167 resp.push_back(_T("Failed to extract return message not 1 payload: ") + strEx::itos(rsp_msg.payload_size())); 1168 LOG_ERROR_CORE_STD(_T("Failed to extract return message not 1 payload: ") + strEx::itos(rsp_msg.payload_size())); 1123 try { 1124 nscapi::functions::parse_simple_exec_result(r, resp); 1125 } catch (std::exception &e) { 1126 resp.push_back(_T("Failed to extract return message: ") + utf8::cvt<std::wstring>(e.what())); 1127 LOG_ERROR_CORE_STD(resp.back()); 1169 1128 return NSCAPI::returnUNKNOWN; 1170 1129 } 1171 resp.push_back(utf8::cvt<std::wstring>(rsp_msg.payload(0).message()));1172 1130 } 1173 1131 return ret; … … 1201 1159 } 1202 1160 1203 ExecuteCommand::ResponseMessage response_message; 1204 ::ExecuteCommand::Header* hdr = response_message.mutable_header(); 1205 1206 hdr->set_type(ExecuteCommand::Header_Type_RESPONSE); 1207 hdr->set_version(ExecuteCommand::Header_Version_VERSION_1); 1161 Plugin::ExecuteResponseMessage response_message; 1162 nscapi::functions::create_simple_header(response_message.mutable_header(), Plugin::Common_Header_Type_EXEC_RESPONSE); 1208 1163 1209 1164 BOOST_FOREACH(std::string r, responses) { 1210 ExecuteCommand::ResponseMessage tmp;1165 Plugin::ExecuteResponseMessage tmp; 1211 1166 tmp.ParseFromString(r); 1212 1167 for (int i=0;i<tmp.payload_size();i++) { 1213 ExecuteCommand::Response *r = response_message.add_payload();1168 Plugin::ExecuteResponseMessage::Response *r = response_message.add_payload(); 1214 1169 r->CopyFrom(tmp.payload(i)); 1215 1170 } -
service/NSClient++.h
r2c95d22 rb38e845 166 166 std::wstring execute(std::wstring password, std::wstring cmd, std::list<std::wstring> args); 167 167 void reportMessage(std::string data); 168 int simple_exec(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std:: vector<std::wstring> &resp);168 int simple_exec(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std::list<std::wstring> &resp); 169 169 NSCAPI::nagiosReturn exec_command(const wchar_t* raw_command, std::string &request, std::string &response); 170 170 -
service/cli_parser.hpp
r438998b rb38e845 309 309 } 310 310 int ret = 0; 311 std:: vector<std::wstring> resp;311 std::list<std::wstring> resp; 312 312 if (mainClient.simple_exec(module, command, arguments, resp) == NSCAPI::returnIgnored) { 313 313 ret = 1; -
service/logger.hpp
rafd42f1 rb38e845 6 6 #include <boost/thread/mutex.hpp> 7 7 8 #include <protobuf/ log.pb.h>8 #include <protobuf/plugin.pb.h> 9 9 10 10 #include <strEx.h> … … 27 27 public: 28 28 static inline std::string create_debug(const char* file, const int line, std::wstring message) { 29 return create_message( ::LogMessage::Message_Level_LOG_DEBUG, file, line, message);29 return create_message(Plugin::LogEntry_Entry_Level_LOG_DEBUG, file, line, message); 30 30 } 31 31 static inline std::string create_info(const char* file, const int line, std::wstring message) { 32 return create_message( ::LogMessage::Message_Level_LOG_INFO, file, line, message);32 return create_message(Plugin::LogEntry_Entry_Level_LOG_INFO, file, line, message); 33 33 } 34 34 static inline std::string create_error(const char* file, const int line, std::wstring message) { 35 return create_message( ::LogMessage::Message_Level_LOG_ERROR, file, line, message);35 return create_message(Plugin::LogEntry_Entry_Level_LOG_ERROR, file, line, message); 36 36 } 37 37 static inline std::string create_error(const std::string file, const int line, std::wstring message) { 38 return create_message( ::LogMessage::Message_Level_LOG_ERROR, file.c_str(), line, message);38 return create_message(Plugin::LogEntry_Entry_Level_LOG_ERROR, file.c_str(), line, message); 39 39 } 40 40 static inline std::string create_warning(const char* file, const int line, std::wstring message) { 41 return create_message( ::LogMessage::Message_Level_LOG_WARNING, file, line, message);42 } 43 44 static std::string create_message( LogMessage::Message_Level msgType, const char* file, const int line, std::wstring logMessage) {41 return create_message(Plugin::LogEntry_Entry_Level_LOG_WARNING, file, line, message); 42 } 43 44 static std::string create_message(Plugin::LogEntry::Entry::Level msgType, const char* file, const int line, std::wstring logMessage) { 45 45 std::string str; 46 46 try { 47 LogMessage::LogMessagemessage;48 LogMessage::Message *msg = message.add_message();47 Plugin::LogEntry message; 48 Plugin::LogEntry::Entry *msg = message.add_entry(); 49 49 msg->set_level(msgType); 50 50 msg->set_file(file); … … 142 142 } 143 143 144 std::wstring render_log_level( ::LogMessage::Message_Level l) {144 std::wstring render_log_level(Plugin::LogEntry::Entry::Level l) { 145 145 int code = nscapi::functions::gpb_to_log(l); 146 146 switch (code) { … … 173 173 std::wstring str; 174 174 try { 175 LogMessage::LogMessagemessage;175 Plugin::LogEntry message; 176 176 if (!message.ParseFromString(data)) { 177 177 return _T("Failed to parse message: ") + to_wstring(strEx::strip_hex(data)); 178 178 } 179 179 180 for (int i=0;i<message. message_size();i++) {181 LogMessage::Message msg = message.message(i);180 for (int i=0;i<message.entry_size();i++) { 181 Plugin::LogEntry::Entry msg = message.entry(i); 182 182 if (!str.empty()) 183 183 str += _T(" -- ");
Note: See TracChangeset
for help on using the changeset viewer.








