Modifications entre les versions 7 et 8
Version 7 à la date du 2006-09-27 17:52:52
Taille: 6296
Éditeur: ThomasNoël
Commentaire:
Version 8 à la date du 2007-01-30 15:00:17
Taille: 6475
Éditeur: MoussaNombre
Commentaire:
Texte supprimé. Texte ajouté.
Ligne 12: Ligne 12:
* créer une base de donnée mysql pour les y importer les logs * créer une base de donnée mysql pour y importer les logs
Ligne 39: Ligne 39:
-------- {{{
Ligne 42: Ligne 42:
//*** process asterisk cdr file (Master.csv) insert usage // * values into a mysql database which is created for use // * with the Asterisk_addons cdr_addon_mysql.so // * The script will only insert NEW records so it is safe // * to run on the same log over-and-over such as in the // * case where logs have not been rotated. // * // * Author: John Lange ( john.lange@open-it.ca ) // * Date: May 4, 2005. Updated July 21, 2005 // * // * Here is what the script does: // * // * 1) Find the last log entry in the database cdr table. // * 2) scan the asterisk logs until the dates are larger than the last log entry (so we don't duplicate entries) // * 3) parse each row from the text log and insert it into the database. // * //*** process asterisk cdr file (Master.csv) insert usage
// * values into a mysql database which is created for use
// * with the Asterisk_addons cdr_addon_mysql.so
// * The script will only insert NEW records so it is safe
// * to run on the same log over-and-over such as in the
// * case where logs have not been rotated.
// *
// * Author: John Lange ( john.lange@open-it.ca )
// * Date: May 4, 2005. Updated July 21, 2005
// *
// * Here is what the script does:
// *
// * 1) Find the last log entry in the database cdr table.
// * 2) scan the asterisk logs until the dates are larger than the last log entry (so we don't duplicate entries)
// * 3) parse each row from the text log and insert it into the database.
// *
Ligne 44: Ligne 59:
$locale_db_host = 'localhost';  $locale_db_host = 'localhost';
Ligne 52: Ligne 67:
Ligne 53: Ligne 69:
    $logfile = $argv[1];
} else {
    print("Usage ".$argv[0]." <filename>\n");
    print("Where filename is the path to the Asterisk csv file to import (Master.csv)\n");
    print("This script is safe to run multiple times on a growing log file as it only imports records that are newer than the database\n");
    exit(0);
}
Ligne 54: Ligne 77:
 . $logfile = $argv[1];
} else {
// connect to db
$link = mysql_connect($locale_db_host, $locale_db_login, $locale_db_pass);
if (!$link) {
   die('Could not connect: ' . mysql_error());
   }
Ligne 57: Ligne 83:
 . print("Usage ".$argv[0]." <filename>\n"); print("Where filename is the path to the Asterisk csv file to import (Master.csv)\n"); print("This script is safe to run multiple times on a growing log file as it only imports records that are newer than the database\n"); exit(0);
} // connect to db $link = mysql_connect($locale_db_host, $locale_db_login, $locale_db_pass); if (!$link) {
//echo "Connected successfully\n";
mysql_select_db($locale_db_name, $link) or die("Could not select database $locale_db_name");
Ligne 60: Ligne 86:
 . die('Could not connect: ' . mysql_error()); }
//echo "Connected successfully\n"; mysql_select_db($locale_db_name, $link) or die("Could not select database $locale_db_name");
/** 1) Find the last log entry **/
// look in cdr table to see when the last entry was made.
// this establishes the starting point for the asterisk data.
$sql="SELECT UNIX_TIMESTAMP(calldate) as calldate".
    " FROM cdr".
    " ORDER BY calldate DESC".
    " LIMIT 1";
Ligne 63: Ligne 94:
/** 1) Find the last log entry **/ // look in cdr table to see when the last entry was made. // this establishes the starting point for the asterisk data. $sql="SELECT UNIX_TIMESTAMP(calldate) as calldate".

 . " FROM cdr". " ORDER BY calldate DESC". " LIMIT 1";
Ligne 67: Ligne 95:

. print("Invalid query: " . mysql_error()."\n"); print("SQL: $sql\n"); die();
} $result_array = mysql_fetch_array($result); //$lasttimestamp = date("Y-m-d H:i:s", $result_array['voip_stamp']); $lasttimestamp = $result_array['calldate'];
    print("Invalid query: " . mysql_error()."\n");
   
print("SQL: $sql\n");
   
