Domanda

Voglio creare uno script che analizzi o abbia un senso del registro degli errori di Apache per vedere qual è l'errore più recente. Mi chiedevo se qualcuno là fuori ha qualcosa che lo fa o ha idee da dove cominciare?

È stato utile?

Soluzione

Ci sono alcune cose da considerare prima:

  1. Innanzitutto, l'utente PHP potrebbe non avere accesso ai file di registro di Apache.
  2. In secondo luogo, PHP e Apache non ti diranno dove si trova il file di registro,
  3. Infine, i file di registro di Apache possono diventare piuttosto grandi.

Tuttavia, se nessuno di questi si applica, è possibile utilizzare i normali comandi di lettura dei file per farlo. Il modo più semplice per ottenere l'ultimo errore è

$contents = @file('/path/to/error.log', FILE_SKIP_EMPTY_LINES);
if (is_array($contents)) {
    echo end($contents);
}
unset($contents);

Probabilmente c'è un modo migliore di farlo che non riempie di memoria, ma lo lascerò come esercizio per il lettore.

Un ultimo commento: PHP ha anche un'impostazione ini per reindirizzare gli errori PHP su un file di registro: error_log = /path/to/error.log

Puoi impostarlo in httpd.conf o in un file .htaccess (se hai accesso a uno) usando la notazione php_flag:

php_flag error_log /web/mysite/logs/error.log

Altri suggerimenti

per chiunque cerchi uno script di esempio, ho messo insieme qualcosa, ha le basi:

<?php
exec('tail /usr/local/apache/logs/error_log', $output);
?>
<Table border="1">
    <tr>
        <th>Date</th>
        <th>Type</th>
        <th>Client</th>
        <th>Message</th>
    </tr>
<?
    foreach($output as $line) {
        // sample line: [Wed Oct 01 15:07:23 2008] [error] [client 76.246.51.127] PHP 99. Debugger->handleError() /home/gsmcms/public_html/central/cake/libs/debugger.php:0
        preg_match('~^\[(.*?)\]~', $line, $date);
        if(empty($date[1])) {
            continue;
        }
        preg_match('~\] \[([a-z]*?)\] \[~', $line, $type);
        preg_match('~\] \[client ([0-9\.]*)\]~', $line, $client);
        preg_match('~\] (.*)$~', $line, $message);
        ?>
    <tr>
        <td><?=$date[1]?></td>
        <td><?=$type[1]?></td>
        <td><?=$client[1]?></td>
        <td><?=$message[1]?></td>
    </tr>
        <?
    }
?>
</table>

ci sono pile di script php che fanno questo, basta fare una ricerca su google per esempi. se vuoi realizzarne uno tuo, non è niente di più complesso della lettura di qualsiasi altro file. assicurati solo di conoscere la posizione dei tuoi file di log (definiti nel file httpd.conf) e formatta i tuoi file di registro . Il formato è anche definito in httpd.conf

Ecco una piccola classe che semplifica la lettura di un numero di caratteri dal retro di un file di grandi dimensioni senza sovraccarico di memoria. L'impostazione del test ti consente di vederlo in azione cannibalizzando se stesso.

BigFile.php
<?php
$run_test = true;
$test_file = 'BigFile.php';

class BigFile
{
private $file_handle;

/**
 * 
 * Load the file from a filepath 
 * @param string $path_to_file
 * @throws Exception if path cannot be read from
 */
public function __construct( $path_to_log )
{
    if( is_readable($path_to_log) )
    {
        $this->file_handle = fopen( $path_to_log, 'r');
    }
    else
    {
        throw new Exception("The file path to the file is not valid");
    } 
}

/**
 * 
 * 'Finish your breakfast' - Jay Z's homme Strict
 */
public function __destruct()
{
    fclose($this->file_handle); 
}

/**
 * 
 * Returns a number of characters from the end of a file w/o loading the entire file into memory
 * @param integer $number_of_characters_to_get
 * @return string $characters
 */
public function getFromEnd( $number_of_characters_to_get )
{
    $offset = -1*$number_of_characters_to_get;
    $text = "";

    fseek( $this->file_handle, $offset , SEEK_END);

    while(!feof($this->file_handle))
    {
        $text .= fgets($this->file_handle);
    }

    return $text;
}
}

if( $run_test )
{
$number_of_characters_to_get =  100000; 
$bf = new BigFile($test_file);
$text = $bf->getFromEnd( $number_of_characters_to_get );
echo "$test_file has the following $number_of_characters_to_get characters at the end: 
    <br/> <pre>$text</pre>";
}

?> 

Il gruppo adm ha accesso a per visualizzare i log nel sistema linux per accedere al log da apache dobbiamo aggiungere un utente www-data al gruppo adam e riavviare l'apache in modo che tutte le modifiche vengano aggiornate

$ sudo usermod -G adm www-data
$ sudo service apache2 restart

<?php
 exec('tail /var/log/apache2/error_log', $output);
?>
<Table border="1">
<tr>
    <th>Date</th>
    <th>Type</th>
    <th>Client</th>
    <th>Message</th>
</tr>
<?php
foreach($output as $line) {
    // sample line: [Mon Apr 01 07:23:14.217466 2019] [autoindex:error] [pid 19261] [client 114.143.38.172:55801] AH01276:PHP 99. Debugger->handleError() 
   /home/gsmcms/public_html/central/cake/libs/debugger.php:0

    preg_match('~^\[(.*?)\]~', $line, $date);
    if(empty($date[1])) {
        continue;
    }
    preg_match('~\] \[([a-z:]*?)\] \[~', $line, $type);
    preg_match('~\] \[client ([0-9\.:]*)\]~', $line, $client);
    preg_match('~\] (.*)$~', $line, $message);
    ?>
<tr>
    <td><?=$date[1]?></td>
    <td><?=$type[1]?></td>
    <td><?=$client[1]?></td>
    <td><?=$message[1]?></td>
</tr>
    <?
}
?>

Hai provato biterScripting? Sono un amministratore di sistema e ho usato per analizzare i registri. Si tratta di script in stile univx. biterScripting.com - > Download gratuito.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top