![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
The $LogLine string is being set in the program rather than actually read from a log file, and its 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. Perls 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 combineor concatenatethem into one with the dot: $Str3 = $Str1.$Str2; If you print $Str3, youll 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.
Reading Data from a Real Log FileThe example in the previous section was just that: an example, using log data that you hard-coded into the Perl script. However, it isnt too big a job to modify the logs.pl script to read lines from a real log file. Lets broaden your logging experience a bit by using the Sambar servers log file in this example. Remember that the Sambar log format is almost identical to the UNIX Common Log Format:
Decoding a Common Log Format log entry line is not nearly as straightforward as with IIS. Lets 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 entriesthe date/time string and the operation stringhave 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 isnt 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 thats not possible. open (LOG, $LogFile) || die "Cant 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 dont 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 "Clients 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 likeif 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.
|
![]() |
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. |