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

发布于 2024-11-14 17:36:18 字数 380 浏览 2 评论 0原文

我在尝试使用利用 Net::SNMP 的 Nagios 插件时遇到问题。它尝试同时查询大量 OID,从而导致错误,因为响应将超过链路的最大 MTU。 (消息大小 2867 超过了 maxMsgSize 1472。)

本节的代码如下:

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

在 Perl 中是否有一种方法可以将

  1. @oids 分割成更小的片段
  2. 迭代这些片段 将
  3. 返回的 $results 组合成对单个散列的单个引用?

这将是对脚本进行的最小修改,以使其支持更大量的接口,对吗?

I'm having an issue attempting to use a Nagios plugin that utilizes Net::SNMP. 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. (The message size 2867 exceeds the maxMsgSize 1472.)

The code for this section is as follows:

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

Is there a way in Perl to

  1. Split @oids into smaller pieces
  2. Iterate over these pieces
  3. Combine the return $results into a single reference to a single hash?

That would be the smallest modification to make to the script to have it support larger amounts of interfaces, correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七禾 2024-11-21 17:36:18
1) Split @oids into smaller pieces
2) Iterate over these pieces

使用 splice() 将列表分解为更小的列表。如果您一次想要十个:

while (@oids) {
    my @sublist = splice @oids, 0, 10;
    # do something with the 10 (or less) elements in @sublist
}
1) Split @oids into smaller pieces
2) Iterate over these pieces

use splice() to break up the list into smaller lists. 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
}
想念有你 2024-11-21 17:36:18

该代码未经测试,但我提供它作为关于如何划分列表并使用它运行的一般想法。

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
}

现在,如果我的计算正确,您将得到 @results,它将是每次迭代 $session->get_request() 的返回值列表。我不知道那是什么样子;也许你只是将​​它连接在一起。这是你要弄清楚的部分。 ;)

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. I don't know what that looks like; maybe you just concatenate it together. That's your part to figure out. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文