in reply to Need Critique (Good or Bad)
#!/usr/local/bin/perl -w use strict; use DBI; use File::Tail; # query subs, fetch and name the parameters and call the prepared stat +ement sub insert_alerts { my ($sth, $tstamp, $cmnd_id, $cmnd , $client_host, $client_user, $ +svr_host, $svr_host) = @_; $svr_host =~ s/\..*//g; $sth->execute( $tstamp, $cmnd_id, $cmnd , $client_host, $client_us +er, $svr_host, $svr_host ); return $DBI::errstr ? 0 : 1; } sub insert_filters { my ($sth, $tstamp, $cmnd_id, $cmnd , $client_host, $client_user, $ +svr_host, $svr_host) = @_; $svr_host =~ s/\..*//g; $sth->execute( $tstamp, $cmnd_id, $cmnd , $client_host, $client_us +er, $svr_host, $svr_host ); return $DBI::errstr ? 0 : 1; } sub insert_sends { my ($sth, $tstamp, $cmnd_id, $alert_id, $send_id, $user, $destinat +ion, $pin, $to, $message, $state, $svr_host) = @_; $svr_host =~ s/\..*//g; $sth->execute( $tstamp, $cmnd_id, $alert_id, $send_id, $user, $des +tination, $pin||$to, $message, $state, $svr_host, 'TRUE' ); return $DBI::errstr ? 0 : 1; } sub update_sends { my ($sth, $tstamp, $cmnd_id, $alert_id, $send_id, $state, $svr_hos +t = $6) = @_; $svr_host =~ s/\..*//g; $sth->execute( $tstamp, $state, $cmnd_id, $alert_id, $send_it, $sv +r_host ); return $DBI::errstr ? 0 : 1; } sub insert_errors { my ($sth, $tstamp, $cmnd_id, $cmnd , $client_host, $client_user, $ +svr_host, $svr_host) = @_; $svr_host =~ s/\..*//g; $sth->execute( $tstamp, $cmnd_id, $cmnd , $client_host, $client_us +er, $svr_host, $svr_host ); return $DBI::errstr ? 0 : 1; } # Create File::Tail object my $file = File::Tail->new("/opt/app/superspy/tmp/commLogger.log"); # Create DB connection my $dbh = DBI->connect("dbi:mysql:database=P0TA6E01;host=myhost02.fcc. +hide.com:3306;user=xxxxxxxx;password=xxxxxxxx") || die "Couldn't conn +ect to database: $DBI::errstr\n"; # dispatch table and prepared statenents for the logfile entries # this whill be faster than preparing the query for each entry my %dispatch = ( AlertCmd => [ \&insert_alerts, $dbh->prepare('INSERT INTO AL +ERTS (TSTAMP,CMND_ID,CMND,CLIENTHOST,CLIENTUSER,SVR_HOST) VALUES (?,? +,?,?,?,?);' ], SendFiltered => [ \&insert_filters, $dbh->prepare('INSERT INTO FI +LTERS (TSTAMP,CMND_ID,CMND,CLIENTHOST,CLIENTUSER,SVR_HOST) VALUES (?, +?,?,?,?,?);' ], SendOffDuty => [ \&insert_filters, $dbh->prepare('INSERT INTO FI +LTERS (TSTAMP,CMND_ID,CMND,CLIENTHOST,CLIENTUSER,SVR_HOST) VALUES (?, +?,?,?,?,?);' ], SendStarted => [ \&insert_sends, $dbh->prepare('INSERT INTO SE +NDS (START_TIME,CMND_ID,ALERT_ID,SEND_ID,USER,DESTINATION,ADDRESS,MES +SAGE,STATE,SVR_HOST,ACTIVE) VALUES (?,?,?,?,?,?,?,?,?,?,?)' ], SendCompleted => [ \&update_sends, $dbh->prepare('UPDATE SENDS S +ET END_TIME=?, STATE=?,ACTIVE=FALSE WHERE CMND_ID=? AND ALERT_ID=? AN +D SEND_ID=? AND SVR_HOST=?;' ], SendCleared => [ \&update_sends, $dbh->prepare('UPDATE SENDS S +ET END_TIME=?, STATE=?,ACTIVE=FALSE WHERE CMND_ID=? AND ALERT_ID=? AN +D SEND_ID=? AND SVR_HOST=?;' ], AlertError => [ \&insert_errors, $dbh->prepare('INSERT INTO ER +RORS (TSTAMP,CMND_ID,ALERT_ID,SEND_ID,DESTINATION,MESSAGE,STATE,SVR_H +OST) VALUES (?,?,?,?,?,?,?,?)' ], ); # read linewise while (my $line = $file->read)) { chomp($line); # skipt reload next if $line =~ /^AlertCmd~.*~.*~-reload.*/; # check for interesting entries, save keyword and the rest in $1 a +nd $2 if ( $line =~ /^(AlertCmd|SendFiltered|SendOffDuty|SendStarted|Sen +dCompleted|SendCleared|AlertError)~(.+)/ ) { # the callback and the prepared statement from the dispatch ta +ble that are appropriate for the entry my ($function, $sth) = @{ $dispatch->{$1} }; # call the function and show error if something went wrong unless ( $function->( $sth, split /~/, $2 ) ) { print "Error ($DBI::errstr) while processing this line:\n$ +line\n\n"; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Need Critique (Good or Bad)
by onegative (Scribe) on Feb 07, 2008 at 13:37 UTC |