Perl ASCII 变量为带“.”的十进制在每个字母之后

Perl ASCII variable to Decimal with "." after every letter

我正在为 F5 负载平衡器制作 Nagios 的 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 所需的十进制格式吗?

my $poolStatus = join '.', $rootOIDPoolStatus, map ord, split //, $poolName;

不确定 length() 在您的代码中的用途,您的示例中没有显示任何类似内容。

my $PoolStatus = join('.', $rootOIDPoolStatus, unpack('C*', $PoolName));

my $PoolStatus = sprintf("%s.%vd", $rootOIDPoolStatus, $PoolName);

您似乎在使用字符串作为 SNMP table 的索引。 table 的索引可以被认为是 行号 行号 table。 table 的索引通常只是一个数字,从 1 开始,随着 table 的每一行增加。这样的数字按原样编码在 OID 中,即如果 table 有 3 列和两行,它们将具有这些 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:port的组合,或者两个IP地址的组合,尤其是IP相关的table。作为索引的 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 数据类型)。

有时索引是一个字符串(如您的情况)。当使用字符串时,它也必须以某种方式编码以构成 table 的 "row number"。作为索引的字符串按字符编码并以其长度开头,即:

$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')==2ord('A')==65ord('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 ----...--->|