Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: perl cpu usage on windows machines

by leonidlm (Pilgrim)
on Jul 14, 2008 at 11:55 UTC ( [id://697452]=note: print w/replies, xml ) Need Help??


in reply to Re: perl cpu usage on windows machines
in thread perl cpu usage on windows machines

The script is run under windows 2003. This is an example of the script:
#!/usr/bin/perl use strict; use Warnings; use HTTP::Status; use HTTP::Response; use HTTP::Request; use LWP::UserAgent; if (@ARGV < 2) { print "USAGE: IsPageAvailable [-r] <MonitorName> <URL> <Hosts file +> [Http::Host]\n"; } else { # Treat the -r option, in wich case the screap will search in the +response returned to SUCCEED # string and treat the returned monitor accoringly. my $checkResponse = 0; if ($ARGV[0] eq "-r") { $checkResponse = 1; shift @ARGV; } my ($strMonitorName, $strURL, $hosts, $httpHost) = @ARGV; # Use the configuration file only if specefied. if (defined($hosts)) { # Generate the computerName and domainName variables used late +r to search in the hosts file my $computerName = lc($ENV{'COMPUTERNAME'}); $strURL =~ /^http.*\/\/(.+?)[\:\/]/; my $domainName = $1; my $ip; # Convert the domain using the hosts file supplied open (HOSTS, '<', $hosts) or die "Cant open configuration file + $hosts\n"; END: while (my $line = <HOSTS>) { if ($line =~ /$computerName:$domainName:(.+)/) { $ip = $1; chomp($ip); last END; } } close(HOSTS); # No entries in the hosts found die "No entries in $hosts file found for this machine ($comput +erName, $domainName)\n" if (!defined($ip)); # Substitute in the URL to the found $ip $strURL =~ s/$domainName/$ip/; } # Perform the GET command print "Performing GET on $strURL\n"; my $ua = LWP::UserAgent->new; $ua->timeout(15); # We can define the httpHost my $request; if (defined($httpHost)) { $request = HTTP::Request->new(GET => $strURL, HTTP::Headers->n +ew(Host => $httpHost)); } else { $request = HTTP::Request->new(GET => $strURL); } my $response = $ua->request($request); my $nCode = $response->code; my $nMonitorValue; my $nReturnCode = $response->code; print "Return Code = $nReturnCode\n"; # Check the returned code and format the opcmon command if ($nReturnCode == 200) { if ($checkResponse) { if ($response->content =~ /SUCCEEDED/m) { $nMonitorValue = 0; } else { $nMonitorValue = 1; } } else { $nMonitorValue = 0; } } else { $nMonitorValue = 1; } # Send the opcmon print "opcmon value = $nMonitorValue\n"; my $strCmd ="opcmon $strMonitorName=$nMonitorValue"; my $exit = system($strCmd); exit $exit; }

Replies are listed 'Best First'.
Re^3: perl cpu usage on windows machines
by CountZero (Bishop) on Jul 14, 2008 at 14:57 UTC
    What does the opcmon command do? Could it be that this command maxes out your CPU?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      opcmon is a small command to inform our monitoring system about changes in status. It consumes a little amount of CPU (tested).
Re^3: perl cpu usage on windows machines
by GrandFather (Saint) on Jul 14, 2008 at 22:05 UTC

    How long does the script run?

    There is only one loop in the script and it should run very quickly unless the hosts file is huge. The remainder of the script ought to be I/O bound, although a virus checker may gobble up chunks of cpu depending on how it is configured.

    You could insert a few print statements for light weight profiling or trot out Devel::SmallProf to get a handle on where the time is going.


    Perl is environmentally friendly - it saves trees

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://697452]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-20 02:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found