source: nscp/service/simple_client.hpp @ 89838be

0.4.00.4.10.4.2
Last change on this file since 89838be was 89838be, checked in by Michael Medin <michael@…>, 16 months ago
  • Added option to both execute and query item (with a reload in the middle for installing/running unittests)
  • Fixed issue in python unittest framework to handle "unload" (ie. reload)
  • Fixed NRPE certificate path lookup issue
  • Fixed some minor issues here and there
  • Fixed issue in loader to allow loading dummy as dummy (and not only dummy://)
  • Property mode set to 100644
File size: 3.5 KB
Line 
1#pragma once
2#include <nscapi/nscapi_helper.hpp>
3
4class NSClientT;
5namespace nsclient {
6        class simple_client {
7                NSClient *core_;
8        public:
9                simple_client(NSClient *core) : core_(core) {}
10
11                void log(std::wstring msg) {
12                        std::string s = nsclient::logger_helper::create_info(__FILE__, __LINE__, msg);
13                        core_->reportMessage(s);
14                }
15                void start() {
16                        core_->enableDebug(true);
17                        if (!core_->boot_init()) {
18                                core_->log_error(__FILE__, __LINE__, _T("Service failed to init"));
19                                return;
20                        }
21                        if (!core_->boot_load_all_plugins()) {
22                                core_->log_error(__FILE__, __LINE__, _T("Service failed to load plugins"));
23                                return;
24                        }
25                        if (!core_->boot_start_plugins(true)) {
26                                core_->log_error(__FILE__, __LINE__, _T("Service failed to start plugins"));
27                                return;
28                        }
29
30                        if (core_->get_service_control().is_started())
31                                core_->log_info(__FILE__, __LINE__, _T("Service seems to be started (Sockets and such will probably not work)..."));
32
33                        //std::wcout << _T("Using settings from: ") << settings_manager::get_core()->get_settings_type_desc() << std::endl;
34                        core_->log_info(__FILE__, __LINE__, _T("Enter command to inject or exit to terminate..."));
35/*
36                        Settings::get_settings()->clear_cache();
37                        LOG_MESSAGE_STD( _T("test 001: ") + SETTINGS_GET_STRING(NSCLIENT_TEST1) );
38                        LOG_MESSAGE_STD( _T("test 002: ") + SETTINGS_GET_STRING(NSCLIENT_TEST2) );
39                        LOG_MESSAGE_STD( _T("test 003: ") + SETTINGS_GET_STRING(NSCLIENT_TEST3) );
40                        LOG_MESSAGE_STD( _T("test 004: ") + SETTINGS_GET_STRING(NSCLIENT_TEST4) );
41
42                        Settings::get_settings()->save_to(_T("test.ini"));
43*/
44                        std::wstring s = _T("");
45
46                        while (true) {
47                                std::wstring s;
48                                std::getline(std::wcin, s);
49                                if (s == _T("exit")) {
50                                        log(_T("Exiting..."));
51                                        break;
52                                } else if (s == _T("plugins")) {
53                                        log(_T("Plugins: "));
54                                        core_->listPlugins();
55                                } else if (s == _T("list") || s == _T("commands")) {
56                                        log(_T("Commands:"));
57                                        std::list<std::wstring> lst = core_->list_commands();
58                                        for (std::list<std::wstring>::const_iterator cit = lst.begin(); cit!=lst.end();++cit)
59                                                log(_T("| ") + *cit + _T(": ") + core_->describeCommand(*cit));
60                                } else if (s == _T("debug off")) {
61                                        log(_T("Setting debug log off..."));
62                                        core_->enableDebug(false);
63                                } else if (s == _T("debug on")) {
64                                        log(_T("Setting debug log on..."));
65                                        core_->enableDebug(true);
66                                } else if (s == _T("reattach")) {
67                                        log(_T("Reattaching to session 0"));
68                                        core_->startTrayIcon(0);
69                                } else if (s == _T("assert")) {
70                                        int *foo = 0;
71                                        *foo = 0;
72                                        throw "test";
73                                } else {
74                                        try {
75                                                strEx::token t = strEx::getToken(s, ' ');
76                                                std::wstring msg, perf;
77                                                NSCAPI::nagiosReturn ret = core_->inject(t.first, t.second, msg, perf);
78                                                if (ret == NSCAPI::returnIgnored) {
79                                                        log(_T("No handler for command: ") + t.first);
80                                                } else {
81                                                        log(nscapi::plugin_helper::translateReturn(ret) + _T(":") + msg);
82                                                        if (!perf.empty())
83                                                                log(_T(" Performance data: ") + perf);
84                                                }
85                                        } catch (const nscapi::nscapi_exception &e) {
86                                                log(_T("NSCAPI Exception: ") + utf8::cvt<std::wstring>(e.what()));
87                                        } catch (const std::exception &e) {
88                                                log(_T("Exception: ") + utf8::cvt<std::wstring>(e.what()));
89                                        } catch (...) {
90                                                log(_T("Unknown exception"));
91                                        }
92                                }
93                        }
94                        core_->stop_unload_plugins_pre();
95                        core_->stop_exit_pre();
96                        core_->stop_unload_plugins_post();
97                        core_->stop_exit_post();
98                }
99        };
100}
Note: See TracBrowser for help on using the repository browser.