die();
}
$result_array = mysql_fetch_array($result);
//$lasttimestamp = date("Y-m-d H:i:s", $result_array['voip_stamp']);
$lasttimestamp = $result_array['calldate'];
Ligne 73: Ligne 105:
$rows = 0; $handle = fopen($logfile, "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $rows = 0;
$handle = fopen($logfile, "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    // NOTE: the fields in Master.csv can vary. This should work by default on all installations but you may have to edit the next line to match your configuration
    list($accountcode,$src, $dst, $dcontext, $clid, $channel, $dstchannel, $lastapp, $lastdata, $start, $answer, $end, $duration,
     $billsec, $disposition, $amaflags ) = $data;
Ligne 75: Ligne 112:
 . // NOTE: the fields in Master.csv can vary. This should work by default on all installations but you may have to edit the next line to match your configuration list($accountcode,$src, $dst, $dcontext, $clid, $channel, $dstchannel, $lastapp, $lastdata, $start, $answer, $end, $duration, $billsec, $disposition, $amaflags ) = $data; // 3) parse each row and add to the database
 if(strtotime($end) > $lasttimestamp) { //
we found a new record so add it to the DB
  . $sql = "INSERT INTO cdr (calldate, clid, src, dst, userfield, channel, dstchannel, lastapp, lastdata, duration, billsec, disposition, amaflags, accountcode)
   . VALUES('$end', '".mysql_real_escape_string($clid)."', '$src', '$dst', '$dcontext', '$channel', '$dstchannel', '$lastapp', '$lastdata', '$duration', '$billsec',
    . '$disposition', '$amaflags', '$accountcode')";
  if(!($result2 = mysql_query($sql, $link))) {
   . print("Invalid query: " . mysql_error()."\n"); print("SQL: $sql\n"); die();
  } $rows++;
 }
} fclose($handle);
    // 3) parse each row and add to the database
    i
f(strtotime($end) > $lasttimestamp) { // we found a new record so add it to the DB
        $sql = "INSERT INTO cdr (calldate, clid, src, dst, userfield, channel, dstchannel, lastapp, lastdata, duration, billsec, disposition, amaflags, accountcode)
                   VALUES('$end', '".mysql_real_escape_string($clid)."', '$src', '$dst', '$dcontext', '$channel', '$dstchannel', '$lastapp', '$lastdata', '$duration', '$billsec',
                    '$disposition', '$amaflags', '$accountcode')";
       if(!($result2 = mysql_query($sql, $link))) {
            print("Invalid query: " . mysql_error()."\n");
           
print("SQL: $sql\n");
           
die();
        }
       
$rows++;
    }
}
fclose($handle);
Ligne 89: Ligne 130:
-------- }}}

INSTALLATION ASTERISK-STATS

Outils d'analyse des logs d'Asterisk Site web : http://www.voip-info.org/wiki/index.php?page=Asterisk+CDR+Areski+GUI

