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

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

Questo sito usa Akismet per ridurre lo spam. Scopri come i tuoi dati vengono elaborati.