#!/usr/local/bin/perl -w use strict; use DBI; use File::Tail; # query subs, fetch and name the parameters and call the prepared statement 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_user, $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_user, $svr_host, $svr_host ); return $DBI::errstr ? 0 : 1; } sub insert_sends { my ($sth, $tstamp, $cmnd_id, $alert_id, $send_id, $user, $destination, $pin, $to, $message, $state, $svr_host) = @_; $svr_host =~ s/\..*//g; $sth->execute( $tstamp, $cmnd_id, $alert_id, $send_id, $user, $destination, $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_host = $6) = @_; $svr_host =~ s/\..*//g; $sth->execute( $tstamp, $state, $cmnd_id, $alert_id, $send_it, $svr_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_user, $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 connect 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 ALERTS (TSTAMP,CMND_ID,CMND,CLIENTHOST,CLIENTUSER,SVR_HOST) VALUES (?,?,?,?,?,?);' ], SendFiltered => [ \&insert_filters, $dbh->prepare('INSERT INTO FILTERS (TSTAMP,CMND_ID,CMND,CLIENTHOST,CLIENTUSER,SVR_HOST) VALUES (?,?,?,?,?,?);' ], SendOffDuty => [ \&insert_filters, $dbh->prepare('INSERT INTO FILTERS (TSTAMP,CMND_ID,CMND,CLIENTHOST,CLIENTUSER,SVR_HOST) VALUES (?,?,?,?,?,?);' ], SendStarted => [ \&insert_sends, $dbh->prepare('INSERT INTO SENDS (START_TIME,CMND_ID,ALERT_ID,SEND_ID,USER,DESTINATION,ADDRESS,MESSAGE,STATE,SVR_HOST,ACTIVE) VALUES (?,?,?,?,?,?,?,?,?,?,?)' ], SendCompleted => [ \&update_sends, $dbh->prepare('UPDATE SENDS SET END_TIME=?, STATE=?,ACTIVE=FALSE WHERE CMND_ID=? AND ALERT_ID=? AND SEND_ID=? AND SVR_HOST=?;' ], SendCleared => [ \&update_sends, $dbh->prepare('UPDATE SENDS SET END_TIME=?, STATE=?,ACTIVE=FALSE WHERE CMND_ID=? AND ALERT_ID=? AND SEND_ID=? AND SVR_HOST=?;' ], AlertError => [ \&insert_errors, $dbh->prepare('INSERT INTO ERRORS (TSTAMP,CMND_ID,ALERT_ID,SEND_ID,DESTINATION,MESSAGE,STATE,SVR_HOST) 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 and $2 if ( $line =~ /^(AlertCmd|SendFiltered|SendOffDuty|SendStarted|SendCompleted|SendCleared|AlertError)~(.+)/ ) { # the callback and the prepared statement from the dispatch table 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"; } } }