Discussion:
[SR-Users] Keep Number of Messages per Source
AliReza Khoshgoftar Monfared
2014-08-12 19:56:35 UTC
Permalink
Hi List,

I am trying to keep a counter for number of messages received per source ip
in my kamailio script.

The basic challenge is to keep this data in a neat structure.
Ideally, I want some array with keys named after source IP addresses,

sth like number_of_messages["10.10.10.10"] = 23

Of course, this data structure shall be script-persistent (as opposed to
transaction persistent)

Is there any suggestion to do this? (keep a record of # of messages
received from each source IP address and log them)?
Alex Balashov
2014-08-12 19:58:25 UTC
Permalink
Post by AliReza Khoshgoftar Monfared
Hi List,
I am trying to keep a counter for number of messages received per source
ip in my kamailio script.
The basic challenge is to keep this data in a neat structure.
Ideally, I want some array with keys named after source IP addresses,
sth like number_of_messages["10.10.10.10"] = 23
Of course, this data structure shall be script-persistent (as opposed to
transaction persistent)
Is there any suggestion to do this? (keep a record of # of messages
received from each source IP address and log them)?
It sounds to me like you're looking for:

http://kamailio.org/docs/modules/4.1.x/modules/htable.html

The hash tables reside in shared memory, so the data is of global scope
and script-persistent.
--
Alex Balashov - Principal
Evariste Systems LLC
Tel: +1-678-954-0670
Web: http://www.evaristesys.com/, http://www.alexbalashov.com/

Please be kind to the English language:

http://www.entrepreneur.com/article/232906
AliReza Khoshgoftar Monfared
2014-08-13 03:26:13 UTC
Permalink
Thanks Alex,

This is exactly the module I needed.
Now just another small usage question.
How can I reset the value of all entries in my hash table to 0 every, say
"10" seconds?

---------------------

Here is what I tried, but the values seem to persist:

Option 1) in the parameter definition set "initval" to 0 (to have all items
0 initially), "autoexpire" to "10" seconds and "updateexpire" to 0 (to
force expiration upon the end of 10 seconds)


Option 2) Create a fake route block executed every 10 seconds (using
"rtimer" module), and call "sht_rm_value_re("ha=>.*")" inside it.

None of these seem to be resetting "all" of my entries in the hash table to
0, in fact, I can see that the values persist and continue increment (as I
increment them upon receipt of messages). Am I missing sth here?

Thanks
Alireza
Post by Alex Balashov
Post by AliReza Khoshgoftar Monfared
Hi List,
I am trying to keep a counter for number of messages received per source
ip in my kamailio script.
The basic challenge is to keep this data in a neat structure.
Ideally, I want some array with keys named after source IP addresses,
sth like number_of_messages["10.10.10.10"] = 23
Of course, this data structure shall be script-persistent (as opposed to
transaction persistent)
Is there any suggestion to do this? (keep a record of # of messages
received from each source IP address and log them)?
http://kamailio.org/docs/modules/4.1.x/modules/htable.html
The hash tables reside in shared memory, so the data is of global scope
and script-persistent.
--
Alex Balashov - Principal
Evariste Systems LLC
Tel: +1-678-954-0670
Web: http://www.evaristesys.com/, http://www.alexbalashov.com/
http://www.entrepreneur.com/article/232906
_______________________________________________
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list
http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users
Daniel-Constantin Mierla
2014-08-13 08:07:29 UTC
Permalink
Look at the definition of htable parameter, there you have various ways
to set expires for each item in hash table.

