Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Perl CGl Programming: No experience required.
(Publisher: Sybex, Inc.)
Author(s): Erik Strom
ISBN: 0782121578
Publication Date: 11/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


The $LogLine string is being set in the program rather than actually read from a log file, and it’s rather long. You could have typed the whole string on one line, but it would have stretched far past the right margin of the screen. It would have looked ugly.

Perl’s string concatenation operator joins two (or more) strings. It is similar to the addition operator (+) in an arithmetic equation. In other words, given two strings:

   $Str1 = "Now is the time ";
   $Str2 = "for all brown foxes";

you can combine—or concatenate—them into one with the dot:

   $Str3 = $Str1.$Str2;

If you print $Str3, you’ll see

   Now is the time for all brown foxes

In the example, we needed to break $LogLine up into parts of a manageable length. Using the dot operator in the declaration, the parts reassembled themselves.


TIP:  You can join as many strings as you like with the concatenation operator. What would be the result of $Str1.$Str2.$Str3? Give up? Here’s a hint: It’s “Now is the time for all brown foxes” times 2.

Reading Data from a Real Log File

The example in the previous section was just that: an example, using log data that you hard-coded into the Perl script. However, it isn’t too big a job to modify the logs.pl script to read lines from a real log file.

Let’s broaden your logging experience a bit by using the Sambar server’s log file in this example. Remember that the Sambar log format is almost identical to the UNIX Common Log Format:

  Client’s IP address
  Date and time of request
  Operation requested, plus the target of the operation and the HTTP version being used
  Result code from the server
  Result code from Windows NT or Windows 95 (not used in standard Common Log Format)
  Number of bytes transferred

Decoding a Common Log Format log entry line is not nearly as straightforward as with IIS. Let’s take another look at the previous example, refined somewhat to conform to the Sambar format:

   140.172.165.58 - admin [27/Apr/1997:20:47:43 -0700]
   "GET session\adminlogin HTTP/1.0" 200 0 160

You know enough about the individual entries by now to be able to pick them out visually. But your task at the moment is to feed them into a Perl program. Notice that entries are separated with spaces, but two of the entries—the date/time string and the operation string—have spaces in them. What can you do about that?

You could hack together a Perl regular expression string that would extract each field intact. But the regular expression would be totally unintelligible to anyone but an expert in regular expressions. Why bother when you know which fields are which to begin with?

The date/time string

   [27/Apr/1997:20:47:43 -0700]

contains a single space, separating the date/time and the time zone value. The operation string

   "GET session\adminlogin  HTTP/1.0"

has one space after the operation requested and two spaces after the operation target. According to the log format, these spaces will always be in the same place, so you can still use split with a simple pattern argument to split up the string. Besides, it probably isn’t a bad idea to be able to get to these “extra” fields individually anyway.

Your original Perl script, logs.pl, will need a complete rewrite, so crank up the text editor and enter the following code:

   #!perl/bin/perl

   # logs1.pl
   #
   # A Perl script to read, extract, and print a log file from
   # the Sambar Web server.
   #

   # Put the log file name into a local variable — full path.

       $LogFile = "c:/sambar/logs/access.log";

   # Open the file; die if that’s not possible.

       open (LOG, $LogFile) || die "Can’t open $LogFile:
   $!\n";

   # Read, extract, and print each line from the log file.

       while (<LOG>)
           {
           $LogLine = $_;           # Store the line locally.
 
       # Strip out the characters we don’t need.

           $LogLine =~ s/\[|\]|\"//g;
           chop ($LogLine);

       # Extract the components using split()

           ($ClientIP, $Dummy, $UserName, $DateTime, $TimeZone, $Operation,
               $Target, $HTTPVers, $SrvrStatus, $NTStatus,
               $BytesXfer) = split (/[ ]+/, $LogLine);

       # Print the values to the screen.

           print "Client’s IP address = $ClientIP\n";
           print "Name of user on client = $UserName\n";
           print "Date and time of request = $DateTime\n";
           print "Operation requested = $Operation\n";
           print "Operation target = $Target\n";
           print "Server returned status of $SrvrStatus\n";
           print "Windows NT returned status code $NTStatus\n";
           print "Transferred $BytesXfer bytes of data\n\n";
           }            # End while (<LOG>)

       close (LOG);     # Close the log file.

   #                    End logs1.pl

Save the script as logs1.pl and run it from the command line. Stop it from time to time (Ctrl+S works on both UNIX and Windows systems) to see what the output looks like—if the log file has many entries in it, it will roll off the screen in no time. The output should look similar to what is illustrated in Figure 9.6.


Figure 9.6:  Data extracted from the Sambar log file


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.