N
N.Asenjo
Hi,
From a central syslog server logging to a mysql database I want to get
only the errors with a perl script. The format looks like this:
mysql> select host,priority,datetime,msg from logs limit 1;
+------+----------+---------------------+-------------------------------
| host | priority | datetime | msg
|
+------+----------+---------------------+------------------------------+
| tux | info | 2006-01-29 14:13:01 | /USR/SBIN/CRON[24045]: (root)
CMD (/sbin/adslscript) |
+------+----------+---------------------+------------------------------+
I want to get today's error messages, so the mysql query looks like
this:
mysql> select host,priority,datetime,msg from logs where priority in
('err','crit','alert') and datetime like '2006-02-10%' limit 2;
And that works. My problem then:
---------code---------
#!/usr/bin/perl
use warnings;
use strict;
use DBI;
# $date = system(`date +%Y-%m-%d`);
# my $date = "2006-02-09";
my @data;
my $date = system("/bin/date +%Y-%m-%d");
# Tried all these but do not work either
# my $date = `date +%Y-%m-%d"`
# chomp $datetime;
# my $date = system("/bin/date +%Y-%m-%d");
# my $date = system(q{"/bin/date +%Y-%m-%d"},);
# @args = ( "date +%Y-%m-%d" );
# $date = exec { $args[0] } @args;
my $query = "SELECT host,priority,datetime,msg FROM logs where datetime
like '$date%' and priority in ('err','crit','alert')";
my $dbh = DBI->connect('dbi:mysql:syslog', 'user', 'passwd')
or die DBI->errstr;
my $sth = $dbh->prepare($query) or die $sth->errstr;
$sth->execute() or die $sth->errstr;
# read the matching records and print them out
while (@data = $sth->fetchrow_array()) {
my $host = $data[0];
my $priority = $data[1];
my $datetime = $data[2];
my $msg = $data[3];
print "$host $datetime $priority $msg\n";
}
$sth->finish;
$dbh->disconnect;
-----------------end code
and when i run it:
perl datetime.pl
Name "main::sth" used only once: possible typo at datetime.pl line 23.
2006-02-10
and there I am, back to my shell.
If I hardcode
$date = "2006-02-10";
Then it interpolates and runs fine. This is useless because I want to
use
the script in a cronjob. This is a sysadmin script, will not be run
thousands of times in a cgi, so no danger of cross-side scripting or
anything like that.
Anyway, I am stuck. Any help is greatly appreciated.
From a central syslog server logging to a mysql database I want to get
only the errors with a perl script. The format looks like this:
mysql> select host,priority,datetime,msg from logs limit 1;
+------+----------+---------------------+-------------------------------
| host | priority | datetime | msg
|
+------+----------+---------------------+------------------------------+
| tux | info | 2006-01-29 14:13:01 | /USR/SBIN/CRON[24045]: (root)
CMD (/sbin/adslscript) |
+------+----------+---------------------+------------------------------+
I want to get today's error messages, so the mysql query looks like
this:
mysql> select host,priority,datetime,msg from logs where priority in
('err','crit','alert') and datetime like '2006-02-10%' limit 2;
And that works. My problem then:
---------code---------
#!/usr/bin/perl
use warnings;
use strict;
use DBI;
# $date = system(`date +%Y-%m-%d`);
# my $date = "2006-02-09";
my @data;
my $date = system("/bin/date +%Y-%m-%d");
# Tried all these but do not work either
# my $date = `date +%Y-%m-%d"`
# chomp $datetime;
# my $date = system("/bin/date +%Y-%m-%d");
# my $date = system(q{"/bin/date +%Y-%m-%d"},);
# @args = ( "date +%Y-%m-%d" );
# $date = exec { $args[0] } @args;
my $query = "SELECT host,priority,datetime,msg FROM logs where datetime
like '$date%' and priority in ('err','crit','alert')";
my $dbh = DBI->connect('dbi:mysql:syslog', 'user', 'passwd')
or die DBI->errstr;
my $sth = $dbh->prepare($query) or die $sth->errstr;
$sth->execute() or die $sth->errstr;
# read the matching records and print them out
while (@data = $sth->fetchrow_array()) {
my $host = $data[0];
my $priority = $data[1];
my $datetime = $data[2];
my $msg = $data[3];
print "$host $datetime $priority $msg\n";
}
$sth->finish;
$dbh->disconnect;
-----------------end code
and when i run it:
perl datetime.pl
Name "main::sth" used only once: possible typo at datetime.pl line 23.
2006-02-10
and there I am, back to my shell.
If I hardcode
$date = "2006-02-10";
Then it interpolates and runs fine. This is useless because I want to
use
the script in a cronjob. This is a sysadmin script, will not be run
thousands of times in a cgi, so no danger of cross-side scripting or
anything like that.
Anyway, I am stuck. Any help is greatly appreciated.