我正在为Nagios开发一个用于F5负载均衡器的Perl插件。我必须将池名转换为与SNMP的OID匹配的十进制格式。
my ( $PoolName ) = $ARGV[1];
my ( $rootOIDPoolStatus ) = '1.3.6.1.4.1.3375.2.2.5.5.2.1.2';
例如,$PoolName
是"/Common/Atlassian"
,我需要将其转换为/.C.o.m.m.o.n./.A.t.l.a.s.s.i.a.n
,然后转换为47.67.111.109.109.111.110.47.65.116.108.97.115.115.105.97.110
一旦转换完毕,它们就会被拉进一个变量中。
my ( $PoolStatus ) = "$rootOIDPoolStatus.$OIDPoolName"
我一直在为Nagios对其他人的Perl插件进行逆向工程,这就是其他人正在做的事情,但无论我在做什么样的组合,我都无法让它工作。他们的$name
是我的$PoolName
sub to_oid($) {
my $oid;
my ($name) = $_[0];
return "" if ( ! $name );
$oid = ( length $name ) . '.' . ( join '.', ( map { unpack 'C', $ } ( split '',$name ) ) );
return $oid;
}
有人能帮助我构建或理解Perl逻辑,以便将$PoolName
转换为OID所需的十进制格式吗?
发布于 2017-10-26 10:51:43
您似乎在使用字符串作为SNMP表的索引。表的索引可视为该表的行号或行id。通常,表的索引只是一个数字,从1开始,并随着表的每一行的增加而增加。这样的数字是按原样在OID中编码的,也就是说,如果表有3列和2行,它们将具有以下OID:
$base.1 # table
$base.1.1 # table entry
$base.1.1.1.1 # col1, row1
$base.1.1.1.2 # col1, row2
$base.1.1.2.1 # col2, row1
$base.1.1.2.2 # col2, row2
$base.1.1.3.1 # col3, row1
$base.1.1.3.2 # col3, row2
^---index
有时索引是一个IP地址,一个IP :端口的组合,或者两个IP地址的组合,特别是对于与IP相关的表。作为索引的IP地址如下所示:
$base.1 # table
$base.1.1 # table entry
$base.1.1.1.1.0.0.127 # col1, row "127.0.0.1"
$base.1.1.1.0.0.0.0 # col1, row "0.0.0.0"
$base.1.1.2.1.0.0.127 # col2, row "127.0.0.1"
$base.1.1.2.0.0.0.0 # col2, row "0.0.0.0"
$base.1.1.3.1.0.0.127 # col3, row "127.0.0.1"
$base.1.1.3.0.0.0.0 # col3, row "0.0.0.0"
^^^^^^^---- index
如您所见,索引的长度取决于其数据类型(有一个专用的IPV4数据类型)。
有时,索引是一个字符串(在您的情况下)。当使用字符串时,还必须以某种方式对其进行编码,以构成表的“行号”。字符串作为索引按字符顺序编码,并以其长度为先导,即:
$base.1 # table
$base.1.1 # table entry
$base.1.1.1.2.65.66 # col1, row "AB"
$base.1.1.1.3.120.121.122 # col1, row "xyz"
$base.1.1.2.2.65.66 # col2, row "AB"
$base.1.1.2.3.120.121.122 # col2, row "xyz"
$base.1.1.3.2.65.66 # col3, row "AB"
$base.1.1.3.3.120.121.122 # col3, row "xyz"
^^^^^^^^^^^^^---- index
所以"AB“变成了"2.65.66”,因为length('AB')==2
和ord('A')==65
,ord('B')==66
。同样,"xyz“变成"3.120.121.122”。
您的函数to_oid
正是这样做的,尽管我会将它简化如下:
#!/usr/bin/env perl
use strict;
use warnings;
sub to_oid
{
my $string = shift;
return sprintf('%d.%s', length($string), join('.', unpack('C*', $string)));
}
my $rootOIDPoolStatus = '1.3.6.1.4.1.3375.2.2.5.5.2.1.2';
my $PoolName = '/Common/Atlassian';
my $poolname_oid = to_oid($PoolName);
my $complete_oid = "$rootOIDPoolStatus.$poolname_oid";
print $complete_oid, "\n";
输出:
1.3.6.1.4.1.3375.2.2.5.5.2.1.2.17.47.67.111.109.109.111.110.47.65.116.108.97.115.115.105.97.110
|<------- rootOID ----------->|<------------ poolname_oid ----...--->|
发布于 2017-10-25 20:39:43
my $poolStatus = join '.', $rootOIDPoolStatus, map ord, split //, $poolName;
不确定代码中的长度()是什么,在示例中没有显示类似的内容。
发布于 2017-10-25 21:15:04
my $PoolStatus = join('.', $rootOIDPoolStatus, unpack('C*', $PoolName));
或
my $PoolStatus = sprintf("%s.%vd", $rootOIDPoolStatus, $PoolName);
https://stackoverflow.com/questions/46941145
复制相似问题