473,511 Members | 15,852 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Handling log file parsing

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
3 3580
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3570
by: Marian Jancar | last post by:
Hi, Is there a module for parsing spec files available? Marian -- -- Best Regards,
2
3916
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
4
3498
by: Gert Van den Eynde | last post by:
Hi all, Could you give me some pointers on how to parse a text input file in C++? Most will be config-file style input (keyword = data), but some maybe 'structures' like material{ name = n,...
4
11966
by: sunil | last post by:
I am creating a XML document which opens fine in IE. Implies MSXML thinks it is a well formed document. But when I try to load this document in VB.net using the following code Dim doc As New...
1
2440
by: Christoph Bisping | last post by:
Hello! Maybe someone is able to give me a little hint on this: I've written a vb.net app which is mainly an interpreter for specialized CAD/CAM files. These files mainly contain simple movement...
11
2795
by: .Net Sports | last post by:
In VB.net, I'm trying to do a couple of things in a couple of different blocks of code. I need to take the first 25 characters of a text file, then append at the end some ellipses and a MORE link...
2
1915
by: pandurusankar | last post by:
Hi, Need a pointer for managing File type button in HTML page, i am able to handle all the components in HTML using IEAutomation module like: text box, links, radio button, list box etc. but i...
4
1843
by: DjLethal | last post by:
Hey Guys.I am a newbie on Perl.And I have a question about file parsing by perl. I have a log file which contains reports about a communication machine.I need to take some of the reports from the...
2
3597
by: Felipe De Bene | last post by:
I'm having problems parsing an HTML file with the following syntax : <TABLE cellspacing=0 cellpadding=0 ALIGN=CENTER BORDER=1 width='100%'> <TH BGCOLOR='#c0c0c0' Width='3%'>User ID</TH> <TH...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7353
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.