Personalizzare Asterisk Web Manager con i Log Custom di Asterisk

Dopo aver visto come automatizzare il logratate dei file di log di Asterisk, passiamo alla visualizzazione dei file di Log ( cdr-custom)  Custom,  che Asterisk ci mette a disposizione, tramite l’interfaccia web di Asterisk WEB/PHP Management Interface.

Step I

Per prima cosa dobbiamo abilitare il Mappings del custom config file editando il seguente file: /etc/asterisk/cdr_custom.conf

il quale creera’ il file /var/log/asterisk/cdr-custom/Master.cvs con i campi che a noi interessano.

cdr_custom.conf

[sourcecode language=”bash”]

unicondor@server:~$ cat /etc/asterisk/cdr_custom.conf
;
; Mappings for custom config file
;
; To get your CSV output in a format tailored to your liking, uncomment the
; following lines and look for the output in the cdr-custom directory (usually
; in /var/log/asterisk). Depending on which mapping you uncomment, you may see
; Master.csv, Simple.csv, or both.
;
[mappings]
Master.csv =>${CSV_QUOTE(${CDR(src)})},${CSV_QUOTE(${CDR(dst)})},${CSV_QUOTE(${CDR(duration)})},${CSV_QUOTE(${CDR(disposition)})},${CDR(sequence)},${CVS_QUOTE(${CDR(start)})}

[/sourcecode]

La modifica del file riporato il alto servirà a creare un file di log customizzato per le nostre neccessità. Il path ( installazione di default di Asterisk ) dove potete trovare il file è il seguente:

/var/log/asterisk/cdr-custom/Master.csv

Al seguente indirizzo potete trovare le info riguardanti i campi che ho usato per creare il mio file di Log Custom
Ora non ci resta altro che effettuare un bel reload dalla console di Asterisk per fargli vedere le modifiche

[sourcecode language=”bash”]

server*CLI> realod

[/sourcecode]

Step II

Fatto questo, andiamo a modificare il file logs.php che si trova nella directory web del WEB/PHP Management Interface, aggiungendo la voce che riguarda il file di log appena creato

[sourcecode language=”php”]
[rot@server asterisk]# cat /var/www/html/asterisk/logs.php
<?php <br ?>
if (isset($_POST[‘radio’])) {
if ($_POST[‘radio’] == "cdrformat") {
if (isset($_POST[‘pattern’])) $pattern = $_POST[‘pattern’];
header("Location: cdr.php?pattern=$pattern");
// header("Location: http://$_SERVER/asterisk/cdr.php?pattern=$pattern");
}
}

if (isset($_POST[‘radio’])) {
if ($_POST[‘radio’] == "cdrformat_custom") {
if (isset($_POST[‘pattern’])) $pattern = $_POST[‘pattern’];
header("Location: cdr_custom.php?pattern=$pattern");
// header("Location: http://$_SERVER/asterisk/cdr.php?pattern=$pattern");
}
}

include(‘header.php’);
include(‘files.php’);
include(‘functions.php’);

