Perl example


This simple example demonstrates how Perl can be used to display a browser based web server error log.

Code

#!/usr/bin/perl -w
use strict;
use CGI qw(:escape);
use CGI;

use vars qw($query);
my $query = new CGI;

print $query->header();

my ($input_field,$uptime,$tally);
my ($file_dates, $date);

sub create_data_display_table {
print "table frame width="610" border="1" cellpadding\=\"0\" cellspacing=\"0\"";
} # end sub create_data_display_table

sub close_data_display_table {
print "\/table";
} # end sub close_data_display_table

sub display_error_log
{
print "tr>td ALIGN=CENTER bgcolor=#ffebcd>FONT face=Arial size=2>STRONG>Error Number/STRONG>/font>/td>td bgcolor=#ffebcd>FONT face=Arial size=2>STRONG>Error Description/STRONG>/font>/td>/tr>";

open(STDIN, "tail -50 /usr/local/apache/logs/error_log>&1 |") || die "Problem redirecting STDIN.";

my $count = 1;

while (defined ($input_field = STDIN>))
{
print "tr>td ALIGN=CENTER bgcolor=#ffebcd>FONT face=Arial size=2>STRONG>$count./STRONG>/font>/td>td>FONT face=Arial size=2>";
print $input_field;
print "/FONT>/td>/tr>";

$count = $count+1;
}

print "tr>td>/td>td>/td>/tr>";
} # end sub display_error_log

sub display_server_info
{
$tally=0;

print "tr>td ALIGN=CENTER bgcolor=#dcbeff>FONT face=Arial size=2>STRONG>Uptime:\/STRONG>/font>/td>td>FONT face=Arial size=2> ";
$uptime = `uptime`;
print $uptime;
print "/FONT>/td>/tr>";

print "tr>td ALIGN=CENTER bgcolor=#dcbeff>FONT face=Arial size=2>STRONG>Current Users:\/STRONG>/font>/td>td>FONT face=Arial size=2>";
foreach (`who`)
{
print;
print "/BR>";
$tally=$tally+1;
}
print "/FONT>/td>/tr>";

print "tr>td ALIGN=CENTER bgcolor=#dcbeff>FONT face=Arial size=2>STRONG>Number of current users:\/STRONG>/font>/td>td>FONT face=Arial size=2> ";
print $tally;
print "/FONT>/td>/tr>";

} # end sub display_server_info

&create_data_display_table;
&display_error_log;
&display_server_info;
&close_data_display_table;




www.cyberthinc.com