Subversion Repositories logviewer

Rev

Rev 3 | Go to most recent revision | 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
6
 * Copyright 2018-2019 Daniel Marschall, ViaThinkSoft
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
require_once __DIR__.'/../config.inc.php';
26
 
27
$hostname = trim(file_get_contents('/etc/hostname'));
28
 
29
if (file_exists(__DIR__."/../db_$hostname.inc.php")) {
30
	require_once __DIR__."/../db_$hostname.inc.php";
31
} else {
32
	require_once __DIR__.'/../db.inc.php';
33
}
34
 
35
$files = array();
36
foreach (apache_log_locations as $tpl) $files = array_merge($files, glob($tpl));
37
usort($files, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));
38
 
39
$phpfiles = array();
40
foreach (php_log_locations as $tpl) $phpfiles = array_merge($phpfiles, glob($tpl));
41
usort($phpfiles, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));
42
 
43
$file_nr = 0;
44
$file_max = count($files) + count($phpfiles);
3 daniel-mar 45
 
46
// Apache Log Files
47
 
2 daniel-mar 48
foreach ($files as $file) {
49
	$file_nr++;
3 daniel-mar 50
 
51
	if (time()-filemtime($file) > MAX_DAYS_LOGFILE * 3600) continue;
52
 
53
	if (substr($file,-3,3) === '.gz') {
54
		if (IGNORE_GZ) continue;
55
		$cont = file_get_contents($file);
56
		$cont = gzdecode($cont);
57
		if ($cont === false) continue;
58
		$lines = explode("\n", $cont);
59
	} else {
60
		$lines = file($file);
61
	}
62
 
63
	$line_no = 0;
2 daniel-mar 64
	$line_max = count($lines);
65
	$logfile = removeLogrotateSuffix($file);
66
	foreach ($lines as $line) {
3 daniel-mar 67
		$line_no++;
68
		$line = trim($line);
2 daniel-mar 69
 
3 daniel-mar 70
		if (preg_match('@^\[(.*)\] \[(.*)\] \[(.*)\] \[(.*)\] (.*)$@ismU', $line, $m)) {
71
			#[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
72
			$time = $m[1];
73
			$modul = $m[2];
74
			$text = $m[5];
2 daniel-mar 75
 
3 daniel-mar 76
			$time = trim(substr($time, 4, 6)).' '.substr($time, -4).' '.substr($time, 11, 8);
77
			$time_mysql = date('Y-m-d H:i:s', strtotime($time));
78
		} else if (preg_match('@^(.+)\|(.+)\|(.+)\|(.+)$@ismU', $line, $m)) {
79
			#  5.6 | /daten/homes/daniel-marschall/hosts/dracenmarx/public_html/wiki/vendor/oyejorge/less.php/lib:91            | ini              | Ini mbstring.internal_encoding is deprecated.
80
			// A special implementation of PHP codefixer (showing the full path) . TODO: release
81
			$time = filemtime($file);
82
			$modul = 'php_codefixer';
83
			$text = 'PHP Codefixer: ' . trim($m[4]) . ' in ' . trim($m[2]);
84
 
85
			$time_mysql = date('Y-m-d H:i:s', $time);
86
		} else {
87
			continue;
88
		}
89
 
2 daniel-mar 90
		$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)."';");
91
		#echo mysql_error();
92
		if (mysql_num_rows($res) > 0) {
93
			mysql_query("update vts_fehlerlog set anzahl = anzahl + 1, letzter = '$time_mysql' " .
94
			            "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."';");
95
			#echo mysql_error();
96
 
97
		} else {
98
			mysql_query("insert into vts_fehlerlog (modul, text, anzahl, letzter, logfile) " .
99
			            "values ('".mysql_real_escape_string($modul)."', '".mysql_real_escape_string($text)."', 1, '".$time_mysql."', '".mysql_real_escape_string($logfile)."');");
100
			#echo mysql_error();
101
		}
3 daniel-mar 102
		echo "file $file_nr/$file_max (line $line_no/$line_max)                     \r";
2 daniel-mar 103
	}
104
}
3 daniel-mar 105
 
106
// PHP Log files
107
 
2 daniel-mar 108
foreach ($phpfiles as $file) {
109
	$file_nr++;
110
 
3 daniel-mar 111
	if (time()-filemtime($file) > MAX_DAYS_LOGFILE * 3600) continue;
112
 
113
	if (substr($file,-3,3) === '.gz') {
114
		if (IGNORE_GZ) continue;
115
		$cont = file_get_contents($file);
116
		$cont = gzdecode($cont);
117
		if ($cont === false) continue;
118
	} else {
119
		$cont = file_get_contents($file);
120
	}
2 daniel-mar 121
	$cont = str_replace("\r", "", $cont);
122
	$cont = str_replace("\n ", " ", $cont);
123
	$lines = explode("\n", $cont);
124
 
3 daniel-mar 125
	$line_no = 0;
2 daniel-mar 126
	$line_max = count($lines);
127
	$logfile = removeLogrotateSuffix($file);
128
	foreach ($lines as $line) {
3 daniel-mar 129
		$line_no++;
130
		$line = trim($line);
2 daniel-mar 131
 
3 daniel-mar 132
		if (preg_match('@^\[(.*)\] ((.*)(\n ){0,1})$@ismU', $line, $m)) {
133
			# [19-Aug-2017 23:00:54 europe/berlin] PHP Notice:  Undefined variable: ssl in /home/viathinksoft/public_html/serverinfo/index.php on line 364
134
			$time = $m[1];
135
			$modul = '';
136
			$text = $m[2];
2 daniel-mar 137
 
3 daniel-mar 138
			$time = trim(substr($time, 0, 20));
139
			$time_mysql = date('Y-m-d H:i:s', strtotime($time));
140
		} else {
141
			continue;
142
		}
143
 
2 daniel-mar 144
		$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)."';");
145
		#echo mysql_error();
146
		if (mysql_num_rows($res) > 0) {
147
			mysql_query("update vts_fehlerlog set anzahl = anzahl + 1, letzter = '$time_mysql' " .
148
			            "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."';");
149
			#echo mysql_error();
150
 
151
		} else {
152
			mysql_query("insert into vts_fehlerlog (modul, text, anzahl, letzter, logfile) " .
153
			            "values ('".mysql_real_escape_string($modul)."', '".mysql_real_escape_string($text)."', 1, '".$time_mysql."', '".mysql_real_escape_string($logfile)."');");
154
			#echo mysql_error();
155
		}
3 daniel-mar 156
		echo "file $file_nr/$file_max (line $line_no/$line_max)                     \r";
2 daniel-mar 157
	}
158
}
159
echo "\n";
160
 
161
# ---
162
 
163
function removeLogrotateSuffix($filename) {
164
	$filename = preg_replace('@\\.(\\d+)(\\.gz){0,1}$@ismU', '', $filename);
165
	return $filename;
166
}
167