http://www.perlmonks.org?node_id=1018589

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

Hello PerlMonks, i am new to the perl programming. but managed to get some script put together to get the Time zone information for all the servers using WMI Query. i want to implement threads to make this run faster. i have zero knowledge on threads. but using one example i find i tried to implement wmi query. when i run the script with WMI, i was able to get the list of server names with thread. when i add WMI query, the script is crashing perl with the following messsage.

perl TimeZoneCheck.pl Starting main program 0 server1 1 server2 Host server1 is alive Host server2 is alive BPYTSM.JDNET.DEERE.COM India Standard Time (GMT+05:30) Chennai, Kolkata, Mumbai, New Delhi (GMT+05:30) Chennai, Kolkata, Mumbai, New Delhi Free to wrong pool 49c7480 not 163ea8, <FILE> line 5 during global destruction.

i have active perl version 5.10.0

This is perl, v5.10.0 built for MSWin32-x86-multi-thread (with 9 registered patches, see perl -V for more detail) Binary build 1005 290470 provided by ActiveState http://www.ActiveState.com Built May 24 2009 12:17:36

i also request if you have any suggestions on better error handling for wmi queries

Following is the script i am using.
use warnings; use threads; use threads::shared; use Net::Ping; use Switch; use Net::DNS; use Win32::OLE qw(in with); use Carp; use constant wbemFlagReturnImmediately => 0x10; use constant wbemFlagForwardOnly => 0x20; $Win32::OLE::Warn = 3; print "Starting main program\n"; open FILE, "c:\\serverping.txt" or die "could not open the file"; my @lines = <FILE>; $linecount = scalar @lines; $row = 0; $line =$lines[$row]; $line =~ s/^\s+//m; $line =~ s/\s+$//m; $mainparam = $row.':'.$lines[$row]; $thr0 = threads->new(\&GetTimeZone, $mainparam); $row++; $mainparam = $row.':'.$lines[$row]; $thr1 = threads->new(\&GetTimeZone, $mainparam); $row++; while(1){ if ($thr0->is_joinable()) { $thr0->join; if ($row<$linecount){ $mainparam = $row.':'.$lines[$row]; $thr0 = threads->new(\&GetTimeZone, $mainparam); $row++; } } if ($thr1->is_joinable()) { $thr1->join; if ($row<$linecount){ $mainparam = $row.':'.$lines[$row]; $thr1 = threads->new(\&GetTimeZone, $mainparam); $row++; } } $thread_count = threads->list(); #print "$thread_count \n"; last if ($row==$linecount && $thread_count == 0); } print "End of main program\n"; close FILE; sub GetTimeZone { my $paramsub = shift; my @subparam = split(/:/, $paramsub); my $subrow = $subparam[0]; my $subhost = $subparam[1]; $subhost =~ s/^\s+//m; $subhost =~ s/\s+$//m; # Create a new ping object $pingit = Net::Ping->new("icmp"); my $timeout = 10; print "\n$subrow \t $subhost \n"; my $user="domain\\username"; my $pwd="password"; my $locatorObj =Win32::OLE->new("WbemScripting.SWbemLocator") or d +ie "ERROR CREATING OBJ"; $locatorObj->{Security_}->{impersonationlevel} = 3; if( $pingit->ping($subhost, $timeout) ) { print "Host ".$subhost." is alive\n"; eval { my $objWMIService = $locatorObj->ConnectServer($subhost, + "root\\cimv2", $user, $pwd); if (Win32::OLE->LastError()) { print "$_\n"; Win32::OLE->LastError(0); # this clears your error } else { $colItems = $objWMIService->ExecQuery ("Select * from Win32_Timezone","WQL",wbemFlagRetu +rnImmediately | wbemFlagForwardOnly); if (Win32::GetLastError()) { print "$subhost : $_\n"; Win32::OLE->LastError(0); # this clears your error } else{ foreach my $objItem (in $colItems) { print "\n$subhost \t $objItem->{StandardNa +me} \t $objItem->{Caption} \t $objItem->{Description} \n"; } } } }; if ($@) { print "Warning: Error querying object $subhost: $@\n"; } } else { print "Warning: ".$subhost." appears to be down or icmp packets +are blocked by their server\n"; } $pingit->close(); }