Subversion Repositories logviewer

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

#!/usr/bin/php
<?php

if (php_sapi_name() !== 'cli') {
        die("Error: This script can only be called from command line.\n");
}

require_once __DIR__.'/../config.inc.php';

$hostname = trim(file_get_contents('/etc/hostname'));

if (file_exists(__DIR__."/../db_$hostname.inc.php")) {
        require_once __DIR__."/../db_$hostname.inc.php";
} else {
        require_once __DIR__.'/../db.inc.php';
}

$files = array();
foreach (apache_log_locations as $tpl) $files = array_merge($files, glob($tpl));
usort($files, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));

$phpfiles = array();
foreach (php_log_locations as $tpl) $phpfiles = array_merge($phpfiles, glob($tpl));
usort($phpfiles, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));

$file_nr = 0;
$file_max = count($files) + count($phpfiles);
foreach ($files as $file) {
        $file_nr++;
        $lines = file($file);
        $line_nr = 0;
        $line_max = count($lines);
        $logfile = removeLogrotateSuffix($file);
        foreach ($lines as $line) {
                $line_nr++;
                #[Sun Aug 13 15:54:16.054530 2017] [fcgid:warn] [pid 28401] [client 104.236.113.44:52188] mod_fcgid: stderr: PHP Notice:  Undefined offset: 11 in /home/d
                if (!preg_match('@^\[(.*)\] \[(.*)\] \[(.*)\] \[(.*)\] (.*)$@ismU', $line, $m)) continue;
                $time = $m[1];
                $modul = $m[2];
                $text = $m[5];

                $time = trim(substr($time, 4, 6)).' '.substr($time, -4).' '.substr($time, 11, 8);
                $time_mysql = date('Y-m-d H:i:s', strtotime($time));

                $res = mysql_query("select * from vts_fehlerlog where modul = '".mysql_real_escape_string($modul)."' and logfile = '".mysql_real_escape_string($logfile)."' and text = '".mysql_real_escape_string($text)."';");
                #echo mysql_error();
                if (mysql_num_rows($res) > 0) {
                        mysql_query("update vts_fehlerlog set anzahl = anzahl + 1, letzter = '$time_mysql' " .
                                    "where modul = '".mysql_real_escape_string($modul)."' and logfile = '".mysql_real_escape_string($logfile)."' and text = '".mysql_real_escape_string($text)."' and letzter < '".$time_mysql."';");
                        #echo mysql_error();

                } else {
                        mysql_query("insert into vts_fehlerlog (modul, text, anzahl, letzter, logfile) " .
                                    "values ('".mysql_real_escape_string($modul)."', '".mysql_real_escape_string($text)."', 1, '".$time_mysql."', '".mysql_real_escape_string($logfile)."');");
                        #echo mysql_error();
                }
                echo "file $file_nr/$file_max (line $line_nr/$line_max)                     \r";
        }
}
foreach ($phpfiles as $file) {
        $file_nr++;

        $cont = file_get_contents($file);
        $cont = str_replace("\r", "", $cont);
        $cont = str_replace("\n ", " ", $cont);
        $lines = explode("\n", $cont);

        $line_nr = 0;
        $line_max = count($lines);
        $logfile = removeLogrotateSuffix($file);
        foreach ($lines as $line) {
                $line_nr++;
                # [19-Aug-2017 23:00:54 europe/berlin] PHP Notice:  Undefined variable: ssl in /home/viathinksoft/public_html/serverinfo/index.php on line 364
                if (!preg_match('@^\[(.*)\] ((.*)(\n ){0,1})$@ismU', $line, $m)) continue;
                $time = $m[1];
                $modul = '';
                $text = $m[2];

                $time = trim(substr($time, 0, 20));
                $time_mysql = date('Y-m-d H:i:s', strtotime($time));

                $res = mysql_query("select * from vts_fehlerlog where modul = '".mysql_real_escape_string($modul)."' and logfile = '".mysql_real_escape_string($logfile)."' and text = '".mysql_real_escape_string($text)."';");
                #echo mysql_error();
                if (mysql_num_rows($res) > 0) {
                        mysql_query("update vts_fehlerlog set anzahl = anzahl + 1, letzter = '$time_mysql' " .
                                    "where modul = '".mysql_real_escape_string($modul)."' and logfile = '".mysql_real_escape_string($logfile)."' and text = '".mysql_real_escape_string($text)."' and letzter < '".$time_mysql."';");
                        #echo mysql_error();

                } else {
                        mysql_query("insert into vts_fehlerlog (modul, text, anzahl, letzter, logfile) " .
                                    "values ('".mysql_real_escape_string($modul)."', '".mysql_real_escape_string($text)."', 1, '".$time_mysql."', '".mysql_real_escape_string($logfile)."');");
                        #echo mysql_error();
                }
                echo "file $file_nr/$file_max (line $line_nr/$line_max)                     \r";
        }
}
echo "\n";

# ---

function removeLogrotateSuffix($filename) {
        $filename = preg_replace('@\\.(\\d+)(\\.gz){0,1}$@ismU', '', $filename);
        return $filename;
}