Jump to content

Dynamic table rows from a exec() function


rhcci

Recommended Posts

Hi, I'm trying to build a 'simple' page to improve a process where I work.

 

I'm wanting to do this.

 

Read either the last line, the last 10 lines, or something I decide on from a log and display the output in table rows and columns. 

 

I've done this before with loops and db query's, but don't know how to approach it using php only as this is the first time I'm using it.

 

So right now I have this in a table but it will only display one row.

 

<tr><td colspan="5">
<?php
//get auto page log
echo exec('tail -5000 /path/page.log | grep FETAL') ;
?>
</td></tr>
 
This is the output
Wed Oct 29 18:35:25 MDT 2014 1201 : WARNING -- speh1 WEBFETAL PROBLEM: fetal-sa-triage is NOT RUNNING-102
 
The second part of the problem is I'm wanting to find a way to 'extract' various data so it shows in the table colums like this
The first column is just the row #, second is the date, third is a code name (above it's speh1), etc.
 

<tr><td>1</td><td> Mon Oct 27 11:06:30 PDT 2014</td><td>mamc5</td><td>fetal-sa-ldr2 is NOT RUNNING-829</td><td> 1201: WARNING</td></tr>

 

I have built a static page as a template, but next need to move to real data.

 

Any suggestions appreciated.

Edited by rhcci
Link to comment
Share on other sites

I'm thinking you want to write a php script to read that log file and jump to the position you provide.  Then as you read each line you would use an explode() function to break the line down into parts, but looking at that one example I'm not sure how you would do that.  If you can figure out how to map the row into distinct fields then creating the output <td> elements would be a breeze.

Link to comment
Share on other sites

This will break down the line for you (assuming they all have the same basic format)

function split_row($str)
{
   $date = substr($str,0,28);
   $str = substr($str,29);
   $arr = explode(' -- ', $str);
   $err = $arr[0];
   $words = explode(' ', $arr[1]);
   $code = array_shift($words);
   $msg = join(' ', $words);
   
   return "<tr><td>$date</td><td>$code</td><td>$msg</td><td>$err</td></tr>";
}

post-3105-0-36292700-1414799577_thumb.png

Link to comment
Share on other sites

Thanks Barand!  That is exactly what I'm looking to produce.  Each line will have the same format.  A question I have though is if the file/exec() has 10 lines or 50 lines, based on how I tail it or grep it, how can I use a loop to display the multiple table rows?  The number of rows would be fixed, ie, always 10 or always 50.  it would not switch randomly as I will use a tail -f, tail, or grep -100, etc. to set it when I test the result and decide how many rows I need.

Link to comment
Share on other sites

If you get your tail command to write the selected lines to a file, say "log.txt" for example

Wed Oct 29 18:35:25 MDT 2014 1001 : ERROR -- speh1 WEBFETAL PROBLEM: fetal-sa-triage is NOT RUNNING-102
Thu Oct 30 18:35:25 MDT 2014 1201 : WARNING -- abcd1 WEBFETAL PROBLEM: fetal-sa-triage is NOT RUNNING-103
Fri Oct 31 18:35:25 MDT 2014 1302 : NOTICE -- mamc1 WEBFETAL PROBLEM: fetal-sa-triage is NOT RUNNING-104

Then the processing would look like this

<?php
function split_row($str)
{
   $date = substr($str,0,28);
   $str = substr($str,29);
   $arr = explode(' -- ', $str);
   $err = $arr[0];
   $words = explode(' ', $arr[1]);
   $code = array_shift($words);
   $msg = join(' ', $words);
   
   return "<tr><td>$date</td><td>$code</td><td>$msg</td><td>$err</td></tr>\n";
}

$lines = file('log.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$logdata = '';
foreach ($lines as $line) {
    $logdata .= split_row($line);
}

?>
<html>
<head>
<title>Log sample</title>
</head>
<body>
<table border='1' cellpadding='3'>
    <tr><th>Date</th><th>Code</th><th>Message</th><th>Error</th></tr>
    <?=$logdata?>
</table>
</body>
</html>

post-3105-0-49460700-1415036836_thumb.png

Link to comment
Share on other sites

Thanks Barand!

Can you tell why my file is not being written to?  When the page loads I'm just getting a blank table and no pagelog.txt in the /usr/tmp

 

<?php
exec('cat /path/page.log | grep FETAL >> /usr/tmp/pagelog.txt');
 
function split_row($str)
{
   $date = substr($str,0,28);
   $str = substr($str,29);
   $arr = explode(' -- ', $str);
   $err = $arr[0];
   $words = explode(' ', $arr[1]);
   $code = array_shift($words);
   $msg = join(' ', $words);
   
   return "<tr><td>$date</td><td>$code</td><td>$msg</td><td>$err</td></tr>\n";
}
 
$lines = file('/usr/tmp/pagelog.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$logdata = '';
foreach ($lines as $line) {
    $logdata .= split_row($line);
}
 
?>
<html>
<head>
<title>Log sample</title>
</head>
<body>
<table border='1' cellpadding='3'>
    <tr><th>Date</th><th>Code</th><th>Message</th><th>Error</th></tr>
    <?=$logdata?>
</table>
</body>
</html>
Link to comment
Share on other sites

Thanks kicken,

That does capture the data, but it's just a blob of info., however I need to feed it into the formatted table that Barand suggested for it to be meaningful/readable.

 

<?php
exec('cat /cci/support/log/page.log | grep FETAL', $output);
foreach ($output as $line){
   echo split_row($line);
}
 
function split_row($str)
{
   $date = substr($str,0,28);
   $str = substr($str,29);
   $arr = explode(' -- ', $str);
   $err = $arr[0];
   $words = explode(' ', $arr[1]);
   $code = array_shift($words);
   $msg = join(' ', $words);
   
   return "<tr><td>$date</td><td>$code</td><td>$msg</td><td>$err</td></tr>\n";
}
 
$lines = file('/usr/tmp/pagelog.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$logdata = '';
foreach ($lines as $line) {
    $logdata .= split_row($line);
}
 
?>
<html>
<head>
<title>Log sample</title>
</head>
<body>
<table border='1' cellpadding='3'>
    <tr><th>Date</th><th>Code</th><th>Message</th><th>Error</th></tr>
    <?=$logdata?>
</table>
</body>
</html>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.