Changeset 84cdb9b in nscp for include/nscapi/functions.hpp


Ignore:
Timestamp:
02/23/12 07:02:49 (15 months ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
ee52cdd
Parents:
c74d7b6
Message:

Cleaned up the API helper classes to make inclusion simpler and more modular.
Also removed some dead code no longer used

File:
1 edited

Legend:

Unmodified
Added
Removed
  • include/nscapi/functions.hpp

    r523576e r84cdb9b  
    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 ***************************************************************************/ 
    211#pragma once 
     2// For legacy use only, dont use! 
    223 
    23 #include <boost/math/special_functions.hpp> 
     4#include <nscapi/nscapi_protobuf_functions.hpp> 
    245 
    25 #include <string> 
    26 #include <list> 
    27 #include <set> 
    28 #include <iostream> 
    29  
    30 #include <NSCAPI.h> 
    31 #include <charEx.h> 
    32 //#include <arrayBuffer.h> 
    33 #include <types.hpp> 
    34 #include <net/net.hpp> 
    35 #include <unicode_char.hpp> 
    36 #include <strEx.h> 
    37 #include <nscapi/settings_proxy.hpp> 
    38  
    39 #include <protobuf/plugin.pb.h> 
    40  
    41 using namespace nscp::helpers; 
    42  
    43 namespace nscapi { 
    44  
    45   namespace traits { 
    46  
    47     template<class T> 
    48     struct perf_data_consts { 
    49       static const T get_valid_perf_numbers(); 
    50       static const T get_replace_perf_coma_src(); 
    51       static const T get_replace_perf_coma_tgt(); 
    52     }; 
    53  
    54     template<> 
    55     struct perf_data_consts<std::wstring> { 
    56       static const std::wstring get_valid_perf_numbers() { 
    57         return _T("0123456789,."); 
    58       } 
    59       static const std::wstring get_replace_perf_coma_src() { 
    60         return _T(","); 
    61       } 
    62       static const std::wstring get_replace_perf_coma_tgt() { 
    63         return _T("."); 
    64       } 
    65     }; 
    66     template<> 
    67     struct perf_data_consts<std::string> { 
    68       static const std::string get_valid_perf_numbers() { 
    69         return "0123456789,."; 
    70       } 
    71       static const std::string get_replace_perf_coma_src() { 
    72         return ","; 
    73       } 
    74       static const std::string get_replace_perf_coma_tgt() { 
    75         return "."; 
    76       } 
    77     }; 
    78   } 
    79   class functions { 
    80   public: 
    81     static Plugin::Common::ResultCode nagios_status_to_gpb(int ret) { 
    82       if (ret == NSCAPI::returnOK) 
    83         return Plugin::Common_ResultCode_OK; 
    84       if (ret == NSCAPI::returnWARN) 
    85         return Plugin::Common_ResultCode_WARNING; 
    86       if (ret == NSCAPI::returnCRIT) 
    87         return Plugin::Common_ResultCode_CRITCAL; 
    88       return Plugin::Common_ResultCode_UNKNOWN; 
    89     } 
    90     static int gbp_to_nagios_status(Plugin::Common::ResultCode ret) { 
    91       if (ret == Plugin::Common_ResultCode_OK) 
    92         return NSCAPI::returnOK; 
    93       if (ret == Plugin::Common_ResultCode_WARNING) 
    94         return NSCAPI::returnWARN; 
    95       if (ret == Plugin::Common_ResultCode_CRITCAL) 
    96         return NSCAPI::returnCRIT; 
    97       return NSCAPI::returnUNKNOWN; 
    98     } 
    99     static Plugin::Common::Status::StatusType status_to_gpb(int ret) { 
    100       if (ret == NSCAPI::isSuccess) 
    101         return Plugin::Common_Status_StatusType_OK; 
    102       return Plugin::Common_Status_StatusType_PROBLEM; 
    103     } 
    104     static int gbp_to_status(Plugin::Common::Status::StatusType ret) { 
    105       if (ret == Plugin::Common_Status_StatusType_OK) 
    106         return NSCAPI::isSuccess; 
    107       return NSCAPI::hasFailed; 
    108     } 
    109     static Plugin::Common::ResultCode gbp_status_to_gbp_nagios(Plugin::Common::Status::StatusType ret) { 
    110       if (ret == Plugin::Common_Status_StatusType_OK) 
    111         return Plugin::Common_ResultCode_OK; 
    112       return Plugin::Common_ResultCode_UNKNOWN; 
    113     } 
    114     static Plugin::Common::Status::StatusType gbp_to_nagios_gbp_status(Plugin::Common::ResultCode ret) { 
    115       if (ret == Plugin::Common_ResultCode_UNKNOWN||ret == Plugin::Common_ResultCode_WARNING||ret == Plugin::Common_ResultCode_CRITCAL) 
    116         return Plugin::Common_Status_StatusType_CRITICAL; 
    117       return Plugin::Common_Status_StatusType_OK; 
    118     } 
    119      
    120     static Plugin::LogEntry::Entry::Level log_to_gpb(NSCAPI::messageTypes ret) { 
    121       if (ret == NSCAPI::log_level::critical) 
    122         return Plugin::LogEntry_Entry_Level_LOG_CRITICAL; 
    123       if (ret == NSCAPI::log_level::debug) 
    124         return Plugin::LogEntry_Entry_Level_LOG_DEBUG; 
    125       if (ret == NSCAPI::log_level::error) 
    126         return Plugin::LogEntry_Entry_Level_LOG_ERROR; 
    127       if (ret == NSCAPI::log_level::log) 
    128         return Plugin::LogEntry_Entry_Level_LOG_INFO; 
    129       if (ret == NSCAPI::log_level::info) 
    130         return Plugin::LogEntry_Entry_Level_LOG_INFO; 
    131       if (ret == NSCAPI::log_level::warning) 
    132         return Plugin::LogEntry_Entry_Level_LOG_WARNING; 
    133       return Plugin::LogEntry_Entry_Level_LOG_ERROR; 
    134     } 
    135     static NSCAPI::messageTypes gpb_to_log(Plugin::LogEntry::Entry::Level ret) { 
    136       if (ret == Plugin::LogEntry_Entry_Level_LOG_CRITICAL) 
    137         return NSCAPI::log_level::critical; 
    138       if (ret == Plugin::LogEntry_Entry_Level_LOG_DEBUG) 
    139         return NSCAPI::log_level::debug; 
    140       if (ret == Plugin::LogEntry_Entry_Level_LOG_ERROR) 
    141         return NSCAPI::log_level::error; 
    142       if (ret == Plugin::LogEntry_Entry_Level_LOG_INFO) 
    143         return NSCAPI::log_level::info; 
    144       if (ret == Plugin::LogEntry_Entry_Level_LOG_WARNING) 
    145         return NSCAPI::log_level::warning; 
    146       return NSCAPI::log_level::error; 
    147     } 
    148  
    149  
    150     template<class T> 
    151     static double trim_to_double(T s) { 
    152       typename T::size_type pend = s.find_first_not_of(nscapi::traits::perf_data_consts<T>::get_valid_perf_numbers()); 
    153       if (pend != T::npos) 
    154         s = s.substr(0,pend); 
    155       strEx::replace(s, nscapi::traits::perf_data_consts<T>::get_replace_perf_coma_src(), nscapi::traits::perf_data_consts<T>::get_replace_perf_coma_tgt()); 
    156       return strEx::stod(s); 
    157     } 
    158  
    159      
    160  
    161     struct decoded_simple_command_data { 
    162       std::wstring command; 
    163       std::wstring target; 
    164       std::list<std::wstring> args; 
    165       //std::vector<std::wstring> args_vector; 
    166     }; 
    167  
    168  
    169  
    170     static void create_simple_header(Plugin::Common::Header* hdr)  { 
    171       hdr->set_version(Plugin::Common_Version_VERSION_1); 
    172       hdr->set_max_supported_version(Plugin::Common_Version_VERSION_1); 
    173       // @todo add additional fields here! 
    174     } 
    175  
    176     ////////////////////////////////////////////////////////////////////////// 
    177  
    178     struct destination_container { 
    179       std::string id; 
    180       net::url address; 
    181       std::string comment; 
    182       std::set<std::string> tags; 
    183       typedef std::map<std::string,std::string> data_map; 
    184       data_map data; 
    185       void set_host(std::string value) { 
    186         address.host = value; 
    187       } 
    188       void set_address(std::string value) { 
    189         address = net::parse(value); 
    190       } 
    191       void set_port(std::string value) { 
    192         address.port = strEx::stoi(value); 
    193       } 
    194       std::string get_protocol() const { 
    195         return address.protocol; 
    196       } 
    197       bool has_protocol() const { 
    198         return !address.protocol.empty(); 
    199       } 
    200       /* 
    201       net::url get_url(unsigned int port = 80) const { 
    202         return net::parse(address, port); 
    203       } 
    204       */ 
    205       static bool to_bool(std::string value, bool def = false) { 
    206         if (value.empty()) 
    207           return def; 
    208         if (value == "true" || value == "1" || value == "True") 
    209           return true; 
    210         return false; 
    211       } 
    212       static int to_int(std::string value, int def = 0) { 
    213         if (value.empty()) 
    214           return def; 
    215         try { 
    216           return boost::lexical_cast<int>(value); 
    217         } catch (...) { 
    218           return def; 
    219         } 
    220       } 
    221  
    222       inline int get_int_data(std::string key, int def = 0) { 
    223         return to_int(data[key], def); 
    224       } 
    225       inline bool get_bool_data(std::string key, bool def = false) { 
    226         return to_bool(data[key], def); 
    227       } 
    228       inline std::string get_string_data(std::string key, std::string def = "") { 
    229         data_map::iterator it = data.find(key); 
    230         if (it == data.end()) 
    231           return def; 
    232         return it->second; 
    233       } 
    234       inline bool has_data(std::string key) { 
    235         return data.find(key) != data.end(); 
    236       } 
    237  
    238       /* 
    239       inline net::url get_url(int default_port) { 
    240         return net::parse(address, default_port); 
    241       } 
    242       */ 
    243       void set_string_data(std::string key, std::string value) { 
    244         if (key == "host") 
    245           set_host(value); 
    246         else  
    247           data[key] = value; 
    248       } 
    249       void set_int_data(std::string key, int value) { 
    250         if (key == "port") 
    251           address.port = value; 
    252         else  
    253           data[key] = boost::lexical_cast<std::string>(value); 
    254       } 
    255       void set_bool_data(std::string key, bool value) { 
    256         data[key] = value?"true":"false"; 
    257       } 
    258  
    259       std::string to_string() { 
    260         std::stringstream ss; 
    261         ss << "id: " << id; 
    262         ss << ", address: " << address.to_string(); 
    263         ss << ", comment: " << comment; 
    264         int i=0; 
    265         BOOST_FOREACH(std::string a, tags) { 
    266           ss << ", tags[" << i++ << "]: " << a; 
    267         } 
    268         BOOST_FOREACH(const data_map::value_type &kvp, data) { 
    269           ss << ", data[" << kvp.first << "]: " << kvp.second; 
    270         } 
    271         return ss.str(); 
    272       } 
    273  
    274       void import(const destination_container &other) { 
    275         if (id.empty() && !other.id.empty()) 
    276           id = other.id; 
    277         address.import(other.address); 
    278         if (comment.empty() && !other.comment.empty()) 
    279           comment = other.comment; 
    280         BOOST_FOREACH(const std::string &t, other.tags) { 
    281           tags.insert(t); 
    282         } 
    283         BOOST_FOREACH(const data_map::value_type &kvp, other.data) { 
    284           if (data.find(kvp.first) == data.end()) 
    285             data[kvp.first] = kvp.second; 
    286         } 
    287       } 
    288       void apply(const destination_container &other) { 
    289         if (!other.id.empty()) 
    290           id = other.id; 
    291         address.apply(other.address); 
    292         if (!other.comment.empty()) 
    293           comment = other.comment; 
    294         BOOST_FOREACH(const std::string &t, other.tags) { 
    295           tags.insert(t); 
    296         } 
    297         BOOST_FOREACH(const data_map::value_type &kvp, other.data) { 
    298           data[kvp.first] = kvp.second; 
    299         } 
    300       } 
    301     }; 
    302  
    303     static void add_host(Plugin::Common::Header* hdr, const destination_container &dst)  { 
    304       ::Plugin::Common::Host *host = hdr->add_hosts(); 
    305       if (!dst.id.empty()) 
    306         host->set_id(dst.id); 
    307       if (!dst.address.host.empty()) 
    308         host->set_address(dst.address.to_string()); 
    309       if (!dst.has_protocol()) 
    310         host->set_protocol(dst.get_protocol()); 
    311       if (!dst.comment.empty()) 
    312         host->set_comment(dst.comment); 
    313       BOOST_FOREACH(const std::string &t, dst.tags) { 
    314         host->add_tags(t); 
    315       } 
    316       BOOST_FOREACH(const destination_container::data_map::value_type &kvp, dst.data) { 
    317         ::Plugin::Common_KeyValue* x = host->add_metadata(); 
    318         x->set_key(kvp.first); 
    319         x->set_value(kvp.second); 
    320       } 
    321     } 
    322  
    323     static bool parse_destination(const ::Plugin::Common_Header &header, const std::string tag, destination_container &data, const bool expand_meta = false) { 
    324       for (int i=0;i<header.hosts_size();++i) { 
    325         const ::Plugin::Common::Host &host = header.hosts(i); 
    326         if (host.id() == tag) { 
    327           data.id = tag; 
    328           if (!host.address().empty()) 
    329             data.address.import(net::parse(host.address())); 
    330           if (!host.comment().empty()) 
    331             data.comment = host.comment(); 
    332           if (expand_meta) { 
    333             for(int j=0;j<host.tags_size(); ++j) { 
    334               data.tags.insert(host.tags(j)); 
    335             } 
    336             for(int j=0;j<host.metadata_size(); ++j) { 
    337               data.data[host.metadata(j).key()] = host.metadata(j).value(); 
    338             } 
    339           } 
    340           return true; 
    341         } 
    342       } 
    343       return false; 
    344     } 
    345  
    346     ////////////////////////////////////////////////////////////////////////// 
    347  
    348     static void make_submit_from_query(std::string &message, const std::wstring channel, const std::wstring alias = _T(""), const std::wstring target = _T("")) { 
    349       Plugin::QueryResponseMessage response; 
    350       response.ParseFromString(message); 
    351       Plugin::SubmitRequestMessage request; 
    352       request.mutable_header()->CopyFrom(response.header()); 
    353       request.mutable_header()->set_source_id(request.mutable_header()->recipient_id()); 
    354       request.set_channel(to_string(channel)); 
    355       if (!target.empty()) { 
    356         request.mutable_header()->set_recipient_id(to_string(target)); 
    357       } 
    358       for (int i=0;i<response.payload_size();++i) { 
    359         request.add_payload()->CopyFrom(response.payload(i)); 
    360         if (!alias.empty()) 
    361           request.mutable_payload(i)->set_alias(to_string(alias)); 
    362       } 
    363       message = request.SerializeAsString(); 
    364     } 
    365  
    366     static void make_query_from_exec(std::string &data) { 
    367       Plugin::ExecuteResponseMessage exec_response_message; 
    368       exec_response_message.ParseFromString(data); 
    369       Plugin::QueryResponseMessage query_response_message; 
    370       query_response_message.mutable_header()->CopyFrom(exec_response_message.header()); 
    371       for (int i=0;i<exec_response_message.payload_size();++i) { 
    372         Plugin::ExecuteResponseMessage::Response p = exec_response_message.payload(i); 
    373         append_simple_query_response_payload(query_response_message.add_payload(), p.command(), p.result(), p.message()); 
    374       } 
    375       data = query_response_message.SerializeAsString(); 
    376     } 
    377     static void make_query_from_submit(std::string &data) { 
    378       Plugin::SubmitResponseMessage submit_response_message; 
    379       submit_response_message.ParseFromString(data); 
    380       Plugin::QueryResponseMessage query_response_message; 
    381       query_response_message.mutable_header()->CopyFrom(submit_response_message.header()); 
    382       for (int i=0;i<submit_response_message.payload_size();++i) { 
    383         Plugin::SubmitResponseMessage::Response p = submit_response_message.payload(i); 
    384         append_simple_query_response_payload(query_response_message.add_payload(), p.command(), gbp_status_to_gbp_nagios(p.status().status()), p.status().message(), ""); 
    385       } 
    386       data = query_response_message.SerializeAsString(); 
    387     } 
    388  
    389     static void make_exec_from_submit(std::string &data) { 
    390       Plugin::SubmitResponseMessage submit_response_message; 
    391       submit_response_message.ParseFromString(data); 
    392       Plugin::ExecuteResponseMessage exec_response_message; 
    393       exec_response_message.mutable_header()->CopyFrom(submit_response_message.header()); 
    394       for (int i=0;i<submit_response_message.payload_size();++i) { 
    395         Plugin::SubmitResponseMessage::Response p = submit_response_message.payload(i); 
    396         append_simple_exec_response_payload(exec_response_message.add_payload(), p.command(), gbp_status_to_gbp_nagios(p.status().status()), p.status().message()); 
    397       } 
    398       data = exec_response_message.SerializeAsString(); 
    399     } 
    400     static void make_exec_from_query(std::string &data) { 
    401       Plugin::QueryResponseMessage query_response_message; 
    402       query_response_message.ParseFromString(data); 
    403       Plugin::ExecuteResponseMessage exec_response_message; 
    404       exec_response_message.mutable_header()->CopyFrom(query_response_message.header()); 
    405       for (int i=0;i<query_response_message.payload_size();++i) { 
    406         Plugin::QueryResponseMessage::Response p = query_response_message.payload(i); 
    407         std::string s = build_performance_data(p); 
    408         if (!s.empty()) 
    409           s = p.message() + "|" + s; 
    410         else 
    411           s = p.message(); 
    412         append_simple_exec_response_payload(exec_response_message.add_payload(), p.command(), p.result(), s); 
    413       } 
    414       data = exec_response_message.SerializeAsString(); 
    415     } 
    416  
    417  
    418     static void make_return_header(::Plugin::Common_Header *target, const ::Plugin::Common_Header &source) { 
    419       target->CopyFrom(source); 
    420       target->set_source_id(target->recipient_id()); 
    421     } 
    422  
    423     static void create_simple_query_request(std::wstring command, std::vector<std::wstring> arguments, std::string &buffer) { 
    424       Plugin::QueryRequestMessage message; 
    425       create_simple_header(message.mutable_header()); 
    426  
    427       Plugin::QueryRequestMessage::Request *payload = message.add_payload(); 
    428       payload->set_command(to_string(command)); 
    429  
    430       BOOST_FOREACH(std::wstring s, arguments) 
    431         payload->add_arguments(to_string(s)); 
    432  
    433       message.SerializeToString(&buffer); 
    434     } 
    435  
    436     static void create_simple_submit_request(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf, std::string &buffer) { 
    437       Plugin::SubmitRequestMessage message; 
    438       create_simple_header(message.mutable_header()); 
    439       message.set_channel(to_string(channel)); 
    440  
    441       Plugin::QueryResponseMessage::Response *payload = message.add_payload(); 
    442       payload->set_command(to_string(command)); 
    443       payload->set_message(to_string(msg)); 
    444       payload->set_result(nagios_status_to_gpb(ret)); 
    445       if (!perf.empty()) 
    446         parse_performance_data(payload, perf); 
    447  
    448       message.SerializeToString(&buffer); 
    449     } 
    450     static void create_simple_submit_response(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::string &buffer) { 
    451       Plugin::SubmitResponseMessage message; 
    452       create_simple_header(message.mutable_header()); 
    453       //message.set_channel(to_string(channel)); 
    454  
    455       Plugin::SubmitResponseMessage::Response *payload = message.add_payload(); 
    456       payload->set_command(utf8::cvt<std::string>(command)); 
    457       payload->mutable_status()->set_message(to_string(msg)); 
    458       payload->mutable_status()->set_status(status_to_gpb(ret)); 
    459       message.SerializeToString(&buffer); 
    460     } 
    461     static NSCAPI::errorReturn parse_simple_submit_request(const std::string &request, std::wstring &source, std::wstring &command, std::wstring &msg, std::wstring &perf) { 
    462       Plugin::SubmitRequestMessage message; 
    463       message.ParseFromString(request); 
    464  
    465       if (message.payload_size() != 1) { 
    466         throw nscapi_exception(_T("Whoops, invalid payload size (for now)")); 
    467       } 
    468       Plugin::QueryResponseMessage::Response payload = message.payload().Get(0); 
    469       source = utf8::cvt<std::wstring>(payload.source()); 
    470       command = utf8::cvt<std::wstring>(payload.command()); 
    471       msg = utf8::cvt<std::wstring>(payload.message()); 
    472       perf = utf8::cvt<std::wstring>(build_performance_data(payload)); 
    473       return gbp_to_nagios_status(payload.result()); 
    474     } 
    475     /* 
    476     static NSCAPI::errorReturn parse_simple_submit_request_payload(const Plugin::QueryResponseMessage::Response &payload, std::wstring &alias, std::wstring &command, std::wstring &msg, std::wstring &perf) { 
    477       alias = utf8::cvt<std::wstring>(payload.alias()); 
    478       command = utf8::cvt<std::wstring>(payload.command()); 
    479       msg = utf8::cvt<std::wstring>(payload.message()); 
    480       perf = utf8::cvt<std::wstring>(build_performance_data(payload)); 
    481       return gbp_to_nagios_status(payload.result()); 
    482     } 
    483     */ 
    484     static int parse_simple_submit_request_payload(const Plugin::QueryResponseMessage::Response &payload, std::wstring &alias, std::wstring &message) { 
    485       alias = utf8::cvt<std::wstring>(payload.alias()); 
    486       if (alias.empty()) 
    487         alias = utf8::cvt<std::wstring>(payload.command()); 
    488       message = utf8::cvt<std::wstring>(payload.message()); 
    489       return gbp_to_nagios_status(payload.result()); 
    490     } 
    491     static void parse_simple_query_request_payload(const Plugin::QueryRequestMessage::Request &payload, std::wstring &alias, std::wstring &command) { 
    492       alias = utf8::cvt<std::wstring>(payload.alias()); 
    493       command = utf8::cvt<std::wstring>(payload.command()); 
    494     } 
    495     static NSCAPI::errorReturn parse_simple_submit_response(const std::string &request, std::wstring &response) { 
    496       Plugin::SubmitResponseMessage message; 
    497       message.ParseFromString(request); 
    498  
    499       if (message.payload_size() != 1) { 
    500         throw nscapi_exception(_T("Whoops, invalid payload size (for now)")); 
    501       } 
    502       ::Plugin::SubmitResponseMessage::Response payload = message.payload().Get(0); 
    503       response = to_wstring(payload.mutable_status()->message()); 
    504       return gbp_to_status(payload.mutable_status()->status()); 
    505     } 
    506     static NSCAPI::errorReturn parse_simple_submit_response(const std::string &request, std::string response) { 
    507       Plugin::SubmitResponseMessage message; 
    508       message.ParseFromString(request); 
    509  
    510       if (message.payload_size() != 1) { 
    511         throw nscapi_exception(_T("Whoops, invalid payload size (for now)")); 
    512       } 
    513       ::Plugin::SubmitResponseMessage::Response payload = message.payload().Get(0); 
    514       response = payload.mutable_status()->message(); 
    515       return gbp_to_status(payload.mutable_status()->status()); 
    516     } 
    517     /* 
    518     static void create_simple_submit_response(std::wstring msg, int status, std::string &buffer) { 
    519       Plugin::SubmitResponseMessage message; 
    520       create_simple_header(message.mutable_header()); 
    521  
    522       Plugin::SubmitResponseMessage::Response *payload = message.add_payload(); 
    523       payload->mutable_status()->set_message(to_string(msg)); 
    524       payload->mutable_status()->set_status(status_to_gpb(status)); 
    525  
    526       message.SerializeToString(&buffer); 
    527     }*/ 
    528  
    529     static void create_simple_query_request(std::wstring command, std::list<std::wstring> arguments, std::string &buffer) { 
    530       Plugin::QueryRequestMessage message; 
    531       create_simple_header(message.mutable_header()); 
    532  
    533       Plugin::QueryRequestMessage::Request *payload = message.add_payload(); 
    534       payload->set_command(to_string(command)); 
    535  
    536       BOOST_FOREACH(std::wstring s, arguments) 
    537         payload->add_arguments(to_string(s)); 
    538  
    539       message.SerializeToString(&buffer); 
    540     } 
    541     static NSCAPI::nagiosReturn create_simple_query_response_unknown(std::wstring command, std::wstring msg, std::wstring perf, std::string &buffer) { 
    542       create_simple_query_response(command, NSCAPI::returnUNKNOWN, msg, perf, buffer); 
    543       return NSCAPI::returnUNKNOWN; 
    544     } 
    545     static NSCAPI::nagiosReturn create_simple_query_response_unknown(std::wstring command, std::wstring msg, std::string &buffer) { 
    546       create_simple_query_response(command, NSCAPI::returnUNKNOWN, msg, _T(""), buffer); 
    547       return NSCAPI::returnUNKNOWN; 
    548     } 
    549  
    550     static void create_simple_query_response(std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf, std::string &buffer) { 
    551       Plugin::QueryResponseMessage message; 
    552       create_simple_header(message.mutable_header()); 
    553  
    554       append_simple_query_response_payload(message.add_payload(), command, ret, msg, perf); 
    555  
    556       message.SerializeToString(&buffer); 
    557     } 
    558  
    559      
    560     static void append_simple_submit_request_payload(Plugin::QueryResponseMessage::Response *payload, std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf = _T("")) { 
    561       payload->set_command(to_string(command)); 
    562       payload->set_message(to_string(msg)); 
    563       payload->set_result(nagios_status_to_gpb(ret)); 
    564       if (!perf.empty()) 
    565         parse_performance_data(payload, perf); 
    566     } 
    567  
    568     static void append_simple_query_response_payload(Plugin::QueryResponseMessage::Response *payload, std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf) { 
    569       payload->set_command(to_string(command)); 
    570       payload->set_message(to_string(msg)); 
    571       payload->set_result(nagios_status_to_gpb(ret)); 
    572       if (!perf.empty()) 
    573         parse_performance_data(payload, perf); 
    574     } 
    575  
    576     template<class T> 
    577     static void append_response_payloads(T &target_message, std::string &payload) { 
    578       T source_message; 
    579       source_message.ParseFromString(payload); 
    580       for (int i=0;i<source_message.payload_size();++i) 
    581         target_message.add_payload()->CopyFrom(source_message.payload(i)); 
    582     } 
    583  
    584     static void append_simple_query_response_payload(Plugin::QueryResponseMessage::Response *payload, std::string command, NSCAPI::nagiosReturn ret, std::string msg, std::string perf = "") { 
    585       payload->set_command(to_string(command)); 
    586       payload->set_message(to_string(msg)); 
    587       payload->set_result(nagios_status_to_gpb(ret)); 
    588       if (!perf.empty()) 
    589         parse_performance_data(payload, perf); 
    590     } 
    591 /* 
    592     static void append_simple_exec_response_payload(Plugin::ExecuteResponseMessage::Response *payload, std::string command, Plugin::Common::ResultCode ret, std::string msg) { 
    593       payload->set_command(command); 
    594       payload->set_message(msg); 
    595       payload->set_result(ret); 
    596     } 
    597     */ 
    598     static void append_simple_exec_response_payload(Plugin::ExecuteResponseMessage::Response *payload, std::string command, int ret, std::string msg) { 
    599       payload->set_command(command); 
    600       payload->set_message(msg); 
    601       payload->set_result(nagios_status_to_gpb(ret)); 
    602     } 
    603     /* 
    604     static void append_simple_submit_response_payload(Plugin::SubmitResponseMessage::Response *payload, std::string command, Plugin::Common::ResultCode ret, std::string msg) { 
    605       payload->set_command(command); 
    606       payload->mutable_status()->set_status(gbp_to_nagios_gbp_status(ret)); 
    607       payload->mutable_status()->set_message(msg); 
    608     } 
    609     */ 
    610     static void append_simple_submit_response_payload(Plugin::SubmitResponseMessage::Response *payload, std::string command, int ret, std::string msg) { 
    611       payload->set_command(command); 
    612       payload->mutable_status()->set_status(status_to_gpb(ret)); 
    613       payload->mutable_status()->set_message(msg); 
    614     } 
    615  
    616  
    617     static void append_simple_query_request_payload(Plugin::QueryRequestMessage::Request *payload, std::wstring command, std::vector<std::wstring> arguments) { 
    618       payload->set_command(to_string(command)); 
    619       BOOST_FOREACH(const std::wstring &s, arguments) { 
    620         payload->add_arguments(to_string(s)); 
    621       } 
    622     } 
    623  
    624     static void append_simple_exec_request_payload(Plugin::ExecuteRequestMessage::Request *payload, std::wstring command, std::vector<std::wstring> arguments) { 
    625       payload->set_command(to_string(command)); 
    626       BOOST_FOREACH(const std::wstring &s, arguments) { 
    627         payload->add_arguments(to_string(s)); 
    628       } 
    629     } 
    630  
    631  
    632     static decoded_simple_command_data parse_simple_query_request(const wchar_t* char_command, const std::string &request) { 
    633       decoded_simple_command_data data; 
    634  
    635       data.command = char_command; 
    636       Plugin::QueryRequestMessage message; 
    637       message.ParseFromString(request); 
    638  
    639       if (message.payload_size() != 1) { 
    640         throw nscapi_exception(_T("Whoops, invalid payload size (for now)")); 
    641       } 
    642       ::Plugin::QueryRequestMessage::Request payload = message.payload().Get(0); 
    643       for (int i=0;i<payload.arguments_size();i++) { 
    644         data.args.push_back(to_wstring(payload.arguments(i))); 
    645       } 
    646       return data; 
    647     } 
    648     static decoded_simple_command_data parse_simple_query_request(const ::Plugin::QueryRequestMessage::Request &payload) { 
    649       decoded_simple_command_data data; 
    650       data.command = utf8::cvt<std::wstring>(payload.command()); 
    651       for (int i=0;i<payload.arguments_size();i++) { 
    652         data.args.push_back(utf8::cvt<std::wstring>(payload.arguments(i))); 
    653       } 
    654       return data; 
    655     } 
    656  
    657     static int parse_simple_query_response(const std::string &response, std::wstring &msg, std::wstring &perf) { 
    658       Plugin::QueryResponseMessage message; 
    659       message.ParseFromString(response); 
    660  
    661  
    662       if (message.payload_size() == 0) { 
    663         return NSCAPI::returnUNKNOWN; 
    664       } else if (message.payload_size() > 1) { 
    665         throw nscapi_exception(_T("Whoops, invalid payload size (for now)")); 
    666       } 
    667  
    668       Plugin::QueryResponseMessage::Response payload = message.payload().Get(0); 
    669       msg = utf8::cvt<std::wstring>(payload.message()); 
    670       perf = utf8::cvt<std::wstring>(build_performance_data(payload)); 
    671       return gbp_to_nagios_status(payload.result()); 
    672     } 
    673  
    674  
    675     ////////////////////////////////////////////////////////////////////////// 
    676  
    677     static void create_simple_exec_request(const std::wstring &command, const std::list<std::wstring> & args, std::string &request) { 
    678        
    679       Plugin::ExecuteRequestMessage message; 
    680       create_simple_header(message.mutable_header()); 
    681  
    682       Plugin::ExecuteRequestMessage::Request *payload = message.add_payload(); 
    683       payload->set_command(to_string(command)); 
    684  
    685       BOOST_FOREACH(std::wstring s, args) 
    686         payload->add_arguments(to_string(s)); 
    687  
    688       message.SerializeToString(&request); 
    689     } 
    690     static void create_simple_exec_request(const std::wstring &command, const std::vector<std::wstring> & args, std::string &request) { 
    691  
    692       Plugin::ExecuteRequestMessage message; 
    693       create_simple_header(message.mutable_header()); 
    694  
    695       Plugin::ExecuteRequestMessage::Request *payload = message.add_payload(); 
    696       payload->set_command(to_string(command)); 
    697  
    698       BOOST_FOREACH(std::wstring s, args) 
    699         payload->add_arguments(to_string(s)); 
    700  
    701       message.SerializeToString(&request); 
    702     } 
    703     static void parse_simple_exec_result(const std::string &response, std::list<std::wstring> &result) { 
    704       Plugin::ExecuteResponseMessage message; 
    705       message.ParseFromString(response); 
    706  
    707       for (int i=0;i<message.payload_size(); i++) { 
    708         result.push_back(utf8::cvt<std::wstring>(message.payload(i).message())); 
    709       } 
    710     } 
    711     static void parse_simple_exec_result(const std::string &response, std::wstring &result) { 
    712       Plugin::ExecuteResponseMessage message; 
    713       message.ParseFromString(response); 
    714  
    715       for (int i=0;i<message.payload_size(); i++) { 
    716         result += utf8::cvt<std::wstring>(message.payload(i).message()); 
    717       } 
    718     } 
    719  
    720     template<class T> 
    721     static int create_simple_exec_response(T command, NSCAPI::nagiosReturn ret, T result, std::string &response) { 
    722       Plugin::ExecuteResponseMessage message; 
    723       create_simple_header(message.mutable_header()); 
    724  
    725       Plugin::ExecuteResponseMessage::Response *payload = message.add_payload(); 
    726       payload->set_command(to_string(command)); 
    727       payload->set_message(to_string(result)); 
    728  
    729       payload->set_result(nagios_status_to_gpb(ret)); 
    730       message.SerializeToString(&response); 
    731       return ret; 
    732     } 
    733     template<class T> 
    734     static int create_simple_exec_response_unknown(T command, T result, std::string &response) { 
    735       Plugin::ExecuteResponseMessage message; 
    736       create_simple_header(message.mutable_header()); 
    737  
    738       Plugin::ExecuteResponseMessage::Response *payload = message.add_payload(); 
    739       payload->set_command(to_string(command)); 
    740       payload->set_message(to_string(result)); 
    741  
    742       payload->set_result(nagios_status_to_gpb(NSCAPI::returnUNKNOWN)); 
    743       message.SerializeToString(&response); 
    744       return NSCAPI::returnUNKNOWN; 
    745     } 
    746     static decoded_simple_command_data parse_simple_exec_request(const wchar_t* char_command, const std::string &request) { 
    747       Plugin::ExecuteRequestMessage message; 
    748       message.ParseFromString(request); 
    749  
    750       return parse_simple_exec_request(char_command, message); 
    751     } 
    752     static decoded_simple_command_data parse_simple_exec_request(const std::wstring cmd, const Plugin::ExecuteRequestMessage &message) { 
    753       decoded_simple_command_data data; 
    754       data.command = cmd; 
    755       if (message.has_header()) 
    756         data.target = utf8::cvt<std::wstring>(message.header().recipient_id()); 
    757  
    758       if (message.payload_size() != 1) { 
    759         throw nscapi_exception(_T("Whoops, invalid payload size (for now)")); 
    760       } 
    761       Plugin::ExecuteRequestMessage::Request payload = message.payload().Get(0); 
    762       for (int i=0;i<payload.arguments_size();i++) { 
    763         data.args.push_back(to_wstring(payload.arguments(i))); 
    764       } 
    765       return data; 
    766     } 
    767     static decoded_simple_command_data parse_simple_exec_request_payload(const Plugin::ExecuteRequestMessage::Request &payload) { 
    768       decoded_simple_command_data data; 
    769       data.command = utf8::cvt<std::wstring>(payload.command()); 
    770       for (int i=0;i<payload.arguments_size();i++) { 
    771         data.args.push_back(utf8::cvt<std::wstring>(payload.arguments(i))); 
    772       } 
    773       return data; 
    774     } 
    775  
    776  
    777     ////////////////////////////////////////////////////////////////////////// 
    778  
    779     template<class T> 
    780     struct tokenizer_data { 
    781       T perf_lable_enclosure;           // ' 
    782       T perf_separator;             // ' ' 
    783       T perf_item_splitter;           // ;  
    784       T perf_equal_sign;              // = 
    785       T perf_valid_number;            // 0123456789., 
    786  
    787     }; 
    788  
    789     template<class T> 
    790     static void parse_performance_data(Plugin::QueryResponseMessage::Response *payload, T &perf, tokenizer_data<T> tokenizer_data) { 
    791       while (true) { 
    792         if (perf.size() == 0) 
    793           return; 
    794         typename T::size_type p = 0; 
    795         if (perf[0] == tokenizer_data.perf_lable_enclosure[0]) { 
    796           p = perf.find(tokenizer_data.perf_lable_enclosure[0], 1)+1; 
    797           if (p == T::npos) 
    798             return; 
    799         } 
    800         p = perf.find(tokenizer_data.perf_separator, p); 
    801         if (p == 0) 
    802           return; 
    803         T chunk; 
    804         if (p == T::npos) { 
    805           chunk = perf; 
    806           perf = T(); 
    807         } else { 
    808           chunk = perf.substr(0, p); 
    809           p = perf.find_first_not_of(tokenizer_data.perf_separator, p); 
    810           perf = perf.substr(p); 
    811         } 
    812         std::vector<T> items = strEx::splitV(chunk, tokenizer_data.perf_item_splitter); 
    813         if (items.size() < 1) { 
    814           Plugin::Common::PerformanceData* perfData = payload->add_perf(); 
    815           perfData->set_type(Plugin::Common_DataType_STRING); 
    816           std::pair<T,T> fitem = strEx::split(T(), tokenizer_data.perf_equal_sign); 
    817           perfData->set_alias("invalid"); 
    818           Plugin::Common_PerformanceData_StringValue* stringPerfData = perfData->mutable_string_value(); 
    819           stringPerfData->set_value("invalid performance data"); 
    820           break; 
    821         } 
    822  
    823         Plugin::Common::PerformanceData* perfData = payload->add_perf(); 
    824         perfData->set_type(Plugin::Common_DataType_FLOAT); 
    825         std::pair<T,T> fitem = strEx::split(items[0], tokenizer_data.perf_equal_sign); 
    826         T alias = fitem.first; 
    827         if (alias.size() > 0 && alias[0] == tokenizer_data.perf_lable_enclosure[0] && alias[alias.size()-1] == tokenizer_data.perf_lable_enclosure[0]) 
    828           alias = alias.substr(1, alias.size()-2); 
    829            
    830         perfData->set_alias(to_string(alias)); 
    831         Plugin::Common_PerformanceData_FloatValue* floatPerfData = perfData->mutable_float_value(); 
    832  
    833         typename T::size_type pend = fitem.second.find_first_not_of(tokenizer_data.perf_valid_number); 
    834         if (pend == T::npos) { 
    835           floatPerfData->set_value(trim_to_double(fitem.second)); 
    836         } else { 
    837           floatPerfData->set_value(trim_to_double(fitem.second.substr(0,pend))); 
    838           floatPerfData->set_unit(to_string(fitem.second.substr(pend))); 
    839         } 
    840         if (items.size() > 2) { 
    841           floatPerfData->set_warning(trim_to_double(items[1])); 
    842           floatPerfData->set_critical(trim_to_double(items[2])); 
    843         } 
    844         if (items.size() >= 5) { 
    845           floatPerfData->set_minimum(trim_to_double(items[3])); 
    846           floatPerfData->set_maximum(trim_to_double(items[4])); 
    847         } 
    848       } 
    849     } 
    850     static void parse_performance_data(Plugin::QueryResponseMessage::Response *payload, std::wstring &perf) { 
    851       typedef std::wstring t_string; 
    852       typedef wchar_t t_char; 
    853       tokenizer_data<t_string> data; 
    854       data.perf_separator = _T(" "); 
    855       data.perf_lable_enclosure = _T("'"); 
    856       data.perf_equal_sign = _T("="); 
    857       data.perf_item_splitter = _T(";"); 
    858       data.perf_valid_number = _T("0123456789,."); 
    859       parse_performance_data<t_string>(payload, perf, data); 
    860     } 
    861     static void parse_performance_data(Plugin::QueryResponseMessage::Response *payload, std::string &perf) { 
    862       typedef std::string t_string; 
    863       typedef char t_char; 
    864       tokenizer_data<t_string> data; 
    865       data.perf_separator = " "; 
    866       data.perf_lable_enclosure = "'"; 
    867       data.perf_equal_sign = "="; 
    868       data.perf_item_splitter = ";"; 
    869       data.perf_valid_number = "0123456789,."; 
    870       parse_performance_data<t_string>(payload, perf, data); 
    871     } 
    872  
    873     static std::string build_performance_data(Plugin::QueryResponseMessage::Response const &payload) { 
    874       std::stringstream ss; 
    875       ss.precision(5); 
    876  
    877       bool first = true; 
    878       for (int i=0;i<payload.perf_size();i++) { 
    879         Plugin::Common::PerformanceData perfData = payload.perf(i); 
    880         if (!first) 
    881           ss << " "; 
    882         first = false; 
    883         ss << '\'' << perfData.alias() << "'="; 
    884         if (perfData.has_float_value()) { 
    885           Plugin::Common_PerformanceData_FloatValue fval = perfData.float_value(); 
    886  
    887           ss << strEx::s::itos_non_sci(fval.value()); 
    888           if (fval.has_unit()) 
    889             ss << fval.unit(); 
    890           if (!fval.has_warning()) 
    891             continue; 
    892           ss << ";" << fval.warning(); 
    893           if (!fval.has_critical())  
    894             continue; 
    895           ss << ";" << fval.critical(); 
    896           if (!fval.has_minimum()) 
    897             continue; 
    898           ss << ";" << fval.minimum(); 
    899           if (!fval.has_maximum()) 
    900             continue; 
    901           ss << ";" << fval.maximum(); 
    902         } 
    903       } 
    904       return ss.str(); 
    905     } 
    906   }; 
    907 } 
Note: See TracChangeset for help on using the changeset viewer.