0

我正在使用星号 ami 打开一个套接字并将命令发送到星号服务器以执行特定任务。据我所知,套接字已打开,命令已发送,然后套接字将关闭。但是,我在星号机器上的内存使用量随着时间的推移略有增加,直到我不得不重新启动 httpd 服务。它的代码不多,但我是 php 新手,所以我不确定出了什么问题,或者我是否应该在别处寻找。

<?php
echo $sipexten = $_REQUEST["sipexten"];
echo $xferext = $_REQUEST["xferext"];

$wrets = "";
$amiusername = 'XXX';
$amisecret   = 'XXX';
$socket = '';       

//MAIN PROCESS       
//quit if any parameters are missing
if ($sipexten == '' || $xferext == ''){
    exit('not enough parameters');   //stop further processing for not enough     parameters
}

ob_implicit_flush(true);
$socket = fsockopen("127.0.0.1","5038", $errno, $errstr, 0);    
$wrets = fread($socket,30);
fputs($socket, "Action: Login\r\n");
fputs($socket, "UserName: $amiusername\r\n");
fputs($socket, "Events: off\r\n");
fputs($socket, "Secret: $amisecret\r\n\r\n");
fputs($socket, "Action: CoreShowChannels\r\n\r\n");
fputs($socket, "Action: Logoff\r\n\r\n");        
while (!feof($socket)) {
    $wrets .= fread($socket,8192 );
}
fclose($socket);   

$sipexten = "Channel: SIP/$sipexten";
$channelVal = strpos($wrets,$sipexten);
if ($channelVal){
    $channel=trim(substr($wrets,$channelVal+9,strpos(substr($wrets,$channelVal),"UniqueID:")-9)    );
}


ob_implicit_flush(true);
$socket = fsockopen("127.0.0.1","5038", $errno, $errstr, 0);    
$wrets = fread($socket,30);
fputs($socket, "Action: Login\r\n");
fputs($socket, "UserName: $amiusername\r\n");
fputs($socket, "Events: off\r\n");
fputs($socket, "Secret: $amisecret\r\n\r\n");
fputs($socket, "Action: Atxfer\r\n");
fputs($socket, "Channel: $channel\r\n");
fputs($socket, "Exten: $xferext\r\n");
fputs($socket, "Priority: 1\r\n\r\n");
fputs($socket, "Action: Logoff\r\n\r\n");        
while (!feof($socket)) {
    $wrets .= fread($socket,8192 );
}
fclose($socket); 
echo "<br />";         
echo $wrets;
?>

这是我的其他脚本的样子:

<?php

$sipexten = $_REQUEST["sipexten"];

$command = $_REQUEST["command"];

$isgood = $_REQUEST["isgood"]; 

$subfolder = $_REQUEST["subfolder"];

$srcname = $_REQUEST["srcname"];

$dstname = $_REQUEST["dstname"];


$wrets = "";
$amiusername = 'XXX';
$amisecret   = 'XXX';
$sipchannel = '';

$audiofolder = '/var/lib/asterisk/sounds/customaudio2play/';
$badfolder = 'bad';
$goodfolder = 'good';
$recordingformat = 'gsm'; 
$socket = '';        


if ($command == 'start'){  
    if ($sipexten == '' || $subfolder == '' || $srcname ==''){
        exit('not enough parameters');   //stop further processing for not enough parameters
    }
    //Check if SIP/sipexten is having a call. If yes, proceed.

    $sipchannel = amigetchannel();

    if ($sipchannel != '') {

        //AMI LOGIN


        // don't buffer output... flush it immediately
        ob_implicit_flush(true);
        $socket = fsockopen("127.0.0.1","5038", $errno, $errstr, 0);    
        $wrets = fread($socket,30);
        //AMI Login
        fputs($socket, "Action: Login\r\n");
        fputs($socket, "UserName: $amiusername\r\n");
        fputs($socket, "Events: off\r\n");
        fputs($socket, "Secret: $amisecret\r\n\r\n");
        fputs($socket, "Action: Monitor\r\n");
        fputs($socket, "Channel: $sipchannel\r\n");
        fputs($socket, "File: $audiofolder/$subfolder/$srcname\r\n");
        fputs($socket, "Format: $recordingformat\r\n");
        fputs($socket, "Mix: 1\r\n\r\n");

        //LOG OFF AMI
        fputs($socket, "Action: Logoff\r\n\r\n"); 
        while (!feof($socket)) {
            $wrets .= fread($socket,8192 );
        }
        fclose($socket);  
        echo $wrets;
        echo ('start executed');
    } else {
        //No channel found
        echo ('No active channel');
    }       //$sipchannel != '';    
    //DONE

} elseif ($command == 'stop'){
    if ($sipexten == ''){
        exit('not enough parameters');   //stop further processing for not enough parameters
    }

    //Get current channel of $sipexten
    $sipchannel = amigetchannel();

    if ($sipchannel != '') {


        //AMI LOGIN
        // don't buffer output... flush it immediately
        ob_implicit_flush(true);
        $socket = fsockopen("127.0.0.1","5038", $errno, $errstr, 0);    
        $wrets = fread($socket,30);
        //AMI Login
        fputs($socket, "Action: Login\r\n");
        fputs($socket, "UserName: $amiusername\r\n");
        fputs($socket, "Events: off\r\n");
        fputs($socket, "Secret: $amisecret\r\n\r\n");
        fputs($socket, "Action: StopMonitor\r\n");
        fputs($socket, "Channel: $sipchannel\r\n\r\n");

        //LOG OFF AMI
        fputs($socket, "Action: Logoff\r\n\r\n"); 
        while (!feof($socket)) {
            $wrets .= fread($socket,8192 );
        }
        fclose($socket);  
        echo $wrets;

    }
    echo ('stop executed');
} elseif ($command=='showchannels') {

    echo ('</br> sip channel: ' . amigetchannel());

}

echo ('</br>End of file');


function amigetchannel(){


    global $sipexten, $amiusername, $amisecret;

    // don't buffer output... flush it immediately
    ob_implicit_flush(true);
    $socket = fsockopen("127.0.0.1","5038", $errno, $errstr, 0);    
    $wrets = fread($socket,30);
    //AMI Login
    fputs($socket, "Action: Login\r\n");
    fputs($socket, "UserName: $amiusername\r\n");
    fputs($socket, "Events: off\r\n");
    fputs($socket, "Secret: $amisecret\r\n\r\n");

    fputs($socket, "Action: CoreShowChannels\r\n\r\n");

    //LOG OFF AMI
    fputs($socket, "Action: Logoff\r\n\r\n"); 
    while (!feof($socket)) {
        $wrets .= fread($socket,8192 );
    }
    fclose($socket);  

    $needle = "Channel: SIP/" . $sipexten;
    $channelVal = strpos($wrets,$needle);

    if ($channelVal){
$sipchannel=trim(substr($wrets,$channelVal+9,strpos(substr($wrets,$channelVal),"UniqueID:")-9));
            }

}


?>
4

1 回答 1

0

使用 top 或 ps 实用程序检查哪个进程使用内存,然后调试您的代码是否存在内存泄漏。

如果 apache 使用内存,请检查您的 httpd.conf,有参数“MaxRequestsPerChild”可用于重新启动错误线程。

<IfModule worker.c>
StartServers         2
MaxClients         150
MinSpareThreads     25
MaxSpareThreads     75
ThreadsPerChild     25
MaxRequestsPerChild  0
</IfModule>
于 2013-03-22T19:02:15.767 回答