| 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 | #include <string>
|
|---|
| 23 | #include <list>
|
|---|
| 24 |
|
|---|
| 25 | #include <NSCAPI.h>
|
|---|
| 26 | #include <nscapi/nscapi_protobuf_types.hpp>
|
|---|
| 27 |
|
|---|
| 28 | #include <protobuf/plugin.pb.h>
|
|---|
| 29 |
|
|---|
| 30 | #include <strEx.h>
|
|---|
| 31 |
|
|---|
| 32 | namespace nscapi {
|
|---|
| 33 | namespace protobuf {
|
|---|
| 34 |
|
|---|
| 35 | class functions {
|
|---|
| 36 | public:
|
|---|
| 37 |
|
|---|
| 38 | typedef nscapi::protobuf::types::destination_container destination_container;
|
|---|
| 39 | typedef nscapi::protobuf::types::decoded_simple_command_data decoded_simple_command_data;
|
|---|
| 40 | public:
|
|---|
| 41 | static Plugin::Common::ResultCode nagios_status_to_gpb(int ret) {
|
|---|
| 42 | if (ret == NSCAPI::returnOK)
|
|---|
| 43 | return Plugin::Common_ResultCode_OK;
|
|---|
| 44 | if (ret == NSCAPI::returnWARN)
|
|---|
| 45 | return Plugin::Common_ResultCode_WARNING;
|
|---|
| 46 | if (ret == NSCAPI::returnCRIT)
|
|---|
| 47 | return Plugin::Common_ResultCode_CRITCAL;
|
|---|
| 48 | return Plugin::Common_ResultCode_UNKNOWN;
|
|---|
| 49 | }
|
|---|
| 50 | static int gbp_to_nagios_status(Plugin::Common::ResultCode ret) {
|
|---|
| 51 | if (ret == Plugin::Common_ResultCode_OK)
|
|---|
| 52 | return NSCAPI::returnOK;
|
|---|
| 53 | if (ret == Plugin::Common_ResultCode_WARNING)
|
|---|
| 54 | return NSCAPI::returnWARN;
|
|---|
| 55 | if (ret == Plugin::Common_ResultCode_CRITCAL)
|
|---|
| 56 | return NSCAPI::returnCRIT;
|
|---|
| 57 | return NSCAPI::returnUNKNOWN;
|
|---|
| 58 | }
|
|---|
| 59 | static Plugin::Common::Status::StatusType status_to_gpb(int ret) {
|
|---|
| 60 | if (ret == NSCAPI::isSuccess)
|
|---|
| 61 | return Plugin::Common_Status_StatusType_STATUS_OK;
|
|---|
| 62 | return Plugin::Common_Status_StatusType_STATUS_ERROR;
|
|---|
| 63 | }
|
|---|
| 64 | static int gbp_to_status(Plugin::Common::Status::StatusType ret) {
|
|---|
| 65 | if (ret == Plugin::Common_Status_StatusType_STATUS_OK)
|
|---|
| 66 | return NSCAPI::isSuccess;
|
|---|
| 67 | return NSCAPI::hasFailed;
|
|---|
| 68 | }
|
|---|
| 69 | static Plugin::Common::ResultCode gbp_status_to_gbp_nagios(Plugin::Common::Status::StatusType ret) {
|
|---|
| 70 | if (ret == Plugin::Common_Status_StatusType_STATUS_OK)
|
|---|
| 71 | return Plugin::Common_ResultCode_OK;
|
|---|
| 72 | return Plugin::Common_ResultCode_UNKNOWN;
|
|---|
| 73 | }
|
|---|
| 74 | static Plugin::Common::Status::StatusType gbp_to_nagios_gbp_status(Plugin::Common::ResultCode ret) {
|
|---|
| 75 | if (ret == Plugin::Common_ResultCode_UNKNOWN||ret == Plugin::Common_ResultCode_WARNING||ret == Plugin::Common_ResultCode_CRITCAL)
|
|---|
| 76 | return Plugin::Common_Status_StatusType_STATUS_ERROR;
|
|---|
| 77 | return Plugin::Common_Status_StatusType_STATUS_OK;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | static Plugin::LogEntry::Entry::Level log_to_gpb(NSCAPI::messageTypes ret) {
|
|---|
| 81 | if (ret == NSCAPI::log_level::critical)
|
|---|
| 82 | return Plugin::LogEntry_Entry_Level_LOG_CRITICAL;
|
|---|
| 83 | if (ret == NSCAPI::log_level::debug)
|
|---|
| 84 | return Plugin::LogEntry_Entry_Level_LOG_DEBUG;
|
|---|
| 85 | if (ret == NSCAPI::log_level::error)
|
|---|
| 86 | return Plugin::LogEntry_Entry_Level_LOG_ERROR;
|
|---|
| 87 | if (ret == NSCAPI::log_level::info)
|
|---|
| 88 | return Plugin::LogEntry_Entry_Level_LOG_INFO;
|
|---|
| 89 | if (ret == NSCAPI::log_level::warning)
|
|---|
| 90 | return Plugin::LogEntry_Entry_Level_LOG_WARNING;
|
|---|
| 91 | return Plugin::LogEntry_Entry_Level_LOG_ERROR;
|
|---|
| 92 | }
|
|---|
| 93 | static NSCAPI::messageTypes gpb_to_log(Plugin::LogEntry::Entry::Level ret) {
|
|---|
| 94 | if (ret == Plugin::LogEntry_Entry_Level_LOG_CRITICAL)
|
|---|
| 95 | return NSCAPI::log_level::critical;
|
|---|
| 96 | if (ret == Plugin::LogEntry_Entry_Level_LOG_DEBUG)
|
|---|
| 97 | return NSCAPI::log_level::debug;
|
|---|
| 98 | if (ret == Plugin::LogEntry_Entry_Level_LOG_ERROR)
|
|---|
| 99 | return NSCAPI::log_level::error;
|
|---|
| 100 | if (ret == Plugin::LogEntry_Entry_Level_LOG_INFO)
|
|---|
| 101 | return NSCAPI::log_level::info;
|
|---|
| 102 | if (ret == Plugin::LogEntry_Entry_Level_LOG_WARNING)
|
|---|
| 103 | return NSCAPI::log_level::warning;
|
|---|
| 104 | return NSCAPI::log_level::error;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 | static void create_simple_header(Plugin::Common::Header* hdr);
|
|---|
| 109 | static void add_host(Plugin::Common::Header* hdr, const destination_container &dst);
|
|---|
| 110 | static bool parse_destination(const ::Plugin::Common_Header &header, const std::string tag, destination_container &data, const bool expand_meta = false);
|
|---|
| 111 |
|
|---|
| 112 | static void make_submit_from_query(std::string &message, const std::wstring channel, const std::wstring alias = _T(""), const std::wstring target = _T(""));
|
|---|
| 113 | static void make_query_from_exec(std::string &data);
|
|---|
| 114 | static void make_query_from_submit(std::string &data);
|
|---|
| 115 | static void make_exec_from_submit(std::string &data);
|
|---|
| 116 | static void make_exec_from_query(std::string &data);
|
|---|
| 117 | static void make_return_header(::Plugin::Common_Header *target, const ::Plugin::Common_Header &source);
|
|---|
| 118 |
|
|---|
| 119 | static void create_simple_query_request(std::wstring command, std::vector<std::wstring> arguments, std::string &buffer);
|
|---|
| 120 | static void create_simple_submit_request(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf, std::string &buffer);
|
|---|
| 121 | static void create_simple_submit_response(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::string &buffer);
|
|---|
| 122 | static NSCAPI::errorReturn parse_simple_submit_request(const std::string &request, std::wstring &source, std::wstring &command, std::wstring &msg, std::wstring &perf);
|
|---|
| 123 |
|
|---|
| 124 | static int parse_simple_submit_request_payload(const Plugin::QueryResponseMessage::Response &payload, std::wstring &alias, std::wstring &message, std::wstring &perf);
|
|---|
| 125 | static int parse_simple_submit_request_payload(const Plugin::QueryResponseMessage::Response &payload, std::wstring &alias, std::wstring &message);
|
|---|
| 126 | static void parse_simple_query_request_payload(const Plugin::QueryRequestMessage::Request &payload, std::wstring &alias, std::wstring &command);
|
|---|
| 127 | static NSCAPI::errorReturn parse_simple_submit_response(const std::string &request, std::wstring &response);
|
|---|
| 128 | static NSCAPI::errorReturn parse_simple_submit_response(const std::string &request, std::string response);
|
|---|
| 129 | static void create_simple_query_request(std::wstring command, std::list<std::wstring> arguments, std::string &buffer);
|
|---|
| 130 | static NSCAPI::nagiosReturn create_simple_query_response_unknown(std::wstring command, std::wstring msg, std::wstring perf, std::string &buffer);
|
|---|
| 131 | static NSCAPI::nagiosReturn create_simple_query_response_unknown(std::wstring command, std::wstring msg, std::string &buffer);
|
|---|
| 132 | static void create_simple_query_response(std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf, std::string &buffer);
|
|---|
| 133 | static void append_simple_submit_request_payload(Plugin::QueryResponseMessage::Response *payload, std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf = _T(""));
|
|---|
| 134 | static void append_simple_query_response_payload(Plugin::QueryResponseMessage::Response *payload, std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf);
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 | static void append_simple_query_response_payload(Plugin::QueryResponseMessage::Response *payload, std::string command, NSCAPI::nagiosReturn ret, std::string msg, std::string perf = "");
|
|---|
| 138 | static void append_simple_exec_response_payload(Plugin::ExecuteResponseMessage::Response *payload, std::string command, int ret, std::string msg);
|
|---|
| 139 | static void append_simple_submit_response_payload(Plugin::SubmitResponseMessage::Response *payload, std::string command, int ret, std::string msg);
|
|---|
| 140 | static void append_simple_query_request_payload(Plugin::QueryRequestMessage::Request *payload, std::wstring command, std::vector<std::wstring> arguments);
|
|---|
| 141 | static void append_simple_exec_request_payload(Plugin::ExecuteRequestMessage::Request *payload, std::wstring command, std::vector<std::wstring> arguments);
|
|---|
| 142 | static decoded_simple_command_data parse_simple_query_request(const wchar_t* char_command, const std::string &request);
|
|---|
| 143 | static decoded_simple_command_data parse_simple_query_request(const ::Plugin::QueryRequestMessage::Request &payload);
|
|---|
| 144 | static int parse_simple_query_response(const std::string &response, std::wstring &msg, std::wstring &perf);
|
|---|
| 145 | static void create_simple_exec_request(const std::wstring &command, const std::list<std::wstring> & args, std::string &request);
|
|---|
| 146 | static void create_simple_exec_request(const std::wstring &command, const std::vector<std::wstring> & args, std::string &request);
|
|---|
| 147 | static int parse_simple_exec_result(const std::string &response, std::list<std::wstring> &result);
|
|---|
| 148 | static void parse_simple_exec_result(const std::string &response, std::wstring &result);
|
|---|
| 149 |
|
|---|
| 150 | template<class T>
|
|---|
| 151 | static void append_response_payloads(T &target_message, std::string &payload) {
|
|---|
| 152 | T source_message;
|
|---|
| 153 | source_message.ParseFromString(payload);
|
|---|
| 154 | for (int i=0;i<source_message.payload_size();++i)
|
|---|
| 155 | target_message.add_payload()->CopyFrom(source_message.payload(i));
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | template<class T>
|
|---|
| 159 | static int create_simple_exec_response(T command, NSCAPI::nagiosReturn ret, T result, std::string &response) {
|
|---|
| 160 | Plugin::ExecuteResponseMessage message;
|
|---|
| 161 | create_simple_header(message.mutable_header());
|
|---|
| 162 |
|
|---|
| 163 | Plugin::ExecuteResponseMessage::Response *payload = message.add_payload();
|
|---|
| 164 | payload->set_command(utf8::cvt<std::string>(command));
|
|---|
| 165 | payload->set_message(utf8::cvt<std::string>(result));
|
|---|
| 166 |
|
|---|
| 167 | payload->set_result(nagios_status_to_gpb(ret));
|
|---|
| 168 | message.SerializeToString(&response);
|
|---|
| 169 | return ret;
|
|---|
| 170 | }
|
|---|
| 171 | template<class T>
|
|---|
| 172 | static int create_simple_exec_response_unknown(T command, T result, std::string &response) {
|
|---|
| 173 | Plugin::ExecuteResponseMessage message;
|
|---|
| 174 | create_simple_header(message.mutable_header());
|
|---|
| 175 |
|
|---|
| 176 | Plugin::ExecuteResponseMessage::Response *payload = message.add_payload();
|
|---|
| 177 | payload->set_command(utf8::cvt<std::string>(command));
|
|---|
| 178 | payload->set_message(utf8::cvt<std::string>(result));
|
|---|
| 179 |
|
|---|
| 180 | payload->set_result(nagios_status_to_gpb(NSCAPI::returnUNKNOWN));
|
|---|
| 181 | message.SerializeToString(&response);
|
|---|
| 182 | return NSCAPI::returnUNKNOWN;
|
|---|
| 183 | }
|
|---|
| 184 | static decoded_simple_command_data parse_simple_exec_request(const wchar_t* char_command, const std::string &request);
|
|---|
| 185 | static decoded_simple_command_data parse_simple_exec_request(const std::wstring cmd, const Plugin::ExecuteRequestMessage &message);
|
|---|
| 186 | static decoded_simple_command_data parse_simple_exec_request_payload(const Plugin::ExecuteRequestMessage::Request &payload);
|
|---|
| 187 |
|
|---|
| 188 | static void parse_performance_data(Plugin::QueryResponseMessage::Response *payload, std::wstring &perf);
|
|---|
| 189 | static void parse_performance_data(Plugin::QueryResponseMessage::Response *payload, std::string &perf);
|
|---|
| 190 | static std::string build_performance_data(Plugin::QueryResponseMessage::Response const &payload);
|
|---|
| 191 | };
|
|---|
| 192 | }
|
|---|
| 193 | typedef protobuf::functions functions;
|
|---|
| 194 | } |
|---|