Line data Source code
1 : #ifndef ERROR_HANDLER_MA_CONDITION_H
2 : #define ERROR_HANDLER_MA_CONDITION_H
3 :
4 : // from novadaq
5 : #include "ErrorHandler/MessageAnalyzer/ma_cond_test_expr.h"
6 : #include "ErrorHandler/MessageAnalyzer/ma_hitmap.h"
7 : #include "ErrorHandler/MessageAnalyzer/ma_timing_event.h"
8 : #include "ErrorHandler/MessageAnalyzer/ma_types.h"
9 : #include "ErrorHandler/MessageAnalyzer/ma_utils.h"
10 :
11 : // from ups
12 : #include <boost/regex.hpp>
13 : #include <boost/shared_ptr.hpp>
14 :
15 : // sys headers
16 : #include <list>
17 : #include <map>
18 : #include <vector>
19 :
20 : namespace novadaq {
21 : namespace errorhandler {
22 :
23 : typedef boost::regex regex_t;
24 : typedef std::vector<boost::regex> vregex_t;
25 :
26 : // MsgAnalyzer Rule
27 : class ma_condition
28 : {
29 : public:
30 : // c'tor
31 : ma_condition(std::string const &desc, std::string const &sev, std::vector<std::string> const &sources, std::vector<std::string> const &categories, std::string const ®ex, std::string const &test, bool persistent_cond, int trigger_count, bool at_least, int timespan, bool per_source, bool per_target, int target_group, ma_timing_events &events);
32 :
33 : // reset the condition to its ground state
34 : void reset();
35 :
36 : // init
37 : void init();
38 :
39 : // public method that gets called when new message comes in
40 : bool match(qt_mf_msg const &msg, conds_t &status, conds_t &source, conds_t &target);
41 :
42 : // scheduled event
43 : bool event(size_t src, size_t tgt, time_t t, conds_t &status);
44 :
45 : // get fields
46 0 : const std::string &description() const { return description_; }
47 0 : const std::string ®ex() const { return regex_str; }
48 0 : const std::string &sources_str() const { return srcs_str; }
49 :
50 : // update fields with lastest match message
51 : void update_fields();
52 :
53 : // get catched message count
54 0 : int get_msg_count() const { return catched_messages; }
55 :
56 : // get fields from last message
57 : sev_code_t get_msg_severity() const { return last_sev_; }
58 : const std::string &get_msg_category() const { return last_cat_; }
59 0 : const std::string &get_msg_source() const { return last_src_; }
60 0 : const std::string &get_qt_mf_msgarget() const { return last_tgt_; }
61 : const std::string &get_msg_body() const { return last_bdy_; }
62 0 : std::string get_msg_group(size_t i) const
63 : {
64 0 : if (i > last_what_.size()) throw std::runtime_error("group does not exist");
65 0 : return std::string(last_what_[i].first, last_what_[i].second);
66 : }
67 :
68 : // return index of the src/tgt string, or -2 if not found
69 0 : int find_source(std::string const &src) { return hitmap.find_source(src); }
70 0 : int find_target(std::string const &tgt) { return hitmap.find_target(tgt); }
71 0 : int find_arg(std::string const &arg, arg_t type)
72 : {
73 0 : return (type == SOURCE) ? hitmap.find_source(arg) : hitmap.find_target(arg);
74 : }
75 :
76 : // get src/tgt list
77 : const idx_t &get_sources() const { return hitmap.get_sources(); }
78 : const idx_t &get_targets() const { return hitmap.get_targets(); }
79 0 : const idx_t &get_args(arg_t type) const
80 : {
81 0 : if (type == SOURCE) return hitmap.get_sources();
82 0 : if (type == TARGET) return hitmap.get_targets();
83 0 : throw std::runtime_error("condition::get_args() unsupported arg type");
84 : }
85 :
86 : // get src/tgt string. precond: size()>0, 0<=idx<size() or idx=ANY
87 : const std::string &get_source(ma_cond_domain v) const
88 : {
89 : return hitmap.get_source(v);
90 : }
91 : const std::string &get_target(ma_cond_domain v) const
92 : {
93 : return hitmap.get_target(v);
94 : }
95 0 : std::string get_arg(ma_cond_domain v, arg_t type) const
96 : {
97 0 : if (type == SOURCE) return hitmap.get_source(v);
98 0 : if (type == TARGET) return hitmap.get_target(v);
99 0 : if (type == MESSAGE) return hitmap.get_message(v);
100 0 : if (type >= GROUP1) return hitmap.get_message_group(v, (size_t)(type - GROUP1 + 1));
101 0 : throw std::runtime_error("condition::get_arg() unknow arg type");
102 : }
103 :
104 : // get a range of src/target
105 : void
106 0 : get_cond_range(ma_cond_domain d, ma_cond_range &src, ma_cond_range &tgt) const
107 : {
108 0 : return hitmap.get_cond_range(d, src, tgt);
109 : }
110 :
111 : // returns if the condition has been triggered at given spot(src, target)
112 : bool
113 0 : get_status(ma_cond_domain v) const
114 : {
115 0 : return hitmap.get_status(v);
116 : }
117 :
118 : int
119 0 : get_alarm_count(ma_cond_domain v, arg_t arg) const
120 : {
121 0 : return hitmap.get_alarm_count(v, arg);
122 : }
123 :
124 : // notification list
125 0 : void push_notify_source(ma_rule *rule) { push_notify(notify_on_source, rule); }
126 0 : void push_notify_target(ma_rule *rule) { push_notify(notify_on_target, rule); }
127 0 : void push_notify_status(ma_rule *rule) { push_notify(notify_on_status, rule); }
128 :
129 : void
130 0 : push_notify(notify_list_t &list, ma_rule *rule)
131 : {
132 0 : if (std::find(list.begin(), list.end(), rule) == list.end())
133 0 : list.push_back(rule);
134 0 : }
135 :
136 : void
137 0 : sort_notify_lists()
138 : {
139 0 : notify_on_source.sort();
140 0 : notify_on_target.sort();
141 0 : notify_on_status.sort();
142 0 : }
143 :
144 : const notify_list_t &
145 0 : get_notify_list(notify_t type)
146 : {
147 0 : if (type == STATUS_NOTIFY) return notify_on_status;
148 0 : if (type == SOURCE_NOTIFY) return notify_on_source;
149 0 : if (type == TARGET_NOTIFY) return notify_on_target;
150 0 : throw std::runtime_error("get_notify_list: unknow type");
151 : }
152 :
153 0 : int trigger_count() const { return tc; }
154 0 : int timespan() const { return ts; }
155 0 : bool at_least() const { return at_least_; }
156 0 : bool at_most() const { return !at_least_; }
157 0 : bool per_source() const { return ps; }
158 0 : bool per_target() const { return pt; }
159 0 : bool persistent() const { return persistent_; }
160 0 : ma_timing_events &timing_events() { return events; }
161 :
162 : // return a view to the hitmap given a ma_cond_domain
163 : const hitmap_view_t
164 : get_domain_view(ma_cond_domain const &domain)
165 : {
166 : return hitmap.get_domain_view(domain);
167 : }
168 :
169 : private:
170 : // extract severity, source, category, and message body
171 : // from message facility message
172 : void extract_fields(qt_mf_msg const &msg);
173 :
174 : bool match_srcs();
175 : bool match_cats();
176 : bool match_body();
177 : bool match_test();
178 :
179 : private:
180 : // condition description
181 : std::string description_;
182 :
183 : // filtering conditions
184 : sev_code_t severity_;
185 :
186 : std::string srcs_str;
187 : vregex_t e_srcs;
188 : bool any_src;
189 :
190 : std::string cats_str;
191 : vregex_t e_cats;
192 : bool any_cat;
193 :
194 : // match condition
195 : match_type_t match_type;
196 : std::string regex_str;
197 : regex_t e;
198 :
199 : // test condition
200 : ma_cond_test_expr test_expr;
201 :
202 : // hitmap and granularity
203 : int tc;
204 : bool at_least_;
205 : int ts;
206 : bool ps; // per_source
207 : bool pt; // per_target
208 : unsigned int t_group; // target_group
209 : bool persistent_; // persistent cond. never turns off
210 : ma_hitmap hitmap;
211 :
212 : // timing events
213 : ma_timing_events &events;
214 :
215 : // temp variables used in matching
216 : sev_code_t sev_;
217 : std::string src_;
218 : std::string tgt_;
219 : std::string cat_;
220 : std::string bdy_;
221 : boost::smatch what_;
222 :
223 : sev_code_t last_sev_;
224 : std::string last_src_;
225 : std::string last_tgt_;
226 : std::string last_cat_;
227 : std::string last_bdy_;
228 : boost::smatch last_what_;
229 :
230 : // notification lists
231 : notify_list_t notify_on_source;
232 : notify_list_t notify_on_target;
233 : notify_list_t notify_on_status;
234 :
235 : // total number of catched messages which has passed
236 : // filtering, match condition, and test condition. Does not
237 : // matter if it passed the frequency test
238 : int catched_messages;
239 : };
240 :
241 : // typedef boost::shared_ptr<ma_condition> cond_sp;
242 : // typedef std::list<cond_sp> conds_t;
243 : // typedef std::vector<cond_sp> cond_vec_t;
244 : // typedef std::map<std::string, cond_sp> cond_map_t;
245 :
246 : typedef std::vector<ma_condition *> cond_vec_t;
247 : typedef std::map<std::string, ma_condition> cond_map_t;
248 :
249 : } // end of namespace errorhandler
250 : } // end of namespace novadaq
251 :
252 : #endif
|