Question

#!/usr/bin/perl

if (! eval "require LWP::UserAgent;")
{
        $ret = "LWP::UserAgent not found";
}

if ( exists $ARGV[0]) {
        if ($ret)
        {
                print "no ($ret)\n";
                exit 1;
        }

        my $ua = LWP::UserAgent->new(timeout => 5);

        my $response = $ua->request(HTTP::Request->new('GET',$ARGV[0]));
        my @content = split (/\n/, $response->content);

        my $active_connections = -1;
        if ($content[0] =~ /^Active connections:\s+(\d+)\s*$/i) {
                $active_connections = $1;
        }

        my $accepts = -1;
        my $handled = -1;
        my $requests = -1;
        if ($content[2] =~ /^\s+(\d+)\s+(\d+)\s+(\d+)\s*$/) {
                $accepts = $1;
                $handled = $2;
                $requests = $3;
        }

        my $reading = -1;
        my $writing = -1;
        my $waiting = -1;
        if ($content[3] =~ /Reading: (\d+) Writing: (\d+) Waiting: (\d+)\s*$/) {
                $reading = $1;
                $writing = $2;
                $waiting = $3;
        }

        print "nginx_active:$active_connections nginx_reading:$reading nginx_writing:$writing nginx_waiting:$waiting ";
        print "\n";
}

My nginx status page:

Active connections: 2 
server accepts handled requests
 2 2 2 
Reading: 0 Writing: 1 Waiting: 1

running:

./get_nginx_clients_status.pl http://IP/nginx_status
nginx_active:-1 nginx_reading:-1 nginx_writing:-1 nginx_waiting:-1

I should be getting:

nginx_accepts:113869 nginx_handled:113869 nginx_requests:122594

Any idea what could be wrong?

Was it helpful?

Solution

Your code states:

my $accepts = -1;
my $handled = -1;
my $requests = -1;

And your output is:

nginx_active:-1 nginx_reading:-1 nginx_writing:-1 nginx_waiting:-1

Therefore, because you test:

$content[2] =~ /^\s+(\d+)\s+(\d+)\s+(\d+)\s*$/

... it follows that $content[2] does not contain the information your regular expression demands. Have you tried adding print $content[2] . "\n"; to help debug your script?

The Debugger

Please consider use of the Perl debugger. It will answer obvious problems very quickly for you.

Start your script using the -d flag. Then step through your code one line at a time using n to go to the next line, l to list the code at the current point, and x @content (for example) to list the contents of variables each step of the way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top