【问题标题】:Empty hash and 'Use of uninitialized value in numeric ge (>=)'空哈希和“在数字 ge (>=) 中使用未初始化的值”
【发布时间】:2015-12-04 09:50:32
【问题描述】:

我有一个关于我在github 上找到的代码的问题,并根据我的需要进行了更改。它通常工作正常,但我收到一些让我担心的警告。

Use of uninitialized value in numeric ge (>=) at ./check_puppetdash line 192.
Use of uninitialized value in numeric ge (>=) at ./check_puppetdash line 192.
Use of uninitialized value in numeric ge (>=) at ./check_puppetdash line 192.
Use of uninitialized value in numeric ge (>=) at ./check_puppetdash line 192.
Use of uninitialized value in numeric ge (>=) at ./check_puppetdash line 192.
Use of uninitialized value in numeric ge (>=) at ./check_puppetdash line 192.

第 192 行如下所示:

189 # Gather values for status details
190 my $details = '';
191 foreach (@interests) {
192    if ($nodes{$_} >= $thresholds_warning{$_}){          
193        $details = $details . $_ . " = " . $nodes{$_} . "; ";
194  }
195 }

我将它固定到上面定义的哈希 %nodes:

# Parse the markup, loading up anything that matches into the %nodes hash
my %nodes;
foreach my $line(split('\n', $response->decoded_content)) {
if($line=~m/<a href="\/nodes\/(\w+)">(\d+)<\/a>/) {
    $nodes{$1} = $2;
 }
}

#Gather perfomance data
foreach my $interest(@interests) {
$np->add_perfdata(
    label => $interest,
    value => defined($nodes{$interest}) ? $nodes{$interest} : 0,
    uom   => undef,
 );
}

我使用了现有脚本中的那部分。 当我在脚本中的任何位置打印 %nodes 的内容时,我得到一个空行。

我在 perl 方面不是很有经验,希望能得到一些提示。

PS:我也联系了原代码的开发者。

【问题讨论】:

  • HTML a 标签上的正则表达式匹配可能不起作用。在 %nodes 代码中添加print "$line\n"; 以查看内容是什么。可以是任意数量的东西(大写的A、标签中的空格等)。这就是为什么通常不建议使用正则表达式解析 HTML/XML 的原因。
  • 或者,查看您正在解析的源 HTML。在这里提供一个示例也会有所帮助。

标签: perl nagios


【解决方案1】:

这个问题很难回答,因为它的根源是这一行:

if($line=~m/<a href="\/nodes\/(\w+)">(\d+)<\/a>/) {

空的%nodes 表示不匹配。猜测一下,这是因为 URL 格式发生了细微的变化——或者可能是在链接中插入了换行符,或者......好吧,真的是什么。

匹配:

<a href="/nodes/node1">10</a>

你会得到一个哈希元素:

 node1 => 10

但它不会匹配:

<a href="/root/nodes/node1">10</a>
<a href="/nodes/node-1">10</a>
<a href="nodes/node1">10</a>
<a href="/nodes/node1">10 </a>
<a href="/nodes/node1">
   10
</a>

<a href="/nodes/node1">node1</a>
<A HREF="/nodes/node1">10</A>

等等

这就是为什么基于正则表达式的 HTML 解析是一个坏主意的原因之一。解决这个问题需要查阅源 HTML 并找到一个匹配的新正则表达式 - 但是没有 URL 或示例 HTML,我们无能为力。

我建议你需要的是插入:

print $response->decoded_content

看看你在a hrefs 中得到了什么。

【讨论】:

  • 谢谢,我在使用“print $response->decoded_content”时发现了错误。它被定向到错误的 url,因为在执行脚本时使用了错误的参数。脚本本身很好。
  • @chr126,我不会认为它“很好”,直到它在给定错误参数时抛出更有用的错误。想象一下下一个人会如何倒下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多