/*! \file * * \brief Asterisk Logger to mysql * * Logging routines * * \author Tony Plack * based on code from http://forums.digium.com/viewtopic.php?t=4073 and posted by vixtor */ #include 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; void init_mysql_logger (struct ast_config *cfg); void write_mysql_logger(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, va_list ap); void init_mysql_logger (struct ast_config *cfg) { const char *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; } } void write_mysql_logger(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, va_list ap) { 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