source: nscp/service/simple_client.hpp @ 8013c0c

0.4.00.4.10.4.2
Last change on this file since 8013c0c was 8013c0c, checked in by Michael Medin <michael@…>, 16 months ago
  • Fixed so NSCAClient parses address correctly
  • settings exception is now derived from exception meaning it will show up more with details (instead of unknown)
  • Added API for handling log level (replaces older debug flag)
  • Added options for settings debug level
  • Changed to --settings is a global argument (meaning you can use it in any mode)
  • Added arguments parsing to test: so you can use global arguments such as --log and --settings.
  • Removed memory leak in settings parsing interface
  • Property mode set to 100644
File size: 3.4 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                        if (!core_->boot_init()) {
17                                core_->log_error(__FILE__, __LINE__, _T("Service failed to init"));
18                                return;
19                        }
20                        if (!core_->boot_load_all_plugins()) {
21                                core_->log_error(__FILE__, __LINE__, _T("Service failed to load plugins"));
22                                return;
23                        }
24                        if (!core_->boot_start_plugins(true)) {
25                                core_->log_error(__FILE__, __LINE__, _T("Service failed to start plugins"));
26                                return;
27                        }
28
29                        if (core_->get_service_control().is_started())
30                                core_->log_info(__FILE__, __LINE__, _T("Service seems to be started (Sockets and such will probably not work)..."));
31
32                        //std::wcout << _T("Using settings from: ") << settings_manager::get_core()->get_settings_type_desc() << std::endl;
33                        core_->log_info(__FILE__, __LINE__, _T("Enter command to inject or exit to terminate..."));
34/*
35                        Settings::get_settings()->clear_cache();
36                        LOG_MESSAGE_STD( _T("test 001: ") + SETTINGS_GET_STRING(NSCLIENT_TEST1) );
37                        LOG_MESSAGE_STD( _T("test 002: ") + SETTINGS_GET_STRING(NSCLIENT_TEST2) );
38                        LOG_MESSAGE_STD( _T("test 003: ") + SETTINGS_GET_STRING(NSCLIENT_TEST3) );
39                        LOG_MESSAGE_STD( _T("test 004: ") + SETTINGS_GET_STRING(NSCLIENT_TEST4) );
40
41                        Settings::get_settings()->save_to(_T("test.ini"));
42*/
43                        std::wstring s = _T("");
44
45                        while (true) {
46                                std::wstring s;
47                                std::getline(std::wcin, s);
48                                if (s == _T("exit")) {
49                                        log(_T("Exiting..."));
50                                        break;
51                                } else if (s == _T("plugins")) {
52                                        log(_T("Plugins: "));
53                                        core_->listPlugins();
54                                } else if (s == _T("list") || s == _T("commands")) {
55                                        log(_T("Commands:"));
56                                        std::list<std::wstring> lst = core_->list_commands();
57                                        for (std::list<std::wstring>::const_iterator cit = lst.begin(); cit!=lst.end();++cit)
58                                                log(_T("| ") + *cit + _T(": ") + core_->describeCommand(*cit));
59                                } else if (s.size() > 4 && s.substr(0,3) == _T("log")) {
60                                        log(_T("Setting log to: ") + s.substr(4));
61                                        core_->set_loglevel(s.substr(4));
62                                } else if (s == _T("reattach")) {
63                                        log(_T("Reattaching to session 0"));
64                                        core_->startTrayIcon(0);
65                                } else if (s == _T("assert")) {
66                                        int *foo = 0;
67                                        *foo = 0;
68                                        throw "test";
69                                } else {
70                                        try {
71                                                strEx::token t = strEx::getToken(s, ' ');
72                                                std::wstring msg, perf;
73                                                NSCAPI::nagiosReturn ret = core_->inject(t.first, t.second, msg, perf);
74                                                if (ret == NSCAPI::returnIgnored) {
75                                                        log(_T("No handler for command: ") + t.first);
76                                                } else {
77                                                        log(nscapi::plugin_helper::translateReturn(ret) + _T(":") + msg);
78                                                        if (!perf.empty())
79                                                                log(_T(" Performance data: ") + perf);
80                                                }
81                                        } catch (const nscapi::nscapi_exception &e) {
82                                                log(_T("NSCAPI Exception: ") + utf8::cvt<std::wstring>(e.what()));
83                                        } catch (const std::exception &e) {
84                                                log(_T("Exception: ") + utf8::cvt<std::wstring>(e.what()));
85                                        } catch (...) {
86                                                log(_T("Unknown exception"));
87                                        }
88                                }
89                        }
90                        core_->stop_unload_plugins_pre();
91                        core_->stop_exit_pre();
92                        core_->stop_unload_plugins_post();
93                        core_->stop_exit_post();
94                }
95        };
96}
Note: See TracBrowser for help on using the repository browser.