interpolate system() or exec()

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.
 
N

N.Asenjo

Hi, N.Asenjo,

ok, I found it (finally):
my $date = system("/bin/date +%Y-%m-%d");

this has to be backticks instead of system():

chomp(my $date = `date +%Y-%m-%d`);

it works like a charm.
 
A

Anno Siegel

N.Asenjo said:
Hi, N.Asenjo,

ok, I found it (finally):


this has to be backticks instead of system():

chomp(my $date = `date +%Y-%m-%d`);

it works like a charm.

....which is explicitly described in the documentation of system().

There is no need for an external command.

use POSIX;
my $date = strftime "%Y-%m-%d", localtime;

Anno
 
N

N.Asenjo

Hi, Anno Siegel,
...which is explicitly described in the documentation of system().

you are right, I had already read it but as it turns out not correctly
used it at first. It all is a part of the learning process.
There is no need for an external command.

use POSIX;
my $date = strftime "%Y-%m-%d", localtime;

well, look, this is useful. Thanks!
 
A

A. Sinan Unur

# 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;

You are expected to read the documentation before posting here.

Even if you did not know about the POSIX module, you should have known
that Perl provides

perldoc -f time
perldoc -f localtime
perldoc -f gmtime
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top