shockme has asked for the wisdom of the Perl Monks concerning the following question:

I've written a script that remotely queries NT/2K servers for their event logs. It then parses the event logs and sends a summary email to me.

I'm using Win32::EventLog, but it's giving me some unpredictable results. Sometime it returns valid event codes, and other times it returns very odd results (i.e., an event code of -1073597748).

Following is an abbreviated version of my code. If anything seems undefined or out of place, it's an editing error on my part.

#!/usr/bin/perl use strict; use Win32::EventLog; my $emailAddys = "email@address.org"; my @systems = "test.system.org"; foreach my $system (@systems) { print "processing $system\n"; my %event = ('Length', "", 'RecordNumber', "", 'TimeGenerated', "", 'TimeWritten', "", 'EventID', "", 'EventType', "", 'Category', "", 'ClosingRecordNumber', "", 'Source', "", 'Computer', "", 'Strings', "", 'Data', "", ); # retrieve the full text of every message on each Read() $Win32::EventLog::GetMessageText=1; my $event = ""; my $EventLog = ""; my $numEvents = 0; my $oldestEvent = 0; my @errors; my @warnings; my @statistics; foreach my $AppType (qw(Application Security System)) { my $queryResult = Win32::EventLog::Open($EventLog, $AppType, $sy +stem); if (!($queryResult)) { print "Could not open $AppType log for $system:$^E\n"; next; } $EventLog->Win32::EventLog::GetNumber($numEvents); $EventLog->Win32::EventLog::GetOldest($oldestEvent); $EventLog->Win32::EventLog::Read((EVENTLOG_SEEK_READ | EVENTLOG_FORWARDS_READ), $numEvents + $oldestEvent, $ev +ent); # loop through all of the events, recording the number of Source + and # EventTypes my %source; my %types; for ($i=0; $i<$numEvents; $i++) { $EventLog->Read((EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ), 0, $event); $source{$event->{Source}}++; $types{$event->{EventType}}++; if ($type{$event->{EventType}} =~ /error/i) { push @errors, "$event->{TimeGenerated}: " . "$event->{EventID}: " . "$event->{Source}: " . "$event->{Message}\n"; } elsif ($type{$event->{EventType}} =~ /warning/i) { push @warnings, "$event->{TimeGenerated}: " . "$event->{EventID}: " . "$event->{Source}: " . "$event->{Message}\n"; } } $EventLog->Close(); } use Mail::Sendmail; my %mail = ( To => "$emailAddys", From => "stephen.hargrove\@ttuhsc.edu", Subject => "$system Event Log Summaries", Message => "@errors"."@warnings", ); $mail{smtp} = 'smtp.server.org'; sendmail(%mail) or die "\nDoh! $Mail::Sendmail::error\n"; undef @errors; undef @warnings; }

$event->{TimeGenerated}, as near as I can figure, is always correct, as is $event->{Source}. However, as mentioned, $event->{EventID} will sometimes return a valid event code and other times will return something like -2147483646. In these instances, $event->{Message} is (understandably) blank.

As a side note, any time the returned event code is invalid, it's always negative.

Any ideas on what I might be doing that would cause Win32::EventLog to return these type of oddities?

Thanks.

If things get any worse, I'll have to ask you to stop helping me.

Replies are listed 'Best First'.
(shockme) Re: Weird Win32::EventLog Results
by shockme (Chaplain) on Mar 19, 2002 at 15:51 UTC
    This is an update on my progress. Having found no solution to my plight, I emailed one of the authors, Bret Giddings. Bret responded as follows:

    Try

    $event->{EventID} &= 0x0000ffff;

    That should bring the number into the range the MS expect. I don't know why the API returns the wrong number. Microsoft seem to correct for it in the eventviewer - I guess if I ever do any more work on the EventLog module, I should too.

    This modification resolved about 95% of the weirdness. There are still some anomalies, but the script is now much more useable.

    If things get any worse, I'll have to ask you to stop helping me.