Coverage Report

Created: 2025-11-16 06:59

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-25 - 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
  u_int64_t delivery_failures;
49
50
  /* Timestamp of the last dequeue, regardless of queue priority */
51
  time_t last_use;
52
53
  /* Minimum severity for notifications enqueued to this recipient */
54
  AlertLevel minimum_severity;
55
56
  /* Only enable enqueue/dequeue for notifications falling into these categories
57
   */
58
  Bitmap128 enabled_categories; /* MUST be large enough to contain
59
                                   MAX_NUM_SCRIPT_CATEGORIES */
60
61
  /* Only enable enqueue/dequeue for notifications falling into these entities
62
   */
63
  Bitmap128 enabled_entities; /* MUST be large enough to contain
64
                                 ALERT_ENTITY_MAX_NUM_ENTITIES */
65
66
  Bitmap128 enabled_flow_alert_types; /* MUST be large enough to contain
67
                                 FlowAlertTypeEnum */
68
69
  Bitmap128 enabled_host_alert_types; /* MUST be large enough to contain
70
                                 HostAlertID */
71
72
  Bitmap128 enabled_other_alert_types; /* MUST be large enough to contain
73
                                 Other alert IDs defined in other_alert_keys.lua */
74
75
  /* MUST be large enough to contain MAX_NUM_HOST_POOLS */
76
  Bitmap4096 enabled_host_pools;
77
78
  bool doDebug(const AlertFifoItem* const notification);
79
80
 public:
81
  RecipientQueue(u_int16_t recipient_id);
82
  ~RecipientQueue();
83
84
  /**
85
   * @brief Dequeues a notification from a `recipient_id` queue
86
   *
87
   * @return AlertFifoItem on success, NULL if queue is empty
88
   */
89
  AlertFifoItem *dequeue();
90
91
  /**
92
   * @brief Enqueues a notification to a `recipient_id` queue
93
   * @param recipient_id An integer recipient identifier
94
   * @param notification A string containing the notification
95
   *
96
   * @return True if the enqueue succeeded, false otherwise
97
   */
98
  bool enqueue(const AlertFifoItem* const notification);
99
100
  /**
101
   * @brief Sets the minimum severity for notifications to use this recipient
102
   * @param minimum_severity The minimum severity for notifications to use this
103
   * recipient
104
   *
105
   * @return
106
   */
107
0
  inline void setMinimumSeverity(AlertLevel _minimum_severity) {
108
0
    minimum_severity = _minimum_severity;
109
0
  };
110
111
  /**
112
   * @brief Sets enabled notification categories to use this recipient
113
   * @param enabled_categories A bitmap of notification categories to use this
114
   * recipient
115
   *
116
   * @return
117
   */
118
0
  inline void setEnabledCategories(Bitmap128 _enabled_categories) {
119
0
    enabled_categories = _enabled_categories;
120
0
  };
121
122
  /**
123
   * @brief Sets enabled notification entities to use this recipient
124
   * @param enabled_entities A bitmap of notification entities to use this
125
   * recipient
126
   *
127
   * @return
128
   */
129
0
  inline void setEnabledEntities(Bitmap128 _enabled_entities) {
130
0
    enabled_entities = _enabled_entities;
131
0
  };
132
133
  /**
134
   * @brief Sets enabled host pools to use this recipient
135
   * @param enabled_host_pools
136
   *
137
   * @return
138
   */
139
0
  inline void setEnabledHostPools(Bitmap4096 _enabled_pools) {
140
0
    enabled_host_pools = _enabled_pools;
141
0
  };
142
143
  /**
144
   * @brief Sets enabled flow alert types to use this recipient
145
   * @param enabled_flow_alert_types
146
   *
147
   * @return
148
   */
149
0
  inline void setEnabledFlowAlertTypes(Bitmap128 _enabled_flow_alert_types) {
150
0
    enabled_flow_alert_types = _enabled_flow_alert_types;
151
0
  };
152
153
  /**
154
   * @brief Sets enabled host alert types to use this recipient
155
   * @param enabled_host_alert_types
156
   *
157
   * @return
158
   */
159
0
  inline void setEnabledHostAlertTypes(Bitmap128 _enabled_host_alert_types) {
160
0
    enabled_host_alert_types = _enabled_host_alert_types;
161
0
  };
162
163
  /**
164
   * @brief Sets enabled other alert types to use this recipient
165
   * @param enabled_other_alert_types
166
   *
167
   * @return
168
   */
169
0
  inline void setEnabledOtherAlertTypes(Bitmap128 _enabled_other_alert_types) {
170
0
    enabled_other_alert_types = _enabled_other_alert_types;
171
0
  };
172
173
  /**
174
   * @brief Sets alerts to be ignored
175
   * @param skip_alerts
176
   *
177
   * @return
178
   */
179
0
  inline void setSkipAlerts(bool _skip_alerts) {
180
0
    skip_alerts = _skip_alerts;
181
0
  };
182
183
  /**
184
   * @brief Toggle match on alert_id only (ignore severity, category, etc)
185
   * @param match_alert_id
186
   *
187
   * @return
188
   */
189
0
  inline void toggleAlertIDMatch(bool _match_alert_id) {
190
0
    match_alert_id = _match_alert_id;
191
0
  };
192
193
  /**
194
   * @brief Returns queue status (drops and enqueued)
195
   * @param vm A Lua VM instance
196
   *
197
   * @return
198
   */
199
  void lua(lua_State* vm);
200
201
  /**
202
   * @brief Returns true if the recipient has no notifications enqueued
203
   *
204
   * @return A boolean
205
   */
206
  bool empty();
207
208
  /**
209
   * @brief Returns queue last use
210
   *
211
   * @return An epoch with the last use, or 0 if never used.
212
   */
213
0
  inline time_t get_last_use() const { return last_use; };
214
215
  /**
216
   * @brief Inc recipient stats (used by lua recipients)
217
   */
218
0
  inline void incStats(u_int64_t _delivered, u_int64_t _filtered_out, u_int64_t _delivery_failures) {
219
0
    delivered += _delivered;
220
0
    filtered_out += _filtered_out;
221
0
    delivery_failures += _delivery_failures;
222
0
  }
223
};
224
225
#endif /* _RECIPIENT_QUEUES_ */