如何解析报告并生成不同条件的摘要?

时间:2016-02-02 07:13:46

标签: perl

我想删除文件中的特定行。

我想要的结果是:

The Nodes that Running are: ls_sfda-new,risto4,
The Nodes that isnt Running are: atum1,
The Nodes that Buffering are: vmatum3,
The Nodes that Cannot get status are: vcmobile,

我的文字(包含约60个节点):

Node atum1:
HPOM Managed Node status :
-------------------------
OV Performance Core       coda        (3568) isnt running
OV Communication Broker   ovbbccb     (2348) is running
OV Control                ovcd        (2088) is running
OV Config and Deploy      ovconfd     (2516) is running
Subagent EA:
Action Agent              opcacta     (3372) is running
Logfile Encapsulator      opcle       (3788) is running
Monitor Agent             opcmona     (3668) is running
Message Agent             opcmsga     (3456) is running
Message Interceptor       opcmsgi     (3600) isnt running
Done.

Node vmatum3:
HPOM Managed Node status :
-------------------------
OV Performance Core       coda        (3128) is running(buffering)
OV Communication Broker   ovbbccb     (2112) is running
OV Control                ovcd        (444) is running
OV Config and Deploy      ovconfd     (2336) is running
Subagent EA:
Action Agent              opcacta     (3088) is running
Logfile Encapsulator      opcle       (3180) is running
Monitor Agent             opcmona     (3424) is running
Message Agent             opcmsga     (3236) is running
Message Interceptor       opcmsgi     (3348) is running
Done.

Node ls_sfda-new:
HPOM Managed Node status :
-------------------------
OV Control                ovcd        (2236) is running
OV Performance Core       coda        (3132) is running
OV Communication Broker   ovbbccb     (2780) is running
OV Config and Deploy      ovconfd     (3156) is running
Subagent EA:
Action Agent              opcacta     (256) is running
Logfile Encapsulator      opcle       (300) is running
Monitor Agent             opcmona     (392) is running
Message Agent             opcmsga     (400) is running
Message Interceptor       opcmsgi     (960) is running
Done.

Node risto4:
HPOM Managed Node status :
-------------------------
OV Performance Core       coda        (3348) is running
OV Communication Broker   ovbbccb     (1848) is running
OV Control                ovcd        (2276) is running
OV Config and Deploy      ovconfd     (4664) is running
Subagent EA:
Action Agent              opcacta     (4872) is running
Logfile Encapsulator      opcle       (2912) is running
Monitor Agent             opcmona     (2224) is running
Message Agent             opcmsga     (2392) is running
Message Interceptor       opcmsgi     (3436) is running
Done.

Node vcmobile:
Cannot get status information from node vcmobile.  (OpC40-428)
Network communication problems occurred. (OpC40-427)

-------------------------------------------------------------------------------
 CTRL - CommunicationException:
-------------------------------------------------------------------------------
(ctrl-21) Communication error when executing 'Status' method.
 (xpl-333) recv() on '[10.24.33.40]:383' failed.
  (RTL-104) Connection reset by peer (OpC40-2130)
Failed.

我的代码(我知道我需要一个elsif循环,但我需要一些帮助):

#!/usr/bin/perl
use strict;

open (FILE, "/tmp/agentstas.dat");
my @lines = <FILE>;
my $lines;
my $string = "Node";
my $badstring = "hpom";
my $buffering = "buffering";
my $Running = "Running";
my $isnt = "isnt";
my $Cannot = "Cannot";

foreach $lines (@lines) {
    if ($lines =~ /^$string/i || $lines !~ /^$badstring/i) { 
        print "$lines";
    }

1 个答案:

答案 0 :(得分:2)

这比使用$/ - 记录分隔符更容易。将其设置为双线换行,然后您可以匹配记录,而不是单独的行:

#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;

local $/ = "\n\n";

open ( my $input, '<', '/tmp/agentstas.dat') or die $!;
while ( <$input> ) {
    my ($name) = m/Node (\w+)/; 
    my %proc_state = m/(\w+)\s+\(\d+\) (.*)/g; 
    print Dumper \%proc_state; 

    if ( m/isnt running/ ) { 
          print "$name not running\n"; 
    }

    if ( m/buffering/ ) { 
         print "$name buffering\n";
    }

    if ( m/Cannot get status information/ ) {
         print "$name no status information\n"
    }

    if ( defined $name and not keys %proc_state ) { 
        print "No process states from $name\n"; 
    }
}
close ( $input );