Skip to content

Commit

Permalink
#699 Добавил более детальный лог.
Browse files Browse the repository at this point in the history
  • Loading branch information
boffart committed Jun 11, 2024
1 parent bb99391 commit e1633d7
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/Core/Asterisk/AsteriskManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

namespace MikoPBX\Core\Asterisk;

use MikoPBX\Core\System\Processes;
use MikoPBX\Core\System\SystemMessages;
use MikoPBX\Core\System\Util;
use Throwable;
Expand Down Expand Up @@ -612,30 +613,41 @@ public function connect($server = null, $username = null, $secret = null, $event
}

// connect the socket
$errno = $errstr = null;
$errno = $errStr = null;
$timeout = 2;

$this->socket = @fsockopen($this->server, $this->port, $errno, $errstr, $timeout);
if ($this->socket == false) {
SystemMessages::sysLogMsg('asmanager', "Unable to connect to manager {$this->server}:{$this->port} ($errno): $errstr", LOG_ERR);
$netStatPath = Util::which('netstat');
$busyBoxPath = Util::which('busybox');
$chkCommand = "$netStatPath -ntap | $busyBoxPath grep '$server ' | $busyBoxPath grep ESTABLISHED | $busyBoxPath grep asterisk";
if(Processes::mwExec($chkCommand) === 1){
SystemMessages::sysLogMsg('AMI', "Exceptions, Unable to connect to $server: the asterisk process is not running", LOG_ERR);
return false;
}
try {
$this->socket = fsockopen($this->server, $this->port, $errno, $errStr, $timeout);
}catch (Throwable $e){
SystemMessages::sysLogMsg('AMI', "Exceptions, Unable to connect to manager $server ($errno): $errStr", LOG_ERR);
return false;
}
if ($this->socket === false) {
SystemMessages::sysLogMsg('AMI', "Unable to connect to manager $server ($errno): $errStr", LOG_ERR);
return false;
}
// PT1C;
stream_set_timeout($this->socket, 1, 0);

// read the header
$str = $this->getStringDataFromSocket();
if ($str === '') {
// a problem.
Util::sysLogMsg('asmanager', "Asterisk Manager header not received.", LOG_ERR);
SystemMessages::sysLogMsg('AMI', "Asterisk Manager header not received.", LOG_ERR);
return false;
}

// login
$res = $this->sendRequest('login', ['Username' => $username, 'Secret' => $secret, 'Events' => $events]);
if ($res['Response'] != 'Success') {
if ($res['Response'] !== 'Success') {
$this->_loggedIn = false;
Util::sysLogMsg('asmanager', "Failed to login.", LOG_ERR);
SystemMessages::sysLogMsg('AMI', "Failed to login.", LOG_ERR);
$this->disconnect();
return false;
}
Expand Down

0 comments on commit e1633d7

Please sign in to comment.