source: nscp/modules/CheckExternalScripts/commands.hpp @ b67d231

0.4.00.4.10.4.2
Last change on this file since b67d231 was b67d231, checked in by Michael Medin <michael@…>, 12 months ago
  • Added log level = off to disable logging.
  • Added option in NSCAClient to hostname (auto-lc) to use lower case version of hostname. #533
  • Reworked how commands are read. If a command is defined without a section (default) no section will be added and instead a comment will be addded on how to add the section. This should (I hope) resolve the "missing command" for good.
  • Improved error messages for missing commands
  • Fixed scientific notation on performance data (#)
  • Property mode set to 100644
File size: 5.6 KB
Line 
1#pragma once
2
3#include <map>
4#include <string>
5
6#include <boost/foreach.hpp>
7#include <boost/optional.hpp>
8#include <boost/shared_ptr.hpp>
9
10#include <settings/client/settings_client.hpp>
11#include <nscapi/settings_proxy.hpp>
12#include <nscapi/settings_object.hpp>
13#include <nscapi/functions.hpp>
14#include <nscapi/nscapi_helper.hpp>
15
16namespace sh = nscapi::settings_helper;
17
18namespace commands {
19        struct command_object {
20
21                command_object() : is_template(false) {}
22                command_object(const command_object &other)
23                        : path(other.path)
24                        , alias(other.alias)
25                        , value(other.value)
26                        , parent(other.parent)
27                        , is_template(other.is_template)
28                        , command(other.command)
29                        , arguments(other.arguments)
30                        , user(other.user)
31                        , domain(other.domain)
32                        , password(other.password)
33                {}
34                const command_object& operator =(const command_object &other) {
35                        path = other.path;
36                        alias = other.alias;
37                        value = other.value;
38                        parent = other.parent;
39                        is_template = other.is_template;
40                        command = other.command;
41                        arguments = other.arguments;
42                        user = other.user;
43                        domain = other.domain;
44                        password = other.password;
45                        return *this;
46                }
47       
48                // Object keys (managed by object handler)
49                std::wstring path;
50                std::wstring alias;
51                std::wstring value;
52                std::wstring parent;
53                bool is_template;
54
55                // Command keys
56                std::wstring command;
57                std::list<std::wstring> arguments;
58                std::wstring user, domain, password;
59
60
61                std::wstring get_argument() const {
62                        std::wstring args;
63                        BOOST_FOREACH(std::wstring s, arguments) {
64                                if (!args.empty())
65                                        args += _T(" ");
66                                args += s;
67                        }
68                        return args;
69                }
70
71                std::wstring to_wstring() const {
72                        std::wstringstream ss;
73                        ss << alias << _T("[") << alias << _T("] = ")
74                                << _T("{command: ") << command
75                                << _T(", arguments: ") << get_argument();
76                                if (!user.empty()) {
77                                        ss << _T(", user: ") << user
78                                        << _T(", domain: ") << domain
79                                        << _T(", password: ") << password;
80                                }
81                                ss << _T("}");
82                        return ss.str();
83                }
84
85                void set_command(std::wstring str) {
86                        if (str.empty())
87                                return;
88                        try {
89                                strEx::parse_command(str, command, arguments);
90                        } catch (const std::exception &e) {
91                                NSC_LOG_MESSAGE(_T("Failed to parse arguments for command '") + alias + _T("', using old split string method: ") + utf8::to_unicode(e.what()) + _T(": ") + str);
92                                strEx::splitList list = strEx::splitEx(str, _T(" "));
93                                if (list.size() > 0) {
94                                        command = list.front();
95                                        list.pop_front();
96                                }
97                                arguments.clear();
98                                BOOST_FOREACH(std::wstring s, list) {
99                                        arguments.push_back(s);
100                                }
101                        }
102                }
103
104        };
105        typedef boost::optional<command_object> optional_command_object;
106
107        template<class T>
108        inline void import_string(T &object, T &parent) {
109                if (object.empty() && !parent.empty())
110                        object = parent;
111        }
112
113
114        struct command_reader {
115                typedef command_object object_type;
116
117                static void post_process_object(object_type &object) {}
118
119
120                static void read_object(boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object, bool oneliner) {
121                        object.set_command(object.value);
122                        std::wstring alias;
123                        //if (object.alias == _T("default"))
124                        // Pupulate default template!
125
126                        nscapi::settings_helper::settings_registry settings(proxy);
127
128                        if (oneliner) {
129                                std::wstring::size_type pos = object.path.find_last_of(_T("/"));
130                                if (pos != std::wstring::npos) {
131                                        std::wstring path = object.path.substr(0, pos);
132                                        std::wstring key = object.path.substr(pos+1);
133                                        proxy->register_key(path, key, NSCAPI::key_string, object.alias, _T("Alias for ") + object.alias + _T(". To configure this item add a section called: ") + object.path, _T(""), false);
134                                        proxy->set_string(path, key, object.value);
135                                        return;
136                                }
137                        }
138                        settings.path(object.path).add_path()
139                                (_T("COMMAND DEFENITION"), _T("Command definition for: ") + object.alias)
140                                ;
141
142                        settings.path(object.path).add_key()
143                                (_T("command"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_command, &object, _1)),
144                                _T("COMMAND"), _T("Command to execute"))
145
146                                (_T("alias"), sh::wstring_key(&alias),
147                                _T("ALIAS"), _T("The alias (service name) to report to server"), true)
148
149                                (_T("parent"), nscapi::settings_helper::wstring_key(&object.parent, _T("default")),
150                                _T("PARENT"), _T("The parent the target inherits from"), true)
151
152                                (_T("is template"), nscapi::settings_helper::bool_key(&object.is_template, false),
153                                _T("IS TEMPLATE"), _T("Declare this object as a template (this means it will not be available as a separate object)"), true)
154
155                                (_T("user"), nscapi::settings_helper::wstring_key(&object.user),
156                                _T("USER"), _T("The user to run the command as"), true)
157
158                                (_T("domain"), nscapi::settings_helper::wstring_key(&object.domain),
159                                _T("DOMAIN"), _T("The user to run the command as"), true)
160
161                                (_T("password"), nscapi::settings_helper::wstring_key(&object.password),
162                                _T("PASSWORD"), _T("The user to run the command as"), true)
163
164                                ;
165
166                        settings.register_all();
167                        settings.notify();
168                        if (!alias.empty())
169                                object.alias = alias;
170                }
171
172                static void apply_parent(object_type &object, object_type &parent) {
173                        import_string(object.user, parent.user);
174                        import_string(object.domain, parent.domain);
175                        import_string(object.password, parent.password);
176                        import_string(object.command, parent.command);
177                        if (object.arguments.empty() && !parent.arguments.empty())
178                                object.arguments = parent.arguments;
179                }
180
181        };
182        typedef nscapi::settings_objects::object_handler<command_object, command_reader> command_handler;
183}
184
Note: See TracBrowser for help on using the repository browser.