Cheers,
Daniel
Post by AliReza Khoshgoftar Monfared
Thanks Alex,
This is exactly the module I needed.
Now just another small usage question.
How can I reset the value of all entries in my hash table to 0 every,
say "10" seconds?
---------------------
Option 1) in the parameter definition set "initval" to 0 (to have all
items 0 initially), "autoexpire" to "10" seconds and "updateexpire" to
0 (to force expiration upon the end of 10 seconds)
Option 2) Create a fake route block executed every 10 seconds (using
"rtimer" module), and call "sht_rm_value_re("ha=>.*")" inside it.
None of these seem to be resetting "all" of my entries in the hash
table to 0, in fact, I can see that the values persist and continue
increment (as I increment them upon receipt of messages). Am I missing
sth here?
Thanks
Alireza
On Tue, Aug 12, 2014 at 3:58 PM, Alex Balashov
Hi List,
I am trying to keep a counter for number of messages received
per source
ip in my kamailio script.
The basic challenge is to keep this data in a neat structure.
Ideally, I want some array with keys named after source IP addresses,
sth like number_of_messages["10.10.10.10"] = 23
Of course, this data structure shall be script-persistent (as opposed to
transaction persistent)
Is there any suggestion to do this? (keep a record of # of messages
received from each source IP address and log them)?
http://kamailio.org/docs/modules/4.1.x/modules/htable.html
The hash tables reside in shared memory, so the data is of global
scope and script-persistent.
--
Alex Balashov - Principal
Evariste Systems LLC
Tel: +1-678-954-0670 <tel:%2B1-678-954-0670>
Web: http://www.evaristesys.com/, http://www.alexbalashov.com/
http://www.entrepreneur.com/article/232906
_______________________________________________
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing
list
http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users
_______________________________________________
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list
http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users
--
Daniel-Constantin Mierla
http://twitter.com/#!/miconda - http://www.linkedin.com/in/miconda
Next Kamailio Advanced Trainings 2014 - http://www.asipto.com
Sep 22-25, Berlin, Germany ::: Oct 15-17, San Francisco, USA
AliReza Khoshgoftar Monfared
2014-08-13 14:21:44 UTC
Permalink
Hi Daniel,

I am setting the htable as follows:

modparam("htable", "htable",
"message_counters=>size=10;autoexpire=10;initval=0;updateexpire=0;")
* autoexpire=10 to have items expire after 10 seconds (in this example)
* updateexpire=0 to force the expiration no matter the items were updated
or not

Yet, I can see the values persist.


Another concern is that, if I specify a number of child processes in my
config (e.g. children=4), then will it be safe to increment the hash table
entries in the route block upon receipt of messages? i.e will the table
entries that reside in the shared memory be precise? or do I need to
lock/unlock them while updating?


Thanks
Alireza
Look at the definition of htable parameter, there you have various ways
to set expires for each item in hash table.
Cheers,
Daniel
Thanks Alex,
This is exactly the module I needed.
Now just another small usage question.
How can I reset the value of all entries in my hash table to 0 every, say
"10" seconds?
---------------------
Option 1) in the parameter definition set "initval" to 0 (to have all
items 0 initially), "autoexpire" to "10" seconds and "updateexpire" to 0
(to force expiration upon the end of 10 seconds)
Option 2) Create a fake route block executed every 10 seconds (using
"rtimer" module), and call "sht_rm_value_re("ha=>.*")" inside it.
None of these seem to be resetting "all" of my entries in the hash table
to 0, in fact, I can see that the values persist and continue increment (as
I increment them upon receipt of messages). Am I missing sth here?
Thanks
Alireza
Post by Alex Balashov
Post by AliReza Khoshgoftar Monfared
Hi List,
I am trying to keep a counter for number of messages received per source
ip in my kamailio script.
The basic challenge is to keep this data in a neat structure.
Ideally, I want some array with keys named after source IP addresses,
sth like number_of_messages["10.10.10.10"] = 23
Of course, this data structure shall be script-persistent (as opposed to
transaction persistent)
Is there any suggestion to do this? (keep a record of # of messages
received from each source IP address and log them)?
http://kamailio.org/docs/modules/4.1.x/modules/htable.html
The hash tables reside in shared memory, so the data is of global scope
and script-persistent.
--
Alex Balashov - Principal
Evariste Systems LLC
Tel: +1-678-954-0670
Web: http://www.evaristesys.com/, http://www.alexbalashov.com/
http://www.entrepreneur.com/article/232906
_______________________________________________
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list
http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users
_______________________________________________
--
Daniel-Constantin Mierlahttp://twitter.com/#!/miconda - http://www.linkedin.com/in/miconda
Next Kamailio Advanced Trainings 2014 - http://www.asipto.com
Sep 22-25, Berlin, Germany ::: Oct 15-17, San Francisco, USA
_______________________________________________
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list
http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users
Alex Balashov
2014-08-13 14:24:34 UTC
Permalink
Post by AliReza Khoshgoftar Monfared
Another concern is that, if I specify a number of child processes in my
config (e.g. children=4), then will it be safe to increment the hash
table entries in the route block upon receipt of messages? i.e will the
table entries that reside in the shared memory be precise? or do I need
to lock/unlock them while updating?
htable operations are thread-safe, as implemented under the hood. You do
not explicitly need to lock anything.
--
Alex Balashov - Principal
Evariste Systems LLC
Tel: +1-678-954-0670
Web: http://www.evaristesys.com/, http://www.alexbalashov.com/

