I came across this very strange task that I need to have the asterisk get/set data from Redis. My initial thought was this is easy, just going to plug in a perl AGI, use redis connector and everything will be super cool. BUT the condition was I've to stay within the extensions.conf. Yes, I did try convince everyone around but to no avail. NO AGIs, use anything else however I want.
So what I did was create a small shell script which behaves as an API for Redis, and use the Asterisk System() application to GET/SET my desired memcached values.
Insert the following lines in there, add more commands to it see this link: http://search.cpan.org/dist/Redis/lib/Redis.pm
Save and Exit;
give permissions to this script.
Now in Dialplan I call System() Application like this.
Thats all. Now I can keep realtime track of the active calls for any user/trunk/carrier etc etc from my dialplan. The real beauty of using this shared Redis Memcache store is that I've like 8 asterisk servers, all of them using the same Redis store and all of them are aware of the current number of calls from/to a particular user.
I've also used it to share the Statuses of Queues and Conference rooms residing on special servers with normal IVR servers. Without using this shared store I can only think of using a Database used by all and I believe that doesn't perform well when multiple users are accessing and modifying the same field concurrently especially when the usage is heavy.
So what I did was create a small shell script which behaves as an API for Redis, and use the Asterisk System() application to GET/SET my desired memcached values.
root@asterisk1:~# vim /etc/asterisk/redis.pl
Insert the following lines in there, add more commands to it see this link: http://search.cpan.org/dist/Redis/lib/Redis.pm
#!/usr/bin/perl my $redis_db = $ARGV[0]; my $command = $ARGV[1]; my $keyname = $ARGV[2]; my $value = $ARGV[3]; # PERL MODULE require Redis; my $redis = Redis->new(server => '127.0.0.1:6379'); if($redis_db > 0 && $redis_db < 16){ $redis->select("$redis_db"); } else { print "using Default Redis DB\n"; } if($command eq "GET"){ $val=$redis->get("$keyname"); print "$val"; } if($command eq "SET" && $value != ""){ $redis->set("$keyname" => "$value"); } if($command eq "INCR"){ $redis->incr("$keyname"); } if($command eq "DECR"){ $redis->decr("$keyname"); } if($command eq "DECRBY"){ $redis->decrby("$keyname",$value); } if($command eq "DEL"){ $redis->del("$keyname" ) || warn "key doesn't exist"; } $redis->quit;
Save and Exit;
give permissions to this script.
root@asterisk1:~# chmod 755 /etc/asterisk/redis.pl
Now in Dialplan I call System() Application like this.
exten => _XXX,1,SET(CALLS=${SHELL(/etc/asterisk/redis.pl 1 GET ${CALLERID(num)})}) same => n,NOOP(The Caller:${CALLERID(num)} has ${CALLS} calls in the system) same => n,Answer() same => n,System(/etc/asterisk/redis.pl 1 INCR ${CALLERID(num)}) same => n,NOOP(Do some dialplan actions as you want) ... ... exten => h,1,System(/etc/asterisk/redis.pl 1 DECR ${CALLERID(num)}) exten => h,n,Hangup()
Thats all. Now I can keep realtime track of the active calls for any user/trunk/carrier etc etc from my dialplan. The real beauty of using this shared Redis Memcache store is that I've like 8 asterisk servers, all of them using the same Redis store and all of them are aware of the current number of calls from/to a particular user.
I've also used it to share the Statuses of Queues and Conference rooms residing on special servers with normal IVR servers. Without using this shared store I can only think of using a Database used by all and I believe that doesn't perform well when multiple users are accessing and modifying the same field concurrently especially when the usage is heavy.










