gpt4 book ai didi

linux - 脚本无法正确获取

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:36:26 27 4
gpt4 key购买 nike

#!/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";
}

我的 nginx 状态页面:

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

运行:

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

我应该得到:

nginx_accepts:113869 nginx_handled:113869 nginx_requests:122594

知道哪里出了问题吗?

最佳答案

您的代码声明:

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

你的输出是:

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

因此,因为你测试:

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

... 因此 $content[2] 不包含您的正则表达式要求的信息。您是否尝试过添加 print $content[2] 。 "\n"; 来帮助调试您的脚本?

调试器

请考虑使用 Perl 调试器。它会非常快速地为您解决明显的问题。

使用 -d 标志启动您的脚本。然后逐行执行代码,使用 n 转到下一行,使用 l 列出当前点的代码,使用 x @content (例如)列出每一步的变量内容。

关于linux - 脚本无法正确获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17551886/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com