analisa um arquivo de log que recebe um argumento (tempo de execução dos testes)

1

Eu preciso analisar um arquivo de log e dizer ao nome do teste se seu tempo de execução é maior do que um período específico (o usuário entrará desta vez, digamos que o script seja executado por mais de 30 minutos)

start security test suite
Mon Sep 05 00:16:30 PDT 2011: creating threads...
Mon Sep 05 00:16:30 PDT 2011: starting...
Mon Sep 05 00:16:31 PDT 2011: Reporting results...
Mon Sep 05 00:16:31 PDT 2011: Writing results to /space/builder/builds/macosx-64/HEAD/qa/scripts/results/add_role_user_security.xml
add_role_user_security.xml : Passed
Mon Sep 05 00:16:31 PDT 2011: creating threads...
Mon Sep 05 00:16:31 PDT 2011: starting...
Mon Sep 05 00:16:32 PDT 2011: Reporting results...
Mon Sep 05 00:16:32 PDT 2011: Writing results to /space/builder/builds/macosx-64/HEAD/qa/scripts/results/privilege.xml
privilege.xml : Passed
Mon Sep 05 00:16:32 PDT 2011: creating threads...
Mon Sep 05 00:16:32 PDT 2011: starting...
Mon Sep 05 00:16:32 PDT 2011: Reporting results...
Mon Sep 05 00:16:32 PDT 2011: Writing results to /space/builder/builds/macosx-64/HEAD/qa/scripts/results/edit_role_user.xml
edit_role_user.xml : Passed
Mon Sep 05 00:16:32 PDT 2011: creating threads...
Mon Sep 05 00:16:32 PDT 2011: starting...
Mon Sep 05 00:16:33 PDT 2011: Reporting results...
Mon Sep 05 00:16:33 PDT 2011: Writing results to /space/builder/builds/macosx-64/HEAD/qa/scripts/results/remove_roles.xml
remove_roles.xml : Passed
Mon Sep 05 00:16:33 PDT 2011: creating threads...
Mon Sep 05 00:16:33 PDT 2011: starting...
Mon Sep 05 00:16:33 PDT 2011: Reporting results...
Mon Sep 05 00:16:33 PDT 2011: Writing results to /space/builder/builds/macosx-64/HEAD/qa/scripts/results/role_user1.xml
role_user1.xml : Passed
Mon Sep 05 00:16:33 PDT 2011: creating threads...
Mon Sep 05 00:16:33 PDT 2011: starting...
Mon Sep 05 00:16:34 PDT 2011: Reporting results...
Mon Sep 05 00:16:34 PDT 2011: Writing results to /space/builder/builds/macosx-64/HEAD/qa/scripts/results/role_user2.xml
bug10611.xml : Passed
security test suite
start fo test suite
Mon Sep 05 00:18:52 PDT 2011: creating threads...
Mon Sep 05 00:18:52 PDT 2011: starting...
Mon Sep 05 00:18:52 PDT 2011: Reporting results...
Mon Sep 05 00:18:52 PDT 2011: Writing results to /space/builder/builds/macosx-64/HEAD/qa/scripts/results/setup_script.xml
setup_script.xml : Passed
Mon Sep 05 00:18:52 PDT 2011: creating threads...
Mon Sep 05 00:18:52 PDT 2011: starting...
Mon Sep 05 00:18:58 PDT 2011: Reporting results...
Mon Sep 05 00:18:58 PDT 2011: Writing results to /space/builder/builds/macosx-64/HEAD/qa/scripts/results/bug_scripts.xml
bug_scripts.xml : Passed
Mon Sep 05 00:18:58 PDT 2011: creating threads...
Mon Sep 05 00:18:58 PDT 2011: starting...
Mon Sep 05 00:18:58 PDT 2011: Reporting results...
Mon Sep 05 00:18:58 PDT 2011: Writing results to /space/builder/builds/macosx-64/HEAD/qa/scripts/results/loadtime_namespace.xml
[Fatal Error] loadtime_namespace.xml:11:25: The processing instruction target matching "[xX][mM][lL]" is not allowed.
Unable to parse loadtime_namespace.xml: Running diff utility to compare...
rm -f ./scripts/diffs/loadtime_namespace.xml
loadtime_namespace.xml : Passed
Mon Sep 05 00:19:01 PDT 2011: creating threads...
Mon Sep 05 00:19:01 PDT 2011: starting...
Mon Sep 05 00:19:01 PDT 2011: Reporting results...
Mon Sep 05 00:19:01 PDT 2011: Writing results to /space/builder/builds/macosx-64/HEAD/qa/scripts/results/string_script.xml
string_script.xml : Passed
fo test suite

