source: nscp/include/nscapi/functions.hpp @ 04ef932

0.4.00.4.10.4.2
Last change on this file since 04ef932 was 04ef932, checked in by Michael Medin <michael@…>, 22 months ago

2011-08-10

  • Fixed so it builds and runs on linux (but parser had issues so disabled som grammar rules whichneeds to be enabled again)
  • Added a lot of freatures and cleand up the PythonScript module
  • Started to merge som features from PythonScript back to Lua script


2011-08-07

  • Fixed a lot of issues with PythonScript module adding suport for alias and "raw command processing"
  • Fixed issue with loading plugins and aliases as well as duplicate plugin detection


2011-08-01

  • Property mode set to 100644
File size: 8.3 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
34#include <unicode_char.hpp>
35#include <strEx.h>
36#include <nscapi/settings_proxy.hpp>
37
38#include "../libs/protobuf/plugin.proto.h"
39#include "../libs/protobuf/log.proto.h"
40
41using namespace nscp::helpers;
42
43namespace nscapi {
44
45/*
46        class nscapi_exception : public exception {
47                std::string what_;
48        public:
49                nscapi_exception() {}
50                nscapi_exception(std::string what) : what_(what) {}
51                virtual const char* what() const throw() {
52                        return what_;
53                }
54        };
55*/
56        class functions {
57        public:
58                static PluginCommand::Response_Code nagios_to_gpb(int ret) {
59                        if (ret == NSCAPI::returnOK)
60                                return PluginCommand::Response_Code_OK;
61                        if (ret == NSCAPI::returnWARN)
62                                return PluginCommand::Response_Code_WARNING;
63                        if (ret == NSCAPI::returnCRIT)
64                                return PluginCommand::Response_Code_CRITCAL;
65                        return PluginCommand::Response_Code_UNKNOWN;
66                }
67                static LogMessage::Message_Level log_to_gpb(NSCAPI::messageTypes ret) {
68                        if (ret == NSCAPI::critical)
69                                return ::LogMessage::Message_Level_LOG_CRITICAL;
70                        if (ret == NSCAPI::debug)
71                                return ::LogMessage::Message_Level_LOG_DEBUG;
72                        if (ret == NSCAPI::error)
73                                return ::LogMessage::Message_Level_LOG_ERROR;
74                        if (ret == NSCAPI::log)
75                                return ::LogMessage::Message_Level_LOG_INFO;
76                        if (ret == NSCAPI::warning)
77                                return ::LogMessage::Message_Level_LOG_WARNING;
78                        return ::LogMessage::Message_Level_LOG_ERROR;
79                }
80                static NSCAPI::messageTypes gpb_to_log(LogMessage::Message_Level ret) {
81                        if (ret == ::LogMessage::Message_Level_LOG_CRITICAL)
82                                return NSCAPI::critical;
83                        if (ret == ::LogMessage::Message_Level_LOG_DEBUG)
84                                return NSCAPI::debug;
85                        if (ret == ::LogMessage::Message_Level_LOG_ERROR)
86                                return NSCAPI::error;
87                        if (ret == ::LogMessage::Message_Level_LOG_INFO)
88                                return NSCAPI::log;
89                        if (ret == ::LogMessage::Message_Level_LOG_WARNING)
90                                return NSCAPI::warning;
91                        return NSCAPI::error;
92                }
93
94                static double trim_to_double(std::wstring s) {
95                        std::wstring::size_type pend = s.find_first_not_of(_T("0123456789,."));
96                        if (pend != std::wstring::npos)
97                                s = s.substr(0,pend);
98                        strEx::replace(s, _T(","), _T("."));
99                        return strEx::stod(s);
100                }
101
102                struct decoded_simple_command_data {
103                        std::wstring command;
104                        std::list<std::wstring> args;
105                };
106                static decoded_simple_command_data process_simple_command_request(const wchar_t* char_command, const std::string &request) {
107                        decoded_simple_command_data data;
108
109                        data.command = char_command;
110                        PluginCommand::RequestMessage request_message;
111                        request_message.ParseFromString(request);
112
113                        if (request_message.payload_size() != 1) {
114                                throw nscapi_exception(_T("Whoops, invalid payload size (for now)"));
115                        }
116                        ::PluginCommand::Request payload = request_message.payload().Get(0);
117                        for (int i=0;i<payload.arguments_size();i++) {
118                                data.args.push_back(to_wstring(payload.arguments(i)));
119                        }
120                        return data;
121                }
122                static NSCAPI::nagiosReturn process_simple_command_result(std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf, std::string &response) {
123                        PluginCommand::ResponseMessage response_message;
124                        ::PluginCommand::Header* hdr = response_message.mutable_header();
125
126                        hdr->set_type(PluginCommand::Header_Type_RESPONSE);
127                        hdr->set_version(PluginCommand::Header_Version_VERSION_1);
128
129                        PluginCommand::Response *resp = response_message.add_payload();
130                        resp->set_command(to_string(command));
131                        resp->set_message(to_string(msg));
132                        parse_performance_data(resp, perf);
133
134                        resp->set_version(PluginCommand::Response_Version_VERSION_1);
135                        resp->set_result(nagios_to_gpb(ret));
136                        response_message.SerializeToString(&response);
137                        return ret;
138                }
139                static void parse_performance_data(PluginCommand::Response *resp, std::wstring &perf) {
140
141                        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'\''));
142                        BOOST_FOREACH(std::wstring s, tok) {
143                                if (s.size() == 0)
144                                        break;
145                                strEx::splitVector items = strEx::splitV(s, _T(";"));
146                                if (items.size() < 1) {
147                                        ::PluginCommand::PerformanceData* perfData = resp->add_perf();
148                                        perfData->set_type(PluginCommand::PerformanceData_Type_STRING);
149                                        std::pair<std::wstring,std::wstring> fitem = strEx::split(_T(""), _T("="));
150                                        perfData->set_alias("invalid");
151                                        ::PluginCommand::PerformanceData_StringValue* stringPerfData = perfData->mutable_string_value();
152                                        stringPerfData->set_value("invalid performance data");
153                                        break;
154                                }
155
156                                ::PluginCommand::PerformanceData* perfData = resp->add_perf();
157                                perfData->set_type(PluginCommand::PerformanceData_Type_FLOAT);
158                                std::pair<std::wstring,std::wstring> fitem = strEx::split(items[0], _T("="));
159                                perfData->set_alias(to_string(fitem.first));
160                                ::PluginCommand::PerformanceData_FloatValue* floatPerfData = perfData->mutable_float_value();
161
162                                std::wstring::size_type pend = fitem.second.find_first_not_of(_T("0123456789,."));
163                                if (pend == std::wstring::npos) {
164                                        floatPerfData->set_value(trim_to_double(fitem.second.c_str()));
165                                } else {
166                                        floatPerfData->set_value(trim_to_double(fitem.second.substr(0,pend).c_str()));
167                                        floatPerfData->set_unit(to_string(fitem.second.substr(pend)));
168                                }
169                                if (items.size() > 2) {
170                                        floatPerfData->set_warning(trim_to_double(items[1]));
171                                        floatPerfData->set_critical(trim_to_double(items[2]));
172                                }
173                                if (items.size() >= 5) {
174                                        floatPerfData->set_minimum(trim_to_double(items[3]));
175                                        floatPerfData->set_maximum(trim_to_double(items[4]));
176                                }
177                        }
178//                      std::wcout << _T("Converting performance data") << perf << _T(" -- ") << utf8::cvt<std::wstring>(build_performance_data(*resp)) << std::endl;
179                }
180                static std::string build_performance_data(::PluginCommand::Response const &payload) {
181                        std::stringstream ss;
182                        ss.precision(5);
183
184                        bool first = true;
185                        for (int i=0;i<payload.perf_size();i++) {
186                                ::PluginCommand::PerformanceData perfData = payload.perf(i);
187                                if (!first)
188                                        ss << " ";
189                                first = false;
190                                ss << '\'' << perfData.alias() << "'=";
191                                if (perfData.has_float_value()) {
192                                        ::PluginCommand::PerformanceData_FloatValue fval = perfData.float_value();
193
194                                        ss << fval.value();
195                                        if (fval.has_unit())
196                                                ss << fval.unit();
197                                        if (!fval.has_warning())
198                                                continue;
199                                        ss << ";" << fval.warning();
200                                        if (!fval.has_critical())       
201                                                continue;
202                                        ss << ";" << fval.critical();
203                                        if (!fval.has_minimum())
204                                                continue;
205                                        ss << ";" << fval.minimum();
206                                        if (!fval.has_maximum())
207                                                continue;
208                                        ss << ";" << fval.maximum();
209                                }
210                        }
211                        return ss.str();
212                }
213        };
214}
Note: See TracBrowser for help on using the repository browser.