| 1 | /**************************************************************************
|
|---|
| 2 | * Copyright (C) 2004-2007 by Michael Medin <michael@medin.name> *
|
|---|
| 3 | * *
|
|---|
| 4 | * This code is part of NSClient++ - http://trac.nakednuns.org/nscp *
|
|---|
| 5 | * *
|
|---|
| 6 | * This program is free software; you can redistribute it and/or modify *
|
|---|
| 7 | * it under the terms of the GNU General Public License as published by *
|
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or *
|
|---|
| 9 | * (at your option) any later version. *
|
|---|
| 10 | * *
|
|---|
| 11 | * This program is distributed in the hope that it will be useful, *
|
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|---|
| 14 | * GNU General Public License for more details. *
|
|---|
| 15 | * *
|
|---|
| 16 | * You should have received a copy of the GNU General Public License *
|
|---|
| 17 | * along with this program; if not, write to the *
|
|---|
| 18 | * Free Software Foundation, Inc., *
|
|---|
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|---|
| 20 | ***************************************************************************/
|
|---|
| 21 | #include "stdafx.h"
|
|---|
| 22 | #include "DotnetPlugins.h"
|
|---|
| 23 |
|
|---|
| 24 | #include <strEx.h>
|
|---|
| 25 |
|
|---|
| 26 | #include "Vcclr.h"
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 | extern nscapi::helper_singleton* nscapi::plugin_singleton;
|
|---|
| 30 |
|
|---|
| 31 | bool DotnetPlugin::loadModule() {
|
|---|
| 32 | return false;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | const std::wstring settings_path = _T("/modules/dotnet");
|
|---|
| 36 | const std::wstring module_path = _T("${exe-path}/modules/dotnet");
|
|---|
| 37 | const std::wstring factory_key = _T("factory class");
|
|---|
| 38 | const std::wstring factory_default = _T("NSCP.Plugin.PluginFactory");
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 | bool DotnetPlugin::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) {
|
|---|
| 42 |
|
|---|
| 43 | try {
|
|---|
| 44 | root_path = get_core()->expand_path(module_path);
|
|---|
| 45 | get_core()->settings_register_path(settings_path, _T("DOTNET MODULES"), _T("List all dot net modules loaded by the DotNetplugins module here"), false);
|
|---|
| 46 |
|
|---|
| 47 | std::list<std::wstring> keys = get_core()->getSettingsSection(settings_path);
|
|---|
| 48 | BOOST_FOREACH(std::wstring key, keys) {
|
|---|
| 49 | get_core()->settings_register_key(settings_path + _T("/") + key, factory_key, NSCAPI::key_string, _T("DOTNET FACTORY"), _T("The class to instasitate in the dot-net plugin"), factory_default, true);
|
|---|
| 50 | }
|
|---|
| 51 | if (mode == NSCAPI::normalStart) {
|
|---|
| 52 | BOOST_FOREACH(std::wstring key, keys) {
|
|---|
| 53 | load(key, get_core()->getSettingsString(settings_path, key, _T("")));
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 | } catch (...) {
|
|---|
| 57 | NSC_LOG_ERROR_STD(_T("Exception caught: <UNKNOWN EXCEPTION>"));
|
|---|
| 58 | return false;
|
|---|
| 59 | }
|
|---|
| 60 | return true;
|
|---|
| 61 | }
|
|---|
| 62 | bool DotnetPlugin::unloadModule() {
|
|---|
| 63 | BOOST_FOREACH(plugin_type p, plugins)
|
|---|
| 64 | p->unload();
|
|---|
| 65 | plugins.clear();
|
|---|
| 66 | return true;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 | void DotnetPlugin::load(std::wstring key, std::wstring val) {
|
|---|
| 71 | try {
|
|---|
| 72 | std::wstring alias = key;
|
|---|
| 73 | std::wstring plugin = val;
|
|---|
| 74 | if (val.empty())
|
|---|
| 75 | plugin = key;
|
|---|
| 76 | std::wstring factory = get_core()->getSettingsString(settings_path + _T("/") + alias, _T("factory class"), _T("NSCP.Plugin.PluginFactory"));
|
|---|
| 77 | plugin_type instance = plugin_instance::create(this, factory, plugin, root_path + _T("\\") + plugin);
|
|---|
| 78 | plugins.push_back(instance);
|
|---|
| 79 | instance->load(alias, 1);
|
|---|
| 80 | } catch(System::Exception ^e) {
|
|---|
| 81 | NSC_LOG_ERROR_STD(_T("Failed to load module: ") + to_nstring(e->ToString()));
|
|---|
| 82 | } catch(const std::exception &e) {
|
|---|
| 83 | NSC_LOG_ERROR_STD(_T("Failed to load module: ") + utf8::to_unicode(e.what()));
|
|---|
| 84 | } catch(...) {
|
|---|
| 85 | NSC_LOG_ERROR_STD(_T("CLR failed to load!"));
|
|---|
| 86 | }
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | bool DotnetPlugin::register_command(std::wstring command, plugin_instance::plugin_type plugin, std::wstring description){
|
|---|
| 90 | commands[command] = plugin;
|
|---|
| 91 | get_core()->registerCommand(id_, command, description);
|
|---|
| 92 | return true;
|
|---|
| 93 | }
|
|---|
| 94 | bool DotnetPlugin::register_channel(std::wstring channel, plugin_instance::plugin_type plugin) {
|
|---|
| 95 | channels[channel] = plugin;
|
|---|
| 96 | get_core()->registerSubmissionListener(id_, channel);
|
|---|
| 97 | return true;
|
|---|
| 98 | }
|
|---|
| 99 | nscapi::core_wrapper* DotnetPlugin::get_core() {
|
|---|
| 100 | return nscapi::plugin_singleton->get_core();
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | bool DotnetPlugin::hasCommandHandler() {
|
|---|
| 104 | return true;
|
|---|
| 105 | }
|
|---|
| 106 | bool DotnetPlugin::hasMessageHandler() {
|
|---|
| 107 | return false;
|
|---|
| 108 | }
|
|---|
| 109 | bool DotnetPlugin::hasNotificationHandler() {
|
|---|
| 110 | return false;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 | NSCAPI::nagiosReturn DotnetPlugin::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response) {
|
|---|
| 115 | std::wstring command(char_command);
|
|---|
| 116 | try {
|
|---|
| 117 | commands_type::const_iterator cit = commands.find(command);
|
|---|
| 118 | if (cit == commands.end())
|
|---|
| 119 | return NSCAPI::returnIgnored;
|
|---|
| 120 | return cit->second->onCommand(command, request, response);
|
|---|
| 121 | } catch(System::Exception ^e) {
|
|---|
| 122 | NSC_LOG_ERROR_STD(_T("Failed to execute command ") + command + _T(": ") + to_nstring(e->ToString()));
|
|---|
| 123 | } catch (const std::exception &e) {
|
|---|
| 124 | NSC_LOG_ERROR_STD(_T("Failed to execute command ") + command + _T(": ") + utf8::to_unicode(e.what()));
|
|---|
| 125 | return NSCAPI::returnIgnored;
|
|---|
| 126 | }
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | NSCAPI::nagiosReturn DotnetPlugin::handleRAWNotification(const std::wstring &channel, std::string &request, std::string &response) {
|
|---|
| 130 | try {
|
|---|
| 131 | commands_type::const_iterator cit = channels.find(channel);
|
|---|
| 132 | if (cit == channels.end())
|
|---|
| 133 | return NSCAPI::returnIgnored;
|
|---|
| 134 | return cit->second->onSubmit(channel, request, response);
|
|---|
| 135 | } catch(System::Exception ^e) {
|
|---|
| 136 | NSC_LOG_ERROR_STD(_T("Failed to execute command ") + channel + _T(": ") + to_nstring(e->ToString()));
|
|---|
| 137 | } catch (const std::exception &e) {
|
|---|
| 138 | NSC_LOG_ERROR_STD(_T("Failed to execute command ") + channel + _T(": ") + utf8::to_unicode(e.what()));
|
|---|
| 139 | return NSCAPI::returnIgnored;
|
|---|
| 140 | }
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | NSCAPI::nagiosReturn DotnetPlugin::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response) {
|
|---|
| 144 | std::wstring command = char_command;
|
|---|
| 145 | return NSCAPI::returnIgnored;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | #pragma managed(push, off)
|
|---|
| 149 | BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
|
|---|
| 150 | return TRUE;
|
|---|
| 151 | }
|
|---|
| 152 | #pragma managed(pop)
|
|---|
| 153 | nscapi::helper_singleton* nscapi::plugin_singleton = new nscapi::helper_singleton();
|
|---|
| 154 |
|
|---|
| 155 | NSC_WRAPPERS_MAIN_DEF(DotnetPlugin);
|
|---|
| 156 | NSC_WRAPPERS_IGNORE_MSG_DEF();
|
|---|
| 157 | NSC_WRAPPERS_HANDLE_CMD_DEF();
|
|---|
| 158 | NSC_WRAPPERS_CLI_DEF();
|
|---|
| 159 | NSC_WRAPPERS_HANDLE_NOTIFICATION_DEF();
|
|---|