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, alarm_fn_t alarm, cond_match_fn_t cond_match)
18 , cond_match_fn(cond_match)
26 void ma_rule_engine::init_engine()
29 EHS = pset.get<
bool>(
"EHS",
false);
31 ParameterSet conds = pset.get<ParameterSet>(
"conditions", fhicl::ParameterSet());
32 ParameterSet rules = pset.get<ParameterSet>(
"rules", fhicl::ParameterSet());
34 cnames = conds.get_pset_names();
35 rnames = rules.get_pset_names();
38 for (
size_t i = 0; i < cnames.size(); ++i)
42 ParameterSet cond = conds.get<ParameterSet>(cnames[i]);
43 ParameterSet rate = cond.get<ParameterSet>(
"rate", nulp);
44 ParameterSet gran = cond.get<ParameterSet>(
"granularity", nulp);
47 int occur{0}, occur_at_most{0}, occur_at_least{0};
48 bool at_most = rate.get_if_present<
int>(
"occur_at_most", occur_at_most);
49 bool at_least = rate.get_if_present<
int>(
"occur_at_least", occur_at_least);
52 if (!at_least) at_least = rate.get_if_present<
int>(
"occurence", occur_at_least);
54 if (at_most && at_least)
56 throw std::runtime_error(
57 "rule_engine::init_engine() cannot have both 'occur_at_least' "
58 "and 'occur_at_most' in the rate entry, in condition " +
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()), cond.get<std::string>(
"severity"), cond.get<std::vector<std::string>>(
"source"),
73 cond.get<std::vector<std::string>>(
"category", std::vector<std::string>(1,
"*")), cond.get<std::string>(
"regex"), cond.get<std::string>(
"test", std::string()),
74 cond.get<
bool>(
"persistent",
true), occur, at_least, rate.get<
int>(
"timespan", 4372596)
76 gran.get<
bool>(
"per_source",
false), gran.get<
bool>(
"per_target",
false), gran.get<
unsigned int>(
"target_group", 1), events);
79 cond_map_t::iterator it = cmap.insert(std::make_pair(cnames[i], c)).first;
86 for (
size_t i = 0; i < rnames.size(); ++i)
90 ParameterSet rule = rules.get<ParameterSet>(rnames[i]);
93 ma_rule r(rnames[i], rule.get<std::string>(
"description", std::string()), rule.get<
bool>(
"repeat_alarm",
false), rule.get<
int>(
"holdoff", 0));
96 rule_map_t::iterator it = rmap.insert(std::make_pair(rnames[i], r)).first;
104 it->second.parse(rule.get<std::string>(
"expression"), rule.get<std::string>(
"message"), rule.get<ParameterSet>(
"action", nulp), &cmap);
108 cond_map_t::iterator it = cmap.begin();
109 for (; it != cmap.end(); ++it) it->second.sort_notify_lists();
112 event_worker_t = boost::thread(&ma_rule_engine::event_worker,
this);
115 void ma_rule_engine::event_worker()
120 time_t now = time(0);
125 boost::mutex::scoped_lock lock(events.lock);
128 event_queue_t& eq = events.event_queue();
130 while (!eq.empty() && eq.top().timestamp() < now)
133 e.condition().event(e.source_idx(), e.target_idx(), e.timestamp(), status);
138 notify_list_t notify_status;
139 merge_notify_list(notify_status, status, STATUS_NOTIFY);
142 evaluate_rules(notify_status);
149 void ma_rule_engine::feed(
qt_mf_msg const& msg)
158 cond_map_t::iterator it = cmap.begin();
159 for (; it != cmap.end(); ++it)
160 if (it->second.match(msg, status, source, target))
161 cond_match_fn(it->first);
167 notify_list_t notify_status;
168 notify_list_t notify_domain;
170 merge_notify_list(notify_status, status, STATUS_NOTIFY);
171 merge_notify_list(notify_domain, source, SOURCE_NOTIFY);
172 merge_notify_list(notify_domain, target, TARGET_NOTIFY);
175 evaluate_rules_domain(notify_domain);
178 evaluate_rules(notify_status);
181 void ma_rule_engine::evaluate_rules_domain(notify_list_t& notify_domain)
183 notify_list_t::iterator it = notify_domain.begin();
184 for (; it != notify_domain.end(); ++it)
185 (*it)->evaluate_domain();
188 void ma_rule_engine::evaluate_rules(notify_list_t& notify_status)
190 notify_list_t::iterator it = notify_status.begin();
191 for (; it != notify_status.end(); ++it)
193 if ((*it)->evaluate())
196 alarm_fn((*it)->name(), (*it)->get_alarm_message());
201 int now_reset_rule = (*it)->act();
202 if (now_reset_rule > 0)
204 this->reset_rule((*it)->name());
205 alarm_fn((*it)->name(),
"reseting this rule!");
212 void ma_rule_engine::merge_notify_list(notify_list_t& n_list, conds_t
const& c_list, notify_t type)
214 conds_t::const_iterator it = c_list.begin();
215 for (; it != c_list.end(); ++it)
217 notify_list_t notify((*it)->get_notify_list(type));
218 n_list.merge(notify);
224 ma_rule_engine::find_cond_by_name(std::string
const& name)
const
226 cond_map_t::const_iterator it = cmap.find(name);
227 if (it == cmap.end())
228 throw std::runtime_error(
"rule_engine::find_cond_by_name() name not found");
233 ma_rule_engine::find_cond_by_name(std::string
const& name)
235 cond_map_t::iterator it = cmap.find(name);
236 if (it == cmap.end())
237 throw std::runtime_error(
"rule_engine::find_cond_by_name() name not found");
242 ma_rule_engine::find_rule_by_name(std::string
const& name)
const
244 rule_map_t::const_iterator it = rmap.find(name);
245 if (it == rmap.end())
246 throw std::runtime_error(
"rule_engine::find_rule_by_name() name not found");
251 ma_rule_engine::find_rule_by_name(std::string
const& name)
253 rule_map_t::iterator it = rmap.find(name);
254 if (it == rmap.end())
255 throw std::runtime_error(
"rule_engine::find_rule_by_name() name not found");
Qt wrapper around MessageFacility message