[Asterisk-Users] module : cdr_sybase.so

cvasiliu cvasiliu at accessnet.ro
Fri Jul 11 12:56:11 MST 2003


If anyone is interested ... just in case! :-)... I have tried to write , 
based on the cdr_mysql.so module, an Sybase module.
To compile you can use something like that:

export SYBPLATFORM=linux
export SYBASE=/opt/sybase
cc -I$SYBASE/include -c -o cdr_sybase.o cdr_sybase.c
cc -shared -Xlinker -x -o cdr_sybase.so cdr_sybase.o -lsybdb -lm 
-L$SYBASE/lib

(anyone could write the corect Makefile inside the cdr dir.?)

So here it is:

*
 * Asterisk -- A telephony toolkit for Linux.
 *
 * Sybase CDR logger
 *
 * Cristian Vasiliu <cvasiliu at accessnet.ro>
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License.
 *
 */


#include <asterisk/config.h>
#include <asterisk/options.h>
#include <asterisk/channel.h>
#include <asterisk/cdr.h>
#include <asterisk/module.h>
#include <asterisk/logger.h>
#include "../asterisk.h"

#include <stdio.h>
#include <string.h>

#include <stdlib.h>
#include <unistd.h>
#include <time.h>

#include <sybfront.h>
#include <sybdb.h>

#define DATE_FORMAT "%Y-%m-%d %T"

static char *desc = "Sybase CDR Backend";
static char *name = "sybase";
static char *config = "cdr_sybase.conf";

static DBPROCESS *dbproc;
static LOGINREC *login;


static struct tm _date(struct timeval tv)
{
        struct tm tm;
        time_t t;
        t = tv.tv_sec;
        localtime_r(&t,&tm);
        return tm;
}


