|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Startwind\Inventorio\Collector\System\Logs; |
|
4
|
|
|
|
|
5
|
|
|
use Startwind\Inventorio\Collector\Collector; |
|
6
|
|
|
|
|
7
|
|
|
class LogrotateCollector implements Collector |
|
8
|
|
|
{ |
|
9
|
|
|
public function getIdentifier(): string |
|
10
|
|
|
{ |
|
11
|
|
|
return 'SystemLogLogrotate'; |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
public function collect(): array |
|
15
|
|
|
{ |
|
16
|
|
|
return [ |
|
17
|
|
|
'logfiles' => $this->getLogFileStatus() |
|
18
|
|
|
]; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
private function getLogFileStatus(): array |
|
22
|
|
|
{ |
|
23
|
|
|
$searchPath = '/var/log'; |
|
24
|
|
|
$logrotateConfs = ['/etc/logrotate.conf', ...glob('/etc/logrotate.d/*')]; |
|
25
|
|
|
|
|
26
|
|
|
// Step 1: Find all .log files under /var/log |
|
27
|
|
|
$allLogs = []; |
|
28
|
|
|
$iterator = new \RecursiveIteratorIterator( |
|
29
|
|
|
new \RecursiveDirectoryIterator($searchPath, \FilesystemIterator::SKIP_DOTS) |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
foreach ($iterator as $file) { |
|
33
|
|
|
if (preg_match('/\.log$/', $file->getFilename())) { |
|
34
|
|
|
$realPath = realpath($file->getPathname()); |
|
35
|
|
|
if ($realPath !== false && is_file($realPath)) { |
|
36
|
|
|
$allLogs[$realPath] = [ |
|
37
|
|
|
'size' => filesize($realPath), |
|
38
|
|
|
'last_modified' => filemtime($realPath) |
|
39
|
|
|
]; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// Step 2: Extract managed log paths from logrotate config files |
|
45
|
|
|
$managedLogs = []; |
|
46
|
|
|
foreach ($logrotateConfs as $confFile) { |
|
47
|
|
|
if (!is_readable($confFile)) continue; |
|
|
|
|
|
|
48
|
|
|
$lines = file($confFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
|
|
|
|
|
|
49
|
|
|
foreach ($lines as $line) { |
|
50
|
|
|
if (preg_match('#^\s*/[^\s{}]+\.log#', $line, $matches)) { |
|
51
|
|
|
$path = realpath(trim($matches[0])); |
|
52
|
|
|
if ($path !== false) { |
|
53
|
|
|
$managedLogs[] = $path; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$managedLogs = array_unique($managedLogs); |
|
60
|
|
|
|
|
61
|
|
|
// Step 3: Build result arrays |
|
62
|
|
|
$result = [ |
|
63
|
|
|
'managed' => [], |
|
64
|
|
|
'unmanaged' => [] |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
|
|
foreach ($allLogs as $path => $info) { |
|
68
|
|
|
$entry = [ |
|
69
|
|
|
'path' => $path, |
|
70
|
|
|
'size' => $info['size'], |
|
71
|
|
|
'last_modified' => date('c', $info['last_modified']) // ISO 8601 format |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
|
|
if (in_array($path, $managedLogs)) { |
|
75
|
|
|
$result['managed'][] = $entry; |
|
76
|
|
|
} else { |
|
77
|
|
|
$result['unmanaged'][] = $entry; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $result; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|