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

Hello, I'm trying to webify a windows event log script. This script works fine when run from a command line on the server running IIS, but when run as a CGI using CGI.PM it returns the following error: Bad file descriptor at C:\Inetpub\wwwroot\cgi-bin\login.pl line 99.
line 99: $EventLog->GetOldest(\$first) || die $!;
I believe this is might be a permissions problem but not sure where to look anyone have any ideas? Thanks in advance! Subroutine:
sub server_events { print_header(); print"<h2>SysAdmin Tools v0.1 (beta) </h2>\n"; my $servername = $q->param('servername') || 'localhost'; my $eventtype = $q->param('eventtype') || 'System'; my $eventquan = $q->param('eventquan') || '10'; print qq{<b>$servername is Server </b><br>}; print qq{<b>$eventtype is Event Type </b><br>}; print qq{<b>$eventquan is Event Quantity </b><br>}; my ($EventLog, $count, $first, $key); $first = $count = 0; my $event={ 'Source' => NULL, 'Computer' => NULL, 'Length' => NULL, 'Category' => NULL, 'RecordNumber' => NULL, 'TimeGenerated' => NULL, 'Timewritten' => NULL, 'EventID' => NULL, 'EventType' => NULL, 'ClosingRecordNumber' => NULL, 'Strings' => NULL, 'Data', => NULL, }; $EventLog = new Win32::EventLog( "$eventtype","$servername" ) || d +ie $!; $EventLog->GetOldest(\$first) || die $!; $EventLog->GetNumber(\$count) || die $!; $EventLog->Read((EVENTLOG_SEEK_READ | EVENTLOG_BACKWARDS_READ),$fi +rst+$count,$event); for my $i ($first+$count-$eventquan+1..$first+$count) { $EventLog->Read((EVENTLOG_SEQUENTIAL_READ|EVENTLOG_BACKWARDS_READ) +,0,$event); my ($sec,$min,$hour,$mday,$mon,$year,$sday,$yday,$isdst) = localti +me($event->{'TimeGenerated'}); print sprintf("%15s -> %02d\-%02d\-%02d, %02d:%02d\n",'timestamp', +$year,$mon+1,$mday,$hour,$min); #to get a readable EventId $event->{'EventID'} = $event->{'EventID'} & 0xffff; foreach $key ('RecordNumber','Category','Source','EventID', 'Event +Type', 'Strings', 'Data') { print sprintf( "%15s -> %s\n",$key, $event->{$key} ); print "<br>" } print "\n<br>"; } }

Replies are listed 'Best First'.
Re: win32::eventlog file descriptor errors on IIS Server ($^E)
by tye (Sage) on May 15, 2007 at 20:56 UTC

    Where did you read that $! would hold useful information about errors from Win32::EventLog ? You should check $^E instead (not that the module documentation bothers to tell you this). And testing shows that Win32::EventLog->new() doesn't return a false value for failure, *sigh*. So change your code to something more like:

    $EventLog= Win32::EventLog->new( "$eventtyp­e", "$serve­rname" ) or die $^E; die $^E if ! $EventLog->{handle}; $EventLog->GetOldest( \$first ) or die $^E;

    And you'll probably see that you don't have privilege to read remote event logs from the security context of a CGI script (since Win32 CGI scripts are usually run in a security context that has no access to Win32 network resources).

    I suspect there is a FAQ on how to access network resources from an IIS script that you could find.

    - tye        

      Thanks, I set up the IIS directory where I'm executing my CGI scripts to run as an anonymous user where the anon is a network admin. Everything is working great now! Also appreciatted the tip on $^E, being new to perl I'm not totally up on some all of the built-in's! -Jeff
Re: win32::eventlog file descriptor errors on IIS Server
by thezip (Vicar) on May 15, 2007 at 20:09 UTC

    Updated:

    Yes, your privileges will be entirely different on the command-line than when the webserver runs it...



    This advice is garbage... so please excuse the noise:

    You might want to do a check for file existence and then complain when it fails. You'll want to explicitly declare the filename (ie. fullpath/filename).

    Maybe the webserver is looking in a different path than when you run it on the command line?


    Where do you want *them* to go today?