I know this is a little ugly coded, but it works and seems to be ok (at least for me).
It is a patch for logger.c that duplicates the messages in queue_log into a mysql table.
It would be great if somebody with more knowledge of the asterisk coding standards would integrate this into the code, because it is really useful when you want to have a call center with statistics for agents in realtime.
To use it, you have to add in logger.conf a section like:
[mysql]
hostname=localhost
dbname=asterisk
table=queue_log
password=*****
user=asterisk
port=3306
sock=/var/lib/mysql/mysql.sock
The patch is:
- Code: Select all
diff -u -r orig/asterisk-1.2.3/Makefile asterisk-1.2.3/Makefile
--- orig/asterisk-1.2.3/Makefile 2005-12-05 08:47:51.000000000 +0200
+++ asterisk-1.2.3/Makefile 2006-01-26 18:19:45.000000000 +0200
@@ -217,7 +217,7 @@
M4=/usr/local/bin/m4
endif
-INCLUDE+=-Iinclude -I../include
+INCLUDE+=-Iinclude -I../include -I/usr/include/mysql
ASTCFLAGS+=-pipe -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations $(DEBUG) $(INCLUDE) -D_REENTRANT -D_GNU_SOURCE #-DMAKE_VALGRIND_HAPPY
ASTCFLAGS+=$(OPTIMIZE)
ASTOBJ=-o asterisk
@@ -356,7 +356,7 @@
endif
ifeq ($(OSARCH),Linux)
- LIBS+=-ldl -lpthread -lncurses -lm -lresolv #-lnjamd
+ LIBS+=-ldl -lpthread -lncurses -lm -lresolv -L/usr/lib/mysql -lmysqlclient #-lnjamd
else
LIBS+=-lncurses -lm
endif
diff -u -r orig/asterisk-1.2.3/logger.c asterisk-1.2.3/logger.c
--- orig/asterisk-1.2.3/logger.c 2006-01-17 18:55:30.000000000 +0200
+++ asterisk-1.2.3/logger.c 2006-01-27 10:53:35.000000000 +0200
@@ -32,6 +32,7 @@
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
+#include <mysql.h>
#define SYSLOG_NAMES /* so we can map syslog facilities names to their numeric values,
from <syslog.h> which is included by logger.h */
@@ -78,6 +79,17 @@
static char dateformat[256] = "%b %e %T"; /* Original Asterisk Format */
+static MYSQL logdb;
+static char my_hostname[100];
+static char my_dbname[100];
+static char my_table[100];
+static char my_password[100];
+static char my_user[100];
+static unsigned int my_port;
+static char my_sock[100];
+static int use_mysql;
+
+
AST_MUTEX_DEFINE_STATIC(msglist_lock);
AST_MUTEX_DEFINE_STATIC(loglock);
static int filesize_reload_needed = 0;
@@ -335,6 +347,37 @@
logfiles.event_log = ast_true(s);
}
+ if ((s = ast_variable_retrieve(cfg, "mysql", "hostname"))) {
+ ast_copy_string(my_hostname, s, sizeof(my_hostname));
+ }
+ if ((s = ast_variable_retrieve(cfg, "mysql", "dbname"))) {
+ ast_copy_string(my_dbname, s, sizeof(my_dbname));
+ }
+ if ((s = ast_variable_retrieve(cfg, "mysql", "table"))) {
+ ast_copy_string(my_table, s, sizeof(my_table));
+ }
+ if ((s = ast_variable_retrieve(cfg, "mysql", "password"))) {
+ ast_copy_string(my_password, s, sizeof(my_password));
+ }
+ if ((s = ast_variable_retrieve(cfg, "mysql", "user"))) {
+ ast_copy_string(my_user, s, sizeof(my_user));
+ }
+ if ((s = ast_variable_retrieve(cfg, "mysql", "port"))) {
+ if (sscanf(s, "%d", &my_port) < 1) {
+ my_port = 0;
+ }
+ }
+ if ((s = ast_variable_retrieve(cfg, "mysql", "sock"))) {
+ ast_copy_string(my_sock, s, sizeof(my_sock));
+ }
+ if(!mysql_real_connect(&logdb, my_hostname, my_user, my_password, my_dbname, my_port, my_sock, 0)){
+ ast_log(LOG_ERROR, "Failed to connect to mysql database %s on %s.\n", my_dbname, my_hostname);
+ use_mysql = 0;
+ } else {
+ use_mysql = 1;
+ }
+
+
var = ast_variable_browse(cfg, "logfiles");
while(var) {
chan = make_logchannel(var->name, var->value, var->lineno);
@@ -362,6 +405,49 @@
fprintf(qlog, "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event);
vfprintf(qlog, fmt, ap);
fprintf(qlog, "\n");
+
+ if(use_mysql == 1) {
+ char *myquery = malloc(500);
+ char *myquery_ap = malloc(100);
+ char *myfmt = malloc(strlen(fmt)*3+1>8?strlen(fmt)*3+1:9);
+ int x,y;
+ int fmt_count = 0;
+ int len = 500;
+
+ if(snprintf(myquery, 500, "insert into %s(time,callid,queuename,agent,event,arg1,arg2,arg3) values(from_unixtime(%ld),'%s','%s','%s','%s',", my_table, (long)time(NULL), callid, queuename, agent, event)<500) {
+ len = 500 - strlen(myquery);
+ myfmt[0] = '\'';
+ for(x=0,y=0; x<strlen(fmt); x++) {
+ if(fmt[x] == '|') {
+ myfmt[++y] = '\'';
+ myfmt[++y] = ',';
+ myfmt[++y] = '\'';
+ fmt_count ++;
+ } else {
+ myfmt[++y] = fmt[x];
+ }
+ }
+ for(;fmt_count<2; fmt_count++) {
+ myfmt[++y] = '\'';
+ myfmt[++y] = ',';
+ myfmt[++y] = '\'';
+ }
+ myfmt[++y] = '\'';
+ myfmt[++y] = 0;
+ if(vsnprintf(myquery_ap, 100, myfmt, ap)<100) {
+ strncat(myquery, myquery_ap, len-1);
+ len = 500 - strlen(myquery);
+ strncat(myquery, ")", len-1);
+ if(mysql_real_query(&logdb, myquery, strlen(myquery)+1)){
+ printf(mysql_error(&logdb));
+ }
+ }
+ }
+ free(myfmt);
+ free(myquery_ap);
+ free(myquery);
+ }
+
va_end(ap);
fflush(qlog);
}