Find the answer to your Linux question:
Results 1 to 6 of 6
Helllo, I am trying to setup a squid logfile analysis tool, 'squidalyser'/. It has a perl script, that makes enries into a mysql database. I have made a user squidalyser, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2003
    Posts
    70

    perl script not able to make a connection with mysql


    Helllo,
    I am trying to setup a squid logfile analysis tool, 'squidalyser'/. It has a perl script, that makes enries into a mysql database. I have made a user squidalyser, with appropoiate permissions and password, and also created the required database, as was instructed in the readme file. when i execute the sqidparse.pl file, which connects to the database, it gives me the error:
    Running ./squidparse.pl at Sun Sep 12 23:47:11 2004

    DB Name: squid
    DB Host: localhost
    DB User: squidalyser
    Squidlog: /var/log/squid/access.log
    DBI connect('squid:localhost','squidalyser',...) failed: Access denied for user: 'squidalyser@localhost' (Using password: YES) at ./squidparse.pl line 148
    Access denied for user: 'squidalyser@localhost' (Using password: YES) at ./squidparse.pl line 148.
    what could b this problem?
    Thanks in advance...

  2. #2
    Just Joined!
    Join Date
    Dec 2003
    Posts
    70

    one more thing

    Guys,
    I forgot to mention one more thing....
    when i manually connect to the database, i get connectd...i.e.
    [swaroop@localhost squidparse]$ mysql squid -u squidalyser -p
    Enter password:
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A

    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 18 to server version: 3.23.58

    Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

    mysql>

  3. #3
    Linux Guru sarumont's Avatar
    Join Date
    Apr 2003
    Location
    /dev/urandom
    Posts
    3,682
    Do you at any point have to put in the password?
    "Time is an illusion. Lunchtime, doubly so."
    ~Douglas Adams, The Hitchhiker's Guide to the Galaxy

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Dec 2003
    Posts
    70
    yes i have given the password... i have also stored in the squidalyser.conf file

  6. #5
    Just Joined!
    Join Date
    Dec 2003
    Posts
    70
    i am displaying the contents of the squidparse.pl ....
    #!/usr/bin/perl

    use strict;

    use DBI;
    use Time::ParseDate;

    my $t0 = time;
    my (%conf, $dbh);

    open (CONF, "< /usr/local/squidparse/squidalyser.conf") or die $!;
    while (my $line = <CONF>) {
    chomp($line);
    $line =~ /^\#/ and next;
    $line =~ /^$/ and next;
    my ($varname, $varvalue) = split(/\s+/, $line);
    $conf{$varname} = $varvalue;
    }
    close CONF;

    my $now = localtime();
    print qq|
    Running $0 at $now

    DB Name: $conf{dbname}
    DB Host: $conf{dbhost}
    DB User: $conf{dbuser}
    Squidlog: $conf{squidlog}
    |;

    dbconnect($conf{dbhost}, $conf{dbname}, $conf{dbuser}, $conf{dbpass});

    my $query = 'SELECT MAX(time) FROM logfile';
    my $sth = $dbh->prepare($query);
    $sth->execute;
    my $lastrun = $sth->fetchrow;
    $sth->finish;

    $query = qq|
    INSERT INTO logfile (remotehost,rfc931,authuser,request,status,bytes,t ime)
    VALUES ( ? , ? , ? , ? , ?, ? , ? )
    |;
    $sth = $dbh->prepare($query);

    my $count;
    open (LOG, "< $conf{squidlog}") or die $!;
    while (my $line = <LOG>) {
    my $seconds;
    if ($line =~ /\s+\[(\d{2,2}.*?)\]\s+/) {
    $seconds = parsedate($1);
    } else {
    $line =~ /^(\d{10,10})\.\d{3,3}/;
    $seconds = $1;
    }
    if ($seconds >= $lastrun) {
    my ($remhost, $rfc931, $date, $request, $status, $bytes) = parseline($line);
    insertinlog($remhost, $rfc931, $date, $request, $status, $bytes, $line);
    $lastrun = $seconds;
    $count++;
    }
    }

    &expire_db;

    &dbdisconnect;
    my $t1 = time;
    my $elapsed = $t1 - $t0;
    print qq|Took $elapsed seconds to process $count records.\n|;

    exit 0;

    sub insertinlog {
    my ($remhost, $rfc931, $date, $request, $status, $bytes, $line) = @_;
    $bytes = 0 unless ($bytes =~ /\d+/);
    $status = 0 unless ($status =~ /\d+/);
    $sth->execute($remhost,$rfc931,'-',$request,$status,$bytes, $date) or do {
    print $line;
    print $query;
    die $!;
    };
    $sth->finish;
    return;
    }

    sub parseline {
    my $line = shift or return;
    my @f = split(/\s+/, $line);
    if ($line =~ /^(\d{10,}\.\d{3,3})/) {
    # squid logfile format
    my $sec = $1;
    my $sta = $1 if ($line =~ /TCP.*?\/(\d{3,3})/);

    # If we're using NTLM authentication, we have username in DOMAIN\User format,
    # so split domain and user (authuser) at \ and store username back in $f[7].
    # Patch submitted by Neil M. and hacked a bit by me I don't use NTLM so if
    # you spot an error with this one let me know.
    if ($f[7] =~ /\\/) {
    my ($domain, $user) = split(/\\/,$f[7]);
    if ($conf{include_domain} eq 'y') {
    $f[7] = qq|$domain:$user|;
    } else {
    $f[7] = $user;
    }
    }

    return ($f[2], $f[7], $sec, $f[6], $sta, $f[4]);

    } else {
    # http logfile format
    my $date = "$f[3] $f[4]";
    $date =~ s/\[|\]//g;
    my $sec = parsedate($date);
    $f[6] =~ s/"//g;
    print $f[0];
    print $f[1];

    return ($f[0], $f[1], $sec, $f[6], $f[8], $f[9]);
    }
    }

    sub expire_db {
    my ($exp_val, $exp_unit) = split(/\_/, $conf{expire});
    unless ($exp_val > 0) {
    print qq|Error in config file: `expire $conf{expire}'.\nNo records expired from database.|;
    return;
    }
    if ($exp_unit eq 'h') {
    $conf{factor} = 3600;
    } elsif ($exp_unit eq 'd') {
    $conf{factor} = 86400
    } elsif ($exp_unit eq 'w') {
    $conf{factor} = 604800;
    }
    my $nowsec = parsedate($now);
    my $expire_before = $nowsec - ($conf{factor} * $exp_val);
    my $query = qq|DELETE FROM logfile WHERE time < $expire_before|;
    my $sth = $dbh->prepare($query);
    $sth->execute or do {
    print $query;
    die $!;
    };
    my $rows = $sth->rows;
    print qq|\nExpired $rows records from the database.\n|;
    }

    sub dbconnect {
    my ($dbhost, $dbname, $dbuser, $dbpass) = @_;
    $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost",$dbuser,$dbpa ss) or die (DBI::errstr);
    }

    sub dbdisconnect {
    $sth and $sth->finish;
    ($dbh and $dbh->disconnect) or die (DBI::errstr);
    }

  7. #6
    Just Joined!
    Join Date
    Dec 2003
    Posts
    70

    problem solved....

    Hey guys,
    It was my fault... I had not set proper access pemissions for the squidalyser.conf file.... Sorry guys, still making newbie mistakes....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •