source: nscp/modules/NSCAClient/NSCAClient.h @ 6090c98

0.4.10.4.2
Last change on this file since 6090c98 was 6090c98, checked in by Michael Medin <michael@…>, 11 months ago
  • Fixed issue with parsing in test mode
  • Fixed Graphite client using wrong time for datestamp
  • Added so debug NSCA logs host name
  • Property mode set to 100644
File size: 8.5 KB
Line 
1/**************************************************************************
2*   Copyright (C) 2004-2007 by Michael Medin <michael@medin.name>         *
3*                                                                         *
4*   This code is part of NSClient++ - http://trac.nakednuns.org/nscp      *
5*                                                                         *
6*   This program is free software; you can redistribute it and/or modify  *
7*   it under the terms of the GNU General Public License as published by  *
8*   the Free Software Foundation; either version 2 of the License, or     *
9*   (at your option) any later version.                                   *
10*                                                                         *
11*   This program is distributed in the hope that it will be useful,       *
12*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14*   GNU General Public License for more details.                          *
15*                                                                         *
16*   You should have received a copy of the GNU General Public License     *
17*   along with this program; if not, write to the                         *
18*   Free Software Foundation, Inc.,                                       *
19*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20***************************************************************************/
21#pragma once
22
23#include <boost/tuple/tuple.hpp>
24
25#include <client/command_line_parser.hpp>
26#include <nscapi/targets.hpp>
27#include <nscapi/nscapi_protobuf_types.hpp>
28
29#include <nsca/nsca_packet.hpp>
30
31NSC_WRAPPERS_MAIN()
32NSC_WRAPPERS_CLI()
33NSC_WRAPPERS_CHANNELS()
34
35namespace po = boost::program_options;
36namespace sh = nscapi::settings_helper;
37
38class NSCAAgent : public nscapi::impl::simple_plugin {
39private:
40
41        std::wstring channel_;
42        std::wstring target_path;
43        const static std::wstring command_prefix;
44        std::string hostname_;
45        bool cacheNscaHost_;
46        long time_delta_;
47
48        struct custom_reader {
49                typedef nscapi::targets::target_object object_type;
50                typedef nscapi::targets::target_object target_object;
51
52                static void init_default(target_object &target) {
53                        target.set_property_int(_T("timeout"), 30);
54                        target.set_property_string(_T("encryption"), _T("ase"));
55                        target.set_property_int(_T("payload length"), 512);
56                }
57
58                static void add_custom_keys(sh::settings_registry &settings, boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object) {
59                        settings.path(object.path).add_key()
60
61                                (_T("timeout"), sh::int_fun_key<int>(boost::bind(&object_type::set_property_int, &object, _T("timeout"), _1), 30),
62                                _T("TIMEOUT"), _T("Timeout when reading/writing packets to/from sockets."))
63
64                                (_T("payload length"),  sh::int_fun_key<int>(boost::bind(&object_type::set_property_int, &object, _T("payload length"), _1), 512),
65                                _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."), true)
66
67                                (_T("encryption"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("encryption"), _1), _T("aes")),
68                                _T("ENCRYPTION METHOD"), _T("Number corresponding to the various encryption algorithms (see the wiki). Has to be the same as the server or it wont work at all."))
69
70                                (_T("password"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("password"), _1), _T("")),
71                                _T("PASSWORD"), _T("The password to use. Again has to be the same as the server or it wont work at all."))
72
73                                (_T("time offset"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_property_string, &object, _T("delay"), _1), _T("0")),
74                                _T("TIME OFFSET"), _T("Time offset."), true)
75                                ;
76                }
77                static void post_process_target(target_object &target) {
78                }
79        };
80
81        nscapi::targets::handler<custom_reader> targets;
82        client::command_manager commands;
83
84public:
85        struct connection_data {
86                std::string password;
87                std::string encryption;
88                std::string host;
89                std::string port;
90                std::string sender_hostname;
91                int timeout;
92                int buffer_length;
93                int time_delta;
94
95                connection_data(nscapi::protobuf::types::destination_container arguments, nscapi::protobuf::types::destination_container target, nscapi::protobuf::types::destination_container sender) {
96                        arguments.import(target);
97                        timeout = arguments.get_int_data("timeout", 30);
98                        buffer_length = arguments.get_int_data("payload length", 512);
99                        password = arguments.get_string_data("password");
100                        encryption = arguments.get_string_data("encryption");
101                        std::string tmp = arguments.get_string_data("time offset");
102                        if (!tmp.empty())
103                                time_delta = strEx::stol_as_time_sec(arguments.get_string_data("time offset"));
104                        else
105                                time_delta = 0;
106                        host = arguments.address.get_host();
107                        port = strEx::s::xtos(arguments.address.get_port(5667));
108                        sender_hostname = sender.address.host;
109                        if (sender.has_data("host"))
110                                sender_hostname = sender.get_string_data("host");
111                }
112                unsigned int get_encryption() {
113                        return nsca::nsca_encrypt::helpers::encryption_to_int(encryption);
114                }
115
116                std::wstring to_wstring() const {
117                        return utf8::cvt<std::wstring>(to_string());
118                }
119
120                std::string to_string() const {
121                        std::stringstream ss;
122                        ss << "host: " << host;
123                        ss << ", port: " << port;
124                        ss << ", timeout: " << timeout;
125                        ss << ", buffer_length: " << buffer_length;
126                        ss << ", time_delta: " << time_delta;
127                        ss << ", password: " << password;
128                        ss << ", encryption: " << encryption;
129                        ss << ", hostname: " << sender_hostname;
130                        return ss.str();
131                }
132        };
133
134        struct clp_handler_impl : public client::clp_handler, client::target_lookup_interface {
135
136                NSCAAgent *instance;
137                clp_handler_impl(NSCAAgent *instance) : instance(instance) {}
138
139                int query(client::configuration::data_type data, const Plugin::QueryRequestMessage &request_message, std::string &reply);
140                int submit(client::configuration::data_type data, const Plugin::SubmitRequestMessage &request_message, std::string &reply);
141                int exec(client::configuration::data_type data, const Plugin::ExecuteRequestMessage &request_message, std::string &reply);
142
143                virtual nscapi::protobuf::types::destination_container lookup_target(std::wstring &id) {
144                        nscapi::targets::optional_target_object opt = instance->targets.find_object(id);
145                        if (opt)
146                                return opt->to_destination_container();
147                        nscapi::protobuf::types::destination_container ret;
148                        return ret;
149                }
150        };
151
152
153public:
154        NSCAAgent();
155        virtual ~NSCAAgent();
156        // Module calls
157        bool loadModule();
158        bool loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode);
159        bool unloadModule();
160
161        /**
162        * Return the module name.
163        * @return The module name
164        */
165        static std::wstring getModuleName() {
166                return _T("NSCAClient");
167        }
168        /**
169        * Module version
170        * @return module version
171        */
172        static nscapi::plugin_wrapper::module_version getModuleVersion() {
173                nscapi::plugin_wrapper::module_version version = {0, 4, 0 };
174                return version;
175        }
176        static std::wstring getModuleDescription() {
177                return std::wstring(_T("Passive check support over NSCA.\nAvalible crypto are: ")) + getCryptos();
178        }
179
180        bool hasCommandHandler() { return true; };
181        bool hasMessageHandler() { return true; };
182        bool hasNotificationHandler() { return true; };
183        NSCAPI::nagiosReturn handleRAWNotification(const wchar_t* channel, std::string request, std::string &response);
184        NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response);
185        NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response);
186
187private:
188        boost::tuple<int,std::wstring> send(connection_data data, const std::list<nsca::packet> packets);
189        void add_options(po::options_description &desc, connection_data &command_data);
190        static connection_data parse_header(const ::Plugin::Common_Header &header, client::configuration::data_type data);
191
192private:
193        void add_local_options(po::options_description &desc, client::configuration::data_type data);
194        void setup(client::configuration &config, const ::Plugin::Common_Header& header);
195        void add_command(std::wstring key, std::wstring args);
196        void add_target(std::wstring key, std::wstring args);
197
198        static std::wstring getCryptos();
199        void set_delay(std::wstring key) {
200                time_delta_ = strEx::stol_as_time_sec(key, 1);
201        }
202
203};
Note: See TracBrowser for help on using the repository browser.