PHP - Reading Live Text Log ?

This is a discussion on PHP - Reading Live Text Log ? within the PHP Language forums, part of the PHP Programming Forums category; Hi My mail server outputs live logs like this: T 20070325 123318 4605007c Connection from 111.111.111.111 T ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-25-2007
jerryyang_la1@yahoo.com
 
Posts: n/a
Default PHP - Reading Live Text Log ?

Hi

My mail server outputs live logs like this:

T 20070325 123318 4605007c Connection from 111.111.111.111
T 20070325 123319 4605007c HELO testdomain.com
T 20070325 125817 46050083 EHLO S010600110962b82a.ed.roguedomain.net
T 20070325 125818 46050083 MAIL FROM: <gqvnwuhku@roguedomain.net>
E 20070325 125823 46050083 Host 68.150.140.71 blocked by
10-12 .sorbs.net C - Block - message tagged.
T 20070325 125824 46050083 RCPT TO: <dignity@newsite.com>
E 20070325 125824 46050083 RCPT from 68.150.140.71 - user
<dignity@newsite.com> not known.
T 20070325 125824 46050083 Connection closed with 68.150.140.71, 7
sec. elapsed.
T 20070325 123349 4605007c Connection closed with 111.111.111.111, 31
sec. elapsed.
T 20070325 125947 46050084 Connection from 83.28.27.71
T 20070325 125947 46050084 HELO drmac.cl
T 20070325 125947 46050084 MAIL FROM: <bitransmitting@drmac.cl>
E 20070325 125948 46050084 Host 83.28.27.71 blocked by
zen.spamhaus.org sbl/xbl/pbl/zen 2 - message tagged.
T 20070325 125948 46050084 RCPT TO: <a1aaa1azzzz1zaaaaa@newsite.com>
E 20070325 125948 46050084 RCPT from 83.28.27.71 - user
<a1aaa1azzzz1zaaaaa@newsite.com> not known.
T 20070325 125949 46050084 Connection closed with 83.28.27.71, 2 sec.
elapsed.

I'm looking to try and read this file via PHP and colourise each line
based on the connection ID displaying the final result in a php
page...

eg:

T 20070325 123318 4605007c Connection from 111.111.111.111
The ID is 4605007c This appears on 3 lines. Note that the ID's are
not always grouped together.

Can this be done ?? The file is live and constantly changing...

Thanks

Reply With Quote
  #2 (permalink)  
Old 03-25-2007
petersprc
 
Posts: n/a
Default Re: PHP - Reading Live Text Log ?

You could do this with tail and fread. Here's one example of reading a
file that's continuously being updated:

http://groups.google.com/group/alt.c...b2d87c7d0cf1a8

On Mar 25, 8:07 am, jerryyang_...@yahoo.com wrote:
> Hi
>
> My mail server outputs live logs like this:
>
> T 20070325 123318 4605007c Connection from 111.111.111.111
> T 20070325 123319 4605007c HELO testdomain.com
> T 20070325 125817 46050083 EHLO S010600110962b82a.ed.roguedomain.net
> T 20070325 125818 46050083 MAIL FROM: <gqvnwu...@roguedomain.net>
> E 20070325 125823 46050083 Host 68.150.140.71 blocked by
> 10-12 .sorbs.net C - Block - message tagged.
> T 20070325 125824 46050083 RCPT TO: <dign...@newsite.com>
> E 20070325 125824 46050083 RCPT from 68.150.140.71 - user
> <dign...@newsite.com> not known.
> T 20070325 125824 46050083 Connection closed with 68.150.140.71, 7
> sec. elapsed.
> T 20070325 123349 4605007c Connection closed with 111.111.111.111, 31
> sec. elapsed.
> T 20070325 125947 46050084 Connection from 83.28.27.71
> T 20070325 125947 46050084 HELO drmac.cl
> T 20070325 125947 46050084 MAIL FROM: <bitransmitt...@drmac.cl>
> E 20070325 125948 46050084 Host 83.28.27.71 blocked by
> zen.spamhaus.org sbl/xbl/pbl/zen 2 - message tagged.
> T 20070325 125948 46050084 RCPT TO: <a1aaa1azzzz1zaa...@newsite.com>
> E 20070325 125948 46050084 RCPT from 83.28.27.71 - user
> <a1aaa1azzzz1zaa...@newsite.com> not known.
> T 20070325 125949 46050084 Connection closed with 83.28.27.71, 2 sec.
> elapsed.
>
> I'm looking to try and read this file via PHP and colourise each line
> based on the connection ID displaying the final result in a php
> page...
>
> eg:
>
> T 20070325 123318 4605007c Connection from 111.111.111.111
> The ID is 4605007c This appears on 3 lines. Note that the ID's are
> not always grouped together.
>
> Can this be done ?? The file is live and constantly changing...
>
> Thanks



