Subversion Repositories logviewer

Rev

Rev 10 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
#!/usr/bin/php
2
<?php
3
 
4 daniel-mar 4
/*
5
 * ViaThinkSoft LogViewer
11 daniel-mar 6
 * Copyright 2018-2023 Daniel Marschall, ViaThinkSoft
4 daniel-mar 7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
 
2 daniel-mar 21
if (php_sapi_name() !== 'cli') {
22
        die("Error: This script can only be called from command line.\n");
23
}
24
 
25
$hostname = trim(file_get_contents('/etc/hostname'));
26
 
9 daniel-mar 27
if (file_exists(__DIR__."/../config_$hostname.inc.php")) {
28
        require_once __DIR__."/../config_$hostname.inc.php";
8 daniel-mar 29
} else {
9 daniel-mar 30
        require_once __DIR__.'/../config.inc.php';
8 daniel-mar 31
}
32
 
2 daniel-mar 33
if (file_exists(__DIR__."/../db_$hostname.inc.php")) {
34
        require_once __DIR__."/../db_$hostname.inc.php";
35
} else {
36
        require_once __DIR__.'/../db.inc.php';
37
}
38
 
39
$files = array();
40
foreach (apache_log_locations as $tpl) $files = array_merge($files, glob($tpl));
5 daniel-mar 41
usort($files, function($a,$b) { return filemtime($a) - filemtime($b); });
2 daniel-mar 42
 
43
$phpfiles = array();
44
foreach (php_log_locations as $tpl) $phpfiles = array_merge($phpfiles, glob($tpl));
5 daniel-mar 45
usort($phpfiles, function($a,$b) { return filemtime($a) - filemtime($b); });
2 daniel-mar 46
 
47
$file_nr = 0;
48
$file_max = count($files) + count($phpfiles);
3 daniel-mar 49
 
7 daniel-mar 50
$TMP_FILE = __DIR__ . '/.insertlogs.cache';
5 daniel-mar 51
if (file_exists($TMP_FILE)) {
52
        $cont = file_get_contents($TMP_FILE);
53
        $cache = unserialize($cont);
54
} else {
55
        $cache = array();
56
}
57
 
3 daniel-mar 58
// Apache Log Files
59
 
2 daniel-mar 60
foreach ($files as $file) {
61
        $file_nr++;
3 daniel-mar 62
 
5 daniel-mar 63
        if (isset($cache[$file]) && ($cache[$file] == md5_file($file))) continue;
64
 
3 daniel-mar 65
        if (time()-filemtime($file) > MAX_DAYS_LOGFILE * 3600) continue;
66
 
67
        if (substr($file,-3,3) === '.gz') {
68
                if (IGNORE_GZ) continue;
69
                $cont = file_get_contents($file);
70
                $cont = gzdecode($cont);
71
                if ($cont === false) continue;
72
                $lines = explode("\n", $cont);
73
        } else {
74
                $lines = file($file);
75
        }
76
 
77
        $line_no = 0;
2 daniel-mar 78
        $line_max = count($lines);
79
        $logfile = removeLogrotateSuffix($file);
80
        foreach ($lines as $line) {
3 daniel-mar 81
                $line_no++;
82
                $line = trim($line);
2 daniel-mar 83
 
3 daniel-mar 84
                if (preg_match('@^\[(.*)\] \[(.*)\] \[(.*)\] \[(.*)\] (.*)$@ismU', $line, $m)) {
10 daniel-mar 85
                        // [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
3 daniel-mar 86
                        $time = $m[1];
87
                        $modul = $m[2];
88
                        $text = $m[5];
2 daniel-mar 89
 
3 daniel-mar 90
                        $time = trim(substr($time, 4, 6)).' '.substr($time, -4).' '.substr($time, 11, 8);
91
                        $time_mysql = date('Y-m-d H:i:s', strtotime($time));
92
                } else if (preg_match('@^(.+)\|(.+)\|(.+)\|(.+)$@ismU', $line, $m)) {
10 daniel-mar 93
                        // 5.6 | /daten/homes/daniel-marschall/hosts/dracenmarx/public_html/wiki/vendor/oyejorge/less.php/lib:91            | ini              | Ini mbstring.internal_encoding is deprecated.
3 daniel-mar 94
                        // A special implementation of PHP codefixer (showing the full path) . TODO: release
95
                        $time = filemtime($file);
96
                        $modul = 'php_codefixer';
97
                        $text = 'PHP Codefixer: ' . trim($m[4]) . ' in ' . trim($m[2]);
98
 
99
                        $time_mysql = date('Y-m-d H:i:s', $time);
100
                } else {
101
                        continue;
102
                }
103
 
11 daniel-mar 104
                if (strlen($modul) > 30) {
105
                        echo "Attention: Truncate modul: $modul\n";
106
                        $modul = substr($modul, 0, 512);
107
                }
108
 
109
                if (strlen($text) > 512) {
110
                        echo "Attention: Truncate text in file $file: $text\n";
111
                        $text = substr($text, 0, 512);
112
                }
113
 
2 daniel-mar 114
                $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)."';");
115
                #echo mysql_error();
116
                if (mysql_num_rows($res) > 0) {
117
                        mysql_query("update vts_fehlerlog set anzahl = anzahl + 1, letzter = '$time_mysql' " .
118
                                    "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."';");
119
                        #echo mysql_error();
120
 
121
                } else {
122
                        mysql_query("insert into vts_fehlerlog (modul, text, anzahl, letzter, logfile) " .
123
                                    "values ('".mysql_real_escape_string($modul)."', '".mysql_real_escape_string($text)."', 1, '".$time_mysql."', '".mysql_real_escape_string($logfile)."');");
124
                        #echo mysql_error();
125
                }
3 daniel-mar 126
                echo "file $file_nr/$file_max (line $line_no/$line_max)                     \r";
2 daniel-mar 127
        }
5 daniel-mar 128
 
129
        $cache[$file] = md5_file($file);
2 daniel-mar 130
}
3 daniel-mar 131
 
132
// PHP Log files
133
 
2 daniel-mar 134
foreach ($phpfiles as $file) {
135
        $file_nr++;
136
 
5 daniel-mar 137
        if (isset($cache[$file]) && ($cache[$file] == md5_file($file))) continue;
138
 
3 daniel-mar 139
        if (time()-filemtime($file) > MAX_DAYS_LOGFILE * 3600) continue;
140
 
141
        if (substr($file,-3,3) === '.gz') {
142
                if (IGNORE_GZ) continue;
143
                $cont = file_get_contents($file);
144
                $cont = gzdecode($cont);
145
                if ($cont === false) continue;
146
        } else {
147
                $cont = file_get_contents($file);
148
        }
2 daniel-mar 149
        $cont = str_replace("\r", "", $cont);
150
        $cont = str_replace("\n ", " ", $cont);
151
        $lines = explode("\n", $cont);
152
 
3 daniel-mar 153
        $line_no = 0;
2 daniel-mar 154
        $line_max = count($lines);
155
        $logfile = removeLogrotateSuffix($file);
156
        foreach ($lines as $line) {
3 daniel-mar 157
                $line_no++;
158
                $line = trim($line);
2 daniel-mar 159
 
8 daniel-mar 160
                echo "file $file_nr/$file_max (line $line_no/$line_max)                     \r";
161
 
3 daniel-mar 162
                if (preg_match('@^\[(.*)\] ((.*)(\n ){0,1})$@ismU', $line, $m)) {
163
                        # [19-Aug-2017 23:00:54 europe/berlin] PHP Notice:  Undefined variable: ssl in /home/viathinksoft/public_html/serverinfo/index.php on line 364
164
                        $time = $m[1];
165
                        $modul = '';
166
                        $text = $m[2];
2 daniel-mar 167
 
3 daniel-mar 168
                        $time = trim(substr($time, 0, 20));
169
                        $time_mysql = date('Y-m-d H:i:s', strtotime($time));
170
                } else {
171
                        continue;
172
                }
173
 
11 daniel-mar 174
                if (strpos($text, '{"reqId":"') !== false) {
175
                        // For some reason, owncloud or nextcloud seems to write to php_error.log and not in data/nextcloud.log ?? But only sometimes ??
176
                        // TODO: Should we try to parse this JSON log message?
177
 
178
                        // [12-Sep-2023 15:01:24 UTC] {"reqId":"f2uD4QSS9xIjAAWgbeVb","level":3,"time":"2023-09-12T15:01:24+00:00","remoteAddr":"1.2.3.4","user":"--","app":"core","method":"GET","url":"/index.php/settings/admin/overview","message":"Permission denied","userAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36","version":"27.0.2.1","exception":{"Exception":"RedisException","Message":"Permission denied","Code":0,"Trace":[{"file":"/daten/homes/owncloud/public_html/lib/private/RedisFactory.php","line":137,"function":"pconnect","class":"Redis","type":"->"},{"file":"/daten/homes/owncloud/public_html/lib/private/RedisFactory.php","line":178,"function":"create","class":"OC\\RedisFactory","type":"->","args":["*** sensitive parameters replaced ***"]},{"file":"/daten/homes/owncloud/public_html/lib/private/Memcache/Redis.php","line":66,"function":"getInstance","class":"OC\\RedisFactory","type":"->"},{"file":"/daten/homes/owncloud/public_html/lib/private/Memcache/Redis.php","line":72,"function":"getCache","class":"OC\\Memcache\\Redis","type":"->"},{"file":"/daten/homes/owncloud/public_html/lib/private/App/InfoParser.php","line":58,"function":"get","class":"OC\\Memcache\\Redis","type":"->"},{"file":"/daten/homes/owncloud/public_html/lib/private/App/AppManager.php","line":732,"function":"parse","class":"OC\\App\\InfoParser","type":"->"},{"file":"/daten/homes/owncloud/public_html/lib/private/legacy/OC_App.php","line":434,"function":"getAppInfo","class":"OC\\App\\AppManager","type":"->"},{"file":"/daten/homes/owncloud/public_html/lib/private/AppFramework/App.php","line":71,"function":"getAppInfo","class":"OC_App","type":"::"},{"file":"/daten/homes/owncloud/public_html/lib/private/legacy/OC_App.php","line":155,"function":"buildAppNamespace","class":"OC\\AppFramework\\App","type":"::"},{"file":"/daten/homes/owncloud/public_html/lib/private/AppFramework/Bootstrap/Coordinator.php","line":119,"function":"registerAutoloading","class":"OC_App","type":"::"},{"file":"/daten/homes/owncloud/public_html/lib/private/AppFramework/Bootstrap/Coordinator.php","line":90,"function":"registerApps","class":"OC\\AppFramework\\Bootstrap\\Coordinator","type":"->"},{"file":"/daten/homes/owncloud/public_html/lib/base.php","line":703,"function":"runInitialRegistration","class":"OC\\AppFramework\\Bootstrap\\Coordinator","type":"->"},{"file":"/daten/homes/owncloud/public_html/lib/base.php","line":1180,"function":"init","class":"OC","type":"::"},{"file":"/daten/homes/owncloud/public_html/index.php","line":34,"args":["/daten/homes/owncloud/public_html/lib/base.php"],"function":"require_once"}],"File":"/daten/homes/owncloud/public_html/lib/private/RedisFactory.php","Line":137,"CustomMessage":"--"}}
179
 
180
                        continue;
181
                }
182
 
183
                if (strlen($modul) > 30) {
184
                        echo "Attention: Truncate modul: $modul\n";
185
                        $modul = substr($modul, 0, 512);
186
                }
187
 
188
                if (strlen($text) > 512) {
189
                        echo "Attention: Truncate text in file $file: $text\n";
190
                        $text = substr($text, 0, 512);
191
                }
192
 
2 daniel-mar 193
                $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)."';");
194
                #echo mysql_error();
195
                if (mysql_num_rows($res) > 0) {
196
                        mysql_query("update vts_fehlerlog set anzahl = anzahl + 1, letzter = '$time_mysql' " .
197
                                    "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."';");
198
                        #echo mysql_error();
199
 
200
                } else {
201
                        mysql_query("insert into vts_fehlerlog (modul, text, anzahl, letzter, logfile) " .
202
                                    "values ('".mysql_real_escape_string($modul)."', '".mysql_real_escape_string($text)."', 1, '".$time_mysql."', '".mysql_real_escape_string($logfile)."');");
203
                        #echo mysql_error();
204
                }
205
        }
5 daniel-mar 206
 
207
        $cache[$file] = md5_file($file);
2 daniel-mar 208
}
209
echo "\n";
210
 
5 daniel-mar 211
file_put_contents($TMP_FILE, serialize($cache));
212
 
10 daniel-mar 213
// Prune old logs
214
 
215
mysql_query('delete from vts_fehlerlog where letzter < date_sub(now(),interval 3 year)');
216
 
2 daniel-mar 217
# ---
218
 
219
function removeLogrotateSuffix($filename) {
220
        $filename = preg_replace('@\\.(\\d+)(\\.gz){0,1}$@ismU', '', $filename);
221
        return $filename;
222
}
223