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

I'd like to be able to run a Perl script to watch a machine's processes. That led me to PerfLib.pm, so I obtained PerfLib.pm from CPAN and tried to get the example listed in Getting a Local/Remote Win32 Task List but have been unsuccessful in getting it to work.

Next I tried the example in the POD:

#!/usr/bin/perl -w # use strict; { use Win32::PerfLib; my $server = ""; Win32::PerfLib::GetCounterNames($server, \%counter); %r_counter = map { $counter{$_} => $_ } keys %counter; # retrieve the id for process object $process_obj = $r_counter{Process}; # retrieve the id for the process ID counter $process_id = $r_counter{'ID Process'}; # create connection to $server $perflib = new Win32::PerfLib($server); $proc_ref = {}; # get the performance data for the process object $perflib->GetObjectList($process_obj, $proc_ref); $perflib->Close(); $instance_ref = $proc_ref->{Objects}->{$process_obj}->{Instances}; foreach $p (sort keys %{$instance_ref}) { $counter_ref = $instance_ref->{$p}->{Counters}; foreach $i (keys %{$counter_ref}) { if($counter_ref->{$i}->{CounterNameTitleIndex} == $process_id) { printf( "% 6d %s\n", $counter_ref->{$i}->{Counter}, $instance_ref->{$p}->{Name} ); } } } }
The result was
Use of uninitialized value in subroutine entry at C:/Perl/site/lib/Win +32/PerfLib.pm line 199. Use of uninitialized value in hash element at winps2.pl line 21. Use of uninitialized value in hash element at winps2.pl line 21. (in cleanup) Error closing handle!
Without wanting to spend too much time debugging the wheel, amd I doing anything obviouisly wrong here? If not, I'll hack away at it.

--t. alex
but my friends call me T.

Replies are listed 'Best First'.
Re: Has anyone here used PerfLib.pm successfully?
by Dog and Pony (Priest) on Sep 17, 2002 at 16:20 UTC
    I copy/pasted your code and it just worked, so I'll wager that it is no bugs as such.

    I can think of two things:

    First, I'm pretty sure this only works on NT type machines (NT/2000/XP), so if you are using anything else, you are probably out of luck.

    And second, if you are using ActiveState Perl, maybe you should try installing it via PPM instead (though I thought it was already included there). Just in case the other way messed it up somehow. That is, assuming you did install it without PPM. :)


    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.
      Heh, I didn't even try to run the code verbatim until you said it worked. My tendancy is to use strict and make it work under that before I even really look at it. I wonder if that's good lazy or bad lazy...

        Oh, that is definetely good lazy. But in a case like this, it might be useful to first see if you get the same errors using the exact same code, then work from there.

        I never do anything without strict either, usually, and that would have been my next move. :)


        You have moved into a dark place.
        It is pitch black. You are likely to be eaten by a grue.
Re: Has anyone here used PerfLib.pm successfully?
by talexb (Chancellor) on Sep 17, 2002 at 16:34 UTC
    (Replying to my own post.)

    This code no doubt works fine when it is used as directed however I am using Win98 and this code only works on the Windows NT operating systems. Thanks for your time, next time I'll RTFM a little bit more closely.

    And if there's something similar available for Win98, please let me know!

    --t. alex
    but my friends call me T.

Re: Has anyone here used PerfLib.pm successfully?
by Solo (Deacon) on Sep 17, 2002 at 16:17 UTC
    Adding use strict back in and cleaning up the resulting compile errors, I was able to get it to work.
    use warnings; use strict; use Win32::PerfLib; my %counter; my $server = ""; Win32::PerfLib::GetCounterNames($server, \%counter); my %r_counter = map { $counter{$_} => $_ } keys %counter; # retrieve the id for process object my $process_obj = $r_counter{Process}; # retrieve the id for the process ID counter my $process_id = $r_counter{'ID Process'}; # create connection to $server my $perflib = new Win32::PerfLib($server); my $proc_ref = {}; # get the performance data for the process object $perflib->GetObjectList($process_obj, $proc_ref); $perflib->Close(); my $instance_ref = $proc_ref->{Objects}->{$process_obj}->{Instances}; foreach my $p (sort keys %{$instance_ref}) { my $counter_ref = $instance_ref->{$p}->{Counters}; foreach my $i (keys %{$counter_ref}) { if($counter_ref->{$i}->{CounterNameTitleIndex} == $process_id) + { printf( "% 6d %s\n", $counter_ref->{$i}->{Counter}, $instance_ref->{$p}->{Name} ); } } }
    Update: Dog and Pony has identified that the code is not the problem. See this node for info.
    --
    May the Source be with you.

    You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.

      Thanks for your quick reply. However, I don't know what you changed (you didn't indicate what you changed) and the version you provided still gives me the same errors. Were you able to get it to run correctly? What was the output?

      --t. alex
      but my friends call me T.

      Update Sorry, I am using Win98 -- I thought I could use this module but obviously I cannot. My bad.