L. J. Jaeckel
CMPSC 210 — Linux System Administration
April 22, 2011

Listing of PHP script: run-weblog.php

Produces a neatly formtted listing showing the last (most recent) 10 lines from each of the Web Access and Web Error log files. The output includes header lines showing the date and time. The page also includes a meta tag to auto-refresh itself every 10 seconds.

Click here to run this script now a view the resulting page.


     1	<html>
     2	  <head>
     3	    <title>Most recent 10 Web Access and Error messages</head>
     4	    <meta http-equiv="refresh" content="10" />
     5	    <style>
     6	      .grn {color:black; background-color:PaleGreen}
     7	      .wht {color:black; background-color:white}
     8	    </style>
     9	  </head>
    10	  <body>
    11	    <h1><center>Most recent 10 Web Access and Error message lines</center></h1>
    12	
    13	    <?php
    14	
    15	// NOTE: It appears that a PHP script cannot easily read the web log
    16	// files, because the are in a directory /var/log/apache2 with
    17	// access 750, and the access.log and error.log files have
    18	// access 740.  We need to give all of these other-read access
    19	// (755 and 744, respectively) in order for this script to
    20	// be able to read the files.
    21	
    22	      $myhost = `hostname` ;
    23	      $mydate = `date '+%T %A %B %-d, %Y'` ;
    24	      $webmsgs = "/var/log/apache2/access.log" ;
    25	      $weberrs = "/var/log/apache2/error.log" ;
    26	
    27	// Function disp_lines:
    28	// Given a multi-line text in the argument $mtext, display the
    29	// lines with alternating white and green backgrounds:
    30	
    31	      function disp_lines ( $mtext )
    32	      {
    33	          $dline = explode( "\n", $mtext ) ;
    34	          reset( $dline ) ;
    35	          echo "<pre>" ;
    36	          $color = 0 ;
    37	          while ( list($dkey, $dvalue) = each($dline) )  {
    38	              echo '<span class="' . ( $color == 1 ? "grn" : "wht" ) . '">' . $dvalue . " </span>\n" ;
    39	              $color = 1 - $color ;
    40	          }
    41	          echo "</pre>" ;
    42	          echo "<hr />" ;
    43	      }
    44	
    45	
    46	      echo "<h2><center><b>on host machine <i>" . $myhost . "</i></b></center></h2>\n" ;
    47	      echo "<p><center>Refreshing every 10 seconds.<br />\n" ;
    48	      echo "<a href=\"run-weblog-php-list.html\">Click here</a> to view "; 
    49	      echo "the PHP script that generates this page.</center></p>\n" ;
    50	
    51	      echo "<h3>" . $mydate . " &mdash; Web Access messages:</h3>\n" ;
    52	
    53	      $xx = `tail -10 $webmsgs` ;
    54	      disp_lines($xx) ;
    55	
    56	      echo "<h3>" . $mydate . " &mdash; Web Error messages:</h3>\n" ;
    57	
    58	      $xx = `tail -10 $weberrs` ;
    59	      disp_lines($xx) ;
    60	
    61	    ?>
    62	
    63	  </body>
    64	</html>