if (isset($_POST[‘submit’])) {
$submit = $_POST[‘submit’];
if (isset($_POST[‘loglines’])) $loglines = $_POST[‘loglines’];
else $loglines = 0;
if (isset($_POST[‘pattern’])) $pattern = $_POST[‘pattern’];
else $pattern = "";
if (isset($_POST[‘radio’])) $radio = $_POST[‘radio’];
if ($submit =="SUBMIT") {
if ($pattern != "") $cmd = "/bin/grep $pattern ";
else {
if ($loglines == 0) $cmd = "/bin/cat ";
else $cmd = "/usr/bin/tail -$loglines ";
}
if ($radio == "syslog") $cmd .= $SYSTEMMESSAGES;
if ($radio == "cdr") $cmd .= $CDRMASTER;
if ($radio == "events") $cmd .= $ASTEVENTLOG;
if ($radio == "messages") $cmd .= $ASTMESSAGESLOG;
if ($radio == "queue") $cmd .= $ASTQUEUELOG;
echo "
";
mysystem($cmd);
echo "
";
echo "
";
echo $cmd;
echo "
";
echo "Done !";
}
}
else
{
?></pre>
<div align="center"><form action="<?php echo $_SERVER[‘PHP_SELF’]; ?><br />" method="post">
<table>
<tbody>
<tr>
<td>Search String <input type="text" name="pattern" /></td>
<td>
Lines to List [0=All] <input type="text" name="loglines" value="50" /></td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td><input type="radio" name="radio" value="events" /> Asterisk Event Log</td>
<td> </td>
</tr>
<tr>
<td><input type="radio" name="radio" value="messages" /> Asterisk Message Log</td>
<td> </td>
</tr>
<tr>
<td><input type="radio" name="radio" value="queue" /> Asterisk Queue Log</td>
<td> </td>
</tr>
<tr>
<td><input type="radio" name="radio" value="cdr" /> Asterisk CDR Log</td>
<td> </td>
</tr>
<tr>
<td><input type="radio" name="radio" value="cdrformat" /> Asterisk CDR Log [Table Formatted]</td>
<td> </td>
</tr>
<tr>
<td><input type="radio" name="radio" value="cdrformat_custom" checked="checked" /> Asterisk CDR ***Custom*** Log [Table Formatted]</td>
<td> </td>
</tr>
<tr>
<td><input type="radio" name="radio" value="syslog" /> System Log [SYSLOG]</td>
<td> </td>
</tr>
</tbody>
</table>
<input type="submit" name="submit" value="SUBMIT" />

<input type="reset" name="reset" value="RESET" /></form></div>
<pre>
<?php } ?>

[/sourcecode]

Ora non ci resta altro che andare a creare la pagina php cdr_custom.php , responsabile della lettura del file log custom di Asterisk e la sua stampa in una tabella html

[sourcecode language=”php”]

[unicondor@server asterisk]$ cat /var/www/html/asterisk/cdr_custom.php
<?PHP <br ?>/* Modified by Flavio Pace
*/
include("header.php");
include("files.php");

//Fields of the CDR in Asterisk
//—————————–
//
// 1. accountcode: What account number to use, (string, 20 characters)
// 2. src: Caller*ID number (string, 80 characters)
// 3. dst: Destination extension (string, 80 characters)
// 4. dcontext: Destination context (string, 80 characters)
// 5. clid: Caller*ID with text (80 characters)
// 6. channel: Channel used (80 characters)
// 7. dstchannel: Destination channel if appropriate (80 characters)
// 8. lastapp: Last application if appropriate (80 characters)
// 9. lastdata: Last application data (arguments) (80 characters)
// 10. start: Start of call (date/time)
// 11. answer: Answer of call (date/time)
// 12. end: End of call (date/time)
// 13. duration: Total time in system, in seconds (integer), from dial to hangup
// 14. billsec: Total time call is up, in seconds (integer), from answer to hangup
// 15. disposition: What happened to the call: ANSWERED, NO ANSWER, BUSY
// 16. amaflags: What flags to use: DOCUMENTATION, BILL, IGNORE etc,
// specified on a per channel basis like accountcode.
// 17. user field: A user-defined field, maximum 255 characters

if (isset($_GET[‘pattern’])) $pattern = $_GET[‘pattern’];
else $pattern = "";

$fname = $CDRCUSTOM;
$fd = fopen ($fname, "r");
if (!$fd) {
echo "Error opening $fname";
exit(0);
}
echo "</pre>
<table border="1" align="center">";echo "
<tbody>
<tr>";echo "
<th>Src</th>
";echo "
<th>Dst</th>
";echo "
<th>Duration</th>
";echo "
<th>Disposition</th>
";echo "
<th>User Field</th>
";echo "
<th>Start</th>
";echo "</tr>
";$limit = 0;$d = 0;while (!feof ($fd)) { $buffer = fgets($fd, 4096); $l = trim($buffer); if ($pattern != "") { if (!strstr($l,$pattern)) continue; } $badcommapat = ‘/\"[^\"]+,[^\",]+\"/’; if (preg_match($badcommapat,$l,$matches)) { $fixcomma = str_replace(",","-",$matches[0]); $l = str_replace($matches[0],$fixcomma,$l); } $e = explode(",",$l); $len = sizeof($e); echo "
<tr>"; for ($c=0;$c echo "
<td nowrap="nowrap">";
$e[$c] = trim($e[$c],"\r\n \"");
if ($c == 4) $e[$c] = str_replace ("\"", "", $e[$c]);
if ($e[$c] == "") echo " ";
else echo htmlspecialchars($e[$c]);
echo "</td>
"; }while ($c < 6) { echo "
<td> </td>
"; ++$c; } echo "</tr>
\n"; flush(); ++$d; if ($limit != 0) { if ($d >= $limit) break; }}echo "</tbody>
</table>
<pre>
";
fclose ($fd);
?>

