source: nscp/modules/NSClientServer/handler_impl.cpp @ 2c95d22

0.4.00.4.10.4.2
Last change on this file since 2c95d22 was 2c95d22, checked in by Michael Medin <michael@…>, 23 months ago

2011-08-14 MickeM

  • Rename Function to Registry in PythonScript API as well as some other function renames
  • Started to clean up the helpers around the API
  • Added support for execute to PythonScripts? to execute commands
  • BUG: just realised that static plugin instances prevent multiple instances :) Will fix but not now as it is not important (for me)...
  • Added initial support for channels to PythonScript Core still lacks support for subscribing to arbitrary channel
  • Property mode set to 100644
File size: 4.8 KB
Line 
1#include "stdafx.h"
2
3#include <boost/asio.hpp>
4#include <boost/lexical_cast.hpp>
5#include "handler_impl.hpp"
6#include "settings.hpp"
7
8
9#define REQ_CLIENTVERSION       1       // Works fine!
10#define REQ_CPULOAD                     2       // Quirks
11#define REQ_UPTIME                      3       // Works fine!
12#define REQ_USEDDISKSPACE       4       // Works fine!
13#define REQ_SERVICESTATE        5       // Works fine!
14#define REQ_PROCSTATE           6       // Works fine!
15#define REQ_MEMUSE                      7       // Works fine!
16#define REQ_COUNTER                     8       // Works fine!
17#define REQ_FILEAGE                     9       // Works fine! (i hope)
18#define REQ_INSTANCES           10      // Works fine! (i hope)
19
20bool handler_impl::isPasswordOk(std::wstring remotePassword)  {
21        std::wstring localPassword = get_password();
22        if (localPassword == remotePassword) {
23                return true;
24        }
25        if ((remotePassword == _T("None")) && (localPassword.empty())) {
26                return true;
27        }
28        return false;
29}
30
31void split_to_list(std::list<std::wstring> &list, std::wstring str) {
32        strEx::splitList add = strEx::splitEx(str, _T("&"));
33        list.insert(list.begin(), add.begin(), add.end());
34}
35
36check_nt::packet handler_impl::handle(check_nt::packet p) {
37        std::wstring buffer = p.get_payload();
38        NSC_DEBUG_MSG_STD(_T("Data: ") + buffer);
39
40        std::wstring::size_type pos = buffer.find_first_of(_T("\n\r"));
41        if (pos != std::wstring::npos) {
42                std::wstring::size_type pos2 = buffer.find_first_not_of(_T("\n\r"), pos);
43                if (pos2 != std::wstring::npos) {
44                        std::wstring rest = buffer.substr(pos2);
45                        NSC_DEBUG_MSG_STD(_T("Ignoring data: ") + rest);
46                }
47                buffer = buffer.substr(0, pos);
48        }
49
50        strEx::token pwd = strEx::getToken(buffer, '&');
51        if (!isPasswordOk(pwd.first)) {
52                NSC_LOG_ERROR_STD(_T("Invalid password (") + pwd.first + _T(")."));
53                return check_nt::packet("ERROR: Invalid password.");
54        }
55        if (pwd.second.empty())
56                return check_nt::packet("ERROR: No command specified.");
57        strEx::token cmd = strEx::getToken(pwd.second, '&');
58        if (cmd.first.empty())
59                return check_nt::packet("ERROR: No command specified.");
60
61        int c = boost::lexical_cast<int>(cmd.first.c_str());
62
63        NSC_DEBUG_MSG_STD(_T("Data: ") + cmd.second);
64
65        std::list<std::wstring> args;
66
67        // prefix various commands
68        switch (c) {
69                case REQ_CPULOAD:
70                        cmd.first = _T("checkCPU");
71                        split_to_list(args, cmd.second);
72                        args.push_back(_T("nsclient"));
73                        break;
74                case REQ_UPTIME:
75                        cmd.first = _T("checkUpTime");
76                        args.push_back(_T("nsclient"));
77                        break;
78                case REQ_USEDDISKSPACE:
79                        cmd.first = _T("CheckDriveSize");
80                        split_to_list(args, cmd.second);
81                        args.push_back(_T("nsclient"));
82                        break;
83                case REQ_CLIENTVERSION:
84                        {
85                                std::wstring v = SETTINGS_GET_STRING(nsclient::VERSION);
86                                if (v == _T("auto"))
87                                        v = nscapi::plugin_singleton->get_core()->getApplicationName() + _T(" ") + nscapi::plugin_singleton->get_core()->getApplicationVersionString();
88                                return strEx::wstring_to_string(v);
89                        }
90                case REQ_SERVICESTATE:
91                        cmd.first = _T("checkServiceState");
92                        split_to_list(args, cmd.second);
93                        args.push_back(_T("nsclient"));
94                        break;
95                case REQ_PROCSTATE:
96                        cmd.first = _T("checkProcState");
97                        split_to_list(args, cmd.second);
98                        args.push_back(_T("nsclient"));
99                        break;
100                case REQ_MEMUSE:
101                        cmd.first = _T("checkMem");
102                        args.push_back(_T("nsclient"));
103                        break;
104                case REQ_COUNTER:
105                        cmd.first = _T("checkCounter");
106                        args.push_back(_T("Counter=") + cmd.second);
107                        args.push_back(_T("nsclient"));
108                        break;
109                case REQ_FILEAGE:
110                        cmd.first = _T("getFileAge");
111                        args.push_back(_T("path=") + cmd.second);
112                        break;
113                case REQ_INSTANCES:
114                        cmd.first = _T("listCounterInstances");
115                        args.push_back(cmd.second);
116                        break;
117
118
119                default:
120                        split_to_list(args, cmd.second);
121        }
122
123        std::wstring message, perf;
124        NSCAPI::nagiosReturn ret = nscapi::plugin_singleton->get_core()->simple_query(cmd.first.c_str(), args, message, perf);
125        if (!nscapi::plugin_helper::isNagiosReturnCode(ret)) {
126                if (message.empty())
127                        return check_nt::packet("ERROR: Could not complete the request check log file for more information.");
128                return check_nt::packet("ERROR: " + strEx::wstring_to_string(message));
129        }
130        switch (c) {
131                case REQ_UPTIME:                // Some check_nt commands has no return code syntax
132                case REQ_MEMUSE:
133                case REQ_CPULOAD:
134                case REQ_CLIENTVERSION:
135                case REQ_USEDDISKSPACE:
136                case REQ_COUNTER:
137                case REQ_FILEAGE:
138                        return check_nt::packet(message);
139
140                case REQ_SERVICESTATE:  // Some check_nt commands return the return code (coded as a string)
141                case REQ_PROCSTATE:
142                        return check_nt::packet(strEx::itos(nscapi::plugin_helper::nagios2int(ret)) + _T("& ") + message);
143
144                default:                                // "New" check_nscp also returns performance data
145                        if (perf.empty())
146                                return check_nt::packet(nscapi::plugin_helper::translateReturn(ret) + _T("&") + message);
147                        return check_nt::packet(nscapi::plugin_helper::translateReturn(ret) + _T("&") + message + _T("&") + perf);
148        }
149
150        return check_nt::packet("FOO");
151}
152
153
154
155
Note: See TracBrowser for help on using the repository browser.