O usuário estará executando o script passando o tempo de execução do script

sh script_name.sh 60

E deve ter a lista de testes que levam mais de 60 minutos para serem executados

A saída deve ser como:

security test suite   add_role_user_security.xml
security test suite   privilege.xml
security test suite   remove_roles.xml
security test suite   string_script.xml
fo test suite         string_script.xml
fo test suite         setup_script.xml
    
por anurag 27.10.2016 / 09:22

1 resposta

0

O código na parte inferior desta resposta faz o que você pediu e funciona com base na amostra que você forneceu.

Descrição:

Funciona basicamente em duas etapas:

  • awk usa expressões regulares e variáveis de estado para determinar a transição entre os arquivos e os blocos do conjunto de testes. Ele exibe uma lista delimitada por pipe, em que cada linha possui o nome do conjunto de testes, o arquivo de teste, o horário de início e término.
  • while IFS="|" read ... part pega a saída delimitada por pipe de awk e a lê em algumas variáveis em que uso date para converter registros de data e hora em segundos desde a época e, em seguida, calculo a diferença e compara o valor de entrada.

O código não faz muito em termos de validação, então você mesmo terá que adicionar isso.

Código:

#!/usr/bin/sh

# Get the arguments
INPUT_FILENAME="$1"
FILTER_MINUTES="$2"

# Convert minutes to seconds
FILTER_SECONDS="$(( FILTER_MINUTES * 60 ))"

awk '
BEGIN {
    printf("%s,%s,%s,%s\n","TEST_NAME","FILENAME","START_TIME","END_TIME");
    TEST_NAME="";
    START_TIME="";
    END_TIME="";
  }
match($0, "^start (.+)$", arr) {
    TEST_NAME=arr[1];
    START_TIME="";
    END_TIME="";
    FILENAME="";
    /* printf("TEST_NAME START: %s\n", TEST_NAME); */
  }
match($0, /^(.+.xml) : (.+)$/, arr)  {
    FILENAME=sprintf("%s",arr[1]);
    printf("%s|%s|%s|%s\n",TEST_NAME,FILENAME,START_TIME,END_TIME);
    START_TIME="";
    END_TIME="";
    FILENAME="";
  }
match($0, /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2}) ([^\ ]+) ([0-9]{4}):.*$/, arr) {
    if(START_TIME ~ /^$/)
       START_TIME=sprintf("%s-%s-%s %s:%s:%s %s",arr[3],arr[2],arr[8],arr[4],arr[5],arr[6],arr[7]);
    else
       END_TIME=sprintf("%s-%s-%s %s:%s:%s %s",arr[3],arr[2],arr[8],arr[4],arr[5],arr[6],arr[7]);
  }
$0 ~ "^" TEST_NAME "$" {
    /* printf("TEST_NAME END: %s\n", TEST_NAME); */
    TEST_NAME="";
    START_TIME="";
    END_TIME="";
    FILENAME="";
  }
' "$INPUT_FILENAME" | while IFS="|" read TEST_NAME FILENAME START_TIME END_TIME
do
  START_TIME_SEC=$(date -d "$START_TIME" +%s)
  END_TIME_SEC=$(date -d "$END_TIME" +%s)
  ELAPSED_SEC=$((END_TIME_SEC-START_TIME_SEC))

  # No time supplied or elapsed time exceded.
  if [ -z "$FILTER_MINUTES" -o "$ELAPSED_SEC" -gt "$FILTER_SECONDS" ]
  then
    ## Remove $ELAPSED_SEC from here to not print elapsed seconds.
    echo $TEST_NAME $FILENAME $ELAPSED_SEC
  fi
done
    
por 27.10.2016 / 15:41