Skip to content

Commit

Permalink
fraud_detection: Add the possibility to disable stats
Browse files Browse the repository at this point in the history
A (0) value for any of the fraud detection thresholds now means: "ignore
this threshold", instead of: "raise an event on each call for this
threshold?!".  Although (0) values could, in theory, have been useful
for event generation testing purposes before this patch, this is a small
price to pay in order to gain the ability to disable a stat.  Developers
can now add 1 more CPS and still keep testing with a new (1) value :)

Although the DB schema changes slightly, due to the added "DEFAULT 0",
it is fully backwards-compatible with the previous one, except
fraud_detection row INSERTs will now be more quiet, generating no more
warnings from the backend (e.g. "Column does not have a default
value..." -> but still defaults to 0, thanks to non-strict mode, leading
to strange warnings from the module).

Fixes #890

(cherry picked from commit 214f105)
  • Loading branch information
liviuchircu committed Jul 30, 2020
1 parent 6a8b26c commit 1353640
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 44 deletions.
10 changes: 10 additions & 0 deletions db/schema/fraud_detection.xml
Expand Up @@ -75,69 +75,79 @@
<name>cpm_warning</name>
<type>unsigned int</type>
<size>5</size>
<default>0</default>
<description>Warning threshold for calls per minute.</description>
</column>

<column id="cpm_critical">
<name>cpm_critical</name>
<type>unsigned int</type>
<size>5</size>
<default>0</default>
<description>Crtical threshold for calls per minute.</description>
</column>

<column id="call_duration_warning">
<name>call_duration_warning</name>
<type>unsigned int</type>
<size>5</size>
<default>0</default>
<description>Warning threshold for calls per minute.</description>
</column>

<column id="call_duration_critical">
<name>call_duration_critical</name>
<type>unsigned int</type>
<size>5</size>
<default>0</default>
<description>Crtical threshold for call duration.</description>
</column>

<column id="total_calls_warning">
<name>total_calls_warning</name>
<type>unsigned int</type>
<size>5</size>
<default>0</default>
<description>Warning threshold for total calls.</description>
</column>

<column id="total_calls_critical">
<name>total_calls_critical</name>
<type>unsigned int</type>
<size>5</size>
<default>0</default>
<description>Crtical threshold for total calls.</description>
</column>

<column id="concurrent_calls_warning">
<name>concurrent_calls_warning</name>
<type>unsigned int</type>
<size>5</size>
<default>0</default>
<description>Warning threshold for concurrent calls.</description>
</column>

<column id="concurrent_calls_critical">
<name>concurrent_calls_critical</name>
<type>unsigned int</type>
<size>5</size>
<default>0</default>
<description>Crtical threshold for concurrent calls.</description>
</column>

<column id="sequential_calls_warning">
<name>sequential_calls_warning</name>
<type>unsigned int</type>
<size>5</size>
<default>0</default>
<description>Warning threshold for sequential calls.</description>
</column>

<column id="sequential_calls_critical">
<name>sequential_calls_critical</name>
<type>unsigned int</type>
<size>5</size>
<default>0</default>
<description>Crtical threshold for sequential calls.</description>
</column>

Expand Down
2 changes: 1 addition & 1 deletion modules/fraud_detection/fraud_detection.c
Expand Up @@ -386,7 +386,7 @@ static int check_fraud(struct sip_msg *msg, str *user, str *number, int *pid)
frd_thresholds_t *thr = (frd_thresholds_t*)rule->attrs.s;

