#!/usr/bin/perl # # # Author: Bruce S. Garlock # Modified heavily by: Jima # Date: 2006-10-12 # Requirements: Device::SerialPort 1.000002 (from cpan) # # Version: 0.6.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. # # * Thu Oct 12 2006 Patrick "Jima" Laughton 0.6.1 # - Another cleanup, this time for initial general release # - Needs more work # # * Fri Sep 15 2006 Patrick "Jima" Laughton 0.6 # - Changed serial port to /dev/ttyS1 # - I really need to generalize this code to make it a serial logger # # * Tue Jul 12 2005 Patrick "Jima" Laughton 0.5 # - Changed serial port to /dev/ttyS0 # # * Wed Jun 16 2004 Patrick "Jima" Laughton 0.4 # - Cleanup # - Added pidfile support # - Totally daemonized # # * Wed Jun 16 2004 Patrick "Jima" Laughton 0.3 # - Outputs to syslog as $logfac.$loglev # - Created ChangeLog # # * Wed Jun 16 2004 Patrick "Jima" Laughton 0.2 # - Adapted to 9600 8n1 # # * Wed Sep 11 2002 Bruce Garlock 0.1 # - Original version # - Written for logging Multitech's MTASR2-203 T1 Router # - Router outputs text to the command port with 57.6k, 8-1-N, and No flow # control use Device::SerialPort 1.000002; use Sys::Syslog qw(:DEFAULT setlogsock); $logfac = "local4"; $loglev = "info"; $PORT = "/dev/ttyS1"; # port to watch $pidfile = "/var/run/callerid.pid"; $0="[callerid]"; # # # Serial Settings # # $ob = Device::SerialPort->new ($PORT) || die "Can't Open $PORT: $!"; $ob->baudrate(9600) || 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 syslog link, and Port # setlogsock('unix'); openlog('cid', 'cons,ndelay', $logfac); open(DEV, "<$PORT") || die "Cannot open $PORT: $_"; # daemonize &daemonize; # # Loop forver, writing data to syslog # while($_ = ){ # print input device to file chomp($_); syslog($loglev, $_); } undef $ob; sub daemonize { chdir '/' or die "Can't chdir to /: $!"; open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!"; open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!"; defined(my $pid = fork) or die "Can't fork: $!"; if($pid) { open PIDFILE, ">$pidfile" or die "Can't open pidfile: $!"; print PIDFILE $pid; close PIDFILE; exit; } setsid or die "Can't start a new session: $!"; umask 0; }