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)?
On 08/12/2014 03:56 PM, AliReza Khoshgoftar Monfared wrote:
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.
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
On Tue, Aug 12, 2014 at 3:58 PM, Alex Balashov abalashov@evaristesys.com wrote:
On 08/12/2014 03:56 PM, AliReza Khoshgoftar Monfared wrote:
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
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list sr-users@lists.sip-router.org http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users
Look at the definition of htable parameter, there you have various ways to set expires for each item in hash table.
Cheers, Daniel
On 13/08/14 05:26, AliReza Khoshgoftar Monfared wrote:
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
On Tue, Aug 12, 2014 at 3:58 PM, Alex Balashov <abalashov@evaristesys.com mailto:abalashov@evaristesys.com> wrote:
On 08/12/2014 03:56 PM, AliReza Khoshgoftar Monfared wrote: 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 <tel:%2B1-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 _______________________________________________ SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list sr-users@lists.sip-router.org <mailto:sr-users@lists.sip-router.org> http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list sr-users@lists.sip-router.org http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users
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
On Wed, Aug 13, 2014 at 4:07 AM, Daniel-Constantin Mierla <miconda@gmail.com
wrote:
Look at the definition of htable parameter, there you have various ways to set expires for each item in hash table.
Cheers, Daniel
On 13/08/14 05:26, AliReza Khoshgoftar Monfared wrote:
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
On Tue, Aug 12, 2014 at 3:58 PM, Alex Balashov abalashov@evaristesys.com wrote:
On 08/12/2014 03:56 PM, AliReza Khoshgoftar Monfared wrote:
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
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list sr-users@lists.sip-router.org http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing listsr-users@lists.sip-router.orghttp://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 sr-users@lists.sip-router.org http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users
On 08/13/2014 10:21 AM, AliReza Khoshgoftar Monfared wrote:
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.
On 13/08/14 16:24, Alex Balashov wrote:
On 08/13/2014 10:21 AM, AliReza Khoshgoftar Monfared wrote:
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
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 < miconda@gmail.com> wrote:
On 13/08/14 16:24, Alex Balashov wrote:
On 08/13/2014 10:21 AM, AliReza Khoshgoftar Monfared wrote:
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
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list sr-users@lists.sip-router.org http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users