artdaq_mfextensions  v1_05_00
ma_condition.h
1 #ifndef ERROR_HANDLER_MA_CONDITION_H
2 #define ERROR_HANDLER_MA_CONDITION_H
3 
4 // from novadaq
5 #include "ErrorHandler/MessageAnalyzer/ma_types.h"
6 #include "ErrorHandler/MessageAnalyzer/ma_utils.h"
7 #include "ErrorHandler/MessageAnalyzer/ma_hitmap.h"
8 #include "ErrorHandler/MessageAnalyzer/ma_cond_test_expr.h"
9 #include "ErrorHandler/MessageAnalyzer/ma_timing_event.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 <vector>
18 #include <map>
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
28 {
29 public:
30 
31  // c'tor
32  ma_condition( std::string const & desc
33  , std::string const & sev
34  , std::vector<std::string> const & sources
35  , std::vector<std::string> const & categories
36  , std::string const & regex
37  , std::string const & test
38  , bool persistent_cond
39  , int trigger_count
40  , bool at_least
41  , int timespan
42  , bool per_source
43  , bool per_target
44  , int target_group
45  , ma_timing_events & events
46  );
47 
48  // reset the condition to its ground state
49  void reset();
50 
51  // init
52  void init();
53 
54  // public method that gets called when new message comes in
55  bool match( qt_mf_msg const & msg
56  , conds_t & status
57  , conds_t & source
58  , conds_t & target );
59 
60  // scheduled event
61  bool event( size_t src, size_t tgt, time_t t, conds_t & status );
62 
63  // get fields
64  const std::string & description() const { return description_; }
65  const std::string & regex() const { return regex_str; }
66  const std::string & sources_str() const { return srcs_str; }
67 
68  // update fields with lastest match message
69  void update_fields();
70 
71  // get catched message count
72  int get_msg_count ( ) const { return catched_messages; }
73 
74  // get fields from last message
75  sev_code_t get_msg_severity( ) const { return last_sev_; }
76  const std::string & get_msg_category( ) const { return last_cat_; }
77  const std::string & get_msg_source ( ) const { return last_src_; }
78  const std::string & get_qt_mf_msgarget ( ) const { return last_tgt_; }
79  const std::string & get_msg_body ( ) const { return last_bdy_; }
80  std::string get_msg_group ( size_t i ) const
81  { if(i>last_what_.size()) throw std::runtime_error("group does not exist");
82  return std::string(last_what_[i].first, last_what_[i].second); }
83 
84  // return index of the src/tgt string, or -2 if not found
85  int find_source(std::string const & src) { return hitmap.find_source(src); }
86  int find_target(std::string const & tgt) { return hitmap.find_target(tgt); }
87  int find_arg(std::string const & arg, arg_t type)
88  { return (type==SOURCE) ? hitmap.find_source(arg) : hitmap.find_target(arg); }
89 
90  // get src/tgt list
91  const idx_t & get_sources() const { return hitmap.get_sources(); }
92  const idx_t & get_targets() const { return hitmap.get_targets(); }
93  const idx_t & get_args(arg_t type) const
94  { if( type==SOURCE ) return hitmap.get_sources();
95  if( type==TARGET ) return hitmap.get_targets();
96  throw std::runtime_error("condition::get_args() unsupported arg type"); }
97 
98  // get src/tgt string. precond: size()>0, 0<=idx<size() or idx=ANY
99  const std::string & get_source( ma_cond_domain v ) const
100  { return hitmap.get_source(v); }
101  const std::string & get_target( ma_cond_domain v ) const
102  { return hitmap.get_target(v); }
103  std::string get_arg( ma_cond_domain v, arg_t type ) const
104  { if( type==SOURCE ) return hitmap.get_source(v);
105  if( type==TARGET ) return hitmap.get_target(v);
106  if( type==MESSAGE) return hitmap.get_message(v);
107  if( type>=GROUP1 ) return hitmap.get_message_group(v, (size_t)(type-GROUP1+1));
108  throw std::runtime_error("condition::get_arg() unknow arg type");
109  }
110 
111  // get a range of src/target
112  void
113  get_cond_range( ma_cond_domain d
114  , ma_cond_range & src
115  , ma_cond_range & tgt ) const
116  { return hitmap.get_cond_range(d, src, tgt); }
117 
118  // returns if the condition has been triggered at given spot(src, target)
119  bool
120  get_status( ma_cond_domain v ) const
121  { return hitmap.get_status(v); }
122 
123  int
124  get_alarm_count( ma_cond_domain v, arg_t arg ) const
125  { return hitmap.get_alarm_count(v, arg); }
126 
127 
128  // notification list
129  void push_notify_source( ma_rule * rule ) { push_notify(notify_on_source, rule); }
130  void push_notify_target( ma_rule * rule ) { push_notify(notify_on_target, rule); }
131  void push_notify_status( ma_rule * rule ) { push_notify(notify_on_status, rule); }
132 
133  void
134  push_notify( notify_list_t & list, ma_rule * rule )
135  {
136  if( std::find(list.begin(), list.end(), rule) == list.end() )
137  list.push_back(rule);
138  }
139 
140  void
141  sort_notify_lists( )
142  { notify_on_source.sort(); notify_on_target.sort(); notify_on_status.sort(); }
143 
144  const notify_list_t &
145  get_notify_list( notify_t type )
146  {
147  if( type == STATUS_NOTIFY ) return notify_on_status;
148  if( type == SOURCE_NOTIFY ) return notify_on_source;
149  if( type == TARGET_NOTIFY ) return notify_on_target;
150  throw std::runtime_error("get_notify_list: unknow type");
151  }
152 
153  int trigger_count() const { return tc; }
154  int timespan() const { return ts; }
155  bool at_least() const { return at_least_; }
156  bool at_most() const { return !at_least_; }
157  bool per_source() const { return ps; }
158  bool per_target() const { return pt; }
159  bool persistent() const { return persistent_; }
160  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  { return hitmap.get_domain_view(domain); }
166 
167 private:
168 
169  // extract severity, source, category, and message body
170  // from message facility message
171  void extract_fields (qt_mf_msg const & msg);
172 
173  bool match_srcs ( );
174  bool match_cats ( );
175  bool match_body ( );
176  bool match_test ( );
177 
178 private:
179 
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 
242 //typedef boost::shared_ptr<ma_condition> cond_sp;
243 //typedef std::list<cond_sp> conds_t;
244 //typedef std::vector<cond_sp> cond_vec_t;
245 //typedef std::map<std::string, cond_sp> cond_map_t;
246 
247 typedef std::vector<ma_condition *> cond_vec_t;
248 typedef std::map<std::string, ma_condition> cond_map_t;
249 
250 } // end of namespace errorhandler
251 } // end of namespace novadaq
252 
253 #endif
Qt wrapper around MessageFacility message
Definition: qt_mf_msg.hh:37