NB : ces notes sont écrites après une journée de "dur boulot" :(

* s'assurer que les prérequis sont remplis

* créer un compte utilisateur/motdepasse dans mysql

* créer une base de donnée mysql pour y importer les logs

* créer une table "cdr" :


CREATE TABLE cdrtable (id INT not null AUTO_INCREMENT, calldate DATE not null, clid VARCHAR (80) not null, src VARCHAR (80) not null, dst VARCHAR (80) not null, userfield VARCHAR (80) not null, channel VARCHAR (80) not null, dstchannel VARCHAR (80) not null, lastapp VARCHAR (80) not null, lastdata VARCHAR (80) not null, duration INT not null, billsec INT not null, disposition VARCHAR (80) not null, amaflags VARCHAR (80) not null, accountcode VARCHAR (80) not null, PRIMARY KEY (id))


* télécharger et désarchiver la sourse de asterisk-stat, par exemple dans /var/www/asterisk-stats

* modifier le fichier lib/defines.php en précisant les paramètres

  • WEBROOT : url de l'application (http://votre-serveur/asterisk-stats)

  • FSROOT : répertoire où se trouve asterisk-stats (/var/www/asterisk-stats)
  • HOST : localhost
  • PORT : 3306
  • USER : compte utilisateur pour accéder à la base de données
  • PASS : mot de passe du compte
  • DBNAME : nom de la base de données
  • DB_TYPE : type de base de données (mysql)
  • DB_TABLENAME : table créé (cdr)

* ajouter un alias dans Apache pour l'url de l'application (Alias /asterisk-stats /var/www/asterisk-stats/)

* importer les logs asterisk (/var/log/asterisk/cdr-csv/Master/csv) en utilisant le script ci-dessous qu'on peut nommer importcdr.php (ajuster les paramètres de connexion à la base de données). src : http://www.voip-info.org/wiki/view/Asterisk+CDR+csv+mysql+import

NB: Notez qu'Asterisk est capable d'écrire directement les CDR dans une série de base de données (en natif dans PostgreSQL/TDS/ODBC/SQLite et avec asterisk-addons dans MySQL). Si vous utilisez déjà un pilote CDR base de données (cdr_*.conf) vous n'aurez besoin ni de cette importation des CDR CSV, ni du cron décrit ci-dessous.

<?php

//*** process asterisk cdr file (Master.csv) insert usage 
// * values into a mysql database which is created for use
// * with the Asterisk_addons cdr_addon_mysql.so 
// * The script will only insert NEW records so it is safe 
// * to run on the same log over-and-over such as in the 
// * case where logs have not been rotated. 
// * 
// * Author: John Lange ( john.lange@open-it.ca ) 
// * Date: May 4, 2005. Updated July 21, 2005 
// * 
// * Here is what the script does: 
// * 
// * 1) Find the last log entry in the database cdr table. 
// * 2) scan the asterisk logs until the dates are larger than the last log entry (so we don't duplicate entries) 
// * 3) parse each row from the text log and insert it into the database. 
// *

 $locale_db_host  = 'localhost';

 $locale_db_name  = '.......'; //ajuster

 $locale_db_login = '.....';   //ajuster

 $locale_db_pass  = '.......';  //ajuster


if($argc == 2) {
    $logfile = $argv[1];
} else {
    print("Usage ".$argv[0]." <filename>\n");
    print("Where filename is the path to the Asterisk csv file to import (Master.csv)\n");
    print("This script is safe to run multiple times on a growing log file as it only imports records that are newer than the database\n");
    exit(0);
}

// connect to db
$link = mysql_connect($locale_db_host, $locale_db_login, $locale_db_pass);
if (!$link) {
   die('Could not connect: ' . mysql_error());
   }

//echo "Connected successfully\n";
mysql_select_db($locale_db_name, $link) or die("Could not select database $locale_db_name");

/** 1) Find the last log entry **/
// look in cdr table to see when the last entry was made.
// this establishes the starting point for the asterisk data.
$sql="SELECT UNIX_TIMESTAMP(calldate) as calldate".
    "  FROM cdr".
    " ORDER BY calldate DESC".
    " LIMIT 1";

if(!($result = mysql_query($sql, $link))) {
    print("Invalid query: " . mysql_error()."\n");
    print("SQL: $sql\n");
    die();
}
$result_array = mysql_fetch_array($result);
//$lasttimestamp = date("Y-m-d H:i:s", $result_array['voip_stamp']);
$lasttimestamp = $result_array['calldate'];

//** 2) Find new records in the asterisk log file. **

$rows = 0;
$handle = fopen($logfile, "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    // NOTE: the fields in Master.csv can vary. This should work by default on all installations but you may have to edit the next line to match your configuration
    list($accountcode,$src, $dst, $dcontext, $clid, $channel, $dstchannel, $lastapp, $lastdata, $start, $answer, $end, $duration,
     $billsec, $disposition, $amaflags ) = $data;

    // 3) parse each row and add to the database
    if(strtotime($end) > $lasttimestamp) { // we found a new record so add it to the DB
        $sql = "INSERT INTO cdr (calldate, clid, src, dst, userfield, channel, dstchannel, lastapp, lastdata, duration, billsec, disposition, amaflags, accountcode)
                   VALUES('$end', '".mysql_real_escape_string($clid)."', '$src', '$dst', '$dcontext', '$channel', '$dstchannel', '$lastapp', '$lastdata', '$duration', '$billsec',
                    '$disposition', '$amaflags', '$accountcode')";
        if(!($result2 = mysql_query($sql, $link))) {
            print("Invalid query: " . mysql_error()."\n");
            print("SQL: $sql\n");
            die();
        }
        $rows++;
    }
}
fclose($handle);

print("$rows imported\n");

?>

ASTUCES : pour récuperer tous les logs (y compris ceux déjà archivés), créer un fichier tous-les-log.csv (cat /var/log/asterisk/cdr-csv/Master.csv* >> tous-les-log.csv)

Lancer l'importation : php importcdr.php tous-les-log.csv

NB: il faudra créer, après, une tâche cron, pour lancer régulièrement l'importation

Pour utiliser l'application, taper http://votre-serveur/asterisk-stats, et voilà. NB : l'interface est en anglais. Il suffit de franciser soi-même.

Voici ce que ça donne à Montréal : http://voip.ca.auf.org/stats/?s=1, choisir un intervalle de date et cliquer sur "Search".

Asterisk/Stats (dernière édition le 2008-02-21 22:09:46 par localhost)