source: nscp/trunk/modules/NSCAAgent/NSCAThread.h @ e0936e3

Last change on this file since e0936e3 was e0936e3, checked in by Michael Medin <michael@…>, 5 years ago

+ Added so commands starting in host_ (NSCA Handlers) are sent as host-commands

  • Fixed a copy constructor in NSCA Commands (now service checks are sent as service checks)
  • Property mode set to 100644
File size: 8.0 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 <thread.h>
24#include <Mutex.h>
25#include <arrayBuffer.h>
26#include <Socket.h>
27#include "nsca_enrypt.hpp"
28
29/**
30 * @ingroup NSClientCompat
31 * PDH collector thread (gathers performance data and allows for clients to retrieve it)
32 *
33 * @version 1.0
34 * first version
35 *
36 * @date 02-13-2005
37 *
38 * @author mickem
39 *
40 * @par license
41 * This code is absolutely free to use and modify. The code is provided "as is" with
42 * no expressed or implied warranty. The author accepts no liability if it causes
43 * any damage to your computer, causes your pet to fall ill, increases baldness
44 * or makes your car start emitting strange noises when you start it up.
45 * This code has no bugs, just undocumented features!
46 *
47 */
48namespace NSCAPacket {
49#define NSCA_MAX_HOSTNAME_LENGTH        64
50#define NSCA_MAX_DESCRIPTION_LENGTH     128
51#define NSCA_MAX_PLUGINOUTPUT_LENGTH    512
52#define NSCA_MAX_PASSWORD_LENGTH     512
53
54        /******************** MISC DEFINITIONS *****************/
55
56#define NSCA_TRANSMITTED_IV_SIZE     128     /* size of IV to transmit - must be as big as largest IV needed for any crypto algorithm */
57
58
59        /*************** PACKET STRUCTURE DEFINITIONS **********/
60
61#define NSCA_PACKET_VERSION_3   3               /* packet version identifier */
62#define NSCA_PACKET_VERSION_2   2               /* older packet version identifiers */
63#define NSCA_PACKET_VERSION_1   1
64
65        typedef short int16_t;
66        typedef unsigned long u_int32_t;
67
68        /* data packet containing service check results */
69        class NSCAException {
70                std::wstring msg_;
71        public:
72                NSCAException(std::wstring msg) : msg_(msg) {}
73                std::wstring getMessage() { return msg_;}
74        };
75        typedef struct data_packet_struct {
76                int16_t   packet_version;
77                u_int32_t crc32_value;
78                u_int32_t timestamp;
79                int16_t   return_code;
80                char      host_name[NSCA_MAX_HOSTNAME_LENGTH];
81                char      svc_description[NSCA_MAX_DESCRIPTION_LENGTH];
82                char      plugin_output[NSCA_MAX_PLUGINOUTPUT_LENGTH];
83                data_packet_struct() : packet_version(NSCA_PACKET_VERSION_3) {}
84
85        } data_packet;
86
87        /* initialization packet containing IV and timestamp */
88        typedef struct init_packet_struct {
89                char      iv[NSCA_TRANSMITTED_IV_SIZE];
90                u_int32_t timestamp;
91        } init_packet;
92}
93
94
95class Arguments {
96        arrayBuffer::arrayBuffer arglist_;
97        unsigned int arglen_;
98public:
99        Arguments() : arglist_(NULL), arglen_(0) {}
100        ~Arguments() {
101                arrayBuffer::destroyArrayBuffer(arglist_, arglen_);
102                arglist_ = NULL;
103        }
104        Arguments(const Arguments& other) : arglist_(NULL), arglen_(0) {
105                arglen_ = other.getLen();
106                arglist_ = arrayBuffer::copy(other.get(), other.getLen());
107        }
108        Arguments& operator=(Arguments& other) {
109                if (this != &other){
110                        arglen_ = other.getLen();
111                        arglist_ = other.detach();
112                }
113                return *this;
114        }
115        Arguments& operator=(const Arguments& other) {
116                if (this != &other){
117                        arglen_ = other.getLen();
118                        arglist_ = arrayBuffer::copy(other.get(), other.getLen());
119                }
120                return *this;
121        }
122        Arguments(const std::wstring &other) : arglist_(NULL), arglen_(0)  {
123                arglist_ = arrayBuffer::split2arrayBuffer(other, ' ', arglen_, true);
124        }
125        arrayBuffer::arrayBuffer detach() {
126                arrayBuffer::arrayBuffer ret = arglist_;
127                arglist_ = NULL;
128                arglen_ = 0;
129                return ret;
130        }
131        const arrayBuffer::arrayBuffer get() const {
132                return arglist_;
133        }
134        unsigned int getLen() const {
135                return arglen_;
136        }
137};
138class Command {
139        std::wstring cmd_;
140        std::wstring alias_;
141        Arguments args_;
142public:
143
144        class Result {
145        public:
146                Result(std::wstring _host) : host(_host){
147                        _time32(&time);
148                        //time+=60*60 + 2*60;
149                }
150                std::wstring service;
151                std::wstring result;
152                std::wstring host;
153                unsigned int code;
154                __time32_t time;
155                std::wstring toString() const {
156                        return _T("service: ") + service + _T(", ") +
157                                _T("code: ") + strEx::itos(code) + _T(", ") +
158                                _T("time: ") + strEx::format_date(time) + _T(", ") +
159                                _T("result: ") + result;
160                }
161
162                simpleSocket::DataBuffer getBuffer(nsca_encrypt &crypt_inst) const {
163                        std::string s = strEx::wstring_to_string(service);
164                        std::string r = strEx::wstring_to_string(result);
165                        std::string h = strEx::wstring_to_string(host);
166
167                        unsigned int buffer_len = sizeof(NSCAPacket::data_packet);
168                        unsigned char* buffer = crypt_inst.get_rand_buffer(buffer_len);
169                        NSCAPacket::data_packet *data = reinterpret_cast<NSCAPacket::data_packet*>(buffer);
170                        data->packet_version=static_cast<NSCAPacket::int16_t>(htons(NSCA_PACKET_VERSION_3));
171                        data->timestamp=static_cast<NSCAPacket::u_int32_t>(htonl(time));
172                        data->return_code = code;
173                        data->crc32_value=static_cast<NSCAPacket::u_int32_t>(0L);
174
175                        if (h.length() >= NSCA_MAX_HOSTNAME_LENGTH)
176                                throw NSCAPacket::NSCAException(_T("Hostname to long"));
177                        strncpy_s(data->host_name, NSCA_MAX_HOSTNAME_LENGTH, h.c_str(), h.length());
178                        if (s.length() >= NSCA_MAX_DESCRIPTION_LENGTH)
179                                throw NSCAPacket::NSCAException(_T("description to long"));
180                        strncpy_s(data->svc_description, NSCA_MAX_DESCRIPTION_LENGTH, s.c_str(), s.length());
181                        if (r.length() >= NSCA_MAX_PLUGINOUTPUT_LENGTH)
182                                throw NSCAPacket::NSCAException(_T("result to long"));
183                        strncpy_s(data->plugin_output, NSCA_MAX_PLUGINOUTPUT_LENGTH, r.c_str(), r.length());
184
185                        unsigned int calculated_crc32=calculate_crc32(buffer,buffer_len);
186                        data->crc32_value=static_cast<NSCAPacket::u_int32_t>(htonl(calculated_crc32));
187                        crypt_inst.encrypt_buffer(buffer, buffer_len);
188                        simpleSocket::DataBuffer ret(buffer,buffer_len);
189                        crypt_inst.destroy_random_buffer(buffer);
190                        return ret;
191                }
192
193        };
194
195        Command() {}
196        Command(const Command & other) {
197                cmd_ = other.cmd_;
198                args_ = other.args_;
199                alias_ = other.alias_;
200
201        }
202        Command(std::wstring alias, std::wstring raw) : alias_(alias) {
203                strEx::token token = strEx::getToken(raw, ' ');
204                cmd_ = token.first;
205                args_ = token.second;
206        }
207        static unsigned int conv_code(NSCAPI::nagiosReturn ret) {
208                if (ret == NSCAPI::returnOK)
209                        return 0;
210                if (ret == NSCAPI::returnWARN)
211                        return 1;
212                if (ret == NSCAPI::returnCRIT)
213                        return 2;
214                return 3;
215        }
216        Result execute(std::wstring host) const;
217};
218
219class NSCAThread {
220private:
221        MutexHandler mutexHandler;
222        HANDLE hStopEvent_;
223        int checkIntervall_;
224        std::list<Command> commands_;
225        std::wstring hostname_;
226        std::wstring nscahost_;
227        unsigned int nscaport_;
228        std::string password_;
229        int encryption_method_;
230
231public:
232        NSCAThread();
233        virtual ~NSCAThread();
234        DWORD threadProc(LPVOID lpParameter);
235        void exitThread(void);
236        void send(const std::list<Command::Result> &results);
237
238
239private:
240        void addCommand(std::wstring raw);
241
242};
243
244
245typedef Thread<NSCAThread> NSCAThreadImpl;
Note: See TracBrowser for help on using the repository browser.