Repository navigation
Expand file tree
/
Copy pathenvDefinition.php
More file actions
255 lines (236 loc) · 8.78 KB
/
Copy pathenvDefinition.php
File metadata and controls
255 lines (236 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
use foroco\BrowserDetection;
class envDefinition
{
const WPINDEX = "wpindex";
const WPERROR = "wperrors";
const INDEX = "index";
const ERROR = "errors";
const APPMSG = "appmsg";
const APPSYS = "appsys";
const E_PRD = 'prod';
const E_DEV = 'dev';
const E_PREPROD = 'preprod';
const E_STAGE = 'stage';
const E_LOC = 'local';
const E_TEST = 'test';
const TIMEFORMAT = "Y-m-d H:i:s O";
private $env = '';
private $indexType = '';
private $host = '';
private $envs = [
self::E_DEV,
self::E_PRD,
self::E_PREPROD,
self::E_LOC,
self::E_TEST,
self::E_STAGE,
];
public $indexes = [
self::APPMSG,
self::APPSYS,
self::ERROR,
self::INDEX,
self::WPERROR,
self::WPINDEX,
];
public static $mapErrorFields = [
'type', 'message', 'file', 'line', 'trace'
];
/** @var BrowserDetection */
private $browser;
private $application = 'undefined';
public function __construct()
{
global $argv;
$type = isset($argv[1]) ? $argv[1] : self::INDEX;
if (!in_array($type, $this->indexes)) {
throw new Exception("Uknown index type: $type; allowed: " . json_encode($this->indexes) . PHP_EOL);
}
$this->indexType = $type;
$env = isset($argv[2]) ? $argv[2] : self::E_DEV;
if (!in_array($env, $this->envs)) {
throw new Exception("Uknown env type: $env; allowed: " . json_encode($this->envs) . PHP_EOL);
}
$this->application = isset($argv[3]) ? $argv[3] : "undefined";
$this->env = $env;
$this->host = explode(".", gethostname())[0];
$this->browser = new BrowserDetection();
}
public function buildIndexPrefix($indexType){
return strtolower($indexType . '-' . $this->application . '-' . $this->env);
}
public function getIndexName()
{
return strtolower($this->indexType . '-' . $this->application . '-' . $this->env . "-" . $this->host . "-" . date("Y.W"));
}
public function getIndexParams($index)
{
return [
'index' => $index,
'body' => [
'mappings' => json_decode(file_get_contents(__DIR__ . "/config/mapping-" . $this->indexType . ".json"), true)
]
];
}
public function parseLineByType($data)
{
if (isset($data['datetime'])) {
$data['time'] = $data['datetime'];
$data['HeaderXForwardedFor'] = "0.0.0.0:0";
}
$data['application'] = $this->application;
$data['env'] = $this->env;
$data['node'] = $this->host;
$data['time'] = date(self::TIMEFORMAT);
$data['client'] = $this->getClientFromForwarded($data['HeaderXForwardedFor']);
switch ($this->indexType) {
default:
case self::INDEX:
case self::WPINDEX:
if (isset($data['logLevel'])) {
throw new Exception("Not a access log");
}
return $this->parseLineTraffic($data);
case self::WPERROR:
case self::ERROR:
if (!isset($data['logLevel'])) {
throw new Exception("Not a error log");
}
return $this->parseLineError($data);
case self::APPMSG:
if (!isset($data['level_name'])) {
throw new Exception("Not a error log");
}
return $this->parseLineMsg($data);
case self::APPSYS:
return $this->parseLineSys($data);
}
}
public function parseLineTraffic($data)
{
$data['deviceType'] = $this->browser->getOS($data['userAgent'])['os_type'];
return $data;
}
public function parseLineError($data)
{
$data['errorType'] = $data['logLevel'];
$data['inFile'] = "";
$data['inLine'] = "0";
$data['stackTrace'] = "";
$data['wpType'] = "";
$data['wpLocation'] = "";
$data['responseTime'] = "0";
if (isset($data['apacheModule']) && in_array($data['apacheModule'], ['php7', 'php8'])) {
$data = $this->parsePHPErrorLog($data);
}
unset($data['logLevel'], $data['module']);
return $data;
}
public function parsePHPErrorLog($data)
{
$data['errorMessage'] = str_replace(PHP_EOL, '\\n', $data['errorMessage']);
preg_match_all("@^PHP ([a-zA-Z\s]+):\s+(.+)\s+in\s+(.+) on line (\d+)@i", $data['errorMessage'], $m);
unset($m[0]);
$replace = array_column($m, "0");
$replace[] = "";
$parsed = [];
if (count($replace) == 5) {
$replaceWith = self::$mapErrorFields;
$parsed = array_combine($replaceWith, $replace);
$data['inLine'] = $parsed['line'];
$data['inFile'] = $parsed['file'];
$data['errorType'] = $data['errorType'] . ": PHP " . $parsed['type'];
} elseif (strpos($data['errorMessage'], "base64") === 0) {
preg_match_all("@base64:(.+)@i", $data['errorMessage'], $m);
$msg = json_decode(base64_decode($m[1][0]), true);
$msg['client'] = $this->getClientFromForwarded($msg['HeaderXForwardedFor']);
$data = array_merge($data, $msg);
} else {
//if other formats should be supported, please write code in this elif block
throw new Exception("PHP PARSE ERROR " . $data['errorMessage']);
}
if (strpos($data['errorMessage'], "Stack trace") !== false) {
$mm = [];
preg_match_all("@^(.+) in .+Stack trace:(.+)\s+(thrown)?@i", trim($data['errorMessage']), $mm);
$trace = array_column($mm, "0");
$data['errorMessage'] = trim($trace[1]);
$data['stackTrace'] = implode(PHP_EOL, array_filter(explode("\\n", $trace[2]), 'trim'));
}
$m = [];
if (!empty($data['inFile'])) {
preg_match("@((\/wp-content)?\/(themes|plugins|uploads)\/([^\/]*))|(\/wp-([a-zA-Z0-9-_.]+)([^\s]+)?)@", $data['inFile'], $m);
} else {
preg_match("@((\/wp-content)?\/(themes|plugins|uploads)\/([^\/]*))|(\/wp-([a-zA-Z0-9-_.]+)([^\s]+)?)@", $parsed['message'], $m);
}
$data['wpType'] = 'other';
$data['wpLocation'] = 'unknown';
if (!empty($m)) {
if (isset($m[5])) {
$data['wpType'] = 'core';
$data['wpLocation'] = $m[5];
} else {
$data['wpType'] = $m[3];
$data['wpLocation'] = $m[4];
}
}
return $data;
}
public function parseLineMsg($data)
{
$data['inFile'] = $data['context'];
$data['inLine'] = "0";
$data['wpType'] = "";
$data['errorMessage'] = $data['message'];
$data['stackTrace'] = $data['extra'];
$data['errorType'] = $data['level_name'];
$data['wpLocation'] = $data['channel'];
return $data;
}
public function parseLineSys($data)
{
$data['time'] = date(self::TIMEFORMAT);
unset($data['client'], $data['HeaderXForwardedFor']);
$data['tcp'] = $this->getNetstatFormatted();
$la = array_map(fn($v): float => (float) trim($v), explode(", ", explode("load average:", shell_exec("uptime"))[1]));
$data['load'] = array_combine(['01m', '05m', '15m'], $la);
return $data;
}
private function getNetstatFormatted()
{
$d = array_map('trim', explode(PHP_EOL, trim(shell_exec("netstat -la | grep tcp | awk -F ' ' {'print $5,$6'} | sort | uniq -c"))));
$r = [];
foreach ($d as $k => $v) {
$tmp = array_combine(['count', 'client', 'type'], explode(" ", $v));
$tmp['count'] = (int) $tmp['count'];
$cl = explode(":", $tmp['client']);
$tmp['client'] = $cl[0];
$tmp['port'] = $cl[1];
$d[$k] = $tmp;
if (!preg_match('/[^a-zA-Z]/', $tmp['port']) && !empty($tmp['port'])) {
@$r['port'][$tmp['port']] += $tmp['count'];
}
// @$r['client'][$tmp['client']] += $tmp['count'];
@$r['type'][$tmp['type']] += $tmp['count'];
}
return $r;
}
private function getClientFromForwarded($forwarded)
{
$forwardeda = explode(",", $forwarded)[0];
$client = trim(explode(":", $forwardeda)[0]);
$clientf = filter_var($client, FILTER_VALIDATE_IP);
return $clientf ? $clientf : "0.0.0.0";
}
public function excludeUA($logline)
{
switch ($this->indexType) {
case self::INDEX:
case self::WPINDEX:
return (strpos($logline, 'internal dummy connection') !== false);
default:
break;
}
return false;
}
}