| [76540c3] | 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 |
|
|---|
| 16 | namespace sh = nscapi::settings_helper;
|
|---|
| 17 |
|
|---|
| 18 | namespace schedules {
|
|---|
| 19 | struct schedule_object {
|
|---|
| 20 |
|
|---|
| 21 | schedule_object() : id(0), report(0), is_template(false) {}
|
|---|
| 22 | schedule_object(const schedule_object &other)
|
|---|
| 23 | : path(other.path)
|
|---|
| 24 | , alias(other.alias)
|
|---|
| 25 | , value(other.value)
|
|---|
| 26 | , parent(other.parent)
|
|---|
| 27 | , target_id(other.target_id)
|
|---|
| 28 | , duration(other.duration)
|
|---|
| 29 | , channel(other.channel)
|
|---|
| 30 | , report(other.report)
|
|---|
| 31 | , command(other.command)
|
|---|
| 32 | , arguments(other.arguments)
|
|---|
| 33 | , id(other.id)
|
|---|
| 34 | , is_template(other.is_template)
|
|---|
| 35 | {}
|
|---|
| 36 | const schedule_object& operator =(const schedule_object &other) {
|
|---|
| 37 | path = other.path;
|
|---|
| 38 | alias = other.alias;
|
|---|
| 39 | value = other.value;
|
|---|
| 40 | parent = other.parent;
|
|---|
| 41 | target_id = other.target_id;
|
|---|
| 42 | duration = other.duration;
|
|---|
| 43 | channel = other.channel;
|
|---|
| 44 | report = other.report;
|
|---|
| 45 | command = other.command;
|
|---|
| 46 | arguments = other.arguments;
|
|---|
| 47 | id = other.id;
|
|---|
| 48 | is_template = other.is_template;
|
|---|
| 49 | return *this;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | // Object keys (managed by object handler)
|
|---|
| 53 | std::wstring path;
|
|---|
| 54 | std::wstring alias;
|
|---|
| 55 | std::wstring value;
|
|---|
| 56 | std::wstring parent;
|
|---|
| 57 | bool is_template;
|
|---|
| 58 |
|
|---|
| 59 | // Schedule keys
|
|---|
| 60 | std::wstring target_id;
|
|---|
| 61 | boost::posix_time::time_duration duration;
|
|---|
| 62 | std::wstring channel;
|
|---|
| 63 | unsigned int report;
|
|---|
| 64 | std::wstring command;
|
|---|
| 65 | std::list<std::wstring> arguments;
|
|---|
| 66 |
|
|---|
| 67 | // Others keys (Managed by application)
|
|---|
| 68 | int id;
|
|---|
| 69 |
|
|---|
| 70 | void set_report(std::wstring str) {
|
|---|
| 71 | report = nscapi::report::parse(str);
|
|---|
| 72 | }
|
|---|
| 73 | void set_duration(std::wstring str) {
|
|---|
| 74 | duration = boost::posix_time::seconds(strEx::stoui_as_time_sec(str, 1));
|
|---|
| 75 | }
|
|---|
| 76 | void set_command(std::wstring str) {
|
|---|
| [df109f9] | 77 | if (!str.empty()) {
|
|---|
| 78 | strEx::parse_command(str, command, arguments);
|
|---|
| 79 | }
|
|---|
| [76540c3] | 80 | }
|
|---|
| 81 |
|
|---|
| 82 | std::wstring to_wstring() const {
|
|---|
| 83 |
|
|---|
| 84 | std::wstringstream ss;
|
|---|
| 85 | ss << alias << _T("[") << id << _T("] = ")
|
|---|
| 86 | << _T("{alias: ") << alias
|
|---|
| 87 | << _T(", command: ") << command
|
|---|
| 88 | << _T(", channel: ") << channel
|
|---|
| 89 | << _T(", target_id: ") << target_id
|
|---|
| 90 | << _T(", duration: ") << duration.total_seconds()
|
|---|
| 91 | << _T("}");
|
|---|
| 92 | return ss.str();
|
|---|
| 93 | }
|
|---|
| 94 | //std::wstring to_string() {
|
|---|
| 95 |
|
|---|
| 96 | };
|
|---|
| 97 | typedef boost::optional<schedule_object> optional_schedule_object;
|
|---|
| 98 |
|
|---|
| 99 | template<class T>
|
|---|
| 100 | inline void import_string(T &object, T &parent) {
|
|---|
| 101 | if (object.empty() && !parent.empty())
|
|---|
| 102 | object = parent;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 | struct schedule_reader {
|
|---|
| 107 | typedef schedule_object object_type;
|
|---|
| 108 |
|
|---|
| 109 | static void post_process_object(object_type &object) {}
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 | static void read_object(boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object) {
|
|---|
| 113 | object.set_command(object.value);
|
|---|
| [523576e] | 114 | if (object.alias == _T("default")) {
|
|---|
| [76540c3] | 115 | object.set_duration(_T("5m"));
|
|---|
| [523576e] | 116 | object.set_report(_T("all"));
|
|---|
| [52edb15] | 117 | object.channel = _T("NSCA");
|
|---|
| [523576e] | 118 | }
|
|---|
| [76540c3] | 119 |
|
|---|
| 120 | nscapi::settings_helper::settings_registry settings(proxy);
|
|---|
| 121 |
|
|---|
| 122 | settings.path(object.path).add_path()
|
|---|
| 123 | (_T("SCHEDULE DEFENITION"), _T("Schedule definition for: ") + object.alias)
|
|---|
| 124 | ;
|
|---|
| 125 |
|
|---|
| 126 | settings.path(object.path).add_key()
|
|---|
| 127 |
|
|---|
| 128 | (_T("command"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_command, &object, _1)),
|
|---|
| [8d89d7a] | 129 | _T("SCHEDULE COMMAND"), _T("Command to execute"), object.alias == _T("default"))
|
|---|
| [76540c3] | 130 |
|
|---|
| 131 | (_T("alias"), sh::wstring_key(&object.alias),
|
|---|
| [8d89d7a] | 132 | _T("SCHEDULE ALIAS"), _T("The alias (service name) to report to server"), object.alias == _T("default"))
|
|---|
| [76540c3] | 133 |
|
|---|
| 134 | (_T("target"), sh::wstring_key(&object.target_id),
|
|---|
| [8d89d7a] | 135 | _T("TARGET"), _T("The target to send the message to (will be resolved by the consumer)"), true)
|
|---|
| [76540c3] | 136 |
|
|---|
| 137 | (_T("parent"), nscapi::settings_helper::wstring_key(&object.parent, _T("default")),
|
|---|
| [8d89d7a] | 138 | _T("TARGET PARENT"), _T("The parent the target inherits from"), object.alias == _T("default"))
|
|---|
| [76540c3] | 139 |
|
|---|
| 140 | (_T("is template"), nscapi::settings_helper::bool_key(&object.is_template, false),
|
|---|
| [8d89d7a] | 141 | _T("IS TEMPLATE"), _T("Declare this object as a template (this means it will not be avalible as a separate object)"), true)
|
|---|
| [76540c3] | 142 |
|
|---|
| 143 | ;
|
|---|
| [8d89d7a] | 144 | if (object.alias == _T("default")) {
|
|---|
| 145 | settings.path(object.path).add_key()
|
|---|
| 146 |
|
|---|
| 147 | (_T("channel"), sh::wstring_key(&object.channel, _T("NSCA")),
|
|---|
| 148 | _T("SCHEDULE CHANNEL"), _T("Channel to send results on"))
|
|---|
| 149 |
|
|---|
| 150 | (_T("interval"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_duration, &object, _1), _T("5m")),
|
|---|
| 151 | _T("SCHEDULE INTERAVAL"), _T("Time in seconds between each check"))
|
|---|
| 152 |
|
|---|
| 153 | (_T("report"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_report, &object, _1), _T("all")),
|
|---|
| 154 | _T("REPORT MODE"), _T("What to report to the server (any of the following: all, critical, warning, unknown, ok)"))
|
|---|
| 155 |
|
|---|
| 156 | ;
|
|---|
| 157 | } else {
|
|---|
| 158 | settings.path(object.path).add_key()
|
|---|
| 159 | (_T("channel"), sh::wstring_key(&object.channel),
|
|---|
| 160 | _T("SCHEDULE CHANNEL"), _T("Channel to send results on"))
|
|---|
| 161 |
|
|---|
| 162 | (_T("interval"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_duration, &object, _1)),
|
|---|
| 163 | _T("SCHEDULE INTERAVAL"), _T("Time in seconds between each check"), true)
|
|---|
| 164 |
|
|---|
| 165 | (_T("report"), sh::string_fun_key<std::wstring>(boost::bind(&object_type::set_report, &object, _1)),
|
|---|
| 166 | _T("REPORT MODE"), _T("What to report to the server (any of the following: all, critical, warning, unknown, ok)"), true)
|
|---|
| 167 |
|
|---|
| 168 | ;
|
|---|
| 169 |
|
|---|
| 170 | }
|
|---|
| [76540c3] | 171 |
|
|---|
| 172 | settings.register_all();
|
|---|
| 173 | settings.notify();
|
|---|
| 174 |
|
|---|
| 175 | /*
|
|---|
| 176 | BOOST_FOREACH(const object_type::options_type::value_type &kvp, options) {
|
|---|
| 177 | if (!object.has_option(kvp.first))
|
|---|
| 178 | object.options[kvp.first] = kvp.second;
|
|---|
| 179 | }
|
|---|
| 180 | */
|
|---|
| 181 |
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | static void apply_parent(object_type &object, object_type &parent) {
|
|---|
| 185 | import_string(object.target_id, parent.target_id);
|
|---|
| 186 | import_string(object.command, parent.command);
|
|---|
| 187 | //import_string(object.arguments, parent.arguments);
|
|---|
| 188 | if (object.duration.total_seconds() == 0 && parent.duration.total_seconds() != 0)
|
|---|
| 189 | object.duration = parent.duration;
|
|---|
| 190 | import_string(object.channel, parent.channel);
|
|---|
| 191 | if (object.report == 0 && parent.report != 0)
|
|---|
| 192 | object.report = parent.report;
|
|---|
| 193 | /*
|
|---|
| 194 | object.address.import(parent.address);
|
|---|
| 195 | BOOST_FOREACH(object_type::options_type::value_type i, parent.options) {
|
|---|
| 196 | if (object.options.find(i.first) == object.options.end())
|
|---|
| 197 | object.options[i.first] = i.second;
|
|---|
| 198 | }
|
|---|
| 199 | */
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | };
|
|---|
| 203 | typedef nscapi::settings_objects::object_handler<schedule_object, schedule_reader > schedule_handler;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|