简体   繁体   English

将数组拆分为块,对每个块执行 snmp_get_request,重新组合生成的 hash 引用

[英]Split array into chunks, execute snmp_get_request on each chunk, re-combine the resulting hash references

I'm having an issue attempting to use a Nagios plugin that utilizes Net::SNMP.我在尝试使用利用 Net::SNMP 的 Nagios 插件时遇到问题。 It attempts to query a large number of OIDs at the same time, resulting in an error, as the response would exceed the maximum MTU for the link.它尝试同时查询大量 OID,从而导致错误,因为响应将超过链路的最大 MTU。 (The message size 2867 exceeds the maxMsgSize 1472.) (消息大小 2867 超过了 maxMsgSize 1472。)

The code for this section is as follows:本节代码如下:

$result = $session->get_request(
   Varbindlist => \@oids
);

Is there a way in Perl to Perl 中有没有办法

  1. Split @oids into smaller pieces将@oids 拆分成更小的部分
  2. Iterate over these pieces遍历这些片段
  3. Combine the return $results into a single reference to a single hash?将返回的 $results 合并为对单个 hash 的单个引用?

That would be the smallest modification to make to the script to have it support larger amounts of interfaces, correct?这将是对脚本进行的最小修改以使其支持更多的接口,对吗?

1) Split @oids into smaller pieces
2) Iterate over these pieces

use splice() to break up the list into smaller lists.使用 splice() 将列表分解为更小的列表。 If you want ten at a time:如果你一次想要十个:

while (@oids) {
    my @sublist = splice @oids, 0, 10;
    # do something with the 10 (or less) elements in @sublist
}

This code is untested, but I'm providing it as a general idea as to how you might divide up the list and run with it.这段代码未经测试,但我提供它作为关于如何划分列表并使用它运行的一般想法。

my $divisions = int ( @oids / 10 );
my $offset = 0;
my @oids_list;
while ( $offset <= $#oids ) {
    my $top = $offset + $divisions;
    $top = $top <= $#oids ? $top : $#oids
    push @oids_list, [ @oids[ $offset .. $top ] ];
    $offset += $divisions + 1;
}
my @results;

foreach my $oids_ref ( @oids_list ) {
    push @results, $session->get_request(
        Varbindlist => $oids_ref
}

Now if my calculations are correct you will have @results, which will be a list of the the return values from $session->get_request() per iteration.现在,如果我的计算正确,您将拥有@results,它将是每次迭代的 $session->get_request() 的返回值列表。 I don't know what that looks like;我不知道那是什么样子; maybe you just concatenate it together.也许你只是将它连接在一起。 That's your part to figure out.那是你要弄清楚的部分。 ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM