Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 54 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
AccessSslLogsChannel | |
0.00% |
0 / 54 |
|
0.00% |
0 / 5 |
240 | |
0.00% |
0 / 1 |
getId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getLogFileForIndex | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
30 | |||
getIndexForFilePath | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
30 | |||
parseDateTimeOfLine | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace Olz\Apps\Logs\Utils; |
4 | |
5 | use Olz\Utils\WithUtilsTrait; |
6 | |
7 | class AccessSslLogsChannel extends LogrotateLogsChannel { |
8 | use WithUtilsTrait; |
9 | |
10 | public static function getId(): string { |
11 | return 'access-ssl-logs'; |
12 | } |
13 | |
14 | public static function getName(): string { |
15 | return "Access SSL Logs"; |
16 | } |
17 | |
18 | protected function getLogFileForIndex(int $index): LogFileInterface { |
19 | $log_name = 'access_ssl_log'; |
20 | $syslog_path = $this->envUtils()->getSyslogPath(); |
21 | $base_index = $index - 1; |
22 | $basename = "{$log_name}.processed.{$base_index}"; |
23 | $file_path = "{$syslog_path}{$basename}"; |
24 | if ($index === 0) { |
25 | $file_path = "{$syslog_path}{$log_name}"; |
26 | } |
27 | if ($index === 1) { |
28 | $file_path = "{$syslog_path}{$log_name}.processed"; |
29 | } |
30 | $index_path = "{$file_path}.index.json.gz"; |
31 | if (is_file($file_path)) { |
32 | return new PlainLogFile($file_path, $index_path); |
33 | } |
34 | if (is_file("{$file_path}.gz")) { |
35 | return new HybridLogFile("{$file_path}.gz", $file_path, $index_path); |
36 | } |
37 | throw new \Exception("No such file: {$file_path}"); |
38 | } |
39 | |
40 | protected function getIndexForFilePath(string $file_path): int { |
41 | $log_name = 'access_ssl_log'; |
42 | $syslog_path = $this->envUtils()->getSyslogPath(); |
43 | $esc_syslog_path = preg_quote($syslog_path, '/'); |
44 | $pattern = "/^{$esc_syslog_path}{$log_name}($|\\.processed$|\\.processed\\.(\\d+)$)/"; |
45 | $res = preg_match($pattern, $file_path, $matches); |
46 | if (!$res) { |
47 | throw new \Exception("Not an OLZ Log file path: {$file_path}"); |
48 | } |
49 | if ($matches[1] === '') { |
50 | return 0; |
51 | } |
52 | if ($matches[1] === '.processed') { |
53 | return 1; |
54 | } |
55 | $index = intval($matches[2]) + 1; |
56 | if ($index < 0) { |
57 | throw new \Exception("Index is below 0. This should never happen, due to the regex."); |
58 | } |
59 | return $index; |
60 | } |
61 | |
62 | protected function parseDateTimeOfLine(string $line): ?\DateTime { |
63 | $res = preg_match('/(\d{2})\/(\w{3})\/(\d{4})(:|T|\s+)(\d{2}\:\d{2}\:\d{2})/', $line, $matches); |
64 | $month_mapping = [ |
65 | 'jan' => '01', |
66 | 'feb' => '02', |
67 | 'mar' => '03', |
68 | 'apr' => '04', |
69 | 'may' => '05', |
70 | 'jun' => '06', |
71 | 'jul' => '07', |
72 | 'aug' => '08', |
73 | 'sep' => '09', |
74 | 'oct' => '10', |
75 | 'nov' => '11', |
76 | 'dec' => '12', |
77 | ]; |
78 | if (!$res) { |
79 | return null; |
80 | } |
81 | try { |
82 | $month = $month_mapping[strtolower($matches[2])]; |
83 | $date = "{$matches[3]}-{$month}-{$matches[1]}"; |
84 | return new \DateTime("{$date} {$matches[5]}"); |
85 | } catch (\Throwable $th) { |
86 | return null; |
87 | } |
88 | } |
89 | } |