SyntaxHighlighter

domingo, 29 de julio de 2012

Analizando logs de Flash Media Server con Perl

El log de Flash Media Server es muy completo, pero al mismo tiempo difícil de analizar con herramientas estándar de análisis de logs. La solución que utilicé la última vez que vinieron pidiendo estadísticas urgentes fue importar el log en LibreOffice Calc e intentar sacar los datos lo más rápido posible para salir del paso.

Como hace tiempo que no toco Perl (al final siempre acabo utilizando PHP porque me es más familiar), he decidido hacer un pequeño script para obtener algunos datos estadísticos de las aplicaciones que se ejecutan en Flash Media Server.

El script recibe el nombre del fichero log y la aplicación FMS sobre la que queremos información, hace un recuento de las ips que se conectan entre los eventos publish y unpublish e imprime un resumen. Me centro en aplicaciones tipo "live" en las que un cliente publica y el resto conectan para seguir la emisión de ese cliente.

Aquí el código:
#!/usr/bin/perl

(scalar @ARGV == 2) || die ("Wrong number of arguments.\nUse: $0 filename app_name \n");

$filename = $ARGV[0];
$appname = $ARGV[1];


$APP_NAME_FIELD = 12;
$DATE_FIELD = 2;
$TIME_FIELD = 3;
$CLIENT_IP_FIELD = 16;


open FILE, $filename or die "File not found!!\n";

$publishing = 0;
while (<FILE>){
 @line = split;
 
 if (  (($line[0]) eq "publish") and (($line[$APP_NAME_FIELD]) eq $appname)   ){
  $publishing = 1;
  print "Application '$appname' started at $line[$TIME_FIELD] ($line[$DATE_FIELD])\n"; 
 }
 
 if (  (($line[0]) eq "unpublish") and (($line[$APP_NAME_FIELD]) eq $appname)   ){
  $publishing = 0;
  
  print "Application '$appname' stopped at $line[$TIME_FIELD] ($line[$DATE_FIELD])\n"; 
  
  # Print ips and number of connections of each one.
  print scalar keys %ip_array ," different clients received the broadcast\n";
  print "IP \t #Connections\n";
  foreach $ip (keys %ip_array){
   print "$ip \t $ip_array{$ip}\n";
  }
  undef %ip_array;
  print "********************************************************************\n";
 }
 
 if ($publishing){
  if (  (($line[0]) eq "connect") and (($line[$APP_NAME_FIELD]) eq $appname)   ){
   if (exists ( $ip_array{$line[$CLIENT_IP_FIELD]} ) ){
    $ip_array{$line[$CLIENT_IP_FIELD]}++;
   }else{
    $ip_array{$line[$CLIENT_IP_FIELD]}=1;
   }
  }  
 }

}

El resultado es el siguiente:

new-host-2:fms_log emilio$ ./fms_stats.pl access.2012032100.log live 
Application 'live' started at 11:13:22 (2012-03-21)
Application 'live' stopped at 12:11:26 (2012-03-21)
15 clients received the broadcast
IP   #Connections
90.170.72.143   1
172.20.18.215   1
111.111.23.53   2
111.111.17.203   2
111.111.23.152   1
172.20.8.133   1
172.20.18.83   2
83.33.247.36   1
172.20.17.167   1
111.111.31.78   1
111.111.23.173   1
111.111.23.55   1
111.111.204.147   1
83.32.145.161   1
111.111.22.139   5
********************************************************************
Application 'live' started at 17:08:00 (2012-03-21)
Application 'live' stopped at 17:08:08 (2012-03-21)
0 clients received the broadcast
IP   #Connections
********************************************************************
Application 'live' started at 17:09:26 (2012-03-21)
Application 'live' stopped at 17:14:16 (2012-03-21)
1 clients received the broadcast
IP   #Connections
111.111.104.51   1
********************************************************************
Application 'live' started at 17:14:50 (2012-03-21)
Application 'live' stopped at 18:48:12 (2012-03-21)
4 clients received the broadcast
IP   #Connections
90.170.72.143   2
111.111.36.4   3
217.216.52.82   1
111.111.104.51   5
********************************************************************



No hay comentarios: