Passed
Push — develop ( 10d640...881f21 )
by Портнов
04:32
created

SyslogConf::generateConfigFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 14
rs 9.8333
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright (C) MIKO LLC - All Rights Reserved
4
 * Unauthorized copying of this file, via any medium is strictly prohibited
5
 * Proprietary and confidential
6
 * Written by Nikolay Beketov, 7 2020
7
 *
8
 */
9
10
namespace MikoPBX\Core\System\Configs;
11
12
use MikoPBX\Core\System\Processes;
13
use MikoPBX\Core\System\System;
14
use MikoPBX\Core\System\Util;
15
use Phalcon\Di\Injectable;
16
17
class SyslogConf extends Injectable
18
{
19
    public const CONF_FILE='/etc/rsyslog.conf';
20
    public const PROC_NAME='rsyslogd';
21
22
    /**
23
     * Restarts syslog daemon
24
     */
25
    public function reStart(): void
26
    {
27
        $this->generateConfigFile();
28
        $syslogPath = Util::which(self::PROC_NAME);
29
        $pid = Processes::getPidOfProcess(self::PROC_NAME);
30
        if ( ! empty($pid)) {
31
            $busyboxPath = Util::which('busybox');
32
            // Завершаем процесс.
33
            Processes::mwExec("{$busyboxPath} kill '$pid'");
34
        }
35
        Processes::mwExec($syslogPath);
36
    }
37
38
    /**
39
     * Генерация конфигурационного файла.
40
     */
41
    private function generateConfigFile():void{
42
        $log_file    = self::getSyslogFile();
43
        $conf = ''."\n".
44
                '$ModLoad imuxsock'."\n".
45
                '$ModLoad imklog'."\n".
46
                'template(name="mikopbx" type="string"'."\n".
47
                '  string="%TIMESTAMP:::date-rfc3164% %syslogfacility-text%.%syslogseverity-text% %syslogtag% %msg%\n"'."\n".
48
                ')'."\n".
49
                '$ActionFileDefaultTemplate mikopbx'."\n".
50
                '$IncludeConfig /etc/rsyslog.d/*.conf'."\n".
51
                '*.* '."{$log_file}\n";
52
        Util::fileWriteContent(self::CONF_FILE, $conf);
53
        Util::createUpdateSymlink($log_file, '/var/log/messages');
54
        Util::mwMkdir('/etc/rsyslog.d');
55
56
    }
57
58
    /**
59
     * Returns Syslog file path
60
     * @return string
61
     */
62
    public static function getSyslogFile(): string
63
    {
64
        $logdir = System::getLogDir() . '/system';
65
        Util::mwMkdir($logdir);
66
        return "$logdir/messages";
67
    }
68
}