Changeset 2906cda in nscp


Ignore:
Timestamp:
02/13/12 22:50:59 (16 months ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
c710cc7
Parents:
76540c3
Message:
  • Fixed installer issue (could not start service)
  • Did some refactoring in the distributed client
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • changelog

    r76540c3 r2906cda  
    22 * Process times and similar ? 
    33 * Fix configuration GUI (low priority) 
    4  * Add API for rehashing the daemon (or implement it the API is there but does nothing) 
    54 * Fixa dependonservice LanManWorkStation (old win) 
    65 * Fix RtlStringFromGUID problem on NT4 
     6 
     72012-02-13 MickeM 
     8 * Fixed installer issue (could not start service) 
    79 
    8102012-02-12 MickeM 
  • helpers/installers/installer/Product.wxs

    rf7a074d r2906cda  
    4343              <Shortcut Id="StartmenuNSClientTest" Directory="ProgramMenuDir" 
    4444                Name="$(var.App.Title) ($(var.arch), test)" WorkingDirectory="INSTALLDIR" Icon="nscp.exe" 
    45                 Advertise="yes" IconIndex="0" Arguments="--test" Description="Run $(var.App.Title) in test mode" /> 
     45                Advertise="yes" IconIndex="0" Arguments="test" Description="Run $(var.App.Title) in test mode" /> 
    4646              <fire:FirewallException Id="FWX1" Name="NSClient++ Monitoring Agent" Scope="any" IgnoreFailure="yes" /> 
    4747            </File> 
     
    5050              Type="ownProcess" Start="auto" ErrorControl="normal" Interactive="no" 
    5151              Description="Monitoring agent for nagios (and others) used to respond to status queries" 
    52               Arguments="--service --run --name [SERVICE_NAME]" /> 
     52              Arguments="service --run --name [SERVICE_NAME]" /> 
    5353            <ServiceControl Id="StartSWCNSCP" Name="[SERVICE_NAME]" Start="install" Stop="both" Wait="yes" Remove="uninstall" /> 
    5454            <RemoveFolder Id="RemoveMenuShortcuts" Directory="ProgramMenuDir" On="uninstall" /> 
     
    5757            <Shortcut Id="StartmenuNSClientStart" Directory="ProgramMenuDir" Name="Start $(var.App.Title) ($(var.arch))" 
    5858                  WorkingDirectory='INSTALLDIR' Icon="nscp.exe" IconIndex="0" 
    59                   Arguments="--service --name [SERVICE_NAME] --start" Description="Start the $(var.App.Title) service" /> 
     59                  Arguments="service --name [SERVICE_NAME] --start" Description="Start the $(var.App.Title) service" /> 
    6060            <Shortcut Id="StartmenuNSClientStop" Directory="ProgramMenuDir" Name="Stop $(var.App.Title) ($(var.arch))" 
    6161                  WorkingDirectory='INSTALLDIR' Icon="nscp.exe" IconIndex="0" 
    62                   Arguments="--service --name [SERVICE_NAME] --stop" Description="Stop the $(var.App.Title) service" /> 
     62                  Arguments="service --name [SERVICE_NAME] --stop" Description="Stop the $(var.App.Title) service" /> 
    6363            <Shortcut Id="StarmentNSCHelp" Directory="ProgramMenuDir" Name="Documentation" 
    6464                  Target="[HELP_LINK]" Icon="doc.ico" 
  • modules/DistributedClient/DistributedClient.cpp

    rba63b95 r2906cda  
    3535namespace sh = nscapi::settings_helper; 
    3636 
     37const std::wstring DistributedClient::command_prefix = _T("dnscp"); 
    3738/** 
    3839* Default c-tor 
     
    5960  std::map<std::wstring,std::wstring> commands; 
    6061 
    61   std::wstring certificate; 
    62   unsigned int timeout = 30, buffer_length = 1024; 
    63   bool use_ssl = true; 
    6462  try { 
    6563 
    66     register_command(_T("query_dist"), _T("Submit a query to a remote host via NSCP")); 
    67     register_command(_T("submit_dist"), _T("Submit a query to a remote host via NSCP")); 
    68     register_command(_T("exec_dist"), _T("Execute remote command on a remote host via NSCP")); 
    6964 
    7065    sh::settings_registry settings(get_settings_proxy()); 
     
    8984      ; 
    9085 
    91     settings.alias().add_key_to_settings(_T("targets/default")) 
    92  
    93       (_T("timeout"), sh::uint_key(&timeout, 30), 
    94       _T("TIMEOUT"), _T("Timeout when reading/writing packets to/from sockets.")) 
    95  
    96       (_T("use ssl"), sh::bool_key(&use_ssl, true), 
    97       _T("ENABLE SSL ENCRYPTION"), _T("This option controls if SSL should be enabled.")) 
    98  
    99       (_T("certificate"), sh::wpath_key(&certificate, _T("${certificate-path}/nrpe_dh_512.pem")), 
    100       _T("SSL CERTIFICATE"), _T("")) 
    101  
    102       (_T("payload length"),  sh::uint_key(&buffer_length, 1024), 
    103       _T("PAYLOAD LENGTH"), _T("Length of payload to/from the NRPE agent. This is a hard specific value so you have to \"configure\" (read recompile) your NRPE agent to use the same value for it to work.")) 
    104       ; 
    105  
    10686    settings.register_all(); 
    10787    settings.notify(); 
    10888 
     89    targets.add_missing(get_settings_proxy(), target_path, _T("default"), _T(""), true); 
     90 
     91 
    10992    get_core()->registerSubmissionListener(get_id(), channel_); 
    110  
    111     if (!targets.has_target(_T("default"))) { 
    112       add_target(_T("default"), _T("default")); 
    113       targets.rebuild(); 
    114     } 
    115     nscapi::target_handler::optarget t = targets.find_target(_T("default")); 
    116     if (t) { 
    117       if (!t->has_option("certificate")) 
    118         t->options[_T("certificate")] = certificate; 
    119       if (!t->has_option("timeout")) 
    120         t->options[_T("timeout")] = strEx::itos(timeout); 
    121       if (!t->has_option("payload length")) 
    122         t->options[_T("payload length")] = strEx::itos(buffer_length); 
    123       if (!t->has_option("ssl")) 
    124         t->options[_T("ssl")] = use_ssl?_T("true"):_T("false"); 
    125       targets.add(*t); 
    126     } else { 
    127       NSC_LOG_ERROR(_T("Default target not found!")); 
    128     } 
     93    register_command(_T("dnscp_query"), _T("Submit a query to a remote host via NSCP")); 
     94    register_command(_T("dnscp_forward"), _T("Forward query to remote NSCP host")); 
     95    register_command(_T("dnscp_submit"), _T("Submit a query to a remote host via NSCP")); 
     96    register_command(_T("dnscp_exec"), _T("Execute remote command on a remote host via NSCP")); 
     97    register_command(_T("dnscp_help"), _T("Help on using NSCP Client")); 
    12998 
    13099  } catch (nscapi::nscapi_exception &e) { 
     
    154123void DistributedClient::add_target(std::wstring key, std::wstring arg) { 
    155124  try { 
    156     nscapi::target_handler::target t = targets.add(get_settings_proxy(), target_path , key, arg); 
    157     if (t.has_option(_T("certificate"))) { 
    158       boost::filesystem::wpath p = t.options[_T("certificate")]; 
    159       if (!boost::filesystem::is_regular(p)) { 
    160         p = get_core()->getBasePath() / p; 
    161         t.options[_T("certificate")] = utf8::cvt<std::wstring>(p.string()); 
    162         targets.add(t); 
    163       } 
    164       if (boost::filesystem::is_regular(p)) { 
    165         NSC_DEBUG_MSG_STD(_T("Using certificate: ") + p.string()); 
    166       } else { 
    167         NSC_LOG_ERROR_STD(_T("Certificate not found: ") + p.string()); 
    168       } 
    169     } 
     125    targets.add(get_settings_proxy(), target_path , key, arg); 
     126  } catch (const std::exception &e) { 
     127    NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key + _T(", ") + utf8::to_unicode(e.what())); 
    170128  } catch (...) { 
    171129    NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key); 
     
    195153 
    196154NSCAPI::nagiosReturn DistributedClient::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) { 
    197   nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 
    198   std::wstring cmd = client::command_line_parser::parse_command(data.command, _T("syslog")); 
     155  std::wstring cmd = client::command_line_parser::parse_command(char_command, command_prefix); 
     156 
     157  Plugin::QueryRequestMessage message; 
     158  message.ParseFromString(request); 
     159 
    199160  client::configuration config; 
    200   setup(config); 
    201   if (!client::command_line_parser::is_command(cmd)) 
    202     return client::command_line_parser::do_execute_command_as_query(config, cmd, data.args, request, result); 
    203   return commands.exec_simple(config, data.target, char_command, data.args, result); 
     161  setup(config, message.header()); 
     162 
     163  return commands.process_query(cmd, config, message, result); 
    204164} 
    205165 
    206166int DistributedClient::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &result) { 
    207   nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 
    208   std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("syslog")); 
    209   if (!client::command_line_parser::is_command(cmd)) 
    210     return NSCAPI::returnIgnored; 
     167  std::wstring cmd = client::command_line_parser::parse_command(char_command, command_prefix); 
     168 
     169  Plugin::ExecuteRequestMessage message; 
     170  message.ParseFromString(request); 
     171 
    211172  client::configuration config; 
    212   setup(config); 
    213   return client::command_line_parser::do_execute_command_as_exec(config, cmd, data.args, result); 
     173  setup(config, message.header()); 
     174 
     175  return commands.process_exec(cmd, config, message, result); 
    214176} 
    215177 
    216178NSCAPI::nagiosReturn DistributedClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &result) { 
     179  Plugin::SubmitRequestMessage message; 
     180  message.ParseFromString(request); 
     181 
    217182  client::configuration config; 
    218   setup(config); 
    219   return client::command_line_parser::do_relay_submit(config, request, result); 
     183  setup(config, message.header()); 
     184 
     185  return client::command_line_parser::do_relay_submit(config, message, result); 
    220186} 
    221187 
     
    236202} 
    237203 
    238 void DistributedClient::setup(client::configuration &config) { 
     204void DistributedClient::setup(client::configuration &config, const ::Plugin::Common_Header& header) { 
    239205  boost::shared_ptr<clp_handler_impl> handler = boost::shared_ptr<clp_handler_impl>(new clp_handler_impl(this)); 
    240206  add_local_options(config.local, config.data); 
    241207 
    242   net::wurl url; 
    243   url.protocol = _T("dnscp"); 
    244   url.port = 5669; 
    245   nscapi::target_handler::optarget opt = targets.find_target(_T("default")); 
     208  config.data->recipient.id = header.recipient_id(); 
     209  std::wstring recipient = utf8::cvt<std::wstring>(config.data->recipient.id); 
     210  if (!targets.has_object(recipient)) { 
     211    NSC_LOG_ERROR(_T("Target not found (using default): ") + recipient); 
     212    recipient = _T("default"); 
     213  } 
     214  nscapi::targets::optional_target_object opt = targets.find_object(recipient); 
     215 
    246216  if (opt) { 
    247     nscapi::target_handler::target t = *opt; 
    248     url.host = t.host; 
    249     if (t.has_option("port")) { 
    250       try { 
    251         url.port = strEx::stoi(t.options[_T("port")]); 
    252       } catch (...) {} 
    253     } 
    254     std::string keys[] = {"certificate", "timeout", "payload length", "ssl"}; 
    255     BOOST_FOREACH(std::string s, keys) { 
    256       config.data->recipient.data[s] = utf8::cvt<std::string>(t.options[utf8::cvt<std::wstring>(s)]); 
    257     } 
    258   } 
    259   config.data->recipient.id = "default"; 
    260   config.data->recipient.address = utf8::cvt<std::string>(url.to_string()); 
     217    nscapi::targets::target_object t = *opt; 
     218    nscapi::functions::destination_container def = t.to_destination_container(); 
     219    config.data->recipient.apply(def); 
     220  } 
    261221  config.data->host_self.id = "self"; 
    262222  //config.data->host_self.host = hostname_; 
     
    266226} 
    267227 
    268 DistributedClient::connection_data DistributedClient::parse_header(const ::Plugin::Common_Header &header) { 
     228DistributedClient::connection_data DistributedClient::parse_header(const ::Plugin::Common_Header &header, client::configuration::data_type data) { 
    269229  nscapi::functions::destination_container recipient; 
    270230  nscapi::functions::parse_destination(header, header.recipient_id(), recipient, true); 
    271   return connection_data(recipient); 
     231  return connection_data(recipient, data->recipient); 
    272232} 
    273233 
     
    286246  return ret; 
    287247} 
    288 int DistributedClient::clp_handler_impl::query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
     248int DistributedClient::clp_handler_impl::query(client::configuration::data_type data, const Plugin::QueryRequestMessage &request_message, std::string &reply) { 
     249  const ::Plugin::Common_Header& request_header = request_message.header(); 
    289250  int ret = NSCAPI::returnUNKNOWN; 
    290   Plugin::QueryRequestMessage request_message; 
    291   request_message.ParseFromString(request); 
    292   connection_data con = parse_header(*header); 
     251  connection_data con = parse_header(request_header, data); 
    293252 
    294253  Plugin::QueryResponseMessage response_message; 
    295   nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     254  nscapi::functions::make_return_header(response_message.mutable_header(), request_header); 
    296255 
    297256  std::list<nscp::packet> chunks; 
    298257  chunks.push_back(nscp::factory::create_envelope_request(1)); 
    299   chunks.push_back(nscp::factory::create_payload(nscp::data::command_request, request, 0)); 
     258  chunks.push_back(nscp::factory::create_payload(nscp::data::command_request, request_message.SerializeAsString(), 0)); 
    300259  chunks = instance->send(con, chunks); 
    301260  BOOST_FOREACH(nscp::packet &chunk, chunks) { 
     
    316275} 
    317276 
    318 int DistributedClient::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
     277int DistributedClient::clp_handler_impl::submit(client::configuration::data_type data, const Plugin::SubmitRequestMessage &request_message, std::string &reply) { 
     278  const ::Plugin::Common_Header& request_header = request_message.header(); 
    319279  int ret = NSCAPI::returnUNKNOWN; 
    320   Plugin::SubmitRequestMessage request_message; 
    321   request_message.ParseFromString(request); 
    322   connection_data con = parse_header(*header); 
     280  connection_data con = parse_header(request_header, data); 
    323281  Plugin::SubmitResponseMessage response_message; 
    324   nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     282  nscapi::functions::make_return_header(response_message.mutable_header(), request_header); 
    325283 
    326284  std::list<nscp::packet> chunks; 
    327   chunks.push_back(nscp::factory::create_payload(nscp::data::command_response, request, 0)); 
     285  chunks.push_back(nscp::factory::create_payload(nscp::data::command_response, request_message.SerializeAsString(), 0)); 
    328286  chunks = instance->send(con, chunks); 
    329287  BOOST_FOREACH(nscp::packet &chunk, chunks) { 
     
    344302} 
    345303 
    346 int DistributedClient::clp_handler_impl::exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
     304int DistributedClient::clp_handler_impl::exec(client::configuration::data_type data, const Plugin::ExecuteRequestMessage &request_message, std::string &reply) { 
     305  const ::Plugin::Common_Header& request_header = request_message.header(); 
    347306  int ret = NSCAPI::returnOK; 
    348   Plugin::ExecuteRequestMessage request_message; 
    349   request_message.ParseFromString(request); 
    350   connection_data con = parse_header(*header); 
     307  connection_data con = parse_header(request_header, data); 
    351308 
    352309  Plugin::ExecuteResponseMessage response_message; 
    353   nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     310  nscapi::functions::make_return_header(response_message.mutable_header(), request_header); 
    354311 
    355312  std::list<nscp::packet> chunks; 
    356313  chunks.push_back(nscp::factory::create_envelope_request(1)); 
    357   chunks.push_back(nscp::factory::create_payload(nscp::data::exec_request, request, 0)); 
     314  chunks.push_back(nscp::factory::create_payload(nscp::data::exec_request, request_message.SerializeAsString(), 0)); 
    358315  chunks = instance->send(con, chunks); 
    359316  BOOST_FOREACH(nscp::packet &chunk, chunks) { 
  • modules/DistributedClient/DistributedClient.h

    rf33c12f r2906cda  
    2222 
    2323#include <boost/tuple/tuple.hpp> 
     24#include <boost/filesystem.hpp> 
    2425 
    2526#include <client/command_line_parser.hpp> 
     
    3738 
    3839namespace po = boost::program_options; 
    39  
     40namespace sh = nscapi::settings_helper; 
    4041 
    4142class DistributedClient : public nscapi::impl::simple_plugin { 
     
    4445  std::wstring channel_; 
    4546  std::wstring target_path; 
     47  const static std::wstring command_prefix; 
    4648 
    47   nscapi::target_handler targets; 
     49  struct custom_reader { 
     50    typedef nscapi::targets::target_object object_type; 
     51    typedef nscapi::targets::target_object target_object; 
     52 
     53    static void init_default(target_object &target) { 
     54      target.set_property_int(_T("timeout"), 30); 
     55      target.set_property_bool(_T("ssl"), true); 
     56      target.set_property_string(_T("certificate"), _T("${certificate-path}/nrpe_dh_512.pem")); 
     57      target.set_property_int(_T("payload length"), 1024); 
     58    } 
     59 
     60    static void add_custom_keys(sh::settings_registry &settings, boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object) { 
     61      settings.path(object.path).add_key() 
     62 
     63        (_T("timeout"), sh::int_fun_key<int>(boost::bind(&object_type::set_property_int, &object, _T("timeout"), _1), 30), 
     64        _T("TIMEOUT"), _T("Timeout when reading/writing packets to/from sockets.")) 
     65 
     66        ; 
     67    } 
     68 
     69    static void post_process_target(target_object &target) { 
     70      nscapi::core_wrapper* core = GET_CORE(); 
     71      if (core == NULL) { 
     72        NSC_LOG_ERROR_STD(_T("Invalid core")); 
     73        return; 
     74      } 
     75      if (target.has_option(_T("certificate"))) { 
     76        std::wstring value = target.options[_T("certificate")]; 
     77        boost::filesystem::wpath p = value; 
     78        if (!boost::filesystem::is_regular(p)) { 
     79          p = core->getBasePath() / p; 
     80          if (boost::filesystem::is_regular(p)) { 
     81            value = p.string(); 
     82          } else { 
     83            value = core->expand_path(value); 
     84          } 
     85          target.options[_T("certificate")] = value; 
     86        } 
     87        if (boost::filesystem::is_regular(p)) { 
     88          NSC_DEBUG_MSG_STD(_T("Using certificate: ") + p.string()); 
     89        } else { 
     90          NSC_LOG_ERROR_STD(_T("Certificate not found: ") + p.string()); 
     91        } 
     92      } 
     93    } 
     94 
     95  }; 
     96 
     97  nscapi::targets::handler<custom_reader> targets; 
    4898  client::command_manager commands; 
    4999 
     
    51101    std::string address; 
    52102    int timeout; 
    53     int buffer_length; 
    54     bool use_ssl; 
    55103 
    56     connection_data(nscapi::functions::destination_container recipient) { 
    57       timeout = recipient.get_int_data("timeout", 30); 
    58  
    59       address = recipient.address; 
     104    connection_data(nscapi::functions::destination_container arguments, nscapi::functions::destination_container target) { 
     105      arguments.import(target); 
     106      timeout = arguments.get_int_data("timeout", 30); 
     107      address = arguments.address.to_string(); 
    60108    } 
    61109 
     
    73121    clp_handler_impl(DistributedClient *instance) : instance(instance) {} 
    74122 
    75     int query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 
    76     int submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 
    77     int exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 
     123    int query(client::configuration::data_type data, const Plugin::QueryRequestMessage &request_message, std::string &reply); 
     124    int submit(client::configuration::data_type data, const Plugin::SubmitRequestMessage &request_message, std::string &reply); 
     125    int exec(client::configuration::data_type data, const Plugin::ExecuteRequestMessage &request_message, std::string &reply); 
    78126 
    79127    virtual nscapi::functions::destination_container lookup_target(std::wstring &id) { 
     128      nscapi::targets::optional_target_object opt = instance->targets.find_object(id); 
     129      if (opt) 
     130        return opt->to_destination_container(); 
    80131      nscapi::functions::destination_container ret; 
    81       nscapi::target_handler::optarget t = instance->targets.find_target(id); 
    82       if (t) { 
    83         if (!t->alias.empty()) 
    84           ret.id = utf8::cvt<std::string>(t->alias); 
    85         if (!t->host.empty()) 
    86           ret.host = utf8::cvt<std::string>(t->host); 
    87         if (t->has_option("address")) 
    88           ret.address = utf8::cvt<std::string>(t->options[_T("address")]); 
    89         else  
    90           ret.address = utf8::cvt<std::string>(t->host); 
    91         BOOST_FOREACH(const nscapi::target_handler::target::options_type::value_type &kvp, t->options) { 
    92           ret.data[utf8::cvt<std::string>(kvp.first)] = utf8::cvt<std::string>(kvp.second); 
    93         } 
    94       } 
    95132      return ret; 
    96133    } 
     
    143180  bool submit_nscp(std::list<std::wstring> &arguments, std::wstring &result); 
    144181 
    145   static connection_data parse_header(const ::Plugin::Common_Header &header); 
     182  static connection_data parse_header(const ::Plugin::Common_Header &header, client::configuration::data_type data); 
    146183 
    147184private: 
    148185  void add_local_options(po::options_description &desc, client::configuration::data_type data); 
    149   void setup(client::configuration &config); 
     186  void setup(client::configuration &config, const ::Plugin::Common_Header& header); 
    150187  void add_command(std::wstring key, std::wstring args); 
    151188  void add_target(std::wstring key, std::wstring args); 
  • modules/NSCPClient/NSCPClient.h

    r0f7b655 r2906cda  
    6666        _T("SSL CERTIFICATE"), _T("")) 
    6767 
    68         (_T("payload length"),  sh::int_fun_key<int>(boost::bind(&object_type::set_property_int, &object, _T("payload length"), _1), 1024), 
    69         _T("PAYLOAD LENGTH"), _T("Length of payload to/from the NRPE agent. This is a hard specific value so you have to \"configure\" (read recompile) your NRPE agent to use the same value for it to work.")) 
    7068        ; 
    7169    } 
  • version.hpp

    r76540c3 r2906cda  
    11#ifndef VERSION_HPP 
    22#define VERSION_HPP 
    3 #define PRODUCTVER     0,4,0,132 
    4 #define STRPRODUCTVER  "0,4,0,132" 
    5 #define STRPRODUCTDATE "2012-02-12" 
     3#define PRODUCTVER     0,4,0,134 
     4#define STRPRODUCTVER  "0,4,0,134" 
     5#define STRPRODUCTDATE "2012-02-13" 
    66#endif // VERSION_HPP 
  • version.txt

    r76540c3 r2906cda  
    11version=0.4.0 
    2 build=132 
    3 date=2012-02-12 
     2build=134 
     3date=2012-02-13 
Note: See TracChangeset for help on using the changeset viewer.