Changeset 96c1461 in nscp for include/nscapi/functions.hpp


Ignore:
Timestamp:
11/27/11 22:04:35 (18 months ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
9853bc3
Parents:
16198e3
Message:
  • Major refactoring in the command line interface
  • Added support for alias to many common module (command line) so: nscp eventlog (is the same as nscp client --module CheckEventLog)
  • Fixed issue with CheckEventLog message rendering and eventid
  • Refactored all Client modules to all support command line, commands and submissions.
  • Added uniform handling of "everything" to all Client plugins
  • Fixed SyslogClient to work "as advertised" (ie. all hardcoded stuff is removed)
  • Fixed utf8 issue with text strings (now have a working concept which needs to be implementd "all over the place")
  • Many issues and fixes related to clients.
  • Fixed so CheckEvent? log (insert) works much better (added new options)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • include/nscapi/functions.hpp

    rf7a074d r96c1461  
    127127      std::string host; 
    128128      std::string address; 
    129       std::string protocol; 
    130129      std::string comment; 
    131130      std::list<std::string> tags; 
     
    133132      data_map data; 
    134133 
    135       net::url get_url(unsigned int port = 80) { 
     134      std::string get_protocol() const { 
     135        net::url url = get_url(); 
     136        return url.protocol; 
     137      } 
     138      bool has_protocol() const { 
     139        return !address.empty(); 
     140      } 
     141      net::url get_url(unsigned int port = 80) const { 
    136142        return net::parse(address, port); 
     143      } 
     144      static bool to_bool(std::string value) { 
     145        if (value.empty()) 
     146          return false; 
     147        if (value == "true" || value == "1") 
     148          return true; 
     149        return false; 
     150      } 
     151      static int to_int(std::string value, int def = 0) { 
     152        if (value.empty()) 
     153          return def; 
     154        try { 
     155          return boost::lexical_cast<int>(value); 
     156        } catch (...) { 
     157          return def; 
     158        } 
     159      } 
     160 
     161      inline int get_int_data(std::string key, int def = 0) { 
     162        return to_int(data[key]); 
     163      } 
     164      inline bool get_bool_data(std::string key, int def = 0) { 
     165        return to_bool(data[key]); 
     166      } 
     167      inline std::string get_string_data(std::string key) { 
     168        return data[key]; 
     169      } 
     170      inline bool has_data(std::string key) { 
     171        return data.find(key) != data.end(); 
     172      } 
     173 
     174      inline net::url get_url(int default_port) { 
     175        return net::parse(address, default_port); 
     176      } 
     177      void set_string_data(std::string key, std::string value) { 
     178        data[key] = value; 
     179      } 
     180      void set_int_data(std::string key, int value) { 
     181        data[key] = boost::lexical_cast<std::string>(value); 
     182      } 
     183      void set_bool_data(std::string key, bool value) { 
     184        data[key] = value?"true":"false"; 
    137185      } 
    138186 
     
    142190        ss << ", host: " << host; 
    143191        ss << ", address: " << address; 
    144         ss << ", protocol: " << protocol; 
     192//        ss << ", protocol: " << protocol; 
    145193        ss << ", comment: " << comment; 
    146194        int i=0; 
     
    161209        if (!other.address.empty()) 
    162210          address = other.address; 
    163         if (!other.protocol.empty()) 
    164           protocol = other.protocol; 
     211//        if (!other.protocol.empty()) 
     212//          protocol = other.protocol; 
    165213        if (!other.comment.empty()) 
    166214          comment = other.comment; 
     
    182230      if (!dst.address.empty()) 
    183231        host->set_address(dst.address); 
    184       if (!dst.protocol.empty()) 
    185         host->set_protocol(dst.protocol); 
     232      if (!dst.has_protocol()) 
     233        host->set_protocol(dst.get_protocol()); 
    186234      if (!dst.comment.empty()) 
    187235        host->set_comment(dst.comment); 
     
    205253          if (!host.address().empty()) 
    206254            data.address = host.address(); 
    207           if (!host.protocol().empty()) 
    208             data.protocol = host.protocol(); 
     255          if (data.address.empty() && !host.host().empty()) 
     256            data.address = host.host(); 
     257//          if (!host.protocol().empty()) 
     258//            data.protocol = host.protocol(); 
    209259          if (!host.comment().empty()) 
    210260            data.comment = host.comment(); 
     
    291341      return gbp_to_nagios_status(payload.result()); 
    292342    } 
     343    /* 
    293344    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) { 
    294345      alias = utf8::cvt<std::wstring>(payload.alias()); 
     
    298349      return gbp_to_nagios_status(payload.result()); 
    299350    } 
    300     static NSCAPI::errorReturn parse_simple_submit_response(const std::string &request, std::wstring response) { 
     351    */ 
     352    static int parse_simple_submit_request_payload(const Plugin::QueryResponseMessage::Response &payload, std::wstring &alias, std::wstring &message) { 
     353      alias = utf8::cvt<std::wstring>(payload.alias()); 
     354      message = utf8::cvt<std::wstring>(payload.message()); 
     355      return gbp_to_nagios_status(payload.result()); 
     356    } 
     357    static void parse_simple_query_request_payload(const Plugin::QueryRequestMessage::Request &payload, std::wstring &alias, std::wstring &command) { 
     358      alias = utf8::cvt<std::wstring>(payload.alias()); 
     359      command = utf8::cvt<std::wstring>(payload.command()); 
     360    } 
     361    static NSCAPI::errorReturn parse_simple_submit_response(const std::string &request, std::wstring &response) { 
    301362      Plugin::SubmitResponseMessage message; 
    302363      message.ParseFromString(request); 
     
    357418      message.SerializeToString(&buffer); 
    358419    } 
    359     static void append_simple_query_response_payload(Plugin::QueryResponseMessage::Response *payload, std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf) { 
     420 
     421     
     422    static void append_simple_submit_request_payload(Plugin::QueryResponseMessage::Response *payload, std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf = _T("")) { 
    360423      payload->set_command(to_string(command)); 
    361424      payload->set_message(to_string(msg)); 
     
    365428    } 
    366429 
     430    static void append_simple_query_response_payload(Plugin::QueryResponseMessage::Response *payload, std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf) { 
     431      payload->set_command(to_string(command)); 
     432      payload->set_message(to_string(msg)); 
     433      payload->set_result(nagios_status_to_gpb(ret)); 
     434      if (!perf.empty()) 
     435        parse_performance_data(payload, perf); 
     436    } 
     437 
     438    static void append_simple_exec_response_payload(Plugin::ExecuteResponseMessage::Response *payload, std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg) { 
     439      payload->set_command(to_string(command)); 
     440      payload->set_message(to_string(msg)); 
     441      payload->set_result(nagios_status_to_gpb(ret)); 
     442    } 
     443 
     444 
     445    static void append_simple_query_request_payload(Plugin::QueryRequestMessage::Request *payload, std::wstring command, std::vector<std::wstring> arguments) { 
     446      payload->set_command(to_string(command)); 
     447      BOOST_FOREACH(const std::wstring &s, arguments) { 
     448        payload->add_arguments(to_string(s)); 
     449      } 
     450    } 
     451 
     452    static void append_simple_exec_request_payload(Plugin::ExecuteRequestMessage::Request *payload, std::wstring command, std::vector<std::wstring> arguments) { 
     453      payload->set_command(to_string(command)); 
     454      BOOST_FOREACH(const std::wstring &s, arguments) { 
     455        payload->add_arguments(to_string(s)); 
     456      } 
     457    } 
     458 
     459 
    367460    static decoded_simple_command_data parse_simple_query_request(const wchar_t* char_command, const std::string &request) { 
    368461      decoded_simple_command_data data; 
     
    378471      for (int i=0;i<payload.arguments_size();i++) { 
    379472        data.args.push_back(to_wstring(payload.arguments(i))); 
     473      } 
     474      return data; 
     475    } 
     476    static decoded_simple_command_data parse_simple_query_request(const ::Plugin::QueryRequestMessage::Request &payload) { 
     477      decoded_simple_command_data data; 
     478      data.command = utf8::cvt<std::wstring>(payload.command()); 
     479      for (int i=0;i<payload.arguments_size();i++) { 
     480        data.args.push_back(utf8::cvt<std::wstring>(payload.arguments(i))); 
    380481      } 
    381482      return data; 
     
    434535      for (int i=0;i<message.payload_size(); i++) { 
    435536        result.push_back(utf8::cvt<std::wstring>(message.payload(i).message())); 
     537      } 
     538    } 
     539    static void parse_simple_exec_result(const std::string &response, std::wstring &result) { 
     540      Plugin::ExecuteResponseMessage message; 
     541      message.ParseFromString(response); 
     542 
     543      for (int i=0;i<message.payload_size(); i++) { 
     544        result += utf8::cvt<std::wstring>(message.payload(i).message()); 
    436545      } 
    437546    } 
     
    463572      for (int i=0;i<payload.arguments_size();i++) { 
    464573        data.args.push_back(to_wstring(payload.arguments(i))); 
     574      } 
     575      return data; 
     576    } 
     577    static decoded_simple_command_data parse_simple_exec_request_payload(const Plugin::ExecuteRequestMessage::Request &payload) { 
     578      decoded_simple_command_data data; 
     579      data.command = utf8::cvt<std::wstring>(payload.command()); 
     580      for (int i=0;i<payload.arguments_size();i++) { 
     581        data.args.push_back(utf8::cvt<std::wstring>(payload.arguments(i))); 
    465582      } 
    466583      return data; 
Note: See TracChangeset for help on using the changeset viewer.