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; |
16
|
|
|
use Phalcon\Di\Injectable; |
17
|
|
|
|
18
|
|
|
class SyslogConf extends Injectable |
19
|
|
|
{ |
20
|
|
|
public const CONF_FILE ='/etc/rsyslog.conf'; |
21
|
|
|
public const PROC_NAME ='rsyslogd'; |
22
|
|
|
public const SYS_LOG_LINK='/var/log/messages'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Restarts syslog daemon |
26
|
|
|
*/ |
27
|
|
|
public function reStart(): void |
28
|
|
|
{ |
29
|
|
|
$this->generateConfigFile(); |
30
|
|
|
$pidSyslogD = Processes::getPidOfProcess('syslogd', self::PROC_NAME); |
31
|
|
|
if(!empty($pidSyslogD)){ |
32
|
|
|
$logreadPath = Util::which('logread'); |
33
|
|
|
Processes::mwExec("{$logreadPath} >> " . self::SYS_LOG_LINK); |
34
|
|
|
Processes::killByName('syslogd'); |
35
|
|
|
} |
36
|
|
|
$syslogPath = Util::which(self::PROC_NAME); |
37
|
|
|
$pid = Processes::getPidOfProcess(self::PROC_NAME); |
38
|
|
|
if ( ! empty($pid)) { |
39
|
|
|
$busyboxPath = Util::which('busybox'); |
40
|
|
|
// Завершаем процесс. |
41
|
|
|
Processes::mwExec("{$busyboxPath} kill '$pid'"); |
42
|
|
|
} |
43
|
|
|
Processes::mwExec($syslogPath); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Генерация конфигурационного файла. |
48
|
|
|
*/ |
49
|
|
|
private function generateConfigFile():void{ |
50
|
|
|
$log_file = self::getSyslogFile(); |
51
|
|
|
file_put_contents($log_file, '', FILE_APPEND); |
52
|
|
|
$conf = ''."\n". |
53
|
|
|
'$ModLoad imuxsock'."\n". |
54
|
|
|
// '$ModLoad imklog'."\n". |
55
|
|
|
'template(name="mikopbx" type="string"'."\n". |
56
|
|
|
' string="%TIMESTAMP:::date-rfc3164% %syslogfacility-text%.%syslogseverity-text% %syslogtag% %msg%\n"'."\n". |
57
|
|
|
')'."\n". |
58
|
|
|
'$ActionFileDefaultTemplate mikopbx'."\n". |
59
|
|
|
'$IncludeConfig /etc/rsyslog.d/*.conf'."\n". |
60
|
|
|
'*.* '."{$log_file}\n"; |
61
|
|
|
Util::fileWriteContent(self::CONF_FILE, $conf); |
62
|
|
|
Util::createUpdateSymlink($log_file, self::SYS_LOG_LINK); |
63
|
|
|
Util::mwMkdir('/etc/rsyslog.d'); |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns Syslog file path |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public static function getSyslogFile(): string |
72
|
|
|
{ |
73
|
|
|
$logdir = System::getLogDir() . '/system'; |
74
|
|
|
Util::mwMkdir($logdir); |
75
|
|
|
return "$logdir/messages"; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
public static function rotatePbxLog(): void |
80
|
|
|
{ |
81
|
|
|
$syslogPath = Util::which(self::PROC_NAME); |
82
|
|
|
$killAllPath = Util::which('killall'); |
83
|
|
|
$chownPath = Util::which('chown'); |
84
|
|
|
$touchPath = Util::which('touch'); |
85
|
|
|
|
86
|
|
|
$di = Di::getDefault(); |
87
|
|
|
$max_size = 3; |
88
|
|
|
$logFile = self::getSyslogFile(); |
89
|
|
|
$text_config = "{$logFile} { |
90
|
|
|
nocreate |
91
|
|
|
nocopytruncate |
92
|
|
|
delaycompress |
93
|
|
|
nomissingok |
94
|
|
|
start 0 |
95
|
|
|
rotate 9 |
96
|
|
|
size {$max_size}M |
97
|
|
|
missingok |
98
|
|
|
noolddir |
99
|
|
|
postrotate |
100
|
|
|
{$killAllPath} ".self::PROC_NAME." > /dev/null 2> /dev/null |
101
|
|
|
{$touchPath} {$logFile} |
102
|
|
|
{$chownPath} www:www {$logFile}> /dev/null 2> /dev/null |
103
|
|
|
{$syslogPath} > /dev/null 2> /dev/null |
104
|
|
|
endscript |
105
|
|
|
}"; |
106
|
|
|
$varEtcDir = $di->getShared('config')->path('core.varEtcDir'); |
107
|
|
|
$path_conf = $varEtcDir . '/'.self::PROC_NAME.'_logrotate.conf'; |
108
|
|
|
file_put_contents($path_conf, $text_config); |
109
|
|
|
$mb10 = $max_size * 1024 * 1024; |
110
|
|
|
$options = ''; |
111
|
|
|
if (Util::mFileSize($logFile) > $mb10) { |
112
|
|
|
$options = '-f'; |
113
|
|
|
} |
114
|
|
|
$logrotatePath = Util::which('logrotate'); |
115
|
|
|
Processes::mwExecBg("{$logrotatePath} {$options} '{$path_conf}' > /dev/null 2> /dev/null"); |
116
|
|
|
} |
117
|
|
|
} |