static int sybase_log(struct ast_cdr *cdr)
{
  struct tm tm, end, start, answer;
  struct timeval tv;
  struct timezone tz;
  char *sqlcmd, timestr[128], timestart[128], timeend[128], timeanswer[128];
  time_t t;

   sqlcmd = (char *)malloc(2048);
  memset(sqlcmd,0,2048);

  start = _date(cdr->start);
  end = _date(cdr->end);
  answer = _date(cdr->answer);

  gettimeofday(&tv,&tz);
  t = tv.tv_sec;
  localtime_r(&t,&tm);
  strftime(timestr,128,DATE_FORMAT,&tm);
  strftime(timeend,128,DATE_FORMAT,&end);
  strftime(timeanswer,128,DATE_FORMAT,&answer);
  strftime(timestart,128,DATE_FORMAT,&start);

  ast_log(LOG_WARNING,timeend);
  ast_log(LOG_WARNING,timestart);
  ast_log(LOG_WARNING,timeanswer);


  ast_log(LOG_DEBUG,"cdr_sybase: inserting a CDR record.\n");
  sprintf(sqlcmd,"INSERT INTO cdr 
(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,start,end,answer) 
VALUES ('%s','%s','%s','%s','%s', 
'%s','%s','%s','%s',%i,%i,%i,%i,'%s','%s','%s','%s')",timestr,cdr->clid,cdr->src, 
cdr->dst, cdr->dcontext,cdr->channel, cdr->dstchannel, cdr->lastapp, 
cdr->lastdata,cdr->duration,cdr->billsec,cdr->disposition,cdr->amaflags, 
cdr->accountcode, timestart, timeend, timeanswer);
  dbcmd(dbproc,sqlcmd);
  dbsqlexec(dbproc);
   if (dbresults(dbproc) != SUCCESS) {
          ast_log(LOG_WARNING,"cdr_sybase: Could not insert cdr:  
%s\n",sqlcmd);
          return  -1;
   }
  return 0;
}



char *description(void)
{
  return desc;
}

int unload_module(void)
{
  dbexit();
  ast_cdr_unregister(name);
  return 0;
}

int load_module(void)
{
  int res;
  struct ast_config *cfg;
  struct ast_variable *var;

  char *dbuser, *password, *dbserver, *application;

  cfg = ast_load(config);
  if (!cfg) {
    ast_log(LOG_WARNING, "Unable to load config for sybase CDR's: %s\n", 
config);
    return 0;
  }

  var = ast_variable_browse(cfg, "global");
  if (!var) {
    return 0;
  }

  dbuser = ast_variable_retrieve(cfg,"global","dbuser");
  password = ast_variable_retrieve(cfg,"global","password");
  dbserver = ast_variable_retrieve(cfg,"global","dbserver");
  application = ast_variable_retrieve(cfg,"global","application");

  ast_log(LOG_DEBUG,"cdr_sybase: got dbserver of %s\n",dbserver);
  ast_log(LOG_DEBUG,"cdr_sybase: got dbuser of %s\n",dbuser);
  ast_log(LOG_DEBUG,"cdr_sybase: got password of %s\n",password);
  ast_log(LOG_DEBUG,"cdr_sybase: got application of %s\n",application);

  if (dbserver == NULL)
    {
      ast_log(LOG_ERROR,"Database server not specified.\n");
      return -1;
    }

  if (dbuser == NULL)
    {
      ast_log(LOG_ERROR,"Database dbuser not specified.\n");
      return -1;
    }
  if (password == NULL)
    {
      ast_log(LOG_ERROR,"Database password not specified.\n");
      return -1;
    }
  if (application == NULL)
    {
      ast_log(LOG_ERROR,"Application not specified.\n");
      return -1;
    }

  /* Initialize DB-Library. */
  if (dbinit() == FAIL )
 {
     ast_log(LOG_ERROR,"Can not initialize DB-library.\n");
     return -1;
   }


  login = dblogin();
  DBSETLUSER(login,dbuser);
  DBSETLPWD(login,password);
  DBSETLAPP(login,application);
  dbproc = dbopen(login,dbserver);

  if (dbproc == NULL) {
    ast_log(LOG_WARNING, "Failed to connect to sybase database.\n");

    return -1;
  } else {
    ast_log(LOG_DEBUG,"Successfully connected to sybase database.\n");
  }


  res = ast_cdr_register(name, desc, sybase_log);
  if (res) {
    ast_log(LOG_ERROR, "Unable to register Sybase CDR handling\n");
  }
  return res;
}

int reload(void)
{

  struct ast_config *cfg;
  struct ast_variable *var;

  char *hostname, *dbname, *password, *dbuser;


  cfg = ast_load(config);
  if (!cfg) {
    ast_log(LOG_WARNING, "Unable to load Sybase CDR config %s\n", config);
    return 0;
  }

  var = ast_variable_browse(cfg, "global");
  if (!var) {
    /* nothing configured */
    return 0;
  }

  hostname = ast_variable_retrieve(cfg,"global","hostname");
  dbname = ast_variable_retrieve(cfg,"global","dbname");
  dbuser = ast_variable_retrieve(cfg,"global","user");
  password = ast_variable_retrieve(cfg,"global","password");
  ast_log(LOG_DEBUG,"cdr_mysql: got hostname of %s\n",hostname);
  ast_log(LOG_DEBUG,"cdr_mysql: got dbname of %s\n",dbname);
  ast_log(LOG_DEBUG,"cdr_mysql: got dbuser of %s\n",dbuser);
  ast_log(LOG_DEBUG,"cdr_mysql: got password of %s\n",password);

  if (hostname == NULL)
 {
      ast_log(LOG_ERROR,"Database server hostname not specified.\n");
      return -1;
    }
  if (dbname == NULL)
    {
      ast_log(LOG_ERROR,"Database dbname not specified.\n");
      return -1;
    }
  if (dbuser == NULL)
    {
      ast_log(LOG_ERROR,"Database dbuser not specified.\n");
      return -1;
    }

  if (password == NULL)
    {
      ast_log(LOG_ERROR,"Database password not specified.\n");
      return -1;
    }





  return 0;
}

int usecount(void)
{
  return 0;
}

char *key()
{
  return ASTERISK_GPL_KEY;
}
 




More information about the asterisk-users mailing list