Changeset 2c95d22 in nscp for service/NSClient++.cpp
- Timestamp:
- 08/15/11 00:02:39 (22 months ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- b9498ef
- Parents:
- 65a2940
- File:
-
- 1 edited
-
service/NSClient++.cpp (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
service/NSClient++.cpp
r65a2940 r2c95d22 825 825 826 826 827 int NSClientT::command_line_exec(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std::vector<std::wstring> &resp) { 827 828 /** 829 * Unload all plug-ins (in reversed order) 830 */ 831 void NSClientT::unloadPlugins(bool unloadLoggers) { 832 { 833 boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 834 if (!writeLock.owns_lock()) { 835 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (003).")); 836 return; 837 } 838 if (unloadLoggers) 839 logger_master_.remove_all_plugins(); 840 } 841 { 842 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 843 if (!readLock.owns_lock()) { 844 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (004).")); 845 return; 846 } 847 for (pluginList::reverse_iterator it = plugins_.rbegin(); it != plugins_.rend(); ++it) { 848 plugin_type p = *it; 849 if (!p) 850 continue; 851 try { 852 if (unloadLoggers || !p->hasMessageHandler()) { 853 LOG_DEBUG_CORE_STD(_T("Unloading plugin: ") + p->getModule() + _T("...")); 854 p->unload(); 855 } else { 856 LOG_DEBUG_CORE_STD(_T("Skipping log plugin: ") + p->getModule() + _T("...")); 857 } 858 } catch(NSPluginException e) { 859 LOG_ERROR_CORE_STD(_T("Exception raised when unloading plugin: ") + e.error_ + _T(" in module: ") + e.file_); 860 } catch(...) { 861 LOG_ERROR_CORE_STD(_T("Unknown exception raised when unloading plugin")); 862 } 863 } 864 } 865 { 866 boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 867 if (!writeLock.owns_lock()) { 868 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (005).")); 869 return; 870 } 871 commands_.remove_all(); 872 for (pluginList::iterator it = plugins_.begin(); it != plugins_.end();) { 873 plugin_type p = (*it); 874 try { 875 if (!p && (unloadLoggers|| !p->isLoaded())) { 876 it = plugins_.erase(it); 877 //delete p; 878 continue; 879 } 880 } catch(NSPluginException e) { 881 LOG_ERROR_CORE_STD(_T("Exception raised when unloading plugin: ") + e.error_ + _T(" in module: ") + e.file_); 882 } catch(...) { 883 LOG_ERROR_CORE(_T("Unknown exception raised when unloading plugin")); 884 } 885 it++; 886 } 887 } 888 } 889 890 void NSClientT::loadPlugins(NSCAPI::moduleLoadMode mode) { 891 bool hasBroken = false; 892 { 893 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 894 if (!readLock.owns_lock()) { 895 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (006).")); 896 return; 897 } 898 for (pluginList::iterator it=plugins_.begin(); it != plugins_.end();) { 899 LOG_DEBUG_CORE_STD(_T("Loading plugin: ") + (*it)->getName() + _T(" as ") + (*it)->get_alias()); 900 try { 901 if (!(*it)->load_plugin(mode)) { 902 LOG_ERROR_CORE_STD(_T("Plugin refused to load: ") + (*it)->getModule()); 903 it = plugins_.erase(it); 904 } else 905 ++it; 906 } catch (NSPluginException e) { 907 it = plugins_.erase(it); 908 LOG_ERROR_CORE_STD(_T("Could not load plugin: ") + e.file_ + _T(": ") + e.error_); 909 } catch (...) { 910 it = plugins_.erase(it); 911 LOG_ERROR_CORE_STD(_T("Could not load plugin: ") + (*it)->getModule()); 912 } 913 } 914 } 915 logger_master_.all_plugins_loaded(); 916 } 917 /** 918 * Load and add a plugin to various internal structures 919 * @param plugin The plug-in instance to load. The pointer is managed by the 920 */ 921 NSClientT::plugin_type NSClientT::addPlugin(boost::filesystem::wpath file, std::wstring alias) { 922 { 923 LOG_DEBUG_CORE_STD(_T("addPlugin(") + file.string() + _T(" as ") + alias + _T(")")); 924 // Check if this is a duplicate plugin (if so return that instance) 925 boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 926 if (!writeLock.owns_lock()) { 927 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (007a).")); 928 return plugin_type(); 929 } 930 931 BOOST_FOREACH(plugin_type plug, plugins_) { 932 if (plug->is_duplicate(file, alias)) { 933 LOG_DEBUG_CORE_STD(_T("Found duplicate plugin returning old ") + to_wstring(plug->get_id())); 934 return plug; 935 } 936 } 937 938 } 939 940 941 plugin_type plugin(new NSCPlugin(next_plugin_id_++, file, alias)); 942 plugin->load_dll(); 943 { 944 boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 945 if (!writeLock.owns_lock()) { 946 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (007b).")); 947 return plugin; 948 } 949 950 plugins_.insert(plugins_.end(), plugin); 951 commands_.add_plugin(plugin); 952 channels_.add_plugin(plugin); 953 if (plugin->hasNotificationHandler()) { 954 channels_.register_listener(plugin->get_id(), _T("NSCA")); 955 } 956 if (plugin->hasMessageHandler()) 957 logger_master_.add_plugin(plugin); 958 //settings_manager::get_core()->register_key(_T("/modules"), plugin->getModule(), settings::settings_core::key_string, plugin->getName(), plugin->getDescription(), _T(""), false); 959 // TODO add comments elsewhere to the settings store for all loaded modules... 960 } 961 return plugin; 962 } 963 964 965 std::wstring NSClientT::describeCommand(std::wstring command) { 966 return commands_.describe(command); 967 } 968 std::list<std::wstring> NSClientT::getAllCommandNames() { 969 return commands_.list(); 970 } 971 void NSClientT::registerCommand(unsigned int id, std::wstring cmd, std::wstring desc) { 972 return commands_.register_command(id, cmd, desc); 973 } 974 975 NSCAPI::nagiosReturn NSClientT::inject(std::wstring command, std::wstring arguments, std::wstring &msg, std::wstring & perf) { 976 /*if (shared_client_.get() != NULL && shared_client_->hasMaster()) { 977 try { 978 return shared_client_->inject(command, arguments, splitter, escape, msg, perf); 979 } catch (nsclient_session::session_exception &e) { 980 LOG_ERROR_STD(_T("Failed to inject remote command: ") + e.what()); 981 return NSCAPI::returnCRIT; 982 } catch (...) { 983 LOG_ERROR_STD(_T("Failed to inject remote command: Unknown exception")); 984 return NSCAPI::returnCRIT; 985 } 986 } 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 1002 std::string request, response; 1003 message.SerializeToString(&request); 1004 1005 1006 1007 NSCAPI::nagiosReturn ret = injectRAW(command.c_str(), request, response); 1008 if (response.empty()) { 1009 LOG_ERROR_CORE(_T("No data retutned from command")); 1010 return NSCAPI::returnUNKNOWN; 1011 } 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 } 1025 return ret; 1026 } 1027 } 1028 1029 /** 1030 * Inject a command into the plug-in stack. 1031 * 1032 * @param command Command to inject 1033 * @param argLen Length of argument buffer 1034 * @param **argument Argument buffer 1035 * @param *returnMessageBuffer Message buffer 1036 * @param returnMessageBufferLen Length of returnMessageBuffer 1037 * @param *returnPerfBuffer Performance data buffer 1038 * @param returnPerfBufferLen Length of returnPerfBuffer 1039 * @return The command status 1040 */ 1041 NSCAPI::nagiosReturn NSClientT::injectRAW(const wchar_t* raw_command, std::string &request, std::string &response) { 1042 std::wstring cmd = nsclient::commands::make_key(raw_command); 1043 if (logDebug()) { 1044 LOG_DEBUG_CORE_STD(_T("Injecting: ") + cmd + _T("...")); 1045 } 1046 /*if (shared_client_.get() != NULL && shared_client_->hasMaster()) { 1047 try { 1048 std::wstring msg, perf; 1049 int returnCode = shared_client_->inject(command, arrayBuffer::arrayBuffer2string(argument, argLen, _T(" ")), L' ', true, msg, perf); 1050 NSCHelper::wrapReturnString(returnMessageBuffer, returnMessageBufferLen, msg, returnCode); 1051 return NSCHelper::wrapReturnString(returnPerfBuffer, returnPerfBufferLen, perf, returnCode); 1052 } catch (nsclient_session::session_exception &e) { 1053 LOG_ERROR_STD(_T("Failed to inject remote command: ") + e.what()); 1054 int returnCode = NSCHelper::wrapReturnString(returnMessageBuffer, returnMessageBufferLen, _T("Failed to inject remote command: ") + e.what(), NSCAPI::returnCRIT); 1055 return NSCHelper::wrapReturnString(returnPerfBuffer, returnPerfBufferLen, _T(""), returnCode); 1056 } catch (...) { 1057 LOG_ERROR_STD(_T("Failed to inject remote command: Unknown exception")); 1058 int returnCode = NSCHelper::wrapReturnString(returnMessageBuffer, returnMessageBufferLen, _T("Failed to inject remote command: + e.what()"), NSCAPI::returnCRIT); 1059 return NSCHelper::wrapReturnString(returnPerfBuffer, returnPerfBufferLen, _T(""), returnCode); 1060 } 1061 } else */{ 1062 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 1063 if (!readLock.owns_lock()) { 1064 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (008).")); 1065 return NSCAPI::returnUNKNOWN; 1066 } 1067 try { 1068 nsclient::commands::plugin_type plugin = commands_.get(cmd); 1069 if (!plugin) { 1070 LOG_ERROR_CORE(_T("No handler for command: ") + cmd + _T(" avalible commands: ") + commands_.to_wstring()); 1071 return NSCAPI::returnIgnored; 1072 } 1073 NSCAPI::nagiosReturn c = plugin->handleCommand(cmd.c_str(), request, response); 1074 LOG_DEBUG_CORE_STD(_T("Result ") + cmd + _T(": ") + nscapi::plugin_helper::translateReturn(c)); 1075 return c; 1076 } catch (nsclient::commands::command_exception &e) { 1077 LOG_ERROR_CORE(_T("No handler for command: ") + cmd + _T(": ") + to_wstring(e.what())); 1078 return NSCAPI::returnIgnored; 1079 } catch (...) { 1080 LOG_ERROR_CORE(_T("Error handling command: ") + cmd); 1081 return NSCAPI::returnIgnored; 1082 } 1083 } 1084 } 1085 1086 1087 int NSClientT::simple_exec(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std::vector<std::wstring> &resp) { 828 1088 bool found = false; 829 1089 std::vector<std::string> responses; … … 915 1175 916 1176 917 /** 918 * Unload all plug-ins (in reversed order) 919 */ 920 void NSClientT::unloadPlugins(bool unloadLoggers) { 1177 NSCAPI::nagiosReturn NSClientT::exec_command(const wchar_t* raw_command, std::string &request, std::string &response) { 1178 std::list<std::string> responses; 1179 bool found = false; 921 1180 { 922 boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 923 if (!writeLock.owns_lock()) { 924 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (003).")); 925 return; 926 } 927 if (unloadLoggers) 928 logger_master_.remove_all_plugins(); 929 } 930 { 931 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 1181 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(5)); 932 1182 if (!readLock.owns_lock()) { 933 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (004).")); 934 return; 935 } 936 for (pluginList::reverse_iterator it = plugins_.rbegin(); it != plugins_.rend(); ++it) { 937 plugin_type p = *it; 938 if (!p) 939 continue; 940 try { 941 if (unloadLoggers || !p->hasMessageHandler()) { 942 LOG_DEBUG_CORE_STD(_T("Unloading plugin: ") + p->getModule() + _T("...")); 943 p->unload(); 944 } else { 945 LOG_DEBUG_CORE_STD(_T("Skipping log plugin: ") + p->getModule() + _T("...")); 1183 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (001).")); 1184 return -1; 1185 } 1186 BOOST_FOREACH(plugin_type p, plugins_) { 1187 if (p && p->has_command_line_exec()) { 1188 try { 1189 std::string response; 1190 NSCAPI::nagiosReturn r = p->commandLineExec(raw_command, request, response); 1191 if (r != NSCAPI::returnIgnored && !response.empty()) { 1192 LOG_DEBUG_CORE_STD(_T("Got response from: ") + p->getName()); 1193 found = true; 1194 responses.push_back(response); 1195 } 1196 } catch (NSPluginException e) { 1197 LOG_ERROR_CORE_STD(_T("Could not execute command: ") + e.error_ + _T(" in ") + e.file_); 946 1198 } 947 } catch(NSPluginException e) {948 LOG_ERROR_CORE_STD(_T("Exception raised when unloading plugin: ") + e.error_ + _T(" in module: ") + e.file_);949 } catch(...) {950 LOG_ERROR_CORE_STD(_T("Unknown exception raised when unloading plugin"));951 1199 } 952 1200 } 953 1201 } 954 { 955 boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 956 if (!writeLock.owns_lock()) { 957 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (005).")); 958 return; 959 } 960 commands_.remove_all(); 961 for (pluginList::iterator it = plugins_.begin(); it != plugins_.end();) { 962 plugin_type p = (*it); 963 try { 964 if (!p && (unloadLoggers|| !p->isLoaded())) { 965 it = plugins_.erase(it); 966 //delete p; 967 continue; 968 } 969 } catch(NSPluginException e) { 970 LOG_ERROR_CORE_STD(_T("Exception raised when unloading plugin: ") + e.error_ + _T(" in module: ") + e.file_); 971 } catch(...) { 972 LOG_ERROR_CORE(_T("Unknown exception raised when unloading plugin")); 973 } 974 it++; 975 } 976 } 977 } 978 979 void NSClientT::loadPlugins(NSCAPI::moduleLoadMode mode) { 980 bool hasBroken = false; 981 { 982 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 983 if (!readLock.owns_lock()) { 984 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (006).")); 985 return; 986 } 987 for (pluginList::iterator it=plugins_.begin(); it != plugins_.end();) { 988 LOG_DEBUG_CORE_STD(_T("Loading plugin: ") + (*it)->getName() + _T(" as ") + (*it)->get_alias()); 989 try { 990 if (!(*it)->load_plugin(mode)) { 991 LOG_ERROR_CORE_STD(_T("Plugin refused to load: ") + (*it)->getModule()); 992 it = plugins_.erase(it); 993 } else 994 ++it; 995 } catch (NSPluginException e) { 996 it = plugins_.erase(it); 997 LOG_ERROR_CORE_STD(_T("Could not load plugin: ") + e.file_ + _T(": ") + e.error_); 998 } catch (...) { 999 it = plugins_.erase(it); 1000 LOG_ERROR_CORE_STD(_T("Could not load plugin: ") + (*it)->getModule()); 1001 } 1002 } 1003 } 1004 logger_master_.all_plugins_loaded(); 1005 } 1006 /** 1007 * Load and add a plugin to various internal structures 1008 * @param plugin The plug-in instance to load. The pointer is managed by the 1009 */ 1010 NSClientT::plugin_type NSClientT::addPlugin(boost::filesystem::wpath file, std::wstring alias) { 1011 { 1012 LOG_DEBUG_CORE_STD(_T("addPlugin(") + file.string() + _T(" as ") + alias + _T(")")); 1013 // Check if this is a duplicate plugin (if so return that instance) 1014 boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 1015 if (!writeLock.owns_lock()) { 1016 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (007a).")); 1017 return plugin_type(); 1018 } 1019 1020 BOOST_FOREACH(plugin_type plug, plugins_) { 1021 if (plug->is_duplicate(file, alias)) { 1022 LOG_DEBUG_CORE_STD(_T("Found duplicate plugin returning old ") + to_wstring(plug->get_id())); 1023 return plug; 1024 } 1025 } 1026 1027 } 1028 1029 1030 plugin_type plugin(new NSCPlugin(next_plugin_id_++, file, alias)); 1031 plugin->load_dll(); 1032 { 1033 boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 1034 if (!writeLock.owns_lock()) { 1035 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (007b).")); 1036 return plugin; 1037 } 1038 1039 plugins_.insert(plugins_.end(), plugin); 1040 commands_.add_plugin(plugin); 1041 channels_.add_plugin(plugin); 1042 if (plugin->hasNotificationHandler()) { 1043 channels_.register_listener(plugin->get_id(), _T("NSCA")); 1044 } 1045 if (plugin->hasMessageHandler()) 1046 logger_master_.add_plugin(plugin); 1047 //settings_manager::get_core()->register_key(_T("/modules"), plugin->getModule(), settings::settings_core::key_string, plugin->getName(), plugin->getDescription(), _T(""), false); 1048 // TODO add comments elsewhere to the settings store for all loaded modules... 1049 } 1050 return plugin; 1051 } 1052 1053 1054 std::wstring NSClientT::describeCommand(std::wstring command) { 1055 return commands_.describe(command); 1056 } 1057 std::list<std::wstring> NSClientT::getAllCommandNames() { 1058 return commands_.list(); 1059 } 1060 void NSClientT::registerCommand(unsigned int id, std::wstring cmd, std::wstring desc) { 1061 return commands_.register_command(id, cmd, desc); 1062 } 1063 1064 NSCAPI::nagiosReturn NSClientT::inject(std::wstring command, std::wstring arguments, std::wstring &msg, std::wstring & perf) { 1065 /*if (shared_client_.get() != NULL && shared_client_->hasMaster()) { 1066 try { 1067 return shared_client_->inject(command, arguments, splitter, escape, msg, perf); 1068 } catch (nsclient_session::session_exception &e) { 1069 LOG_ERROR_STD(_T("Failed to inject remote command: ") + e.what()); 1070 return NSCAPI::returnCRIT; 1071 } catch (...) { 1072 LOG_ERROR_STD(_T("Failed to inject remote command: Unknown exception")); 1073 return NSCAPI::returnCRIT; 1074 } 1075 } else */{ 1076 PluginCommand::RequestMessage message; 1077 PluginCommand::Header *hdr = message.mutable_header(); 1078 hdr->set_type(PluginCommand::Header_Type_REQUEST); 1079 hdr->set_version(PluginCommand::Header_Version_VERSION_1); 1080 1081 PluginCommand::Request *req = message.add_payload(); 1082 req->set_command(to_string(command)); 1083 req->set_version(PluginCommand::Request_Version_VERSION_1); 1084 1085 std::string args = to_string(arguments); 1086 1087 boost::tokenizer<boost::escaped_list_separator<char> > tok(args, boost::escaped_list_separator<char>('\\', ' ', '\"')); 1088 BOOST_FOREACH(std::string s, tok) 1089 req->add_arguments(s); 1090 1091 std::string request, response; 1092 message.SerializeToString(&request); 1093 1094 1095 1096 NSCAPI::nagiosReturn ret = injectRAW(command.c_str(), request, response); 1097 if (response.empty()) { 1098 LOG_ERROR_CORE(_T("No data retutned from command")); 1099 return NSCAPI::returnUNKNOWN; 1100 } 1101 1102 PluginCommand::ResponseMessage rsp_msg; 1103 1104 rsp_msg.ParseFromString(response); 1105 if (rsp_msg.payload_size() != 1) { 1106 LOG_ERROR_CORE_STD(_T("Failed to extract return message not 1 payload: ") + strEx::itos(rsp_msg.payload_size())); 1107 return NSCAPI::returnUNKNOWN; 1108 } 1109 msg = utf8::cvt<std::wstring>(rsp_msg.payload(0).message()); 1110 perf = utf8::cvt<std::wstring>(nscapi::functions::build_performance_data(rsp_msg.payload(0))); 1111 if ( (ret == NSCAPI::returnInvalidBufferLen) || (ret == NSCAPI::returnIgnored) ) { 1112 return ret; 1113 } 1114 return ret; 1115 } 1116 } 1117 1118 /** 1119 * Inject a command into the plug-in stack. 1120 * 1121 * @param command Command to inject 1122 * @param argLen Length of argument buffer 1123 * @param **argument Argument buffer 1124 * @param *returnMessageBuffer Message buffer 1125 * @param returnMessageBufferLen Length of returnMessageBuffer 1126 * @param *returnPerfBuffer Performance data buffer 1127 * @param returnPerfBufferLen Length of returnPerfBuffer 1128 * @return The command status 1129 */ 1130 NSCAPI::nagiosReturn NSClientT::injectRAW(const wchar_t* raw_command, std::string &request, std::string &response) { 1131 std::wstring cmd = nsclient::commands::make_key(raw_command); 1132 if (logDebug()) { 1133 LOG_DEBUG_CORE_STD(_T("Injecting: ") + cmd + _T("...")); 1134 } 1135 /*if (shared_client_.get() != NULL && shared_client_->hasMaster()) { 1136 try { 1137 std::wstring msg, perf; 1138 int returnCode = shared_client_->inject(command, arrayBuffer::arrayBuffer2string(argument, argLen, _T(" ")), L' ', true, msg, perf); 1139 NSCHelper::wrapReturnString(returnMessageBuffer, returnMessageBufferLen, msg, returnCode); 1140 return NSCHelper::wrapReturnString(returnPerfBuffer, returnPerfBufferLen, perf, returnCode); 1141 } catch (nsclient_session::session_exception &e) { 1142 LOG_ERROR_STD(_T("Failed to inject remote command: ") + e.what()); 1143 int returnCode = NSCHelper::wrapReturnString(returnMessageBuffer, returnMessageBufferLen, _T("Failed to inject remote command: ") + e.what(), NSCAPI::returnCRIT); 1144 return NSCHelper::wrapReturnString(returnPerfBuffer, returnPerfBufferLen, _T(""), returnCode); 1145 } catch (...) { 1146 LOG_ERROR_STD(_T("Failed to inject remote command: Unknown exception")); 1147 int returnCode = NSCHelper::wrapReturnString(returnMessageBuffer, returnMessageBufferLen, _T("Failed to inject remote command: + e.what()"), NSCAPI::returnCRIT); 1148 return NSCHelper::wrapReturnString(returnPerfBuffer, returnPerfBufferLen, _T(""), returnCode); 1149 } 1150 } else */{ 1151 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 1152 if (!readLock.owns_lock()) { 1153 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (008).")); 1154 return NSCAPI::returnUNKNOWN; 1155 } 1156 try { 1157 nsclient::commands::plugin_type plugin = commands_.get(cmd); 1158 if (!plugin) { 1159 LOG_ERROR_CORE(_T("No handler for command: ") + cmd + _T(" avalible commands: ") + commands_.to_wstring()); 1160 return NSCAPI::returnIgnored; 1161 } 1162 NSCAPI::nagiosReturn c = plugin->handleCommand(cmd.c_str(), request, response); 1163 LOG_DEBUG_CORE_STD(_T("Result ") + cmd + _T(": ") + nscapi::plugin_helper::translateReturn(c)); 1164 return c; 1165 } catch (nsclient::commands::command_exception &e) { 1166 LOG_ERROR_CORE(_T("No handler for command: ") + cmd + _T(": ") + to_wstring(e.what())); 1167 return NSCAPI::returnIgnored; 1168 } catch (...) { 1169 LOG_ERROR_CORE(_T("Error handling command: ") + cmd); 1170 return NSCAPI::returnIgnored; 1171 } 1172 } 1173 } 1202 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); 1208 1209 BOOST_FOREACH(std::string r, responses) { 1210 ExecuteCommand::ResponseMessage tmp; 1211 tmp.ParseFromString(r); 1212 for (int i=0;i<tmp.payload_size();i++) { 1213 ExecuteCommand::Response *r = response_message.add_payload(); 1214 r->CopyFrom(tmp.payload(i)); 1215 } 1216 } 1217 response_message.SerializeToString(&response); 1218 if (found) 1219 return NSCAPI::returnOK; 1220 return NSCAPI::returnIgnored; 1221 } 1222 1174 1223 1175 1224
Note: See TracChangeset
for help on using the changeset viewer.








