open.itworld.com
  Search  
Security Home Page Security Webcasts Security White Papers Security Newsletters Security News Open Topics Careers ITworld Voices ITwhirled The Security site of ITworld.com

Unix Tip: Scanning your messages file for warnings: Take two

ITworld.com 10/09/2007

Sandra Henry-Stocker, ITworld.com

Send in your Unix questions today! | See additional Unix tips and tricks

Several weeks ago, I encouraged readers to automate the extraction of warning messages from their /var/adm/messages (or /var/log/messages) files and provided a script to do just that. In this article, we will look at a re-implementation of that script.

The new version of this script was written by Jared Still, Certifiable Oracle DBA and Part Time Perl Evangelist and author of "Perl for Oracle DBAs". Jared's rewrite of the look4warnings script illustrates how elegantly Perl can be used to extract and summarize the contents of log files.

The first commands in the script rewrite are, more or less, the same as in the original version. We assign our messages file to a variable ($msgs) and attempt to open the file. If we run into problems, we exit, displaying a message like this generated by the die command:

Cannot open /var/log/messages - No such file or directory
The script, then, begins with a comment and the lines to set up our input file.

#!/usr/bin/perl -w

# look through messages file for warnings, show summaries
$msgs="/var/log/messages";

open (MSGS,"<$msgs") || die "Cannot open $msgs - $!\n";
The extraction of all lines containing the word "warning", on the other hand, is accomplished far more simply and elegantly than in the original version. Notice that we accomplished this using a single command.

# get all warnings
my @warnings=grep(/warning/i,<MSGS>);
At this point, every line in the messages file that contains the word "warning" in any combination or uppercase and lowercase letters has been added to the @warnings array. This includes duplicate warnings with the same or different timestamps. If a particular warning has arrived once a day for a week, for example, that warning will be saved in the array seven times. The following command then strips the time stamps off of each line with the map command. This particular map command is preserving in each element of the array the text beginning with the word "warning" (or "WARNING", "Warning" and so on) as indicated by the "i" following the pattern we are matching. Specifically, warning matches "warning", \s* matches some amount of white space, (.*) matches anything (i.e., the rest of the line) and "i" says to ignore case. Once this command is run, we have the same number of elements in the @warnings array, but each has been shortened, the time stamp having been stripped.

# get string without timestamp
@warnings = map( /warning:\s*(.*)/i, @warnings);
Perl's map function is perfect for simplifying potentially repetitive operations, such as capitalizing strings of text or selecting some portion of each line. If no warnings were found in the messages file, we will issue a message to this effect and exit. We exit with a return code of 0 since no error is indicated by a messages file that doesn't contains warnings.

unless (@warnings) {
        print "No warnings found in $msgs\n"; 
        exit 0;
}
Next, we do the trickier part. We set %warncount up as an empty hash and then use it to accumulate a count of each warning message. $warncount{$_}++ is a count of the current warning message, incremented each time a particular warning is encountered as we move through the array.

# create hash with count
%warncount=();
%warnings = map{$_,$warncount{$_}++} @warnings;
Finally, we display the list of warning messages along with the number of times each warning appears in the messages file:

foreach my $warning ( sort keys %warnings ) {
        print "warning: $warning: ", $warnings{$warning}+1,"\n"; 
}
If you want to also display a count of the unique warnings found in the file, you can add this to the bottom of the script:

print "\nunique warning messages: $#warnings\n";
For your cutting and pasting pleasure, here's the script in non-interrupted format:

#!/usr/bin/perl -w

# look through messages file for warnings, show summaries
$msgs="/var/log/messages";

open (MSGS,"<$msgs") || die "Cannot open $msgs - $!\n";

# get all warnings
my @warnings=grep(/warning/i,<MSGS>);

# get string without timestamp
@warnings = map( /warning:\s*(.*)/i, @warnings);

unless (@warnings) {
        print "No warnings found in $msgs\n";
        exit 0;
}

# create hash with count
%warncount=();
%warnings = map{$_,$warncount{$_}++} @warnings;

foreach my $warning ( sort keys %warnings ) {
        print "warning: $warning: ", $warnings{$warning}+1,"\n";
}
For more on Jared's adeptness with Perl, I recommend his excellent text (with coauthor Andy Duncan) -- Perl for Oracle DBAs, O'Reilly, 2002 and his personal web site, http://www.jaredstill.com.

On this topic

 

Sandra Henry-Stocker has been administering Unix systems for more than 18 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She currently works for TeleCommunication Systems, a wireless communications company, in Annapolis, Maryland, where no one else necessarily shares any of her opinions. She lives with her second family on a small farm on Maryland's Eastern Shore. Send comments and suggestions to bugfarm@gmail.com.




Sponsored Links

Sign up for a Microsoft Dynamics® CRM WEBCAST
Hear globally recognized leaders in customer strategy discuss the importance and evolution of CRM.
TRY NEW SUN SERVERS FREE for 60 Days!
Test The Latest Sun Servers In Your Environment BEFORE YOU BUY. Pay Nothing, Not Even Shipping.
Workflow Enabled Help Desk & IT Service Management
Automate service desk activities and integrate processes across IT. Learn more here.
Sign up for a FREE NETWORK RISK ASSESSMENT!
MORE THAN 70% OF NETWORKS ARE INFECTED by hidden Malware. Find out if your network is infected now!
Enterprise IP Goes Mobile
To maximize full productivity, companies must integrate their mobile applications with the IP network.
» Buy a link now

Advertisements
Sponsored links
Top 5 Reasons to Combine App Performance and Security
Locate Hidden Software on business PCs with this free tool
KODAK i1400 Series Scanners stand up to the challenge
Bring harmony to your mix of UNIX-Linux-Windows computing environments
 Home   Open source  Operating systems  Unix
www.itworld.com    open.itworld.com     security.itworld.com     smallbusiness.itworld.com
storage.itworld.com     utilitycomputing.itworld.com     wireless.itworld.com

 
Contact Us   About Us   Privacy Policy    Terms of Service   Reprints  

CIO   Computerworld   CSO   GamePro   Games.net   IDG Connect   IDG World Expo   Infoworld   ITworld   JavaWorld   LinuxWorld  MacUser   Macworld   Network World   PC World   Playlist  

Copyright © Computerworld, Inc. All rights reserved

Reproduction in whole or in part in any form or medium without express written permission of Computerworld Inc. is prohibited. Computerworld and Computerworld.com and the respective logos are trademarks of International Data Group Inc.