source: nscp/service/simple_client.hpp @ 6090c98

0.4.10.4.2
Last change on this file since 6090c98 was 6090c98, checked in by Michael Medin <michael@…>, 11 months ago
  • Fixed issue with parsing in test mode
  • Fixed Graphite client using wrong time for datestamp
  • Added so debug NSCA logs host name
  • Property mode set to 100644
File size: 4.0 KB
Line 
1#pragma once
2#include <nscapi/nscapi_helper.hpp>
3#include <nsclient/logger.hpp>
4class NSClientT;
5namespace nsclient {
6        class simple_client {
7                NSClient *core_;
8        public:
9                simple_client(NSClient *core) : core_(core) {}
10
11                inline nsclient::logging::logger_interface* get_logger() const {
12                        return nsclient::logging::logger::get_logger();
13                }
14                void start(std::wstring log) {
15                        if (!core_->boot_init(log)) {
16                                get_logger()->error(__FILE__, __LINE__, _T("Service failed to init"));
17                                return;
18                        }
19                        if (!core_->boot_load_all_plugins()) {
20                                get_logger()->error(__FILE__, __LINE__, _T("Service failed to load plugins"));
21                                return;
22                        }
23                        if (!core_->boot_start_plugins(true)) {
24                                get_logger()->error(__FILE__, __LINE__, _T("Service failed to start plugins"));
25                                return;
26                        }
27
28                        if (core_->get_service_control().is_started())
29                                get_logger()->info(__FILE__, __LINE__, _T("Service seems to be started (Sockets and such will probably not work)..."));
30
31                        //std::wcout << _T("Using settings from: ") << settings_manager::get_core()->get_settings_type_desc() << std::endl;
32                        get_logger()->info(__FILE__, __LINE__, _T("Enter command to inject or exit to terminate..."));
33/*
34                        Settings::get_settings()->clear_cache();
35                        LOG_MESSAGE_STD( _T("test 001: ") + SETTINGS_GET_STRING(NSCLIENT_TEST1) );
36                        LOG_MESSAGE_STD( _T("test 002: ") + SETTINGS_GET_STRING(NSCLIENT_TEST2) );
37                        LOG_MESSAGE_STD( _T("test 003: ") + SETTINGS_GET_STRING(NSCLIENT_TEST3) );
38                        LOG_MESSAGE_STD( _T("test 004: ") + SETTINGS_GET_STRING(NSCLIENT_TEST4) );
39
40                        Settings::get_settings()->save_to(_T("test.ini"));
41*/
42                        std::wstring s = _T("");
43
44                        while (true) {
45                                std::wstring s;
46                                std::getline(std::wcin, s);
47                                if (s == _T("exit")) {
48                                        get_logger()->info(__FILE__, __LINE__, _T("Exiting..."));
49                                        break;
50                                } else if (s == _T("plugins")) {
51                                        get_logger()->info(__FILE__, __LINE__, _T("Plugins: "));
52                                        core_->listPlugins();
53                                } else if (s == _T("list") || s == _T("commands")) {
54                                        get_logger()->info(__FILE__, __LINE__, _T("Commands:"));
55                                        std::list<std::wstring> lst = core_->list_commands();
56                                        for (std::list<std::wstring>::const_iterator cit = lst.begin(); cit!=lst.end();++cit)
57                                                get_logger()->info(__FILE__, __LINE__, _T("| ") + *cit + _T(": ") + core_->describeCommand(*cit));
58                                } else if (s.size() > 4 && s.substr(0,3) == _T("log")) {
59                                        get_logger()->info(__FILE__, __LINE__, _T("Setting log to: ") + s.substr(4));
60                                        nsclient::logging::logger::set_log_level(s.substr(4));
61                                } else if (s == _T("reattach")) {
62                                        get_logger()->info(__FILE__, __LINE__, _T("Reattaching to session 0"));
63                                        core_->startTrayIcon(0);
64                                } else if (s == _T("assert")) {
65                                        int *foo = 0;
66                                        *foo = 0;
67                                        throw "test";
68                                } else {
69                                        try {
70                                                strEx::token t = strEx::getToken(s, L' ');
71                                                std::wstring msg, perf;
72                                                NSCAPI::nagiosReturn ret = core_->inject(t.first, t.second, msg, perf);
73                                                if (ret == NSCAPI::returnIgnored) {
74                                                        get_logger()->info(__FILE__, __LINE__, _T("No handler for command: ") + t.first);
75                                                } else {
76                                                        if (msg.size() > 4096) {
77                                                                get_logger()->info(__FILE__, __LINE__, _T("Command returned: ") + strEx::itos(msg.size()) + _T(" bytes of data will only display first 4k."));
78                                                                msg = msg.substr(0, 4096);
79                                                        }
80                                                        get_logger()->info(__FILE__, __LINE__, nscapi::plugin_helper::translateReturn(ret) + _T(":") + msg);
81                                                        if (!perf.empty())
82                                                                get_logger()->info(__FILE__, __LINE__, _T(" Performance data: ") + perf);
83                                                }
84                                        } catch (const nscapi::nscapi_exception &e) {
85                                                get_logger()->error(__FILE__, __LINE__, _T("NSCAPI Exception: ") + utf8::cvt<std::wstring>(e.what()));
86                                        } catch (const std::exception &e) {
87                                                get_logger()->error(__FILE__, __LINE__, _T("Exception: ") + utf8::cvt<std::wstring>(e.what()));
88                                        } catch (...) {
89                                                get_logger()->error(__FILE__, __LINE__, _T("Unknown exception"));
90                                        }
91                                }
92                        }
93                        core_->stop_unload_plugins_pre();
94                        core_->stop_exit_pre();
95                        core_->stop_exit_post();
96                }
97        };
98}
Note: See TracBrowser for help on using the repository browser.