rhcci Posted October 31, 2014 Share Posted October 31, 2014 (edited) 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 October 31, 2014 by rhcci Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 31, 2014 Share Posted October 31, 2014 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. Quote Link to comment Share on other sites More sharing options...
rhcci Posted October 31, 2014 Author Share Posted October 31, 2014 Our DBA suggested sending the output of the exec() to a file and then processing the file. Then print the result. Is that along the lines you're talking about? He suggested using strings to process the file. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 31, 2014 Share Posted October 31, 2014 If your current exec process does what you want then perhaps you can send that to a file. But either way, you're going to be reading a file and having to break down the lines into meaningful columns in order to do that table you mentioned. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 31, 2014 Share Posted October 31, 2014 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>"; } Quote Link to comment Share on other sites More sharing options...
rhcci Posted November 3, 2014 Author Share Posted November 3, 2014 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 3, 2014 Share Posted November 3, 2014 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> Quote Link to comment Share on other sites More sharing options...
rhcci Posted November 6, 2014 Author Share Posted November 6, 2014 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> Quote Link to comment Share on other sites More sharing options...
kicken Posted November 6, 2014 Share Posted November 6, 2014 (edited) There's no need to write to then read from a file, just capture the output. exec('cat /path/page.log | grep FETAL', $output); foreach ($output as $line){ echo split_row($line); } Edited November 6, 2014 by kicken Quote Link to comment Share on other sites More sharing options...
rhcci Posted November 6, 2014 Author Share Posted November 6, 2014 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.