Hi List
Good morning.
I found the following perl script in the web that logs data from a
serial port to a log file.
I tested it on a PBX and it worked Ok - logging the the SMDR data to a
log file.
Can this script be extended so that it inserts the data to a MySQL table
instead of using a log file? Or better yet using both log file and MySQL.
I hope the perl gurus can help me on this one. Thanks in advance.
Sample SMDR data:
Date Time Ext CO Dial Number Ring Duration Acc code CD
--------------------------------------------------------------------------------
05/19/11 11:49AM 537 27 5411xxxxxxx 00:01'35
05/19/11 11:49AM 581 03 361xxx 00:00'45 TR
05/19/11 11:50AM 522 26 838xxxx 00:01'36
05/19/11 11:50AM 537 27 549xxxxxxx 00:01'05
05/19/11 11:51AM 589 33 0922xxxxxxx 00:00'14
05/19/11 11:51AM 500 24<D><I> 0'01 00:00'00
05/19/11 11:51AM 500 30<D><I> 0'01 00:00'00
05/19/11 11:51AM 509 24<D><I> 0'00 00:00'25
05/19/11 11:52AM 509 30<D><I> 0'00 00:00'13
05/19/11 11:52AM 500 30<D><I> 0'01 00:00'00
05/19/11 11:52AM 509 30<D><I> 0'00 00:00'09
05/19/11 11:52AM 514 17 0921xxxxxxx 00:05'00
The perl script:
#!/usr/bin/perl
use warnings;
#
#
# Author: Bruce S. Garlock
# Date: 2002-09-11
#
# Version: 0.1
#
#
# Description: This perl script is for logging of data from a serial
# port, to a specified logfile. The logfile can then be parsed with
# other programs for reporting purposes.
#
# This program was written for obecifically logging Multitech's
# MTASR2-203 T1 Router. The router outputs text to the command
# port with 57.6k, 8-1-N, and No flow control.
#
# The port can be setup below using ditty or stty. Please
# obecify this in the TTYPROG name variable.
#
#
use Device::SerialPort 0.12;
$LOGDIR = "/var/log"; # path to data file
$LOGFILE = "pbx.log"; # file name to output to
$PORT = "/dev/ttyS0"; # port to watch
#
#
# Serial Settings
#
#
$ob = Device::SerialPort->new ($PORT) || die "Can't Open $PORT: $!";
$ob->baudrate(19200) || die "failed setting baudrate";
$ob->parity("none") || die "failed setting parity";
$ob->databits(8) || die "failed setting databits";
$ob->handshake("none") || die "failed setting handshake";
$ob->write_settings || die "no settings";
#
# Send a string to the port
#
#
$pass=$ob->write("AT");
sleep 1;
#
# open the logfile, and Port
#
open(LOG,">>${LOGDIR}/${LOGFILE}")
||die "can't open smdr file $LOGDIR/$LOGFILE for append: $SUB $!\n";
open(DEV, "<$PORT")
|| die "Cannot open $PORT: $_";
select(LOG), $| = 1; # set nonbufferd mode
#
# Loop forver, logging data to the log file
#
#while($_ =<DEV>){ # print input device to file
# print LOG $_;
#}
while(1) {
($iCount, $sStr) = $ob->read(1);
if ($iCount>= 1) {
print LOG $sStr;
}
}
undef $ob;
_________________________________________________
Philippine Linux Users' Group (PLUG) Mailing List
http://lists.linux.org.ph/mailman/listinfo/plug
Searchable Archives: http://archives.free.net.ph