| 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 | #pragma once
|
|---|
| 22 |
|
|---|
| 23 | #include <boost/tuple/tuple.hpp>
|
|---|
| 24 |
|
|---|
| 25 | #include <client/command_line_parser.hpp>
|
|---|
| 26 | #include <nscapi/targets.hpp>
|
|---|
| 27 | #include <nscapi/nscapi_protobuf_types.hpp>
|
|---|
| 28 |
|
|---|
| 29 | NSC_WRAPPERS_MAIN();
|
|---|
| 30 | NSC_WRAPPERS_CLI();
|
|---|
| 31 | NSC_WRAPPERS_CHANNELS();
|
|---|
| 32 |
|
|---|
| 33 | namespace po = boost::program_options;
|
|---|
| 34 | namespace sh = nscapi::settings_helper;
|
|---|
| 35 |
|
|---|
| 36 | class SyslogClient : public nscapi::impl::simple_plugin {
|
|---|
| 37 | private:
|
|---|
| 38 |
|
|---|
| 39 | std::wstring channel_;
|
|---|
| 40 | std::wstring target_path;
|
|---|
| 41 | const static std::wstring command_prefix;
|
|---|
| 42 |
|
|---|
| 43 | struct custom_reader {
|
|---|
| 44 | typedef nscapi::targets::target_object object_type;
|
|---|
| 45 | typedef nscapi::targets::target_object target_object;
|
|---|
| 46 |
|
|---|
| 47 | static void init_default(target_object &target) {
|
|---|
| 48 | target.set_property_string(_T("severity"), _T("error"));
|
|---|
| 49 | target.set_property_string(_T("facility"), _T("kernel"));
|
|---|
| 50 | target.set_property_string(_T("tag syntax"), _T("NSCA"));
|
|---|
| 51 | target.set_property_string(_T("message syntax"), _T("%message%"));
|
|---|
| 52 | target.set_property_string(_T("ok severity"), _T("informational"));
|
|---|
| 53 | target.set_property_string(_T("warning severity"), _T("warning"));
|
|---|
| 54 | target.set_property_string(_T("critical severity"), _T("critical"));
|
|---|
| 55 | target.set_property_string(_T("unknown severity"), _T("emergency"));
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 | static void add_custom_keys(sh::settings_registry &settings, boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object) {
|
|---|
| 60 | settings.path(object.path).add_key()
|
|---|
| 61 |
|
|---|
| 62 | (_T("severity"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("severity"), _1), _T("error")),
|
|---|
| 63 | _T("TODO"), _T(""))
|
|---|
| 64 |
|
|---|
| 65 | (_T("facility"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("facility"), _1), _T("kernel")),
|
|---|
| 66 | _T("TODO"), _T(""))
|
|---|
| 67 |
|
|---|
| 68 | (_T("tag_syntax"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("tag syntax"), _1), _T("NSCA")),
|
|---|
| 69 | _T("TODO"), _T(""))
|
|---|
| 70 |
|
|---|
| 71 | (_T("message_syntax"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("message syntax"), _1), _T("%message%")),
|
|---|
| 72 | _T("TODO"), _T(""))
|
|---|
| 73 |
|
|---|
| 74 | (_T("ok severity"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("ok severity"), _1), _T("informational")),
|
|---|
| 75 | _T("TODO"), _T(""))
|
|---|
| 76 |
|
|---|
| 77 | (_T("warning severity"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("warning severity"), _1), _T("warning")),
|
|---|
| 78 | _T("TODO"), _T(""))
|
|---|
| 79 |
|
|---|
| 80 | (_T("critical severity"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("critical severity"), _1), _T("critical")),
|
|---|
| 81 | _T("TODO"), _T(""))
|
|---|
| 82 |
|
|---|
| 83 | (_T("unknown severity"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("unknown severity"), _1), _T("emergency")),
|
|---|
| 84 | _T("TODO"), _T(""))
|
|---|
| 85 | ;
|
|---|
| 86 | }
|
|---|
| 87 | static void post_process_target(target_object &target) {
|
|---|
| 88 | }
|
|---|
| 89 | };
|
|---|
| 90 |
|
|---|
| 91 | nscapi::targets::handler<custom_reader> targets;
|
|---|
| 92 | client::command_manager commands;
|
|---|
| 93 |
|
|---|
| 94 | typedef std::map<std::string,int> syslog_map;
|
|---|
| 95 | syslog_map facilities;
|
|---|
| 96 | syslog_map severities;
|
|---|
| 97 | std::string hostname_;
|
|---|
| 98 |
|
|---|
| 99 | struct connection_data {
|
|---|
| 100 | std::string severity;
|
|---|
| 101 | std::string facility;
|
|---|
| 102 | std::string tag_syntax;
|
|---|
| 103 | std::string message_syntax;
|
|---|
| 104 | std::string host;
|
|---|
| 105 | std::string port;
|
|---|
| 106 | std::string ok_severity, warn_severity, crit_severity, unknown_severity;
|
|---|
| 107 |
|
|---|
| 108 | connection_data(nscapi::protobuf::types::destination_container arguments, nscapi::protobuf::types::destination_container target) {
|
|---|
| 109 | arguments.import(target);
|
|---|
| 110 | severity = arguments.data["severity"];
|
|---|
| 111 | facility = arguments.data["facility"];
|
|---|
| 112 | tag_syntax = arguments.data["tag template"];
|
|---|
| 113 | message_syntax = arguments.data["message template"];
|
|---|
| 114 |
|
|---|
| 115 | ok_severity = arguments.data["ok severity"];
|
|---|
| 116 | warn_severity = arguments.data["warning severity"];
|
|---|
| 117 | crit_severity = arguments.data["critical severity"];
|
|---|
| 118 | unknown_severity = arguments.data["unknown severity"];
|
|---|
| 119 |
|
|---|
| 120 | host = arguments.address.host;
|
|---|
| 121 | port = arguments.address.get_port(514);
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | std::wstring to_wstring() const {
|
|---|
| 125 | std::wstringstream ss;
|
|---|
| 126 | ss << _T("host: ") << utf8::cvt<std::wstring>(host);
|
|---|
| 127 | ss << _T(", port: ") << utf8::cvt<std::wstring>(port);
|
|---|
| 128 | ss << _T(", severity: ") << utf8::cvt<std::wstring>(severity);
|
|---|
| 129 | ss << _T(", facility: ") << utf8::cvt<std::wstring>(facility);
|
|---|
| 130 | ss << _T(", tag_syntax: ") << utf8::cvt<std::wstring>(tag_syntax);
|
|---|
| 131 | ss << _T(", message_syntax: ") << utf8::cvt<std::wstring>(message_syntax);
|
|---|
| 132 | return ss.str();
|
|---|
| 133 | }
|
|---|
| 134 | };
|
|---|
| 135 |
|
|---|
| 136 | struct clp_handler_impl : public client::clp_handler, client::target_lookup_interface {
|
|---|
| 137 |
|
|---|
| 138 | SyslogClient *instance;
|
|---|
| 139 | clp_handler_impl(SyslogClient *instance) : instance(instance) {}
|
|---|
| 140 |
|
|---|
| 141 | int query(client::configuration::data_type data, const Plugin::QueryRequestMessage &request_message, std::string &reply);
|
|---|
| 142 | int submit(client::configuration::data_type data, const Plugin::SubmitRequestMessage &request_message, std::string &reply);
|
|---|
| 143 | int exec(client::configuration::data_type data, const Plugin::ExecuteRequestMessage &request_message, std::string &reply);
|
|---|
| 144 |
|
|---|
| 145 | virtual nscapi::protobuf::types::destination_container lookup_target(std::wstring &id) {
|
|---|
| 146 | nscapi::targets::optional_target_object opt = instance->targets.find_object(id);
|
|---|
| 147 | if (opt)
|
|---|
| 148 | return opt->to_destination_container();
|
|---|
| 149 | nscapi::protobuf::types::destination_container ret;
|
|---|
| 150 | return ret;
|
|---|
| 151 | }
|
|---|
| 152 | };
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 | public:
|
|---|
| 156 | SyslogClient();
|
|---|
| 157 | virtual ~SyslogClient();
|
|---|
| 158 | // Module calls
|
|---|
| 159 | bool loadModule();
|
|---|
| 160 | bool loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode);
|
|---|
| 161 | bool unloadModule();
|
|---|
| 162 |
|
|---|
| 163 | /**
|
|---|
| 164 | * Return the module name.
|
|---|
| 165 | * @return The module name
|
|---|
| 166 | */
|
|---|
| 167 | static std::wstring getModuleName() {
|
|---|
| 168 | return _T("SyslogClient");
|
|---|
| 169 | }
|
|---|
| 170 | /**
|
|---|
| 171 | * Module version
|
|---|
| 172 | * @return module version
|
|---|
| 173 | */
|
|---|
| 174 | static nscapi::plugin_wrapper::module_version getModuleVersion() {
|
|---|
| 175 | nscapi::plugin_wrapper::module_version version = {0, 4, 0 };
|
|---|
| 176 | return version;
|
|---|
| 177 | }
|
|---|
| 178 | static std::wstring getModuleDescription() {
|
|---|
| 179 | return _T("Passive check support via Syslog");
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | bool hasCommandHandler() { return true; };
|
|---|
| 183 | bool hasMessageHandler() { return true; };
|
|---|
| 184 | bool hasNotificationHandler() { return true; };
|
|---|
| 185 | NSCAPI::nagiosReturn handleRAWNotification(const wchar_t* channel, std::string request, std::string &response);
|
|---|
| 186 | NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response);
|
|---|
| 187 | NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response);
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 | private:
|
|---|
| 191 | boost::tuple<int,std::wstring> send(connection_data con, std::list<std::string> messages);
|
|---|
| 192 | static connection_data parse_header(const ::Plugin::Common_Header &header, client::configuration::data_type data);
|
|---|
| 193 |
|
|---|
| 194 | private:
|
|---|
| 195 | void add_local_options(po::options_description &desc, client::configuration::data_type data);
|
|---|
| 196 | void setup(client::configuration &config, const ::Plugin::Common_Header& header);
|
|---|
| 197 | void add_command(std::wstring key, std::wstring args);
|
|---|
| 198 | void add_target(std::wstring key, std::wstring args);
|
|---|
| 199 | std::string parse_priority(std::string severity, std::string facility);
|
|---|
| 200 |
|
|---|
| 201 | };
|
|---|
| 202 |
|
|---|