正则表达式提取Asterisk队列统计信息

时间:2012-11-20 14:26:03

标签: regex perl asterisk

我正在尝试构建一些正则表达式来提取Asterisk中队列状态的统计信息。我对正则表达式相对较新,所以我的解决方案还很遥远。我有以下输出要解析:

Parsing /etc/asterisk/extconfig.conf
0009*007 has 2 calls (max unlimited) in 'ringall' strategy (0s holdtime), W:0, C:0, A:7, SL:0.0% within 0s
   Members: 
      0009*001 (Local/0009*001@queue/nj) (In use) has taken no calls yet
   Callers: 
      1. SIP/chan5-000a29f2 (wait: 0:08, prio: 0)
      2. SIP/0139*741-000a29f7 (wait: 0:03, prio: 0)

实际输出将包含多个队列的信息,因此它将从第二行重复。第一行只显示一次。

我需要最终得到队列ID(在本例中为0009 * 007)以及一个具有各自等待时间的呼叫列表。

到目前为止,我已使用以下正则表达式来匹配队列号:

\b^[0-9]{4}\*[0-9]{3}\b

但这不起作用。不确定如何将呼叫与等待时间匹配。 理想情况下,我想输出如下:

0009*007,1,0:08
0009*007,2,0:03

我最有可能在Perl中编写最终脚本。

2 个答案:

答案 0 :(得分:2)

这是一个简单的状态机解决方案。正则表达式可能需要根据您在日志文件中期望的变化类型进行更改。

use Modern::Perl;

my $current_queue;
my $in_callers = 0;
while (<DATA>)
{
    if (!defined $current_queue)
    {
         /(\d{4}\*\d{3})/ and $current_queue = $1; 
    }
    elsif (!$in_callers)
    {
        /Callers:/ and $in_callers++;
    }
    elsif (/^\s*(\d+)\..*wait:\s+(\d+:\d+),\s+prio:\s+(\d+)/)
    {
        say "$current_queue,$1,$2,$3";
    }
    else
    {
        #end of this queue; reset.
        undef $current_queue; $in_callers = 0; 
    }
}

__DATA__
Parsing /etc/asterisk/extconfig.conf
0009*007 has 2 calls (max unlimited) in 'ringall' strategy
   Members: 
      0009*001 (Local/0009*001@queue/nj) (In use) has taken no calls yet
   Callers: 
      1. SIP/chan5-000a29f2 (wait: 0:08, prio: 0)
      2. SIP/0139*741-000a29f7 (wait: 0:03, prio: 0)

答案 1 :(得分:2)

正如m.buettner所说,你将无法用一个正则表达式做到这一点。您可以依赖于您对数据重复性质的了解来生成包含所需数据的哈希值,然后在最后打印哈希值:

#!/usr/bin/perl

my %queues;
my $current_queue;

while (<DATA>) {
  chomp;

  if (m/^(\d+\*\d+)/) {
    $current_queue = $1;
  }
  elsif (m/^\s+(\d)\..+?\(wait:\s+([\d\:]+),/) {
    $queues{$current_queue}{$1} = $2;
  }
}

foreach my $queue (sort keys %queues) {
  foreach my $caller (sort keys %{ $queues{$queue} }) {
    print join (',', $queue, $caller, $queues{$queue}{$caller}) . "\n";
  }
}

exit;

__DATA__
Parsing /etc/asterisk/extconfig.conf
0009*007 has 2 calls (max unlimited) in 'ringall' strategy (0s holdtime), W:0, C:0, A:7, SL:0.0% within 0s
   Members:
      0009*001 (Local/0009*001@queue/nj) (In use) has taken no calls yet
   Callers:
      1. SIP/chan5-000a29f2 (wait: 0:08, prio: 0)
      2. SIP/0139*741-000a29f7 (wait: 0:03, prio: 0)