Post details: Asterisk modification for Queue logging to mySQL

01/07/07

Permalink02:44:44 pm, Categories: Software, 511 words   English (US)

Asterisk modification for Queue logging to mySQL

Looking on the Digium forums, I found a very nice idea for a patch to the logger.c file in Asterisk from vixtor {link is above}.

The patch was created for 1.2.3 by vixtor so I ported the patch over to 1.4.0 and causes the queue log to be written to SQL in real-time. This allows you to get the statistics for the queue in real-time instead of having to import the existing queue log.

I always have issues with bulky patches to code because they are not a quick hook of the code but a lengthy addition. Therefore, what I have done is move the bulk of the code out of the patch and placed it in a file to be included. While I should have created a .h and a .c file to be proper, this will have to do to make things simpler.

That way the changes to the core files can be minimal and even if you need to hack this for future version, all you need to do is place 4 lines into the existing code.

I doubt that Digium will include this in their production system due to the difference between Digium licensing and mySQL licensing, so my guess is that we will need to patch this for each version.

1. You will need to place the logger-mysql.c file in the main directory below your build directory.
2. You will need to create a sql table something like this:

CREATE TABLE `asterisk_queue_log` (
`id` int(11) NOT NULL auto_increment,
`time` datetime NOT NULL default '0000-00-00 00:00:00',
`callid` varchar(20) NOT NULL default '',
`queuename` varchar(20) NOT NULL default '',
`agent` varchar(20) NOT NULL default '',
`event` varchar(20) NOT NULL default '',
`arg1` varchar(100) NOT NULL default '',
`arg2` varchar(100) NOT NULL default '',
`arg3` varchar(100) NOT NULL default '',
PRIMARY KEY (`id`)
) ;

3. You will need to patch the Makefile and main/logger.c fiel, which is also here.

diff -u -r ./Makefile.sav ./Makefile
--- ./Makefile.sav 2006-12-22 16:33:46.000000000 -0600
+++ ./Makefile 2007-01-07 10:19:04.000000000 -0600
@@ -183,6 +183,8 @@
ASTCFLAGS+=-pipe -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations $(DEBUG)

ASTCFLAGS+=-include $(ASTTOPDIR)/include/asterisk/autoconfig.h
+ASTCFLAGS+=-I/usr/include/mysql
+ASTLDFLAGS+=-L/usr/lib/mysql -lmysqlclient

ifeq ($(AST_DEVMODE),yes)
ASTCFLAGS+=-Werror -Wunused
diff -u -r ./main/logger.c.sav ./main/logger.c
--- ./main/logger.c.sav 2006-11-10 20:04:28.000000000 -0600
+++ ./main/logger.c 2007-01-07 02:51:54.000000000 -0600
@@ -86,6 +86,9 @@
static int filesize_reload_needed = 0;
static int global_logmask = -1;

+/* Added for mysql */
+#include "logger-mysql.c"
+
static struct {
unsigned int queue_log:1;
unsigned int event_log:1;
@@ -337,6 +340,9 @@
if ((s = ast_variable_retrieve(cfg, "general", "event_log")))
logfiles.event_log = ast_true(s);

+/* Added for mysql */
+ init_mysql_logger(cfg);
+
AST_LIST_LOCK(&logchannels);
var = ast_variable_browse(cfg, "logfiles");
for (; var; var = var->next) {
@@ -359,6 +365,8 @@
fprintf(qlog, "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event);
vfprintf(qlog, fmt, ap);
fprintf(qlog, "\n");
+/* Added for mysql */
+ write_mysql_logger(queuename,callid,agent,event,fmt, ap);
va_end(ap);
fflush(qlog);
}

4. You will need to add these lines into /etc/asterisk/logger.conf:

[mysql]
hostname=localhost
dbname=asterisk
table=asterisk_queue_log
password=*
user=asterisk
port=3306
sock=/var/lib/mysql/mysql.sock

UPDATE: Fixed the diff file so it is more proper

Comments:

Sajjad,
The error you show is that you do not have the mysqlclient library installed on that machine or your LIB path is wrong. Check your ./configure in asterisk about the mysqlclient. This has nothing really to do with the patch.

"/usr/bin/ld: cannot find -lmysqlclient"
You did not specify which database you are using, but I assume mySQL because the mySQL client will timeout if there are no queries. This may be an asterisk bug, but I have the issue with other clients to mySQL. Check the version of client you are using. This is not an issue with the patch, just an issue with the client.
I tried your patch and I changed it to support the next table schema in the next stable release and 500 len is to small for a long event name.
The files are here http://thierry.randrianiriana.org/asterisk/, it's for asterisk 1.4.17 .