#!/usr/bin/perl use DBI; $|++; use vars qw/ $query %cache %dates $res $sth /; my ($DATABASE) = ""; my ($HOSTNAME) = undef; my ($USER) = ""; my ($PASSWORD) = ""; # Setup the DB connection. my $dsn = "DBI:mysql:database=$DATABASE"; if ( defined $HOSTNAME ) { $dsn .= ";host=$HOSTNAME"; } my $dbh = DBI->connect( $dsn, $USER, $PASSWORD, { RaiseError => 0, PrintError => 0 } ); if ( !defined($dbh) ) { die "DBI->connect failed: $DBI::errstr"; } my $table = "reported"; if ( @ARGV ) { $table = shift @ARGV; die "Illegal table name!\n" if ( $table !~ /^(syslog|reported)$/ ); } if ( $table ne "reported" ) { $query = "INSERT INTO $table (date,value) VALUES(?,?)"; } else { $query = "INSERT INTO $table (date,value,rules) VALUES(?,?,?)"; } while( defined($_=<>) && chomp ) { my($d,@v) = split(/\t/,$_); runsql( $dbh, $query, $d, @v ); } # Clean up the sth cache endsql(); # Disconnect and exit. $dbh->disconnect(); exit 0; { my %queries = (); sub runsql ($$@) { my ( $dbh, $sql, @bind_values ) = @_; if ( !defined( $queries{$sql} ) ) { $queries{$sql} = $dbh->prepare($sql); } my $sth = $queries{$sql}; if ( !$sth ) { die "Error:" . $dbh->errstr . "\n"; } my $res = $sth->execute(@bind_values); return ( $res, $sth ); } sub endsql () { while ( my ( $k, $v ) = each %queries ) { $v->finish; } } }