By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

Handling log file parsing

P: n/a
I have a a few log files in a directory. Each of these log files have
status messages of the system.

Eg:

2007-06-07 14:30 Critical
2007-06-07 14:40 Error

What I need to do is list all the files in the directory which have
critical messages in the recent 10 lines. So I have to read 10 lines
of each of the file, look for the first instance of 'Critical' in it
and list is the file name there. Can anybody here give me some
pointers on how to go about it.

thanks,
Anush
Oct 14 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
anush schreef:
I have a a few log files in a directory. Each of these log files have
status messages of the system.

Eg:

2007-06-07 14:30 Critical
2007-06-07 14:40 Error

What I need to do is list all the files in the directory which have
critical messages in the recent 10 lines. So I have to read 10 lines
of each of the file, look for the first instance of 'Critical' in it
and list is the file name there. Can anybody here give me some
pointers on how to go about it.

thanks,
Anush
Hi,

If you are on *nix with PHP, have a look at a command named: tail
It is NOT a PHP command but a UNIX command, so you'll need exec,
passthru, etc to pull that trick.
http://nl3.php.net/manual/en/ref.exec.php

tail -n 10 filename

http://en.wikipedia.org/wiki/Tail_(Unix)

If that is not possible, I think you must do it yourself in PHP, eg load
the whole file using something like:
$lines = file("path/to/your/logfile");
And then take the last 10 elements in that array.
http://nl3.php.net/manual/en/function.file.php
Good luck.

Regards,
Erwin Moller

--
Oct 14 '08 #2

P: n/a
On Oct 14, 10:16 pm, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
anushschreef:
I have a a few log files in a directory. Each of these log files have
status messages of the system.
Eg:
2007-06-07 14:30 Critical
2007-06-07 14:40 Error
What I need to do is list all the files in the directory which have
critical messages in the recent 10 lines. So I have to read 10 lines
of each of the file, look for the first instance of 'Critical' in it
and list is the file name there. Can anybody here give me some
pointers on how to go about it.
thanks,
Anush

Hi,

If you are on *nix with PHP, have a look at a command named: tail
It is NOT a PHP command but a UNIX command, so you'll need exec,
passthru, etc to pull that trick.http://nl3.php.net/manual/en/ref.exec.php

tail -n 10 filename

http://en.wikipedia.org/wiki/Tail_(Unix)

If that is not possible, I think you must do it yourself in PHP, eg load
the whole file using something like:
$lines = file("path/to/your/logfile");
And then take the last 10 elements in that array.http://nl3.php.net/manual/en/function.file.php

Good luck.

Regards,
Erwin Moller

--
Thanks Erwin. I tried that. I Tail-ed it and then piped it to the
Grep. WOrks but I am receving only the output of 2 files in the PHP
code while it works perfectly in the shell.

$cmd=shell_exec("tail -n 10 ".$logf2." | grep -m 1 Critical ");

-
Anush
Oct 16 '08 #3

P: n/a
anush schreef:
On Oct 14, 10:16 pm, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
>anushschreef:
>>I have a a few log files in a directory. Each of these log files have
status messages of the system.
Eg:
2007-06-07 14:30 Critical
2007-06-07 14:40 Error
What I need to do is list all the files in the directory which have
critical messages in the recent 10 lines. So I have to read 10 lines
of each of the file, look for the first instance of 'Critical' in it
and list is the file name there. Can anybody here give me some
pointers on how to go about it.
thanks,
Anush
Hi,

If you are on *nix with PHP, have a look at a command named: tail
It is NOT a PHP command but a UNIX command, so you'll need exec,
passthru, etc to pull that trick.http://nl3.php.net/manual/en/ref.exec.php

tail -n 10 filename

http://en.wikipedia.org/wiki/Tail_(Unix)

If that is not possible, I think you must do it yourself in PHP, eg load
the whole file using something like:
$lines = file("path/to/your/logfile");
And then take the last 10 elements in that array.http://nl3.php.net/manual/en/function.file.php

Good luck.

Regards,
Erwin Moller

--

Thanks Erwin. I tried that. I Tail-ed it and then piped it to the
Grep. WOrks but I am receving only the output of 2 files in the PHP
code while it works perfectly in the shell.

$cmd=shell_exec("tail -n 10 ".$logf2." | grep -m 1 Critical ");

-
Anush
Hi Anush,

WHat do you mean that you receive only output of 2 files?
Do you mean that the file your variable $logf2 points to can only open
some files (2 in this case), and a few others not?
If so, I think you do not have readrights on the other files.

try this:
if (!is_readable($logf2)){
echo "Cannot read $logf2.";
exit;
}
$cmd=shell_exec("tail -n 10 ".$logf2." | grep -m 1 Critical ");
If that says it cannot read it, then you know where the problem lies:
PHP cannot open the file.
Remember that PHP runs seldom as user PHP, but most often as user that
Apache runs as (often www-data, or apache, or nobody).

You can test this easily by letting PHP create a file (in a directory
that is wide open, eg writing rights for everybody), and then check who
owns the file.

Regards,
Erwin Moller

--
Oct 17 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.