首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Perl:输出检查例程返回错误状态

Perl:输出检查例程返回错误状态
EN

Stack Overflow用户
提问于 2014-03-20 08:06:58
回答 2查看 77关注 0票数 0

大家好!

我对Perl几乎一无所知,但必须处理一个脚本,该脚本旨在处理Celerra的几个系统检查命令的输出。其中一个例程总是在$rc变量中返回"2“,而据我所知,它应该是"0”。

下面是脚本中用于处理输出的部分:

代码语言:javascript
复制
#(7) Subroutine to handle 'enclosure_status -v -e 0'
#    Sample command output:
# DEVICE A                         DEVICE B
#---------------ENCLOSURE ALARMS----------------
# Pass        PSA OK               Pass
sub enclosure_status {
#   Find column positions given variable word count, space delimited output
    $deva_ix=index($result[1],"DEVICE A");  
    $devb_ix=index($result[1],"DEVICE B");  
    $msg="";
    $rc=-1;
    $skip=1;    # skip items other than alarms & errors
    foreach (@result) {
    if ($deva_ix eq -1) { last; }   # command output not as expected
    if (length($_) < $devb_ix+4) { next; }      # blank line
    if ($_ =~ m/------/) {      # process block header
        if ($rc eq -1) { $rc=0; }
        if ($_ =~ /ALARM/) { $skip=0; next; }
        if ($_ =~ /ERRORS/) { $skip=0; next; }
        if ($_ =~ /FRU STATUS/) { $skip=0; next; }
        $skip=1; next; }
    if ($skip eq 1) { next; }
        if (substr($_,$deva_ix,4) eq "Pass" 
        and substr($_,$devb_ix,4) eq "Pass") { next; }
        $rc=2;
        if ($msg ne "") { $msg = $msg . ", "; }
    $msg = $msg . $_;
        } # end foreach
    if ($rc eq 0) {
        $msg="All alarms are in condition Pass"; }
    elsif ($rc<0) {
        $rc=3; $msg="Unexpected results from command " . $parm[1]; }
    &print_results($host,$parm[0],$rc,$msg);
    } # end subroutine

这是命令输出的一部分:

代码语言:javascript
复制
    DEVICE A                         DEVICE B
-------------------PRESENCE--------------------
 00 60 16 43 C7 EB    MAC     00 60 16 43 C9 85
 Scorpion    Hardware Platform    Scorpion
 Present     Peer Compute Blade   Present
 Present     Compute Blade        Present
 Powered On  Compute Blade        Powered On
 Present     Power Supply A       Present
 Present     Power Supply B       Present
 Inactive    Manufacturing Mode   Inactive
 Inactive    Margin High Mode     Inactive
 Inactive    Margin Low Mode      Inactive
 Inactive    MP I2C Bus in Reset  Inactive
---------------BROADCOM STATUS-----------------
 Active      Dnlink/P4 FullDuplex Active
 Active      Dnlink/P4 100 MBPS   Active
 Active      Dnlink/P4 Up         Active
 Active      PeerSP/P3 FullDuplex Active
 Active      PeerSP/P3 100 MBPS   Active
 Active      PeerSP/P3 Up         Active
 Active      Uplink/P1 FullDuplex Active
 Active      Uplink/P1 100 MBPS   Active
 Active      Uplink/P1 Up         Active
 Active      SP    /P0 FullDuplex Active
 Active      SP    /P0 100 MBPS   Active
 Active      SP    /P0 Up         Active
---------------ENCLOSURE ALARMS----------------
 Pass        PSA OK               Pass
 Pass        PSA Overtemp         Pass
 Pass        PSA AC OK            Pass
 Pass        PSB OK               Pass
 Pass        PSB Overtemp         Pass
 Pass        PSB AC OK            Pass
----------------BROADCOM ALARMS----------------
 Pass        5325 Comm Status     Pass
 Pass        5325 MIB RAM         Pass
 Pass        5325 MIN MEM         Pass
 Pass        5325 BUFF Con        Pass
--------------RESUME CSUM ERRORS---------------
 Pass        PSA Csum Error       Pass
 Pass        PSB Csum Error       Pass
 Pass        CB x Csum Error      Pass
 Pass        Midplane Csum Error  Pass
 Pass        Resume Read Timeout  Pass
