CREER UN MODULE CDR ASTERISK AVEC SA BASE DE DONNEE SQLITE

Publié le par Kachallah Abagana Mahamat

Cet article vous permet de récupérer les détails d'appels (asterisk) et les stockent dans une base de donnée SQLite.

prérequis

CODE SOURCE 

#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/logger.h"
#include "asterisk/cdr.h"
#include<sqlite3.h>   // apt-get install libsqlite3-dev
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>


sqlite3 *db;
char *err_msg = 0;


/***********************************************************************************************************************************************************************************/
static int cdr_kachou(struct ast_cdr *cdr)
{

// duration : duree d'appel meme sans que le recepteur ne decroche
//billsec : duree réelle d'appel apartir du decrochage (answer)

    ast_log(LOG_NOTICE, "CDR pour le canal '%s'.  ""FROM: '%s', TO: '%s', Duration: %ld, Durée réel: %ld seconde(s)",cdr->channel, cdr->src, cdr->dst, cdr->duration, cdr->billsec);

    struct timeval tv;
    time_t t;
    struct tm *info;
    char buffer[64];

    gettimeofday(&tv, NULL);
    t = tv.tv_sec;

    info = localtime(&t);
    //printf("%s",asctime (info));
    strftime (buffer, sizeof buffer, "%d-%m-%Y %H:%M:%S\n", info);
    //printf("%s",buffer);


    //int rc = sqlite3_open("/var/lib/asterisk/res_kachoucdr.db", &db);
    sqlite3 *db;
    if (sqlite3_open("/var/lib/asterisk/res_kachoucdr.db", &db)) {
        printf("Could not open the.db\n");
        exit(-1);
    }
    if (sqlite3_exec(db, "create table if not exists cdr(date, fromUser, toUser, duration)", NULL, NULL, NULL)) {
        printf("Error executing sql statement\n");
    }
    else {
        printf("Table created\n");
    }
 //
 // Prepare a statement for multiple use:
 //
    sqlite3_stmt *stmt;
    if (sqlite3_prepare_v2(db, "insert into cdr values(?, ?, ?, ?)", -1, &stmt, NULL)) {
        printf("Error executing sql statement\n");
        sqlite3_close(db);
        exit(-1);
    }
 //
 // Bind the values for the first insert:
 //
    sqlite3_bind_text (stmt, 1, buffer, -1, NULL);
    sqlite3_bind_text (stmt, 2, cdr->src, -1, NULL);
    sqlite3_bind_text (stmt, 3, cdr->dst, -1, NULL);
    sqlite3_bind_int (stmt, 4, cdr->billsec);

 // Do the first insert:
 //
    sqlite3_step(stmt);


    sqlite3_finalize(stmt);

    sqlite3_close(db);

    return 0;

}

/******************************************************************************/

static int load_module(void)
{
    ast_log(LOG_NOTICE, "Bonjour!\n");
    ast_cdr_register("Bonjour", "CDR KACHOU", cdr_kachou);
    return AST_MODULE_LOAD_SUCCESS;
}
/******************************************************************************/

static int unload_module(void)
{
    ast_log(LOG_NOTICE, "Au revoir!\n");
    ast_cdr_unregister("CDR KACHOU BYE!\n");
    return 0;
}
/******************************************************************************/
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "CDR KACHOU");
/******************************************************************************/

Test

 

Pour être informé des derniers articles, inscrivez vous :
Commenter cet article