Digesting Log data - part 2

Be the first to comment | 1I like it!
April 22, 2009, 08:32 AM —  ITworld — 

Last week, we looked at a script that digests log files by making clever use of Perl's impressive implementation of arrays. This week, we look at a pared down version of the same script, paying close attention to performance and making some significant efficiency improvements. Though Perl seems to provide us with many ways of accomplishing the same task as does Unix in general, some methods are considerably more efficient than others. 

The first change we'll make to the script this week is that, instead of reading the log file into an array (i.e., into memory), we'll simply read it and digest one line at a time. This won't make a lot of difference if you're digesting small files, but if you're using the script to digest huge log files on a busy computer, this can make a big difference in the amount of memory the script will use and how long it takes to run.

Another change is that we no longer check or manipulate the @ARGV structure. In very terse Perl style, we're simply going to read the file that's passed on the command line. The only down side to this approach is that, if you don't include a file name on the command line, the script will not warn you. Instead, it will simply process nothing, waiting for you to realize what you did and type control-C.

We continue to use an associative array (or "hash") to count up the number of duplicate messages. After all, this proved to be a very convenient way to keep track of each unique message type, but we no longer bother with removing duplicates from the original array. This is both because the original array no longer exists (i.e., we're reading the file directly instead) and more importantly, because the index of the hash already contains a single entry for each message type.

Now, let's look at the extremely simplified script. In its barest form, the script to digest a log file while removing digits looks like this:

#!/bin/perl

while ( <> ) {
s/\d+/#/g; # change digits to # signs $count{$_}++; # count repeats }

for $line ( sort keys %count) {
print "$count{$line}: $line"; }

This script reads the log file, changes digits to # signs, and creates a hash with each message type as an index and the count as value. It then runs through the hash, printing out each line and each count. This is about the simplest and fastest way to digest a log file down to the unique record types. However, for some log files, this might still leave you with too much data to examine. Let's look at one reason why this might be the case.

Complications

The script that we've been looking at turns unique messages into message types by removing the numbers that, under many circumstances, make messages unique without adding to their value in a log file summary. Certain log files (such as the /var/adm/messages file) also contain date information such as the day of week and month of the year in a non-numeric format.

Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world

I like it!
Close

On Twitter now

unix

Powered by Twitter
You are logged in | Sign out
Sign in and post to Twitter

What are you thinking?

Cancel Tweet sent

On Twitter now

Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
peer-to-peer

Esther Schindler
If the comments are ugly, the code is ugly

claird
SVG a graphics format for 21st century

pasmith
Take Chrome OS for a test spin

Sandra Henry-Stocker
Solaris Tip: Have Your Files Changed Since Installation?

sjvn
64-bits of protection?

jfruh
Android fragments vs. the iPhone monolith

mikelgan
What Gizmodo missed about the Pro WX Wireless USB disk drive

 

Sidekick: The Good News & the Bad News
Either way you look at it Microsoft Data Center management did not follow standards or best practices in this failure. In which case it makes me wonder more about the outsourcing of corporate data much less personal data.
- mburton325

Join the conversation here

The Daily Tip

The Daily TipQuick, practical advice for IT pros. Made fresh daily.

Hot tips:

Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.

Newsletters

Subscribe to ITWORLD TODAY and receive the latest IT news and analysis.

I would like to receive offers via email from ITworld partners.
By clicking submit you agree to the terms and conditions outlined in ITworld's privacy policy.
Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

Marketplace