Help with PHP in data capture.

Asked

Viewed 63 times

0

Hello, I have a certain log file, and would like to capture certain data from it and store in [].

see the log file:

+++ Statistics Dump +++ (1499195839)
++ Incoming Requests ++
                 552 QUERY
++ Incoming Queries ++
                 546 A
                   1 NS
                   5 AAAA
++ Outgoing Queries ++
[View: default]
                1350 A
                  53 NS
                 446 AAAA
                 544 DS
                  35 DNSKEY
[View: _bind]
++ Name Server Statistics ++
                 552 IPv4 requests received
                   1 requests with EDNS(0) received
                 550 responses sent
                   1 responses with EDNS(0) sent
                 300 queries resulted in successful answer
                 541 queries resulted in non authoritative answer
                   6 queries resulted in nxrrset
                   9 queries resulted in SERVFAIL
                 235 queries resulted in NXDOMAIN
                 385 queries caused recursion
                   2 duplicate queries received
++ Zone Maintenance Statistics ++
++ Resolver Statistics ++
[Common]

Now via php I would like to take for example the 3 line given 552 and print like this: [552].

Thank you.

  • It’s just that or you’ll need to pick up more stuff, like the details in front of the number and split them up ++ in an item of a "multidimensional array"?

  • @Guilhermebirth if I could: [552] query [546] A [1] NS [5]AAAA is already of good size.

  • Okay, but you’ll only need 552 or you’ll need to split into blocks for each "group" (which used the sign ++)? For example: ++ Incoming Queries ++ would be a group and ++ Incoming Requests ++ would be another.

  • I’ll need the 552. Thanks @Guilhermenascimento.

2 answers

0

Can use regex with preg_match_all and read line by line, the regex should look like this (more or less, can adjust):

\s+(\d{1,3})\s[^\n]+\n

^   ^       ^  ^   ^
|   |       |  |   |
|   |       |  |   |
|   |       |  |   +--- Procura a quebra de linha (para separar os resultados)
|   |       |  |
|   |       |  +---Procura qualquer coisa que não seja quebra de linha
|   |       |  
|   |       +-- Verifica se há um espaço após o numero
|   |       
|   +-- Pega o numero (o parenteses é para separar e um novo `match`)
|   
+------ procura qualquer espaço até achar o numero

Code should look like this:

<?php
$foo = file_get_contents('meulog.log');

if (preg_match_all('#\s+(\d{1,3})\s[^\n]+\n#', $foo, $matches)) {
    $retornou = $matches[1];
    print_r($retornou); //Exibe todos numeros que pegou
} else {
    echo 'Nada encontrado';
}

Online example on IDEONE, code:

<?php
$foo = '+++ Statistics Dump +++ (1499195839)
++ Incoming Requests ++
                 552 QUERY
++ Incoming Queries ++
                 546 A
                   1 NS
                   5 AAAA
++ Outgoing Queries ++
[View: default]
                1350 A
                  53 NS
                 446 AAAA
                 544 DS
                  35 DNSKEY
[View: _bind]
++ Name Server Statistics ++
                 552 IPv4 requests received
                   1 requests with EDNS(0) received
                 550 responses sent
                   1 responses with EDNS(0) sent
                 300 queries resulted in successful answer
                 541 queries resulted in non authoritative answer
                   6 queries resulted in nxrrset
                   9 queries resulted in SERVFAIL
                 235 queries resulted in NXDOMAIN
                 385 queries caused recursion
                   2 duplicate queries received
++ Zone Maintenance Statistics ++
++ Resolver Statistics ++
[Common]';

if (preg_match_all('#\s+(\d{1,3})\s[^\n]+\n#', $foo, $matches)) {
    $retornou = $matches[1];
    print_r($retornou); //Exibe todos numeros que pegou
} else {
    echo 'Nada encontrado';
}

The example returns this:

Array
(
    [0] => 552
    [1] => 546
    [2] => 1
    [3] => 5
    [4] => 53
    [5] => 446
    [6] => 544
    [7] => 35
    [8] => 552
    [9] => 1
    [10] => 550
    [11] => 1
    [12] => 300
    [13] => 541
    [14] => 6
    [15] => 9
    [16] => 235
    [17] => 385
    [18] => 2
)

Just iterate with a for or while

Perfect, however how do I leave the result within the [] by example:

0=> [552] 1=> [546]

You can use the array_map or a for normal, example:

if (preg_match_all('#\s+(\d{1,3})\s[^\n]+\n#', $foo, $matches)) {
    $retornou = array_map(function ($value) {
        //Adiciona os colchetes (parênteses retos)
        return '[' . $value . ']';
    }, $matches[1]);

    print_r($retornou); //Exibe todos numeros que pegou
} else {
    echo 'Nada encontrado';
}

-1

Perfect, but with makes for me to leave the result within the [] for example:

0=> [552] 1=> [546]

  • That one [...] is string or wants numbers to be inside arrays?

  • I edited the answer: https://answall.com/a/217983/3635

Browser other questions tagged

You are not signed in. Login or sign up in order to post.