#define CHECK_AND_RAISE(pname, type) \
(se->stats.pname >= thr->pname ## _thr.type) { \
(thr->pname ## _thr.type && se->stats.pname >= thr->pname ## _thr.type) { \
raise_ ## type ## _event(&pname ## _name, &se->stats.pname,\
&thr->pname ## _thr.type, user, number, &rule->id);\
rc = rc_ ## type ## _thr;\
Expand Down
4 changes: 2 additions & 2 deletions modules/fraud_detection/frd_events.c
Expand Up @@ -153,12 +153,12 @@ void dialog_terminate_CB(struct dlg_cell *dlg, int type,

if (type & (DLGCB_TERMINATED|DLGCB_EXPIRED)) {
unsigned int duration = time(NULL) - dlg->start_ts;
if (duration >= frdparam->calldur_crit)
if (frdparam->calldur_crit && duration >= frdparam->calldur_crit)
raise_critical_event(&call_dur_name, &duration,
&frdparam->calldur_crit,
&frdparam->user, &frdparam->number, &frdparam->ruleid);

else if (duration >= frdparam->calldur_warn)
else if (frdparam->calldur_warn && duration >= frdparam->calldur_warn)
raise_warning_event(&call_dur_name, &duration,
&frdparam->calldur_warn,
&frdparam->user, &frdparam->number, &frdparam->ruleid);
Expand Down
2 changes: 1 addition & 1 deletion scripts/db_berkeley/opensips/fraud_detection
Expand Up @@ -7,4 +7,4 @@ METADATA_READONLY
METADATA_LOGFLAGS
0
METADATA_DEFAULTS
NIL|NIL|NIL|'00:00'|'23:59'|'Mon-Sun'|NIL|NIL|NIL|NIL|NIL|NIL|NIL|NIL|NIL|NIL
NIL|NIL|NIL|'00:00'|'23:59'|'Mon-Sun'|0|0|0|0|0|0|0|0|0|0
20 changes: 10 additions & 10 deletions scripts/mysql/fraud_detection-create.sql
Expand Up @@ -6,15 +6,15 @@ CREATE TABLE fraud_detection (
start_hour CHAR(5) DEFAULT '00:00' NOT NULL,
end_hour CHAR(5) DEFAULT '23:59' NOT NULL,
daysoftheweek CHAR(64) DEFAULT 'Mon-Sun' NOT NULL,
cpm_warning INT(5) UNSIGNED NOT NULL,
cpm_critical INT(5) UNSIGNED NOT NULL,
call_duration_warning INT(5) UNSIGNED NOT NULL,
call_duration_critical INT(5) UNSIGNED NOT NULL,
total_calls_warning INT(5) UNSIGNED NOT NULL,
total_calls_critical INT(5) UNSIGNED NOT NULL,
concurrent_calls_warning INT(5) UNSIGNED NOT NULL,
concurrent_calls_critical INT(5) UNSIGNED NOT NULL,
sequential_calls_warning INT(5) UNSIGNED NOT NULL,
sequential_calls_critical INT(5) UNSIGNED NOT NULL
cpm_warning INT(5) UNSIGNED DEFAULT 0 NOT NULL,
cpm_critical INT(5) UNSIGNED DEFAULT 0 NOT NULL,
call_duration_warning INT(5) UNSIGNED DEFAULT 0 NOT NULL,
call_duration_critical INT(5) UNSIGNED DEFAULT 0 NOT NULL,
total_calls_warning INT(5) UNSIGNED DEFAULT 0 NOT NULL,
total_calls_critical INT(5) UNSIGNED DEFAULT 0 NOT NULL,
concurrent_calls_warning INT(5) UNSIGNED DEFAULT 0 NOT NULL,
concurrent_calls_critical INT(5) UNSIGNED DEFAULT 0 NOT NULL,
sequential_calls_warning INT(5) UNSIGNED DEFAULT 0 NOT NULL,
sequential_calls_critical INT(5) UNSIGNED DEFAULT 0 NOT NULL
) ENGINE=InnoDB;

20 changes: 10 additions & 10 deletions scripts/oracle/fraud_detection-create.sql
Expand Up @@ -6,16 +6,16 @@ CREATE TABLE fraud_detection (
start_hour VARCHAR2(5) DEFAULT '00:00',
end_hour VARCHAR2(5) DEFAULT '23:59',
daysoftheweek VARCHAR2(64) DEFAULT 'Mon-Sun',
cpm_warning NUMBER(10),
cpm_critical NUMBER(10),
call_duration_warning NUMBER(10),
call_duration_critical NUMBER(10),
total_calls_warning NUMBER(10),
total_calls_critical NUMBER(10),
concurrent_calls_warning NUMBER(10),
concurrent_calls_critical NUMBER(10),
sequential_calls_warning NUMBER(10),
sequential_calls_critical NUMBER(10)
cpm_warning NUMBER(10) DEFAULT 0 NOT NULL,
cpm_critical NUMBER(10) DEFAULT 0 NOT NULL,
call_duration_warning NUMBER(10) DEFAULT 0 NOT NULL,
call_duration_critical NUMBER(10) DEFAULT 0 NOT NULL,
total_calls_warning NUMBER(10) DEFAULT 0 NOT NULL,
total_calls_critical NUMBER(10) DEFAULT 0 NOT NULL,
concurrent_calls_warning NUMBER(10) DEFAULT 0 NOT NULL,
concurrent_calls_critical NUMBER(10) DEFAULT 0 NOT NULL,
sequential_calls_warning NUMBER(10) DEFAULT 0 NOT NULL,
sequential_calls_critical NUMBER(10) DEFAULT 0 NOT NULL
);

CREATE OR REPLACE TRIGGER fraud_detection_tr
Expand Down
20 changes: 10 additions & 10 deletions scripts/postgres/fraud_detection-create.sql
Expand Up @@ -6,16 +6,16 @@ CREATE TABLE fraud_detection (
start_hour VARCHAR(5) DEFAULT '00:00' NOT NULL,
end_hour VARCHAR(5) DEFAULT '23:59' NOT NULL,
daysoftheweek VARCHAR(64) DEFAULT 'Mon-Sun' NOT NULL,
cpm_warning INTEGER NOT NULL,
cpm_critical INTEGER NOT NULL,
call_duration_warning INTEGER NOT NULL,
call_duration_critical INTEGER NOT NULL,
total_calls_warning INTEGER NOT NULL,
total_calls_critical INTEGER NOT NULL,
concurrent_calls_warning INTEGER NOT NULL,
concurrent_calls_critical INTEGER NOT NULL,
sequential_calls_warning INTEGER NOT NULL,
sequential_calls_critical INTEGER NOT NULL
cpm_warning INTEGER DEFAULT 0 NOT NULL,
cpm_critical INTEGER DEFAULT 0 NOT NULL,
call_duration_warning INTEGER DEFAULT 0 NOT NULL,
call_duration_critical INTEGER DEFAULT 0 NOT NULL,
total_calls_warning INTEGER DEFAULT 0 NOT NULL,
total_calls_critical INTEGER DEFAULT 0 NOT NULL,
concurrent_calls_warning INTEGER DEFAULT 0 NOT NULL,
concurrent_calls_critical INTEGER DEFAULT 0 NOT NULL,
sequential_calls_warning INTEGER DEFAULT 0 NOT NULL,
sequential_calls_critical INTEGER DEFAULT 0 NOT NULL
);

ALTER SEQUENCE fraud_detection_ruleid_seq MAXVALUE 2147483647 CYCLE;
20 changes: 10 additions & 10 deletions scripts/sqlite/fraud_detection-create.sql
Expand Up @@ -6,15 +6,15 @@ CREATE TABLE fraud_detection (
start_hour CHAR(5) DEFAULT '00:00' NOT NULL,
end_hour CHAR(5) DEFAULT '23:59' NOT NULL,
daysoftheweek CHAR(64) DEFAULT 'Mon-Sun' NOT NULL,
cpm_warning INTEGER NOT NULL,
cpm_critical INTEGER NOT NULL,
call_duration_warning INTEGER NOT NULL,
call_duration_critical INTEGER NOT NULL,
total_calls_warning INTEGER NOT NULL,
total_calls_critical INTEGER NOT NULL,
concurrent_calls_warning INTEGER NOT NULL,
concurrent_calls_critical INTEGER NOT NULL,
sequential_calls_warning INTEGER NOT NULL,
sequential_calls_critical INTEGER NOT NULL
cpm_warning INTEGER DEFAULT 0 NOT NULL,
cpm_critical INTEGER DEFAULT 0 NOT NULL,
call_duration_warning INTEGER DEFAULT 0 NOT NULL,
call_duration_critical INTEGER DEFAULT 0 NOT NULL,
total_calls_warning INTEGER DEFAULT 0 NOT NULL,
total_calls_critical INTEGER DEFAULT 0 NOT NULL,
concurrent_calls_warning INTEGER DEFAULT 0 NOT NULL,
concurrent_calls_critical INTEGER DEFAULT 0 NOT NULL,
sequential_calls_warning INTEGER DEFAULT 0 NOT NULL,
sequential_calls_critical INTEGER DEFAULT 0 NOT NULL
);

0 comments on commit 1353640

Please sign in to comment.