Patch for mysql logging of queue_log

I agree. This would be really great if it was built-in.

Hi merlyn, Hi all,

I’ve tryed to apply patch about unsuccessfully. What happens’ posted follow:

[root@Comunica-WS1 src]# cd asterisk-1.2.13/
[root@Comunica-WS1 asterisk-1.2.13]#
[root@Comunica-WS1 asterisk-1.2.13]#
[root@Comunica-WS1 asterisk-1.2.13]# patch -p1 </usr/src/asterisk-diff
(Stripping trailing CRs from patch.)
patching file logger.c
Hunk #1 FAILED at 18.
Hunk #2 succeeded at 32 with fuzz 1.
Hunk #3 succeeded at 79 with fuzz 2.
Hunk #4 FAILED at 161.
Hunk #5 FAILED at 227.
Hunk #6 FAILED at 276.
Hunk #7 FAILED at 292.
Hunk #8 FAILED at 316.
Hunk #9 FAILED at 339.
Hunk #10 FAILED at 362.
Hunk #11 FAILED at 415.
Hunk #12 FAILED at 476.
Hunk #13 FAILED at 501.
Hunk #14 FAILED at 511.
Hunk #15 FAILED at 535.
Hunk #16 FAILED at 563.
Hunk #17 FAILED at 604.
Hunk #18 FAILED at 622.
Hunk #19 FAILED at 642.
Hunk #20 FAILED at 664.
Hunk #21 FAILED at 700.
Hunk #22 FAILED at 747.
Hunk #23 FAILED at 785.
Hunk #24 FAILED at 814.
Hunk #25 FAILED at 859.
Hunk #26 FAILED at 879.
Hunk #27 FAILED at 913.
Hunk #28 FAILED at 994.
Hunk #29 FAILED at 1015.
27 out of 29 hunks FAILED – saving rejects to file logger.c.rej
(Stripping trailing CRs from patch.)
patching file Makefile
Reversed (or previously applied) patch detected! Assume -R? [n]
Apply anyway? [n]
Skipping patch.
patch unexpectedly ends in middle of line
2 out of 2 hunks ignored – saving rejects to file Makefile.rej

Can you give me any suggestions?
I use asterisk 1.2.13.

Thanks 4 all

it’s going to be a bit of a slog, but if you can’t get an updated patch file from the developer, you could manually apply the patch to the source. open the patch file with a text editor, open the source files in the same way, and fine & replace the changes. 29 mods shouldn’t take too long :open_mouth:

I have updated this to 1.4.0 and placed the files out on my site.
I have moved the bulk of the changes to a single file so only 4 lines need to be added/changed in the future if these files change radically and the diff stops working.

Thanks to vixtor for starting this out. I would give him more credit in the files, but I put the reference back to this forum in the files as that is all I had.

I hope that is okay. If not, let me know so I can fix it.

Updated: Fixed link

Has anyone posted this to bugs.digium.com???

Matt Brooks
Digium, Inc.

I am not sure that it is a bug or feature request… at least for mySQL. The problem is that mySQL’s license is incompatible for Digium to use their code. Or so I understand. Maybe if we ported it to ODBC…

[quote=“aplack”]I have updated this to 1.4.0 and placed the files out on my site.
I have moved the bulk of the changes to a single file so only 4 lines need to be added/changed in the future if these files change radically and the diff stops working.

Thanks to vixtor for starting this out. I would give him more credit in the files, but I put the reference back to this forum in the files as that is all I had.

I hope that is okay. If not, let me know so I can fix it.[/quote]
I’m sorry, but I can’t find the files on your site. Could you post new location of them?

Thanx!

kokoska.rokoska

I see that somehow a _ was placed in the link. I have fixed the link to the site, it should work from the above link or this link

Thanx a lot, this link is OK :smile:
But - I have trouble with using your patch. When I try to “patch” logger.c in Asterisk 1.4.0 I have got following error (I run patch from the Asterisk sources directory - one level above “main”):

[root@pbx asterisk-1.4.0]# patch -p0 < logger-mysql.diff
(Stripping trailing CRs from patch.)
patching file Makefile
(Stripping trailing CRs from patch.)
patching file main/logger.c
patch unexpectedly ends in middle of line
Hunk #3 succeeded at 365 with fuzz 1.

Could you point me what should I do to patch logger.c succesfuly?

Thanx!

kokoska.rokoska

Okay, found the problem there as well. Made some assumptions that were not correct. Download a new copy of the patch or copy it from the website. Both have been corrected.

Thank you very much for your new .diff file, it works well :smile:
But - Asterisk compilation failed:

logger.o(.text+0x24c): In function `write_mysql_logger':
/var/pbxinstall/asterisk14/asterisk-1.4.0/main/logger-mysql.c:97: undefined reference to `mysql_real_query'
logger.o(.text+0x260):/var/pbxinstall/asterisk14/asterisk-1.4.0/main/logger-mysql.c:98: undefined reference to `mysql_error'
logger.o(.text+0x1c54): In function `init_mysql_logger':
/var/pbxinstall/asterisk14/asterisk-1.4.0/main/logger-mysql.c:53: undefined reference to `mysql_real_connect'
collect2: ld returned 1 exit status
make[1]: *** [asterisk] Error 1
make: *** [main] Error 2

I am using current version of MySQL (5.0.27) on CentOS 4.4 and have all header files installed.
Does anyone know what is wrong?

Thanx!

kokoska.rokoska

For those interested, here is a patch that works for me against 1.2.14:

Careful with line wrapping.

diff -ru asterisk/Makefile asterisk_test/Makefile
--- asterisk/Makefile   2006-12-29 03:43:14.000000000 -0500
+++ asterisk_test/Makefile      2006-12-31 06:47:39.000000000 -0500
@@ -225,7 +225,7 @@
   INSTALL=ginstall
 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
@@ -367,7 +367,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
   ifeq ($(OSARCH),SunOS)
     LIBS+=-lm -lcurses
diff -ru asterisk/logger.c asterisk_test/logger.c
--- asterisk/logger.c   2006-12-29 03:43:16.000000000 -0500
+++ asterisk_test/logger.c      2006-12-31 06:46:20.000000000 -0500
@@ -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;
@@ -349,6 +361,36 @@
                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);
@@ -373,6 +415,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);
        }

Just FYI: new patch for Asterisk SVN trunk revision 94615 is available. It allows you to store queue_log in your RT engine.

bugs.digium.com/view.php?id=11625

I have tried to apply this patch to x86_64 system (CentOS 5.1) and Asterisk died with core-dump during startup. I have tried Asterisk version 1.4.6 and 1.4.17, both with same result…
On i386 system everything works fine.

Could anyone point me to some solution?

Thanx. kokoska.rokoska