Sunday, July 8, 2012

OpenSIPS working with Redis

I recently was trying some experiment to make opensips work with redis. This was pretty easy task but since Redis support in opensips is very new and not much of help is available and to be honest it doesn't require huge documentation to follow to make this work.

As I started on it, I got few minor problems which, I think, are worth writing here so that some other day when I forget these things I could read from here and refresh my memory.

We already know what is OpenSIPS, now what is Redis !?

Redis is a Key-Value pair storage tool running in memory - making it super fast, accessible over the network. Redis can be configured in cluster mode to save your data in case one node dies. To know more start from here. ;)



The two things which you obviously require for this are
1- Redis server installation
2- Hiredis client for Redis (required for opensips)
3- OpenSIPS server installation (version 1.8)

But before you proceed with OpenSIPS installation we need to install a prerequisite library hiredis.

Hiredis installtion.
These are the steps I performed in order to have Hiredis installed.

root@debian:# cd /usr/src
root@debian:/usr/src# apt-get install git
root@debian:/usr/src# git clone https://github.com/redis/hiredis
root@debian:/usr/src# cd hiredis/
root@debian:/usr/src/antirez-hiredis-f8debbf# make
root@debian:/usr/src/antirez-hiredis-f8debbf# make install

Now edit the opensips Makefile and make sure you've "cachedb_redis" not mentioned in exlude_module
Mine looked like this.


exclude_modules?= osp db_oracle \
#exclude_modules?= b2b_logic jabber cpl-c xmpp rls mi_xmlrpc xcap_client \
#       db_mysql db_postgres db_unixodbc db_oracle db_berkeley aaa_radius \
#       osp perl snmpstats perlvdb carrierroute mmgeoip \
#       presence presence_xml presence_mwi presence_dialoginfo \
#       pua pua_bla pua_mi pua_usrloc pua_xmpp pua_dialoginfo \
#       ldap h350 identity regex cachedb_memcached cachedb_redis event_rabbitmq \
#       db_http json python dialplan mi_http


Once opensips installed I tooka quick look if the required module is placed in directory or not !! and surprisingly it wasn't there.

root@debian:#ls /usr/local/lib/opensips/modules/

No cachedb_redis.so found :( !!

I tried reinstalling carefully but the module shared-object (.so) file wasn't in modules. 

I even tried a module specific installation:

root@debian:#make include_modules="cachedb_redis" modules

and again no cachedb_redis.so file !!

what resolved this was a symbolic link from the source installation directory of the module where the cachedb_redis.so file was created to the modules directory.

root@debian:#ln -s /usr/src/svn-src/opensips_1_8/modules/cachedb_redis/cachedb_redis.so /usr/local/lib/opensips/modules/

This wasn't enough - OpenSIPS  did recognize the new module but it agve me an error for hiredis like this.

ERROR:core:sr_load_module: could not open module </usr/local/lib/opensips/modules/cachedb_redis.so>: libhiredis.so.0.10: cannot open shared object file: No such file or directory

so quickly I executed this command:

root@debian:# ldconfig -v

and then opensips started perfectly fine.

Now using redis in the configurations.

loading the module

#### ---- REDIS MODULE ---- #####
loadmodule "cachedb_redis.so"
modparam("cachedb_redis", "cachedb_url","redis:group1://localhost:6379/")


Now in configuration store values in some key like this

cache_store("redis:group1","Call_Count","$avp(counter)");

To fetch the data:

cache_fetch("redis:group1","Call_Count",$avp(101));

xlog("L_NOTICE","[$pr:$fU@$si:$sp]: Total Calls processed = $avp(101)\n");
See this link for some more reading.

No comments:

Post a Comment