2 #include "ErrorHandler/MessageAnalyzer/ma_rule_engine.h"
4 #include <cetlib/filepath_maker.h>
5 #include <fhiclcpp/make_ParameterSet.h>
7 using fhicl::ParameterSet;
9 using namespace novadaq::errorhandler;
11 ma_rule_engine::ma_rule_engine( fhicl::ParameterSet
const & pset
13 , cond_match_fn_t cond_match )
20 , cond_match_fn ( cond_match )
28 void ma_rule_engine::init_engine( )
31 EHS = pset.get<
bool>(
"EHS",
false);
33 ParameterSet conds = pset.get<ParameterSet>(
"conditions", fhicl::ParameterSet());
34 ParameterSet rules = pset.get<ParameterSet>(
"rules", fhicl::ParameterSet());
36 cnames = conds.get_pset_names();
37 rnames = rules.get_pset_names();
40 for(
size_t i=0; i<cnames.size(); ++i )
44 ParameterSet cond = conds.get<ParameterSet>(cnames[i]);
45 ParameterSet rate = cond .get<ParameterSet>(
"rate", nulp);
46 ParameterSet gran = cond .get<ParameterSet>(
"granularity", nulp);
49 int occur, occur_at_most, occur_at_least;
50 bool at_most = rate.get_if_present<
int>(
"occur_at_most", occur_at_most);
51 bool at_least = rate.get_if_present<
int>(
"occur_at_least", occur_at_least);
54 if (!at_least) at_least = rate.get_if_present<
int>(
"occurence", occur_at_least);
56 if (at_most && at_least)
58 throw std::runtime_error(
"rule_engine::init_engine() cannot have both 'occur_at_least' "
59 "and 'occur_at_most' in the rate entry, in condition " + cnames[i]);
61 else if (!at_most && !at_least)
68 occur = at_least ? occur_at_least : occur_at_most;
72 ma_condition c( cond.get<std::string>(
"description", std::string())
73 , cond.get<std::string>(
"severity")
74 , cond.get<std::vector<std::string>>(
"source")
75 , cond.get<std::vector<std::string>>(
"category", std::vector<std::string>(1,
"*"))
76 , cond.get<std::string>(
"regex")
77 , cond.get<std::string>(
"test", std::string())
78 , cond.get<
bool>(
"persistent",
true)
81 , rate.get<
int>(
"timespan", 4372596)
82 , gran.get<
bool>(
"per_source",
false)
83 , gran.get<
bool>(
"per_target",
false)
84 , gran.get<
unsigned int>(
"target_group", 1)
89 cond_map_t::iterator it = cmap.insert(std::make_pair(cnames[i], c)).first;
96 for(
size_t i=0; i<rnames.size(); ++i )
100 ParameterSet rule = rules.get<ParameterSet>(rnames[i]);
104 , rule.get<std::string>(
"description", std::string())
105 , rule.get<
bool>(
"repeat_alarm",
false)
106 , rule.get<
int>(
"holdoff", 0)
110 rule_map_t::iterator it = rmap.insert(std::make_pair(rnames[i], r)).first;
118 it->second.parse( rule.get<std::string>(
"expression")
119 , rule.get<std::string>(
"message")
120 , rule.get<ParameterSet>(
"action", nulp)
125 cond_map_t::iterator it = cmap.begin();
126 for( ; it!=cmap.end(); ++it ) it->second.sort_notify_lists();
129 event_worker_t = boost::thread(&ma_rule_engine::event_worker,
this);
132 void ma_rule_engine::event_worker()
137 time_t now = time(0);
143 boost::mutex::scoped_lock lock(events.lock);
146 event_queue_t & eq = events.event_queue();
148 while (!eq.empty() && eq.top().timestamp() < now)
151 e.condition().event(e.source_idx(), e.target_idx(), e.timestamp(), status);
156 notify_list_t notify_status;
157 merge_notify_list( notify_status, status, STATUS_NOTIFY );
160 evaluate_rules( notify_status );
167 void ma_rule_engine::feed(
qt_mf_msg const & msg )
176 cond_map_t::iterator it = cmap.begin();
177 for( ; it!=cmap.end(); ++it )
178 if( it->second.match( msg, status, source, target ) )
179 cond_match_fn( it->first );
185 notify_list_t notify_status;
186 notify_list_t notify_domain;
188 merge_notify_list( notify_status, status, STATUS_NOTIFY );
189 merge_notify_list( notify_domain, source, SOURCE_NOTIFY );
190 merge_notify_list( notify_domain, target, TARGET_NOTIFY );
193 evaluate_rules_domain( notify_domain );
196 evaluate_rules( notify_status );
199 void ma_rule_engine::evaluate_rules_domain( notify_list_t & notify_domain )
201 notify_list_t::iterator it = notify_domain.begin();
202 for( ; it!=notify_domain.end(); ++it )
203 (*it)->evaluate_domain();
206 void ma_rule_engine::evaluate_rules( notify_list_t & notify_status )
208 notify_list_t::iterator it = notify_status.begin();
209 for( ; it!=notify_status.end(); ++it )
211 if( (*it)->evaluate() )
214 alarm_fn((*it)->name(), (*it)->get_alarm_message());
219 int now_reset_rule = (*it)->act();
220 if( now_reset_rule > 0 )
222 this->reset_rule((*it)->name());
223 alarm_fn((*it)->name(),
"reseting this rule!");
231 void ma_rule_engine::merge_notify_list( notify_list_t & n_list
232 , conds_t
const & c_list
235 conds_t::const_iterator it = c_list.begin();
236 for( ; it!=c_list.end(); ++it )
238 notify_list_t notify((*it)->get_notify_list(type));
239 n_list.merge(notify);
245 ma_rule_engine::find_cond_by_name( std::string
const & name )
const
247 cond_map_t::const_iterator it = cmap.find(name);
248 if( it == cmap.end() )
249 throw std::runtime_error(
"rule_engine::find_cond_by_name() name not found");
254 ma_rule_engine::find_cond_by_name( std::string
const & name )
256 cond_map_t::iterator it = cmap.find(name);
257 if( it == cmap.end() )
258 throw std::runtime_error(
"rule_engine::find_cond_by_name() name not found");
263 ma_rule_engine::find_rule_by_name( std::string
const & name )
const
265 rule_map_t::const_iterator it = rmap.find(name);
266 if( it == rmap.end() )
267 throw std::runtime_error(
"rule_engine::find_rule_by_name() name not found");
272 ma_rule_engine::find_rule_by_name( std::string
const & name )
274 rule_map_t::iterator it = rmap.find(name);
275 if( it == rmap.end() )
276 throw std::runtime_error(
"rule_engine::find_rule_by_name() name not found");
Qt wrapper around MessageFacility message