Passed
Push — master ( 3b7eff...ae6bea )
by Nils
02:43
created

LogrotateCollector::collect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
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->findLogFiles()
18
        ];
19
    }
20
21
    function findLogFiles(): array
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
22
    {
23
        // Only search under /var/log
24
        $searchPath = '/var/log';
25
26
        // Paths to logrotate configuration files
27
        $logrotateConfs = ['/etc/logrotate.conf', ...glob('/etc/logrotate.d/*')];
28
29
        // Step 1: Find all .log files under /var/log
30
        $allLogs = [];
31
        $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($searchPath, \FilesystemIterator::SKIP_DOTS));
32
        foreach ($iterator as $file) {
33
            if (preg_match('/\.log$/', $file->getFilename())) {
34
                $allLogs[] = realpath($file->getPathname());
35
            }
36
        }
37
38
        // Step 2: Extract managed log file paths from logrotate configs
39
        $managedLogs = [];
40
        foreach ($logrotateConfs as $confFile) {
41
            if (!is_readable($confFile)) continue;
0 ignored issues
show
Bug introduced by
It seems like $confFile can also be of type array; however, parameter $filename of is_readable() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
            if (!is_readable(/** @scrutinizer ignore-type */ $confFile)) continue;
Loading history...
42
            $lines = file($confFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
0 ignored issues
show
Bug introduced by
It seems like $confFile can also be of type array; however, parameter $filename of file() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
            $lines = file(/** @scrutinizer ignore-type */ $confFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
Loading history...
43
            foreach ($lines as $line) {
44
                // Look for lines that contain a log file path
45
                if (preg_match('#^\s*/[^\s{}]+\.log#', $line, $matches)) {
46
                    $managedLogs[] = realpath(trim($matches[0]));
47
                }
48
            }
49
        }
50
51
        // Step 3: Filter out invalid paths and duplicates
52
        $allLogs = array_unique(array_filter($allLogs));
53
        $managedLogs = array_unique(array_filter($managedLogs));
54
55
        // Step 4: Return logs that are not listed in logrotate configs
56
        return [
57
            'unmanaged' => array_values(array_diff($allLogs, $managedLogs)),
58
            'managed' => $managedLogs
59
        ];
60
    }
61
}
62