@@ -47,25 +47,92 @@ namespace parsers {
4747 typedef typename runtime_data::transient_data_type transient_data_type;
4848 typedef boost::optional<boost::posix_time::time_duration> op_duration;
4949 struct container {
50+ private:
5051 std::string alias;
52+ std::string event_name;
5153 std::string target;
5254 std::string target_id;
5355 std::string source_id;
56+ public:
5457 std::string command;
5558 std::string timeout_msg;
5659 NSCAPI::nagiosReturn severity;
57- runtime_data data;
58- filter_type filter;
5960 boost::optional<boost::posix_time::time_duration> max_age;
61+ boost::optional<boost::posix_time::time_duration> silent_period;
62+
63+ // should be private...
64+ filter_type filter;
65+
66+ private:
6067 boost::posix_time::ptime next_ok_;
68+ boost::posix_time::ptime next_alert_;
69+ public:
6170 bool debug;
6271 bool escape_html;
63- std::string event_name;
64- container () : debug(false ), escape_html(false ) {}
72+ runtime_data data;
73+
74+ container (std::string alias, std::string event_name, runtime_data data) : alias(alias), event_name(event_name), command(alias), debug(false ), escape_html(false ), data(data) {}
75+
76+ void set_target (std::string new_target, std::string new_target_id, std::string new_source_id) {
77+ target = new_target;
78+ target_id = new_target_id;
79+ source_id = new_source_id;
80+ }
81+ bool is_event () const {
82+ return target == " event" ;
83+ }
84+ bool is_events () const {
85+ return target == " events" ;
86+ }
87+
88+ std::string get_event_name () const {
89+ return event_name;
90+ }
91+ std::string get_alias () const {
92+ return alias;
93+ }
94+ std::string get_target () const {
95+ return target;
96+ }
97+ std::string get_target_id () const {
98+ return target_id;
99+ }
100+ std::string get_source_id () const {
101+ return source_id;
102+ }
103+
104+ bool build_filters (nscapi::settings_filters::filter_object config, std::string &error) {
105+ std::string message;
106+ if (!filter.build_syntax (config.debug , config.syntax_top , config.syntax_detail , config.perf_data , config.perf_config , config.syntax_ok , config.syntax_empty , message)) {
107+ error = " Failed to build strings " + alias + " : " + message;
108+ return false ;
109+ }
110+ if (!filter.build_engines (config.debug , config.filter_string , config.filter_ok , config.filter_warn , config.filter_crit )) {
111+ error = " Failed to build filters: " + alias;
112+ return false ;
113+ }
114+
115+ if (!filter.validate (message)) {
116+ error = " Failed to validate filter for " + alias + " : " + message;
117+ return false ;
118+ }
119+ return true ;
120+ }
121+
122+ bool is_silent (const boost::posix_time::ptime &now) {
123+ if (!silent_period) {
124+ return false ;
125+ }
126+ return now < next_alert_;
127+ }
65128
66- void touch (const boost::posix_time::ptime &now) {
129+ void touch (const boost::posix_time::ptime &now, bool alert ) {
67130 if (max_age)
68131 next_ok_ = now + (*max_age);
132+ if (alert && silent_period)
133+ next_alert_ = now + (*silent_period);
134+ else if (silent_period)
135+ next_alert_ = now;
69136 data.touch (now);
70137 }
71138
@@ -90,37 +157,23 @@ namespace parsers {
90157 std::list<container_type> items;
91158
92159 bool add_item (const boost::shared_ptr<config_object> object, const runtime_data &source_data, const std::string event_name) {
93- container_type item (new container);
94- item->event_name = event_name;
95- item->alias = object->get_alias ();
96- item->data = source_data;
97- item->target = object->filter .target ;
98- item->target_id = object->filter .target_id ;
99- item->source_id = object->filter .source_id ;
100- item->command = item->alias ;
160+ container_type item (new container (object->get_alias (), event_name, source_data));
161+ item->set_target (object->filter .target , object->filter .target_id , object->filter .source_id );
101162 item->timeout_msg = object->filter .timeout_msg ;
102163 item->severity = object->filter .severity ;
103164 item->max_age = object->filter .max_age ;
165+ item->silent_period = object->filter .silent_period ;
104166 item->debug = object->filter .debug ;
105167 item->escape_html = object->filter .escape_html ;
106168 if (!object->filter .command .empty ())
107169 item->command = object->filter .command ;
108170 std::string message;
109171
110- if (!item->filter .build_syntax (object->filter .debug , object->filter .syntax_top , object->filter .syntax_detail , object->filter .perf_data , object->filter .perf_config , object->filter .syntax_ok , object->filter .syntax_empty , message)) {
111- NSC_LOG_ERROR (" Failed to build strings " + object->get_alias () + ": " + message);
112- return false ;
113- }
114- if (!item->filter.build_engines(object->filter.debug, object->filter.filter_string, object->filter.filter_ok, object->filter.filter_warn, object->filter.filter_crit)) {
115- NSC_LOG_ERROR (" Failed to build filters: " + object->get_alias ());
172+ if (!item->build_filters (object->filter , message)) {
173+ NSC_LOG_ERROR (message);
116174 return false ;
117175 }
118176
119- std::string error;
120- if (!item->filter.validate(error)) {
121- NSC_LOG_ERROR (" Failed to validate filter for " + object->get_alias () + ": " + error);
122- return false ;
123- }
124177 item->data .boot ();
125178 items.push_back (item);
126179 return true ;
@@ -129,14 +182,14 @@ namespace parsers {
129182 void process_timeout (const container_type item) {
130183 std::string response;
131184 nscapi::core_helper ch (core, plugin_id);
132- if (!ch.submit_simple_message (item->target , item->source_id , item->target_id , item->command , NSCAPI::query_return_codes::returnOK, item->timeout_msg , " " , response)) {
185+ if (!ch.submit_simple_message (item->get_target () , item->get_source_id () , item->get_target_id () , item->command , NSCAPI::query_return_codes::returnOK, item->timeout_msg , " " , response)) {
133186 NSC_LOG_ERROR (" Failed to submit result: " + response);
134187 }
135188 }
136189
137- bool process_item (container_type item, transient_data_type data) {
190+ bool process_item (container_type item, transient_data_type data, bool is_silent ) {
138191 std::string response;
139- if (item->target == " events " || item->target == " event " ) {
192+ if (item->is_events () || item->is_event () ) {
140193 item->filter .fetch_hash (true );
141194 }
142195 item->filter .start_match ();
@@ -148,22 +201,27 @@ namespace parsers {
148201 return false ;
149202 }
150203
204+ if (is_silent) {
205+ NSC_TRACE_MSG (" Eventlog filter is silenced " + item->get_alias ());
206+ return true ;
207+ }
208+
151209 nscapi::core_helper ch (core, plugin_id);
152- if (item->target == " event " ) {
210+ if (item->is_event () ) {
153211 typedef std::list<std::map<std::string, std::string> > list_type;
154212 typedef std::map<std::string, std::string> hash_type;
155213
156214 list_type keys = item->filter .records_ ;
157215 BOOST_FOREACH (hash_type &bundle, keys) {
158- if (!ch.emit_event (item->event_name , item->alias , bundle, response)) {
216+ if (!ch.emit_event (item->get_event_name () , item->get_alias () , bundle, response)) {
159217 NSC_LOG_ERROR (" Failed to submit '" + response);
160218 }
161219 }
162220 return true ;
163221 }
164- if (item->target == " events " ) {
222+ if (item->is_events () ) {
165223 std::list<std::map<std::string, std::string> > keys = item->filter .records_ ;
166- if (!ch.emit_event (item->event_name , item->alias , keys, response)) {
224+ if (!ch.emit_event (item->get_event_name () , item->get_alias () , keys, response)) {
167225 NSC_LOG_ERROR (" Failed to submit '" + response);
168226 }
169227 return true ;
@@ -172,7 +230,7 @@ namespace parsers {
172230 std::string message = item->filter.get_message();
173231 if (message.empty ())
174232 message = " Nothing matched" ;
175- if (!ch.submit_simple_message (item->target , item->source_id , item->target_id , item->command , item->filter .summary .returnCode , message, " " , response)) {
233+ if (!ch.submit_simple_message (item->get_target () , item->get_source_id () , item->get_target_id () , item->command , item->filter .summary .returnCode , message, " " , response)) {
176234 NSC_LOG_ERROR (" Failed to submit '" + message);
177235 }
178236 return true ;
@@ -181,7 +239,7 @@ namespace parsers {
181239 void touch_all () {
182240 boost::posix_time::ptime current_time = boost::posix_time::second_clock::local_time ();
183241 BOOST_FOREACH (container_type item, items) {
184- item->touch (current_time);
242+ item->touch (current_time, false );
185243 }
186244 }
187245
@@ -197,9 +255,9 @@ namespace parsers {
197255 BOOST_FOREACH (container_type item, items) {
198256 if (item->data .has_changed (data)) {
199257 has_changed = true ;
200- if (process_item (item, data)) {
258+ if (process_item (item, data, item-> is_silent (current_time) )) {
201259 has_matched = true ;
202- item->touch (current_time);
260+ item->touch (current_time, true );
203261 }
204262 }
205263 }
@@ -224,7 +282,7 @@ namespace parsers {
224282 BOOST_FOREACH (container_type item, items) {
225283 if (item->has_timedout (current_time)) {
226284 process_timeout (item);
227- item->touch (current_time);
285+ item->touch (current_time, false );
228286 }
229287 }
230288 } catch (...) {
0 commit comments