|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* MikoPBX - free phone system for small business |
|
4
|
|
|
* Copyright (C) 2017-2020 Alexey Portnov and Nikolay Beketov |
|
5
|
|
|
* |
|
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU General Public License as published by |
|
8
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* This program is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU General Public License for more details. |
|
15
|
|
|
* |
|
16
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
|
17
|
|
|
* If not, see <https://www.gnu.org/licenses/>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace MikoPBX\Core\System\Configs; |
|
21
|
|
|
|
|
22
|
|
|
use MikoPBX\Core\System\Processes; |
|
23
|
|
|
use MikoPBX\Core\System\System; |
|
24
|
|
|
use MikoPBX\Core\System\Util; |
|
25
|
|
|
use Phalcon\Di; |
|
26
|
|
|
use Phalcon\Di\Injectable; |
|
27
|
|
|
|
|
28
|
|
|
class SyslogConf extends Injectable |
|
29
|
|
|
{ |
|
30
|
|
|
public const CONF_FILE ='/etc/rsyslog.conf'; |
|
31
|
|
|
public const PROC_NAME ='rsyslogd'; |
|
32
|
|
|
public const SYS_LOG_LINK='/var/log/messages'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Restarts syslog daemon |
|
36
|
|
|
*/ |
|
37
|
|
|
public function reStart(): void |
|
38
|
|
|
{ |
|
39
|
|
|
$this->generateConfigFile(); |
|
40
|
|
|
$pidSyslogD = Processes::getPidOfProcess('syslogd', self::PROC_NAME); |
|
41
|
|
|
if(!empty($pidSyslogD)){ |
|
42
|
|
|
$logreadPath = Util::which('logread'); |
|
43
|
|
|
Processes::mwExec("$logreadPath >> " . self::SYS_LOG_LINK); |
|
44
|
|
|
Processes::killByName('syslogd'); |
|
45
|
|
|
} |
|
46
|
|
|
Processes::safeStartDaemon(self::PROC_NAME, '-n'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Генерация конфигурационного файла. |
|
51
|
|
|
*/ |
|
52
|
|
|
private function generateConfigFile():void{ |
|
53
|
|
|
$pathScript = $this->createRotateScript(); |
|
54
|
|
|
$log_file = self::getSyslogFile(); |
|
55
|
|
|
file_put_contents($log_file, '', FILE_APPEND); |
|
56
|
|
|
$conf = ''.PHP_EOL. |
|
57
|
|
|
'$ModLoad imuxsock'.PHP_EOL. |
|
58
|
|
|
'template(name="mikopbx" type="string"'.PHP_EOL. |
|
59
|
|
|
' string="%TIMESTAMP:::date-rfc3164% %syslogfacility-text%.%syslogseverity-text% %syslogtag% %msg%\n"'."\n". |
|
60
|
|
|
')'.PHP_EOL. |
|
61
|
|
|
'$ActionFileDefaultTemplate mikopbx'.PHP_EOL. |
|
62
|
|
|
'$IncludeConfig /etc/rsyslog.d/*.conf'.PHP_EOL. |
|
63
|
|
|
'*.* '.$log_file.PHP_EOL. |
|
64
|
|
|
PHP_EOL. |
|
65
|
|
|
'$outchannel log_rotation,'.$log_file.',2621440,'.$pathScript.PHP_EOL |
|
66
|
|
|
.'*.* :omfile:$log_rotation'.PHP_EOL; |
|
67
|
|
|
Util::fileWriteContent(self::CONF_FILE, $conf); |
|
68
|
|
|
Util::createUpdateSymlink($log_file, self::SYS_LOG_LINK); |
|
69
|
|
|
Util::mwMkdir('/etc/rsyslog.d'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Returns Syslog file path |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function getSyslogFile(): string |
|
77
|
|
|
{ |
|
78
|
|
|
$logDir = System::getLogDir() . '/system'; |
|
79
|
|
|
Util::mwMkdir($logDir); |
|
80
|
|
|
return "$logDir/messages"; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Скрипт ротации логов. |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
public function createRotateScript(): string |
|
88
|
|
|
{ |
|
89
|
|
|
$mvPath = Util::which('mv'); |
|
90
|
|
|
$chmodPath = Util::which('chmod'); |
|
91
|
|
|
$di = Di::getDefault(); |
|
92
|
|
|
$logFile = self::getSyslogFile(); |
|
93
|
|
|
$textScript = '#!/bin/sh'.PHP_EOL. |
|
94
|
|
|
"logName='{$logFile}';".PHP_EOL. |
|
95
|
|
|
'if [ ! -f "$logName" ]; then exit; fi'.PHP_EOL. |
|
96
|
|
|
'for srcId in 5 4 3 2 1 ""; do'.PHP_EOL. |
|
97
|
|
|
' dstId=$((srcId + 1));'.PHP_EOL. |
|
98
|
|
|
' if [ "x" != "${srcId}x" ]; then'.PHP_EOL. |
|
99
|
|
|
' srcId=".${srcId}"'.PHP_EOL. |
|
100
|
|
|
' fi'.PHP_EOL. |
|
101
|
|
|
' srcFilename="${logName}${srcId}"'.PHP_EOL. |
|
102
|
|
|
' if [ -f "$srcFilename" ];then'.PHP_EOL. |
|
103
|
|
|
' dstFilename="${logName}.${dstId}"'.PHP_EOL. |
|
104
|
|
|
' '.$mvPath.' -f "$srcFilename" "$dstFilename"'.PHP_EOL. |
|
105
|
|
|
' fi'.PHP_EOL. |
|
106
|
|
|
'done'.PHP_EOL. |
|
107
|
|
|
PHP_EOL; |
|
108
|
|
|
if($di){ |
|
109
|
|
|
$varEtcDir = $di->getShared('config')->path('core.varEtcDir'); |
|
110
|
|
|
}else{ |
|
111
|
|
|
$varEtcDir = '/etc'; |
|
112
|
|
|
} |
|
113
|
|
|
$pathScript = $varEtcDir . '/'.self::PROC_NAME.'_logrotate.sh'; |
|
114
|
|
|
file_put_contents($pathScript, $textScript); |
|
115
|
|
|
Processes::mwExec("$chmodPath +x $pathScript"); |
|
116
|
|
|
|
|
117
|
|
|
return $pathScript; |
|
118
|
|
|
} |
|
119
|
|
|
} |