PHP monitor server log

PHP monitor server log

Occasionally you'll be working on a webserver and not have SSH access to it. Below is a simple script I've written that reads a server log (in this case the error log) and outputs a portion of it to a webpage.

PHP:
  1. $no_lines = 25;
  2.  
  3. $filename = $_SERVER['DOCUMENT_ROOT']."/../logs/error.log";
  4. $handle = fopen($filename, "r");
  5. $contents = fread($handle, filesize($filename));
  6. fclose($handle);
  7.  
  8. $contents = trim($contents, "\n");
  9. $contentsArr = explode("\n", $contents);
  10. $size = sizeof($contentsArr);
  11. $n = $size - $no_lines;
  12.  
  13. <h1>monitor log</h1>
  14. ";
  15. <ol start=\"$n\">";
  16.  
  17. for ($n; $n<$size; $n++)
  18. {
  19.     <li>".$contentsArr[$n]."</li>
  20. ";
  21. }
  22.  
  23. echo "</ol>
  24. ";

Obviously in a production environment you'd want to add some sort of password protection to such a script.

/* Pete Graham */

Comments 2

  1. Import from China wrote:

    Great info – keep up the great work.

    Posted 20 Jul 2008 at 10:56 am
  2. Tim Reynolds wrote:

    Nice post. Thank you for the info. Keep it up.

    Posted 07 Dec 2008 at 6:09 pm

Post a Comment

Your email is never published nor shared. Required fields are marked *