Coverage Report

Created: 2026-05-24 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ntopng/include/RecipientQueue.h
Line
Count
Source
1
/*
2
 *
3
 * (C) 2014-26 - ntop.org
4
 *
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software Foundation,
18
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
 *
20
 */
21
22
#ifndef _RECIPIENT_QUEUES_
23
#define _RECIPIENT_QUEUES_
24
25
#include "ntop_includes.h"
26
27
class RecipientQueue {
28
 private:
29
  u_int16_t recipient_id;
30
  bool skip_alerts;
31
  bool match_alert_id;
32
33
  AlertFifoQueue* queue;
34
35
  /* Counters for the number of enqueues */
36
  u_int64_t enqueued;
37
38
  /* Counters for the number of drops occurred when enqueuing */
39
  u_int64_t drops;
40
41
  /* Counters for the number of alerts filtered out (e.g. silenced) */
42
  u_int64_t filtered_out;
43
44
  /* Counters for the number of alerts delivered */
45
  u_int64_t delivered;
46
47
  /* Counters for the number of alerts not delivered due to an endpoint failure
48
   */
49
  u_int64_t delivery_failures;
50
51
  /* Timestamp of the last dequeue, regardless of queue priority */
52
  time_t last_use;
53
54
  /* Minimum severity for notifications enqueued to this recipient */
55
  AlertLevel minimum_severity;
56
57
  /* Only enable enqueue/dequeue for notifications falling into these categories
58
   */
59
  Bitmap128 enabled_categories; /* MUST be large enough to contain
60
                                   MAX_NUM_SCRIPT_CATEGORIES */
61
62
  /* Only enable enqueue/dequeue for notifications falling into these entities
63
   */
64
  Bitmap128 enabled_entities; /* MUST be large enough to contain
65
                                 ALERT_ENTITY_MAX_NUM_ENTITIES */
66
67
  Bitmap128 enabled_flow_alert_types; /* MUST be large enough to contain
68
                                 FlowAlertTypeEnum */
69
70
  Bitmap128 enabled_host_alert_types; /* MUST be large enough to contain
71
                                 HostAlertID */
72
73
  Bitmap128
74
      enabled_other_alert_types; /* MUST be large enough to contain
75
                           Other alert IDs defined in other_alert_keys.lua */
76
77
  /* MUST be large enough to contain MAX_NUM_HOST_POOLS */
78
  Bitmap4096 enabled_host_pools;
79
80
  /* Tags bitmap (all zero = no filter) */
81
  Bitmap64 enabled_tags;
82
83
  bool doDebug(const AlertFifoItem* const notification);
84
85
 public:
86
  RecipientQueue(u_int16_t recipient_id);
87
  ~RecipientQueue();
88
89
  /**
90
   * @brief Dequeues a notification from a `recipient_id` queue
91
   *
92
   * @return AlertFifoItem on success, NULL if queue is empty
93
   */
94
  AlertFifoItem* dequeue();
95
96
  /**
97
   * @brief Enqueues a notification to a `recipient_id` queue
98
   * @param recipient_id An integer recipient identifier
99
   * @param notification A string containing the notification
100
   *
101
   * @return True if the enqueue succeeded, false otherwise
102
   */
103
  bool enqueue(const AlertFifoItem* const notification);
104
105
  /**
106
   * @brief Sets the minimum severity for notifications to use this recipient
107
   * @param minimum_severity The minimum severity for notifications to use this
108
   * recipient
109
   *
110
   * @return
111
   */
112
0
  inline void setMinimumSeverity(AlertLevel _minimum_severity) {
113
0
    minimum_severity = _minimum_severity;
114
0
  };
115
116
  /**
117
   * @brief Sets enabled notification categories to use this recipient
118
   * @param enabled_categories A bitmap of notification categories to use this
119
   * recipient
120
   *
121
   * @return
122
   */
123
0
  inline void setEnabledCategories(Bitmap128 _enabled_categories) {
124
0
    enabled_categories = _enabled_categories;
125
0
  };
126
127
  /**
128
   * @brief Sets enabled notification entities to use this recipient
129
   * @param enabled_entities A bitmap of notification entities to use this
130
   * recipient
131
   *
132
   * @return
133
   */
134
0
  inline void setEnabledEntities(Bitmap128 _enabled_entities) {
135
0
    enabled_entities = _enabled_entities;
136
0
  };
137
138
  /**
139
   * @brief Sets enabled host pools to use this recipient
140
   * @param enabled_host_pools
141
   *
142
   * @return
143
   */
144
0
  inline void setEnabledHostPools(Bitmap4096 _enabled_pools) {
145
0
    enabled_host_pools = _enabled_pools;
146
0
  };
147
148
  /**
149
   * @brief Sets enabled tag bitmap for this recipient (0 = no filter)
150
   * @param _enabled_tags A tag bitmap
151
   *
152
   * @return
153
   */
154
0
  inline void setEnabledTags(Bitmap64 _enabled_tags) {
155
0
    enabled_tags = _enabled_tags;
156
0
  };
157
158
  /**
159
   * @brief Sets enabled flow alert types to use this recipient
160
   * @param enabled_flow_alert_types
161
   *
162
   * @return
163
   */
164
0
  inline void setEnabledFlowAlertTypes(Bitmap128 _enabled_flow_alert_types) {
165
0
    enabled_flow_alert_types = _enabled_flow_alert_types;
166
0
  };
167
168
  /**
169
   * @brief Sets enabled host alert types to use this recipient
170
   * @param enabled_host_alert_types
171
   *
172
   * @return
173
   */
174
0
  inline void setEnabledHostAlertTypes(Bitmap128 _enabled_host_alert_types) {
175
0
    enabled_host_alert_types = _enabled_host_alert_types;
176
0
  };
177
178
  /**
179
   * @brief Sets enabled other alert types to use this recipient
180
   * @param enabled_other_alert_types
181
   *
182
   * @return
183
   */
184
0
  inline void setEnabledOtherAlertTypes(Bitmap128 _enabled_other_alert_types) {
185
0
    enabled_other_alert_types = _enabled_other_alert_types;
186
0
  };
187
188
  /**
189
   * @brief Sets alerts to be ignored
190
   * @param skip_alerts
191
   *
192
   * @return
193
   */
194
0
  inline void setSkipAlerts(bool _skip_alerts) { skip_alerts = _skip_alerts; };
195
196
  /**
197
   * @brief Toggle match on alert_id only (ignore severity, category, etc)
198
   * @param match_alert_id
199
   *
200
   * @return
201
   */
202
0
  inline void toggleAlertIDMatch(bool _match_alert_id) {
203
0
    match_alert_id = _match_alert_id;
204
0
  };
205
206
  /**
207
   * @brief Returns queue status (drops and enqueued)
208
   * @param vm A Lua VM instance
209
   *
210
   * @return
211
   */
212
  void lua(lua_State* vm);
213
214
  /**
215
   * @brief Returns true if the recipient has no notifications enqueued
216
   *
217
   * @return A boolean
218
   */
219
  bool empty();
220
221
  /**
222
   * @brief Returns queue last use
223
   *
224
   * @return An epoch with the last use, or 0 if never used.
225
   */
226
0
  inline time_t get_last_use() const { return last_use; };
227
228
  /**
229
   * @brief Inc recipient stats (used by lua recipients)
230
   */
231
  inline void incStats(u_int64_t _delivered, u_int64_t _filtered_out,
232
0
                       u_int64_t _delivery_failures) {
233
0
    delivered += _delivered;
234
0
    filtered_out += _filtered_out;
235
0
    delivery_failures += _delivery_failures;
236
0
  }
237
};
238
239
#endif /* _RECIPIENT_QUEUES_ */