----------------COLDFIRE ALARMS----------------
 Pass        P On Self Flash Status Pass
 Pass        P On Self RAM Status  Pass
 Pass        P On Self FEC Status  Pass
 Pass        P On Self 5325 Status  Pass
 Pass        Run Self Flash Status  Pass
 Pass        Run Self RAM Status  Pass
 Pass        Diag 5325 Status     Pass
 Pass        Encl Resume Block    Pass
 Pass        Peer RS232 traffic   Pass
 Pass        MidPlane ID Read     Pass
 Pass        I2C Arbit Error      Pass
 Pass        I2C MP Bus Error     Pass
-------------STATUS CONDITIONS-----------------
 Valid       Peer Coldfire Status Valid
 Valid       Fault Expander       Valid
 Valid       Data Mover Status    Valid
 Valid       PSB Status           Valid
 Valid       PSA Status           Valid
------------------FRU STATUS-------------------
 Pass        FRU CPU DIMM 0       Pass
 Pass        FRU CPU DIMM 1       Pass
 Pass        FRU CPU DIMM 2       Pass
 Pass        FRU CPU DIMM 3       Pass
 Pass        FRU CPU Module       Pass
 Pass        FRU CPU IO Module    Pass
 Pass        NAS Personality Card Pass
 Pass        FRU Enclosure        Pass
 Pass        FRU Coldfire         Pass
 Pass        FRU Power Supply B   Pass
 Pass        FRU Power Supply A   Pass
---------------SYSTEM VARIABLES----------------
 02          Slot ID              03
 00          Enclosure ID         00
 00          BackPlane ID         01
 61          Post Code            61
 05          Reason Code          05
 2C          Blade Status Code    2C
 80          Post Middle 8 bits   80
 00          I2C Error Mask       00
 00          I2C MP Error Mask    00

该脚本形成要发送到服务器的消息,如下所示:

代码语言:javascript
复制
emcns120        Alarms  2        Pass P On Self Flash Status Pass, Pass P On Self RAM Status Pass, Pass P On Self FEC Status Pass, Pass P On Self 5325 Status Pass, Pass Run Self Flash Status Pass

任何帮助都将不胜感激!

鲍里斯

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-20 15:38:33

这里的问题是,您认为Pass/Fail状态总是位于同一列的假设被违反了。如果您仔细查看您的COLDFILE警报,您会发现在某些情况下,最终状态被空格推开,导致您的例程失败。

我建议在你所关心的部分中分割空格上的行,并总是将行中的第一个单词和最后一个单词与"Pass“进行比较,看看是否一切都是正确的。以下是更新的代码:

代码语言:javascript
复制
sub enclosure_status {
$msg="";
$rc=-1;
$skip=1;    # skip items other than alarms & errors
foreach (@result) {
if ($_ =~ m/------/) {      # process block header
    if ($rc eq -1) { $rc=0; }
    if ($_ =~ /ALARM/) { $skip=0; next; }
    if ($_ =~ /ERRORS/) { $skip=0; next; }
    if ($_ =~ /FRU STATUS/) { $skip=0; next; }
    $skip=1; next; }
if ($skip eq 1) { next; }
@words = split;
if ($words[0] eq "Pass" && $words[-1] eq "Pass") { next; }
    $rc=2;
    if ($msg ne "") { $msg = $msg . ", "; }
$msg = $msg . $_;
    } # end foreach
if ($rc eq 0) {
    $msg="All alarms are in condition Pass"; }
elsif ($rc<0) {
    $rc=3; $msg="Unexpected results from command " . $parm[1]; }
&print_results($host,$parm[0],$rc,$msg);
} # end subroutine

我试着坚持你上面的风格。

票数 0
EN

Stack Overflow用户

发布于 2014-03-20 10:15:09

好吧..。似乎有一个名为@result的数组,希望包含字符串。对于这些字符串中的每一个,检查以$deva_ix$devb_ix开头的两个字母子字符串是否等于字符串"Pass",如果即使有一次没有,则以$rc等于2结尾。

然而,奇怪的是,$deva_ix$devb_ix被设置为子字符串"DEVICE A""DEVICE B"@result第二个字符串中的位置。虽然跳过包含------的标头以及在头中不包含"ALARM""ERROR""FRU STATUS"的部分,但是您的示例文件(我只是假设这些行存储在@result中)不是不完整的,就是不兼容的,因为它在第二行中没有设备名称。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22526919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档