[/sourcecode]

Alcuni Screenshot dell’interfaccia grafica customizzata

Asterisk: Automatizzare il rotate dei file di Log

Dopo qualche settimana d’uso di Asterisk, mi sono reso conto che i file di log non vengono gestiti automaticamente da Logrotate, e questo puo’ crearci qualche difficolta’ nel debugging , soprattutto quando tali file iniziano a diventare molto grossi.

Se siamo sotto Linux, possiamo sfruttare la potenza del demone Logrotate per effettuare il “rotate” in automatico dei file senza scrivere complicati script.

Questo e’ il file di esempio sul mio server Asterisk
[sourcecode language=”bash”]

/etc/logrotate.d/asterisk
[/sourcecode]

[sourcecode language=”bash”]
/var/log/asterisk/messages {
missingok
rotate 5
weekly
postrotate
/usr/sbin/asterisk -rx ‘logger reload’ > /dev/null 2  /dev/null
endscript
}

/var/log/asterisk/queue_log {
missingok
rotate 5
size=100k
postrotate
/usr/sbin/asterisk -rx ‘logger reload’> /dev/null 2> /dev/null
endscript
}

/var/log/asterisk/cdr-csv/*csv {
missingok
rotate 5
weekly
}
[/sourcecode]

Con queste poche linee , dico a Logrotate di ruotare i file : /var/log/asterisk/cdr-csv/*csv e /var/log/asterisk/messages,  in automatico ogni settimana ( weekly ), mentre il file /var/log/asterisk/queue_log verra’ ruotato appena raggiunge la grandezza di 100k (size=100k)

La direttiva rotate=5 indica il numero massimo di log che il demone gestira’. Ex. messages.1: messages.2 ecc..

 

VoIP Asterisk: Eseguire Script Bash Chiamate in Ingresso/Uscita

 

Da qualche giorno ho iniziato ad interessarmi al VoIP, devo dire che e’ una tecnologia che ho sempre sottovalutato e che meriterà molta attenzione nel mio prossimo futuro, dato che ho passato il numero geografico che avevo con Telecom sul VoIP con tutte le comodità’ e le funzionalità’ ad esso connesse ( nel prossimo articolo entrerò’ più nel dettaglio su questa tecnologia) .

Lo scopo di questo piccolo articolo e’ quello di vedere come poter lanciare/eseguire in automatico uno script in bash ( funziona con qualsiasi altro eseguibile Unix-Like ) alla ricezione o alla partenza di una chiamata su Asterisk.

la funzione che ci mette a disposizione Asterisk per l’esecuzione di uno script (Linux command) e’ System() .

Vediamo subito un esempio preso dal mio extensions.conf

[sourcecode language=”bash”]

exten =› 1234,2,Verbose(Incoming Call From Messagenet)
exten =› 1234,1,System(/home/unicondor/start_incoming_call.sh)
exten =› 1234,n,Wait(3)
exten =› 1234,n,Answer
exten =› 1234,n,DIAL(${CASA},45)
exten =› 1234,n,System(/home/unicondor/stop_incoming_call.sh)
exten =› 1234,n,Hangup

[/sourcecode]

 

[sourcecode language=”bash”]

#!/bin/sh

/home/unicondor/set_limit_mldonkey.sh 1

exit 0

[/sourcecode]

 

L’unica accortezza per la corretta esecuzione di start_incoming_call.sh da parte di Asterisk e’ di aggiungere exit 0 alla fine dello script in bash, altrimenti Asterisk non riuscendo a capire lo stato con cui e’ terminato il vostro script non lo eseguirà’.