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

Listing of PHP script: run-dmesg.php

Runs dmesg to get the System Messages, keeps the last 20 lines of that, and produces a complete web page of that. The output includes header lines showing the date and time. The page also includes a meta tag to auto-refresh itself every five seconds.

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


     1	<html>
     2	  <head>
     3	    <title>Most recent 20 System Message (dmesg) lines</head>
     4	    <meta http-equiv="refresh" content="5" />
     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 20 System Message (dmesg) lines</center></h1>
    12	
    13	    <?php
    14	      $myhost = `hostname` ;
    15	      echo "<h2><center><b>on host machine <i>" . $myhost . "</i></b></center></h2>\n" ;
    16	      echo "<p><center>Refreshing every 5 seconds (not that this output ever changes much).<br />\n" ;
    17	      echo "<a href=\"run-dmesg-php-list.html\">Click here</a> to view "; 
    18	      echo "the PHP script that generates this page.</center></p>\n" ;
    19	      echo "<center>For a more interesting page, with messages that actually\n" ;
    20	      echo "        change from time to time,<br />\n" ;
    21	      echo "        take a look at the <a href=\"run-weblog.php\">web activity log</a>\n" ;
    22	      echo "        page instead.\n" ;
    23	      echo "</center>\n" ;
    24	      echo "<h3>" . `date '+%T %A %B %-d, %Y'` . "</h3>\n" ;
    25	      $xx = `dmesg | tail -20` ;
    26	
    27	// Deal with the dmesg output line-by-line, displaying the lines
    28	// with alternate green and white background:
    29	
    30	      $dline = explode( "\n", $xx ) ;
    31	      reset( $dline ) ;
    32	      echo "<pre>" ;
    33	      $color = 0 ;
    34	      while ( list($dkey, $dvalue) = each($dline) )  {
    35	          echo '<span class="' . ( $color == 1 ? "grn" : "wht" ) . '">' . $dvalue . " </span>\n" ;
    36	      $color = 1 - $color ;
    37	      }
    38	      echo "</pre>" ;
    39	    ?>
    40	
    41	    <hr />
    42	  </body>
    43	</html>