source: nscp/include/nscapi/functions.hpp @ 96c1461

0.4.00.4.10.4.2
Last change on this file since 96c1461 was 96c1461, checked in by Michael Medin <michael@…>, 18 months ago
  • 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)
  • Property mode set to 100644
File size: 25.6 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/math/special_functions.hpp>
24
25#include <string>
26#include <list>
27#include <iostream>
28
29#include <NSCAPI.h>
30#include <charEx.h>
31//#include <arrayBuffer.h>
32#include <types.hpp>
33#include <net/net.hpp>
34#include <unicode_char.hpp>
35#include <strEx.h>
36#include <nscapi/settings_proxy.hpp>
37
38#include <protobuf/plugin.pb.h>
39
40using namespace nscp::helpers;
41
42namespace nscapi {
43        class functions {
44        public:
45                static Plugin::Common::ResultCode nagios_status_to_gpb(int ret) {
46                        if (ret == NSCAPI::returnOK)
47                                return Plugin::Common_ResultCode_OK;
48                        if (ret == NSCAPI::returnWARN)
49                                return Plugin::Common_ResultCode_WARNING;
50                        if (ret == NSCAPI::returnCRIT)
51                                return Plugin::Common_ResultCode_CRITCAL;
52                        return Plugin::Common_ResultCode_UNKNOWN;
53                }
54                static int gbp_to_nagios_status(Plugin::Common::ResultCode ret) {
55                        if (ret == Plugin::Common_ResultCode_OK)
56                                return NSCAPI::returnOK;
57                        if (ret == Plugin::Common_ResultCode_WARNING)
58                                return NSCAPI::returnWARN;
59                        if (ret == Plugin::Common_ResultCode_CRITCAL)
60                                return NSCAPI::returnCRIT;
61                        return NSCAPI::returnUNKNOWN;
62                }
63                static Plugin::Common::Status::StatusType status_to_gpb(int ret) {
64                        if (ret == NSCAPI::isSuccess)
65                                return Plugin::Common_Status_StatusType_OK;
66                        return Plugin::Common_Status_StatusType_PROBLEM;
67                }
68                static int gbp_to_status(Plugin::Common::Status::StatusType ret) {
69                        if (ret == Plugin::Common_Status_StatusType_OK)
70                                return NSCAPI::isSuccess;
71                        return NSCAPI::hasFailed;
72                }
73                static Plugin::LogEntry::Entry::Level log_to_gpb(NSCAPI::messageTypes ret) {
74                        if (ret == NSCAPI::critical)
75                                return Plugin::LogEntry_Entry_Level_LOG_CRITICAL;
76                        if (ret == NSCAPI::debug)
77                                return Plugin::LogEntry_Entry_Level_LOG_DEBUG;
78                        if (ret == NSCAPI::error)
79                                return Plugin::LogEntry_Entry_Level_LOG_ERROR;
80                        if (ret == NSCAPI::log)
81                                return Plugin::LogEntry_Entry_Level_LOG_INFO;
82                        if (ret == NSCAPI::warning)
83                                return Plugin::LogEntry_Entry_Level_LOG_WARNING;
84                        return Plugin::LogEntry_Entry_Level_LOG_ERROR;
85                }
86                static NSCAPI::messageTypes gpb_to_log(Plugin::LogEntry::Entry::Level ret) {
87                        if (ret == Plugin::LogEntry_Entry_Level_LOG_CRITICAL)
88                                return NSCAPI::critical;
89                        if (ret == Plugin::LogEntry_Entry_Level_LOG_DEBUG)
90                                return NSCAPI::debug;
91                        if (ret == Plugin::LogEntry_Entry_Level_LOG_ERROR)
92                                return NSCAPI::error;
93                        if (ret == Plugin::LogEntry_Entry_Level_LOG_INFO)
94                                return NSCAPI::log;
95                        if (ret == Plugin::LogEntry_Entry_Level_LOG_WARNING)
96                                return NSCAPI::warning;
97                        return NSCAPI::error;
98                }
99
100                static double trim_to_double(std::wstring s) {
101                        std::wstring::size_type pend = s.find_first_not_of(_T("0123456789,."));
102                        if (pend != std::wstring::npos)
103                                s = s.substr(0,pend);
104                        strEx::replace(s, _T(","), _T("."));
105                        return strEx::stod(s);
106                }
107
108                struct decoded_simple_command_data {
109                        std::wstring command;
110                        std::wstring target;
111                        std::list<std::wstring> args;
112                        //std::vector<std::wstring> args_vector;
113                };
114
115
116
117                static void create_simple_header(Plugin::Common::Header* hdr)  {
118                        hdr->set_version(Plugin::Common_Version_VERSION_1);
119                        hdr->set_max_supported_version(Plugin::Common_Version_VERSION_1);
120                        // @todo add additional fields here!
121                }
122
123                //////////////////////////////////////////////////////////////////////////
124
125                struct destination_container {
126                        std::string id;
127                        std::string host;
128                        std::string address;
129                        std::string comment;
130                        std::list<std::string> tags;
131                        typedef std::map<std::string,std::string> data_map;
132                        data_map data;
133
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 {
142                                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";
185                        }
186
187                        std::string to_string() {
188                                std::stringstream ss;
189                                ss << "id: " << id;
190                                ss << ", host: " << host;
191                                ss << ", address: " << address;
192//                              ss << ", protocol: " << protocol;
193                                ss << ", comment: " << comment;
194                                int i=0;
195                                BOOST_FOREACH(std::string a, tags) {
196                                        ss << ", tags[" << i++ << "]: " << a;
197                                }
198                                BOOST_FOREACH(const data_map::value_type &kvp, data) {
199                                        ss << ", data[" << kvp.first << "]: " << kvp.second;
200                                }
201                                return ss.str();
202                        }
203
204                        void import(const destination_container &other) {
205                                if (!other.id.empty())
206                                        id = other.id;
207                                if (!other.host.empty())
208                                        host = other.host;
209                                if (!other.address.empty())
210                                        address = other.address;
211//                              if (!other.protocol.empty())
212//                                      protocol = other.protocol;
213                                if (!other.comment.empty())
214                                        comment = other.comment;
215                                BOOST_FOREACH(const std::string &t, other.tags) {
216                                        tags.push_back(t);
217                                }
218                                BOOST_FOREACH(const data_map::value_type &kvp, other.data) {
219                                        data[kvp.first] = kvp.second;
220                                }
221                        }
222                };
223
224                static void add_host(Plugin::Common::Header* hdr, const destination_container &dst)  {
225                        ::Plugin::Common::Host *host = hdr->add_hosts();
226                        if (!dst.id.empty())
227                                host->set_id(dst.id);
228                        if (!dst.host.empty())
229                                host->set_host(dst.host);
230                        if (!dst.address.empty())
231                                host->set_address(dst.address);
232                        if (!dst.has_protocol())
233                                host->set_protocol(dst.get_protocol());
234                        if (!dst.comment.empty())
235                                host->set_comment(dst.comment);
236                        BOOST_FOREACH(const std::string &t, dst.tags) {
237                                host->add_tags(t);
238                        }
239                        BOOST_FOREACH(const destination_container::data_map::value_type &kvp, dst.data) {
240                                ::Plugin::Common_KeyValue* x = host->add_metadata();
241                                x->set_key(kvp.first);
242                                x->set_value(kvp.second);
243                        }
244                }
245
246                static bool parse_destination(const ::Plugin::Common_Header &header, const std::string tag, destination_container &data, const bool expand_meta = false) {
247                        for (int i=0;i<header.hosts_size();++i) {
248                                const ::Plugin::Common::Host &host = header.hosts(i);
249                                if (host.id() == tag) {
250                                        data.id = tag;
251                                        if (!host.host().empty())
252                                                data.host = host.host();
253                                        if (!host.address().empty())
254                                                data.address = host.address();
255                                        if (data.address.empty() && !host.host().empty())
256                                                data.address = host.host();
257//                                      if (!host.protocol().empty())
258//                                              data.protocol = host.protocol();
259                                        if (!host.comment().empty())
260                                                data.comment = host.comment();
261                                        if (expand_meta) {
262                                                for(int j=0;j<host.tags_size(); ++j) {
263                                                        data.tags.push_back(host.tags(j));
264                                                }
265                                                for(int j=0;j<host.metadata_size(); ++j) {
266                                                        data.data[host.metadata(j).key()] = host.metadata(j).value();
267                                                }
268                                        }
269                                        return true;
270                                }
271                        }
272                        return false;
273                }
274
275                //////////////////////////////////////////////////////////////////////////
276
277                static void make_submit_from_query(std::string &message, const std::wstring channel, const std::wstring alias = _T("")) {
278                        Plugin::QueryResponseMessage response;
279                        response.ParseFromString(message);
280                        Plugin::SubmitRequestMessage request;
281                        request.mutable_header()->CopyFrom(response.header());
282                        request.set_channel(to_string(channel));
283                        for (int i=0;i<response.payload_size();++i) {
284                                request.add_payload()->CopyFrom(response.payload(i));
285                                if (!alias.empty())
286                                        request.mutable_payload(i)->set_alias(to_string(alias));
287                        }
288                        message = request.SerializeAsString();
289                }
290
291                static void create_simple_query_request(std::wstring command, std::vector<std::wstring> arguments, std::string &buffer) {
292                        Plugin::QueryRequestMessage message;
293                        create_simple_header(message.mutable_header());
294
295                        Plugin::QueryRequestMessage::Request *payload = message.add_payload();
296                        payload->set_command(to_string(command));
297
298                        BOOST_FOREACH(std::wstring s, arguments)
299                                payload->add_arguments(to_string(s));
300
301                        message.SerializeToString(&buffer);
302                }
303
304                static void create_simple_submit_request(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf, std::string &buffer) {
305                        Plugin::SubmitRequestMessage message;
306                        create_simple_header(message.mutable_header());
307                        message.set_channel(to_string(channel));
308
309                        Plugin::QueryResponseMessage::Response *payload = message.add_payload();
310                        payload->set_command(to_string(command));
311                        payload->set_message(to_string(msg));
312                        payload->set_result(nagios_status_to_gpb(ret));
313                        if (!perf.empty())
314                                parse_performance_data(payload, perf);
315
316                        message.SerializeToString(&buffer);
317                }
318                static void create_simple_submit_response(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::string &buffer) {
319                        Plugin::SubmitResponseMessage message;
320                        create_simple_header(message.mutable_header());
321                        //message.set_channel(to_string(channel));
322
323                        Plugin::SubmitResponseMessage::Response *payload = message.add_payload();
324                        payload->set_command(utf8::cvt<std::string>(command));
325                        payload->mutable_status()->set_message(to_string(msg));
326                        payload->mutable_status()->set_status(status_to_gpb(ret));
327                        message.SerializeToString(&buffer);
328                }
329                static NSCAPI::errorReturn parse_simple_submit_request(const std::string &request, std::wstring &source, std::wstring &command, std::wstring &msg, std::wstring &perf) {
330                        Plugin::SubmitRequestMessage message;
331                        message.ParseFromString(request);
332
333                        if (message.payload_size() != 1) {
334                                throw nscapi_exception(_T("Whoops, invalid payload size (for now)"));
335                        }
336                        Plugin::QueryResponseMessage::Response payload = message.payload().Get(0);
337                        source = utf8::cvt<std::wstring>(payload.source());
338                        command = utf8::cvt<std::wstring>(payload.command());
339                        msg = utf8::cvt<std::wstring>(payload.message());
340                        perf = utf8::cvt<std::wstring>(build_performance_data(payload));
341                        return gbp_to_nagios_status(payload.result());
342                }
343                /*
344                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) {
345                        alias = utf8::cvt<std::wstring>(payload.alias());
346                        command = utf8::cvt<std::wstring>(payload.command());
347                        msg = utf8::cvt<std::wstring>(payload.message());
348                        perf = utf8::cvt<std::wstring>(build_performance_data(payload));
349                        return gbp_to_nagios_status(payload.result());
350                }
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) {
362                        Plugin::SubmitResponseMessage message;
363                        message.ParseFromString(request);
364
365                        if (message.payload_size() != 1) {
366                                throw nscapi_exception(_T("Whoops, invalid payload size (for now)"));
367                        }
368                        ::Plugin::SubmitResponseMessage::Response payload = message.payload().Get(0);
369                        response = to_wstring(payload.mutable_status()->message());
370                        return gbp_to_status(payload.mutable_status()->status());
371                }
372                static NSCAPI::errorReturn parse_simple_submit_response(const std::string &request, std::string response) {
373                        Plugin::SubmitResponseMessage message;
374                        message.ParseFromString(request);
375
376                        if (message.payload_size() != 1) {
377                                throw nscapi_exception(_T("Whoops, invalid payload size (for now)"));
378                        }
379                        ::Plugin::SubmitResponseMessage::Response payload = message.payload().Get(0);
380                        response = payload.mutable_status()->message();
381                        return gbp_to_status(payload.mutable_status()->status());
382                }
383                /*
384                static void create_simple_submit_response(std::wstring msg, int status, std::string &buffer) {
385                        Plugin::SubmitResponseMessage message;
386                        create_simple_header(message.mutable_header());
387
388                        Plugin::SubmitResponseMessage::Response *payload = message.add_payload();
389                        payload->mutable_status()->set_message(to_string(msg));
390                        payload->mutable_status()->set_status(status_to_gpb(status));
391
392                        message.SerializeToString(&buffer);
393                }*/
394
395                static void create_simple_query_request(std::wstring command, std::list<std::wstring> arguments, std::string &buffer) {
396                        Plugin::QueryRequestMessage message;
397                        create_simple_header(message.mutable_header());
398
399                        Plugin::QueryRequestMessage::Request *payload = message.add_payload();
400                        payload->set_command(to_string(command));
401
402                        BOOST_FOREACH(std::wstring s, arguments)
403                                payload->add_arguments(to_string(s));
404
405                        message.SerializeToString(&buffer);
406                }
407                static NSCAPI::nagiosReturn create_simple_query_response_unknown(std::wstring command, std::wstring msg, std::wstring perf, std::string &buffer) {
408                        create_simple_query_response(command, NSCAPI::returnUNKNOWN, msg, perf, buffer);
409                        return NSCAPI::returnUNKNOWN;
410                }
411
412                static void create_simple_query_response(std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf, std::string &buffer) {
413                        Plugin::QueryResponseMessage message;
414                        create_simple_header(message.mutable_header());
415
416                        append_simple_query_response_payload(message.add_payload(), command, ret, msg, perf);
417
418                        message.SerializeToString(&buffer);
419                }
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("")) {
423                        payload->set_command(to_string(command));
424                        payload->set_message(to_string(msg));
425                        payload->set_result(nagios_status_to_gpb(ret));
426                        if (!perf.empty())
427                                parse_performance_data(payload, perf);
428                }
429
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
460                static decoded_simple_command_data parse_simple_query_request(const wchar_t* char_command, const std::string &request) {
461                        decoded_simple_command_data data;
462
463                        data.command = char_command;
464                        Plugin::QueryRequestMessage message;
465                        message.ParseFromString(request);
466
467                        if (message.payload_size() != 1) {
468                                throw nscapi_exception(_T("Whoops, invalid payload size (for now)"));
469                        }
470                        ::Plugin::QueryRequestMessage::Request payload = message.payload().Get(0);
471                        for (int i=0;i<payload.arguments_size();i++) {
472                                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)));
481                        }
482                        return data;
483                }
484
485                static int parse_simple_query_response(const std::string &response, std::wstring &msg, std::wstring &perf) {
486                        Plugin::QueryResponseMessage message;
487                        message.ParseFromString(response);
488
489
490                        if (message.payload_size() == 0) {
491                                return NSCAPI::returnUNKNOWN;
492                        } else if (message.payload_size() > 1) {
493                                throw nscapi_exception(_T("Whoops, invalid payload size (for now)"));
494                        }
495
496                        Plugin::QueryResponseMessage::Response payload = message.payload().Get(0);
497                        msg = utf8::cvt<std::wstring>(payload.message());
498                        perf = utf8::cvt<std::wstring>(build_performance_data(payload));
499                        return gbp_to_nagios_status(payload.result());
500                }
501
502
503                //////////////////////////////////////////////////////////////////////////
504
505                static void create_simple_exec_request(const std::wstring &command, const std::list<std::wstring> & args, std::string &request) {
506                       
507                        Plugin::ExecuteRequestMessage message;
508                        create_simple_header(message.mutable_header());
509
510                        Plugin::ExecuteRequestMessage::Request *payload = message.add_payload();
511                        payload->set_command(to_string(command));
512
513                        BOOST_FOREACH(std::wstring s, args)
514                                payload->add_arguments(to_string(s));
515
516                        message.SerializeToString(&request);
517                }
518                static void create_simple_exec_request(const std::wstring &command, const std::vector<std::wstring> & args, std::string &request) {
519
520                        Plugin::ExecuteRequestMessage message;
521                        create_simple_header(message.mutable_header());
522
523                        Plugin::ExecuteRequestMessage::Request *payload = message.add_payload();
524                        payload->set_command(to_string(command));
525
526                        BOOST_FOREACH(std::wstring s, args)
527                                payload->add_arguments(to_string(s));
528
529                        message.SerializeToString(&request);
530                }
531                static void parse_simple_exec_result(const std::string &response, std::list<std::wstring> &result) {
532                        Plugin::ExecuteResponseMessage message;
533                        message.ParseFromString(response);
534
535                        for (int i=0;i<message.payload_size(); i++) {
536                                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());
545                        }
546                }
547
548                static void create_simple_exec_response(std::wstring command, NSCAPI::nagiosReturn ret, std::wstring result, std::string &response) {
549                        Plugin::ExecuteResponseMessage message;
550                        create_simple_header(message.mutable_header());
551
552                        Plugin::ExecuteResponseMessage::Response *payload = message.add_payload();
553                        payload->set_command(to_string(command));
554                        payload->set_message(to_string(result));
555
556                        payload->set_result(nagios_status_to_gpb(ret));
557                        message.SerializeToString(&response);
558                }
559                static decoded_simple_command_data parse_simple_exec_request(const wchar_t* char_command, const std::string &request) {
560                        decoded_simple_command_data data;
561
562                        data.command = char_command;
563                        Plugin::ExecuteRequestMessage message;
564                        message.ParseFromString(request);
565                        if (message.has_header())
566                                data.target = utf8::cvt<std::wstring>(message.header().recipient_id());
567
568                        if (message.payload_size() != 1) {
569                                throw nscapi_exception(_T("Whoops, invalid payload size (for now)"));
570                        }
571                        Plugin::ExecuteRequestMessage::Request payload = message.payload().Get(0);
572                        for (int i=0;i<payload.arguments_size();i++) {
573                                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)));
582                        }
583                        return data;
584                }
585
586
587                //////////////////////////////////////////////////////////////////////////
588
589                static void parse_performance_data(Plugin::QueryResponseMessage::Response *payload, std::wstring &perf) {
590                        boost::tokenizer<boost::escaped_list_separator<wchar_t>, std::wstring::const_iterator, std::wstring> tok(perf, boost::escaped_list_separator<wchar_t>(L'\\', L' ', L'\''));
591                        BOOST_FOREACH(std::wstring s, tok) {
592                                if (s.size() == 0)
593                                        break;
594                                strEx::splitVector items = strEx::splitV(s, _T(";"));
595                                if (items.size() < 1) {
596                                        Plugin::Common::PerformanceData* perfData = payload->add_perf();
597                                        perfData->set_type(Plugin::Common_DataType_STRING);
598                                        std::pair<std::wstring,std::wstring> fitem = strEx::split(_T(""), _T("="));
599                                        perfData->set_alias("invalid");
600                                        Plugin::Common_PerformanceData_StringValue* stringPerfData = perfData->mutable_string_value();
601                                        stringPerfData->set_value("invalid performance data");
602                                        break;
603                                }
604
605                                Plugin::Common::PerformanceData* perfData = payload->add_perf();
606                                perfData->set_type(Plugin::Common_DataType_FLOAT);
607                                std::pair<std::wstring,std::wstring> fitem = strEx::split(items[0], _T("="));
608                                perfData->set_alias(to_string(fitem.first));
609                                Plugin::Common_PerformanceData_FloatValue* floatPerfData = perfData->mutable_float_value();
610
611                                std::wstring::size_type pend = fitem.second.find_first_not_of(_T("0123456789,."));
612                                if (pend == std::wstring::npos) {
613                                        floatPerfData->set_value(trim_to_double(fitem.second.c_str()));
614                                } else {
615                                        floatPerfData->set_value(trim_to_double(fitem.second.substr(0,pend).c_str()));
616                                        floatPerfData->set_unit(to_string(fitem.second.substr(pend)));
617                                }
618                                if (items.size() > 2) {
619                                        floatPerfData->set_warning(trim_to_double(items[1]));
620                                        floatPerfData->set_critical(trim_to_double(items[2]));
621                                }
622                                if (items.size() >= 5) {
623                                        floatPerfData->set_minimum(trim_to_double(items[3]));
624                                        floatPerfData->set_maximum(trim_to_double(items[4]));
625                                }
626                        }
627//                      std::wcout << _T("Converting performance data") << perf << _T(" -- ") << utf8::cvt<std::wstring>(build_performance_data(*resp)) << std::endl;
628                }
629                static std::string build_performance_data(Plugin::QueryResponseMessage::Response const &payload) {
630                        std::stringstream ss;
631                        ss.precision(5);
632
633                        bool first = true;
634                        for (int i=0;i<payload.perf_size();i++) {
635                                Plugin::Common::PerformanceData perfData = payload.perf(i);
636                                if (!first)
637                                        ss << " ";
638                                first = false;
639                                ss << '\'' << perfData.alias() << "'=";
640                                if (perfData.has_float_value()) {
641                                        Plugin::Common_PerformanceData_FloatValue fval = perfData.float_value();
642
643                                        ss << fval.value();
644                                        if (fval.has_unit())
645                                                ss << fval.unit();
646                                        if (!fval.has_warning())
647                                                continue;
648                                        ss << ";" << fval.warning();
649                                        if (!fval.has_critical())       
650                                                continue;
651                                        ss << ";" << fval.critical();
652                                        if (!fval.has_minimum())
653                                                continue;
654                                        ss << ";" << fval.minimum();
655                                        if (!fval.has_maximum())
656                                                continue;
657                                        ss << ";" << fval.maximum();
658                                }
659                        }
660                        return ss.str();
661                }
662        };
663}
Note: See TracBrowser for help on using the repository browser.