Reply With Quote
  #3 (permalink)  
Old 03-25-2007
jerryyang_la1@yahoo.com
 
Posts: n/a
Default Re: PHP - Reading Live Text Log ?

Thanks

But what about colourising the matching lines ?

Reply With Quote
  #4 (permalink)  
Old 03-25-2007
petersprc
 
Posts: n/a
Default Re: PHP - Reading Live Text Log ?

On Mar 25, 10:34 am, jerryyang_...@yahoo.com wrote:
> Thanks
>
> But what about colourising the matching lines ?


Here's one (ugly) way to do it:

<?

error_reporting(E_ALL);

function printLog($text)
{
$lines = explode("\n", $text);
$buf = '';
$head = null;
$extra = array();

for ($i = 0; $i < count($lines); $i++) {
$parts = explode(' ', $lines[$i], 5);
if (count($parts) >= 4 && ($parts[0] == 'T' || $parts[1] == 'E'))
{
if (!is_null($head)) {
$buf .= printEntry($head, $extra);
}
$head = $parts;
$extra = array();
if ($i == count($lines) - 1) {
$buf .= printEntry($head, $extra);
}
} else {
$extra[] = $lines[$i];
}
}

echo $buf;
flush();
}

function printEntry($head, $extra)
{
static $rnd = null;
if (is_null($rnd)) {
$rnd = rand();
}

$hex = md5($head[3] . $rnd);
$fg = substr($hex, 0, 6);
$bg = sprintf("%02x%02x%02x",
((('0x' . substr($hex, 0, 2)) + 0x7f) % 0xff),
((('0x' . substr($hex, 2, 2)) + 0x7f) % 0xff),
((('0x' . substr($hex, 4, 2)) + 0x7f) % 0xff));

$msg = join(' ', $head) . "\n" . join("\n", $extra);
$str = "<span style=\"width: 100%; color: #{$fg}; background: #{$bg};
\">" .
htmlentities($msg, ENT_QUOTES) . "</span>";
$str = nl2br($str);

return $str;
}

$text = "T 20070325 123318 4605007c Connection from 111.111.111.111
T 20070325 123319 4605007c HELO testdomain.com
T 20070325 125817 46050083 EHLO S010600110962b82a.ed.roguedomain.net
T 20070325 125818 46050083 MAIL FROM: <gqvnwu...@roguedomain.net>
E 20070325 125823 46050083 Host 68.150.140.71 blocked by
10-12 .sorbs.net C - Block - message tagged.
T 20070325 125824 46050083 RCPT TO: <dign...@newsite.com>
E 20070325 125824 46050083 RCPT from 68.150.140.71 - user
<dign...@newsite.com> not known.
T 20070325 125824 46050083 Connection closed with 68.150.140.71, 7
sec. elapsed.
T 20070325 123349 4605007c Connection closed with 111.111.111.111, 31
sec. elapsed.
T 20070325 125947 46050084 Connection from 83.28.27.71
T 20070325 125947 46050084 HELO drmac.cl
T 20070325 125947 46050084 MAIL FROM: <bitransmitt...@drmac.cl>
E 20070325 125948 46050084 Host 83.28.27.71 blocked by
zen.spamhaus.org sbl/xbl/pbl/zen 2 - message tagged.
T 20070325 125948 46050084 RCPT TO: <a1aaa1azzzz1zaa...@newsite.com>
E 20070325 125948 46050084 RCPT from 83.28.27.71 - user
<a1aaa1azzzz1zaa...@newsite.com> not known.
T 20070325 125949 46050084 Connection closed with 83.28.27.71, 2 sec.
elapsed.
";

printLog($text);

?>

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 04:51 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0