G'day most honorable Monks,

I am asking for assistance in my approach to coding a writer into a MySQL database. As you can no doubt see from my coding I am sure you all would consider me a novice Perl coder and I would appreciate any critiques about my approach.

The purpose of this code will be to sit on four or more servers where work is performed and therefore watch an active log from each server. I intend on running a File::Tail script to watch these logs and then process the entries into a single MySQL database, four tables. My concern is with the use of this script on four or more servers and the possible contention/locks on tables as different servers attempt to write their data. I am wondering are there ways to change my code to help reduce this situation or whether this is a valid concern in the first place. All these servers are a minimum of 4 processor/4G memory with very low loads.

The stored data will then be used as a central advance log search tool via a PHP interface.

Again, I welcome constructive criticism as it helps me learn and grow my skill with Perl.

Thank you in Advance,
Danny

p.s. I forgot to mention the code does function as it has been tested on my development system. But short-cuts are always welcomed.

#!/usr/local/bin/perl -w use strict; use DBI; use File::Tail; # Create File::Tail object my $file; $file=File::Tail->new("/opt/app/superspy/tmp/commLogger.log"); my $line; while (defined($line=$file->read)) { chomp($line); # print "$line\n"; # Create DB connection my $db_handle = DBI->connect("dbi:mysql:database=P0TA6E01;host +=myhost02.fcc.hide.com:3306;user=xxxxxxxx;password=xxxxxxxx") || die +"Couldn't connect to database: $DBI::errstr\n"; if($line =~ /^AlertCmd~.*~.*~-reload.*/){next;} if($line =~ /^AlertCmd~(.*)~(.*)~(.*)~(.*)~(.*)~(.*)/){ my $tstamp = $1; my $cmnd_id = $2; my $cmnd = $3; my $client_host = $4; my $client_user = $5; my $svr_host = $6; $svr_host =~ s/\..*//g; my $sql = "INSERT INTO ALERTS (TSTAMP,CMND_ID,CMND,CLI +ENTHOST,CLIENTUSER,SVR_HOST) VALUES (\'$tstamp\',\'$cmnd_id\',\'$cmnd +\',\'$client_host\',\'$client_user\',\'$svr_host\')\;\n"; my $statement = $db_handle->prepare($sql) or die "Couldn't prepare query '$sql': $DBI::errst +r\n"; $statement->execute() or die "Couldn't execute query '$sql': $DBI::errst +r\n"; } if($line =~ /^SendFiltered~(.*)~(.*)~(.*)~(.*)~(.*)~(.*)/){ my $tstamp = $1; my $cmnd_id = $2; my $alert_id = $3; my $send_id = $4; my $state = $5; my $svr_host = $6; $svr_host =~ s/\..*//g; my $sql = "INSERT INTO FILTERED (TSTAMP,CMND_ID,ALERT_ +ID,SEND_ID,STATE,SVR_HOST) VALUES (\'$tstamp\',\'$cmnd_id\',\'$alert_ +id\',\'$send_id\',\'$state\',\'$svr_host\')\;\n"; my $statement = $db_handle->prepare($sql) or die "Couldn't prepare query '$sql': $DBI::errst +r\n"; $statement->execute() or die "Couldn't execute query '$sql': $DBI::errst +r\n"; } if($line =~ /^SendOffDuty~(.*)~(.*)~(.*)~(.*)~(.*)~(.*)/){ my $tstamp = $1; my $cmnd_id = $2; my $alert_id = $3; my $send_id = $4; my $state = $5; my $svr_host = $6; $svr_host =~ s/\..*//g; my $sql = "INSERT INTO FILTERED (TSTAMP,CMND_ID,ALERT_ +ID,SEND_ID,STATE,SVR_HOST) VALUES (\'$tstamp\',\'$cmnd_id\',\'$alert_ +id\',\'$send_id\',\'$state\',\'$svr_host\')\;\n"; my $statement = $db_handle->prepare($sql) or die "Couldn't prepare query '$sql': $DBI::errst +r\n"; $statement->execute() or die "Couldn't execute query '$sql': $DBI::errst +r\n"; } if($line =~ /^SendStarted~(.*)~(.*)~(.*)~(.*)~(.*)~(.*)~(.*)~( +.*)~(.*)~(.*)~(.*)/){ my $tstamp = $1; my $cmnd_id = $2; my $alert_id = $3; my $send_id = $4; my $user = $5; my $destination = $6; my $pin = $7; my $to = $8; my $message = $9; my $state = $10; my $svr_host = $11; $svr_host =~ s/\..*//g; my $address; if($pin eq ""){$address = $to;}else{$address = $pin;} my $sql = "INSERT INTO SENDS (START_TIME,CMND_ID,ALERT +_ID,SEND_ID,USER,DESTINATION,ADDRESS,MESSAGE,STATE,SVR_HOST,ACTIVE) V +ALUES (\'$tstamp\',\'$cmnd_id\',\'$alert_id\',\'$send_id\',\'$user\', +\'$destination\',\'$address\',\'$message\',\'$state\',\'$svr_host\',\ +'TRUE\')\;\n"; my $statement = $db_handle->prepare($sql) or die "Couldn't prepare query '$sql': $DBI::errst +r\n"; $statement->execute() or die "Couldn't execute query '$sql': $DBI::errst +r\n"; } if($line =~ /^SendCompleted~(.*)~(.*)~(.*)~(.*)~(.*)~(.*)/){ my $tstamp = $1; my $cmnd_id = $2; my $alert_id = $3; my $send_id = $4; my $state = $5; my $svr_host = $6; $svr_host =~ s/\..*//g; my $sql = "UPDATE SENDS SET END_TIME=\'$tstamp\',STATE +=\'$state\',ACTIVE=\'FALSE\' WHERE CMND_ID=\'$cmnd_id\' AND ALERT_ID= +\'$alert_id\' AND SEND_ID=\'$send_id\' AND SVR_HOST=\'$svr_host\'\;\n +"; my $statement = $db_handle->prepare($sql) or die "Couldn't prepare query '$sql': $DBI::errst +r\n"; $statement->execute() or die "Couldn't execute query '$sql': $DBI::errst +r\n"; } if($line =~ /^SendCleared~(.*)~(.*)~(.*)~(.*)~(.*)~(.*)/){ my $tstamp = $1; my $cmnd_id = $2; my $alert_id = $3; my $send_id = $4; my $state = $5; my $svr_host = $6; $svr_host =~ s/\..*//g; my $sql = "UPDATE SENDS SET END_TIME=\'$tstamp\',STATE +=\'$state\',ACTIVE=\'FALSE\' WHERE CMND_ID=\'$cmnd_id\' AND ALERT_ID= +\'$alert_id\' AND SEND_ID=\'$send_id\' AND SVR_HOST=\'$svr_host\'\;\n +"; my $statement = $db_handle->prepare($sql) or die "Couldn't prepare query '$sql': $DBI::errst +r\n"; $statement->execute() or die "Couldn't execute query '$sql': $DBI::errst +r\n"; } if($line =~ /^AlertError~(.*)~(.*)~(.*)~(.*)~(.*)~(.*)~(.*)~(. +*)/){ my $tstamp = $1; my $cmnd_id = $2; my $alert_id = $3; my $send_id = $4; my $destination = $5; my $message = $6; my $state = $7; my $svr_host = $8; $svr_host =~ s/\..*//g; my $sql = "INSERT INTO ERRORS (TSTAMP,CMND_ID,ALERT_ID +,SEND_ID,DESTINATION,MESSAGE,STATE,SVR_HOST) VALUES (\'$tstamp\',\'$c +mnd_id\',\'$alert_id\',\'$send_id\',\'$destination\',\'$message\',\'$ +state\',\'$svr_host\')\;\n"; my $statement = $db_handle->prepare($sql) or die "Couldn't prepare query '$sql': $DBI::errst +r\n"; $statement->execute() or die "Couldn't execute query '$sql': $DBI::errst +r\n"; } $statement->finish(); $db_handle->disconnect(); }

In reply to Need Critique (Good or Bad) by onegative

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.