log2sql: added UPDATE support to exec_query

This commit is contained in:
Matt Simerson 2013-03-27 18:14:23 -04:00
parent 8b548e392d
commit 498016828e

View File

@ -120,8 +120,7 @@ sub create_message {
my ( $fid, $ts, $pid, $message ) = @_; my ( $fid, $ts, $pid, $message ) = @_;
my ($host, $ip) = split /\s/, $message; my ($host, $ip) = split /\s/, $message;
$ip = substr $ip, 1, -1; # remote brackets $ip = substr $ip, 1, -1; # remove brackets
#print "new from $ip\n";
my $id = exec_query( my $id = exec_query(
"INSERT INTO message SET file_id=?, connect_start=FROM_UNIXTIME(?), qp_pid=?, ip=INET_ATON(?)", "INSERT INTO message SET file_id=?, connect_start=FROM_UNIXTIME(?), qp_pid=?, ip=INET_ATON(?)",
@ -131,6 +130,7 @@ sub create_message {
if ( $host && $host ne 'Unknown' ) { if ( $host && $host ne 'Unknown' ) {
exec_query( "UPDATE message SET hostname=? WHERE id=?", [ $host, $id ] ); exec_query( "UPDATE message SET hostname=? WHERE id=?", [ $host, $id ] );
}; };
#warn "host updated: $host\n";
}; };
sub insert_plugin { sub insert_plugin {
@ -200,6 +200,7 @@ sub parse_logfile {
next; next;
}; };
#warn "type: $type\n";
if ( $type eq 'plugin' ) { if ( $type eq 'plugin' ) {
next if $plugin eq 'naughty'; # housekeeping only next if $plugin eq 'naughty'; # housekeeping only
insert_plugin( $msg_id, $plugin, $message ); insert_plugin( $msg_id, $plugin, $message );
@ -255,7 +256,7 @@ sub check_logfile {
my $size = stat($path)->size or die "unable to get size for $path\n"; my $size = stat($path)->size or die "unable to get size for $path\n";
my $exists; my $exists;
# check if this tai file is in the DB as 'current' #warn "check if file $file is in the DB as 'current'\n";
if ( $file =~ /^\@/ ) { if ( $file =~ /^\@/ ) {
$exists = exec_query( $exists = exec_query(
'SELECT * FROM log WHERE inode=? AND name=?', 'SELECT * FROM log WHERE inode=? AND name=?',
@ -277,7 +278,7 @@ sub check_logfile {
[ $inode, $file ] [ $inode, $file ]
); );
if ( @$exists ) { if ( @$exists ) {
$exists = exec_query( exec_query(
'UPDATE log SET size=? WHERE inode=? AND name=?', 'UPDATE log SET size=? WHERE inode=? AND name=?',
[ $size, $inode, 'current' ] [ $size, $inode, 'current' ]
); );
@ -522,6 +523,7 @@ sub exec_query {
$err .= join(',', @params); $err .= join(',', @params);
}; };
#warn "err: $err\n";
if ( $query =~ /INSERT INTO/ ) { if ( $query =~ /INSERT INTO/ ) {
my ( $table ) = $query =~ /INSERT INTO (\w+)\s/; my ( $table ) = $query =~ /INSERT INTO (\w+)\s/;
$db->query( $query, @params ); $db->query( $query, @params );
@ -529,8 +531,11 @@ sub exec_query {
my $id = $db->last_insert_id(undef,undef,$table,undef) or die $err; my $id = $db->last_insert_id(undef,undef,$table,undef) or die $err;
return $id; return $id;
} }
elsif ( $query =~ /^UPDATE/i ) {
return $db->query( $query, @params );
}
elsif ( $query =~ /DELETE/ ) { elsif ( $query =~ /DELETE/ ) {
$db->query( $query, @params )->hashes or die $err; $db->query( $query, @params ) or die $err;
return $db->query("SELECT ROW_COUNT()")->list; return $db->query("SELECT ROW_COUNT()")->list;
}; };