Please be kind to the English language:

http://www.entrepreneur.com/article/232906
Daniel-Constantin Mierla
2014-08-13 14:58:19 UTC
Permalink
Post by Alex Balashov
Post by AliReza Khoshgoftar Monfared
Another concern is that, if I specify a number of child processes in my
config (e.g. children=4), then will it be safe to increment the hash
table entries in the route block upon receipt of messages? i.e will the
table entries that reside in the shared memory be precise? or do I need
to lock/unlock them while updating?
htable operations are thread-safe, as implemented under the hood. You
do not explicitly need to lock anything.
While that is true for htable (direct) operations, one should be careful
when using hash table items in expressions.

So:

- next is an atomic increment of $sht(a=>x)

$var(x) = $shtinc(a=>x);

- but next might rise races:

$sht(a=>x) = $sht(a=>x) + 1;

because first the value is read, an add expression is evaluated and then
the item in hash table is set to new value. During the evaluation of the
expression, the current process can lose CPU, and another process can
update the item. So you can make it safer with:

sht_lock("a=>x");
$sht(a=>x) = $sht(a=>x) + 1;
sht_unlock("a=>x");

Cheers,
Daniel
--
Daniel-Constantin Mierla
http://twitter.com/#!/miconda - http://www.linkedin.com/in/miconda
Next Kamailio Advanced Trainings 2014 - http://www.asipto.com
Sep 22-25, Berlin, Germany ::: Oct 15-17, San Francisco, USA
AliReza Khoshgoftar Monfared
2014-08-13 15:27:26 UTC
Permalink
Thanks very much Daniel and Alex,

I switched to the atomic increase ("$var(x) = $shtinc(a=>x);"),

My attempt to get automatic expiration and resetting of values to 0 by
doing "modparam("htable", "htable", "message_counters=>size=10;
autoexpire=10;initval=0;updateexpire=0;")" was not successful (values
persisted), but I could achieve it by having a timer route block (using
"rtimer") that expires every 10 second and calling
"sht_rm_name_re("message_counters=>.*");" inside it (in case that it may
help people who will get into the same issue later)


All the best,
Alireza


On Wed, Aug 13, 2014 at 10:58 AM, Daniel-Constantin Mierla <
Post by Daniel-Constantin Mierla
Post by AliReza Khoshgoftar Monfared
Another concern is that, if I specify a number of child processes in my
Post by AliReza Khoshgoftar Monfared
config (e.g. children=4), then will it be safe to increment the hash
table entries in the route block upon receipt of messages? i.e will the
table entries that reside in the shared memory be precise? or do I need
to lock/unlock them while updating?
htable operations are thread-safe, as implemented under the hood. You do
not explicitly need to lock anything.
While that is true for htable (direct) operations, one should be careful
when using hash table items in expressions.
- next is an atomic increment of $sht(a=>x)
$var(x) = $shtinc(a=>x);
$sht(a=>x) = $sht(a=>x) + 1;
because first the value is read, an add expression is evaluated and then
the item in hash table is set to new value. During the evaluation of the
expression, the current process can lose CPU, and another process can
sht_lock("a=>x");
$sht(a=>x) = $sht(a=>x) + 1;
sht_unlock("a=>x");
Cheers,
Daniel
--
Daniel-Constantin Mierla
http://twitter.com/#!/miconda - http://www.linkedin.com/in/miconda
Next Kamailio Advanced Trainings 2014 - http://www.asipto.com
Sep 22-25, Berlin, Germany ::: Oct 15-17, San Francisco, USA
_______________________________________________
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list
http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users
Loading...